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”...
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...
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...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT