Question

In: Computer Science

C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...

C++ Assignment

Hi, I need to create a program that:

1.Reads a source file (.txt) with following information:

1,2,3,4,5

red,blue,green,yellow,orange

left, right,front, back

2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count :

Example:

Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file)

===========================================================================

1 ----digit ----1

2 ----digit ----1

red ----color ----1

blue ----color ----1

left ----direction ----1

right ----direction    ----1

Solutions

Expert Solution

Please give thumb up, thanks

codE:

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
   int digit[6]={0,0,0,0,0,0};
   int color[6]={0,0,0,0,0,0};
   int direction[5]={0,0,0,0,0};
   ifstream infile;
   string line;
   infile.open("filename1.txt");
   if(!infile)
   {
       cout<<"Unable to open file"<<endl;
       return 0;
   }
   string Element;
while( infile>>Element )
{
if(Element=="1")
{
   digit[1]+=1;
                   }
                   else if(Element=="2")
{
   digit[2]+=1;
                   }
                       else if(Element=="3")
{
   digit[3]+=1;
                   }
                       else if(Element=="4")
{
   digit[4]+=1;
                   }
                       else if(Element=="5")
{
   digit[5]+=1;
                   }        
                   else if(Element=="red")
{
   color[1]+=1;
                   }
                   else if(Element=="blue")
{
   color[2]+=1;
                   }
                       else if(Element=="green")
{
   color[3]+=1;
                   }
                       else if(Element=="yellow")
{
   color[4]+=1;
                   }
                       else if(Element=="orange")
{
   color[5]+=1;
                   }
                   else if(Element=="left")
{
   direction[1]+=1;
                   }
                   else if(Element=="right")
{
   direction[2]+=1;
                   }
                       else if(Element=="front")
{
   direction[3]+=1;
                   }
                       else if(Element=="back")
{
   direction[4]+=1;
                   }
}
  
for(int i=1; i<6; i++)
{
   if(digit[i]!=0)
   {
       cout<<i<<"\t"<<"digit\t"<<digit[i]<<endl;
               }
           }
for(int i=1; i<6; i++)
{
   if(color[i]!=0)
   {
                   if(i==1)
                   cout<<"red\tcolor\t";
                   else if(i==2)
                   cout<<"blue\tcolor\t";
                   else if(i==3)
                   cout<<"green\tcolor\t";
                   else if(i==4)
                   cout<<"yellow\tcolor\t";
                   else if(i==5)
                   cout<<"orange\tcolor\t";
                   cout<<color[i]<<endl;
               }
           }
           for(int i=1; i<5; i++)
{
   if(direction[i]!=0)
   {
                   if(i==1)
                   cout<<"left\tdirection\t";
                   else if(i==2)
                   cout<<"right\tdirection\t";
                   else if(i==3)
                   cout<<"frontt\tdirection\t";
                   else if(i==4)
                   cout<<"back\tdirection\t";
                   cout<<direction[i]<<endl;
               }
           }
           infile.close();
}


Related Solutions

C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT