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

main.cpp

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<cstdlib>
#include<string.h>
#include<ctype.h>

using namespace std;

int isColor(char buff[]){
   char colors[7][7] = {"red","blue","green","yellow","orange"};
   int x, chnge = 0;
   for(x = 0; x < 5; ++x){
       if(strcmp(colors[x], buff) == 0){
           chnge = 1;
           break;
       }
   }  
   return chnge;
}
int main(){
   char choice, buff[15];
   ifstream fin("test.txt");
   int x,y=0;
  
   if(!fin.is_open()){
       cout<<"error while opening the file\n";
       exit(0);
   }
   while(!fin.eof()){
        choice = fin.get();
int ch1 = (int)choice;
        if(ch1>=49 && ch1<=53 ){
cout<<choice<<" --- digit"<<endl;
}
else{
        if(isalnum(choice)){
            buff[y++] = choice;
        }
        else if((choice == ' ' || choice == '\n' || choice == ',') && (y != 0)){
                buff[y] = '\0';
                y = 0;               
                if(isColor(buff) == 1)
                    cout<<buff<<" --- color\n";
                else
                    cout<<buff<<" --- direction\n";
        }  
}
   }
   fin.close();
   return 0;
}

Output:


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”...
I have a C problem. I need to read the data of a txt file to...
I have a C problem. I need to read the data of a txt file to array by struct, then use these data to sum or sort. The txt file and the struct like aa.txt 1 2 3 4 ***************************** struct aaa{ int num1,num2; }bbb[2]; num1 for 1,3, num2 for 2 4 I made a void readfile () function for read data. How can I pass the numbers to another function ( void sum () and void sort() ) for...
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...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a series of emails. Console Email Creator ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi James, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Josephine, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Art, We've got some great deals for you. Check our website! Specifications...
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that 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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT