Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the Concept of vector and string to do the following question 1) Write a pro

ID: 3786305 • Letter: U

Question

Use the Concept of vector and string to do the following question

1)      Write a program that creates a vector of string called “Vec”. Vector “Vec” grows and shrinks as the user processes the transactions from a data file called “Transaction.txt”. The transaction file can only include three types of commands: Add, Remove, and Print. With “Add” command, you get two more information, the information you need to put into the vector and the position the information should be inserted. With “Remove”, you get one more information that indicates which element (index) should be deleted. Print command means the content of the vector should be printed on the screen. For example, you may have the following information in your file:

Add                 Hello               4

Remove                                   5

Print

The first line indicates that the word “Hello” should be located in the forth index which means Vec [4]. Of course you should check if this insert is possible. This insert is possible if the position you are attempting to insert the element is not beyond the size of the vector. The second line means Vec [5] should be removed. Again this operation should only be allowed if the index is not beyond the current size of the vector. Test your program with the following Transaction file:

Add     Student            0

Add     Kid                  0

Add     Final                1

Add     Grow               1

Add     Note                2

Print

Add     Rich                5

Remove                       1

Remove                       7

Add     Mind               2

Remove                       3

Print

Explanation / Answer

using System;
using System.IO;

namespace FileApplication
{
class Program
{
static void Main(string[] args)
{
try
{
string result="Vec";
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("c:/Transaction.txt"))//place local url address of file.
{
string line;
// Read and display lines from the file until
// the end of the file is reached.
while ((line = sr.ReadLine()) != null)
{
string[] array=line.Split(' ');
if(array[0].Trim()=='Add'){
string insertString=array[1].Trim();
int index=Int32.Parse(array[2].Trim());
if(index>=0 && index<=result.Length){
result=result.Substring(0,index)+insertString+result.Substring(index);
}
}
else if(array[0].Trim()=="Remove"){
int index=Int32.Parse(array[1].Trim());
if(index>=0 && index<result.Length){
result.Remove(index,1);
}
}
else if(array[0].Trim()=="Print"){
Console.WriteLine(result);
}
else{
Console.WriteLine("Invalid Operation On Input");
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}