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
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();
}