In: Computer Science
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
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: