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 thumbs up, thaanks

#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...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
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...
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT