Beruflich Dokumente
Kultur Dokumente
#include<iostream>
#include<fstream>
using namespace std;
void expression(char[]);
bool first_char(char);
void tokentype(char,char);
int main()
{
bool flag;
fstream f;
int i=0;
char a[50];
for(i=0;i<50;i++)
a[i]=' ';
i=0;
f.open("TEXT", ios::in|ios::out);
while(f)
{
f.get(a[i++]);
}
flag=first_char(a[0]);
if(flag==true)
{
cout<<"\nThe expression is valid\n";
i=0;
expression(a);
}
else cout<<"\nThe expression is invalid\n";
return 0;
}
void expression(char a[])
{
char b[20][20];
int j=0,k=0;
int i=0,flag=0;;
for(i=0;i<20;i++)
cout<<a[i];
cout<<"\n";
for(i=0;i<20;i++)
for(j=0;j<20;j++)
b[i][j]=' ';
i=1;
j=0;k=1;
b[0][0]=a[0];
while(a[i-1]!=';')
{
while((a[i]!='=') && (a[i]!='+') && (a[i]!='-') && (a[i]!='*') && (a[i]!='/') && (a[i]!=';'))
b[j][k++]=a[i++];
k=0;
j++;
b[j][k++]=a[i];
if((a[i]=='/' || a[i]=='*' || a[i]=='-' || a[i]=='+' ) && a[i+1]=='=')
{
i++;
b[j++][k++]=a[i++];
}
else
{
j++;
i++;
}
k=0;
for(i=0;i<20;i++)
{
for(k=0;k<j;k++)
{
cout<<b[i][k];
}
char x=b[i][0];
char y=b[i][1];
tokentype(x,y);
//cout<<"\n";
}
cout<<"\n\n";
}
bool first_char(char a)
{
if((a>=65 && a<=90) || (a>=97 && a<=122))
return true;
else
return false;
}
OUTPUT:
cc6 is an identifier
/= is a compound assignment
aa is an identifier
+ is an arithmetic operation
bg is an identifier*
* is an arithmetic operator
60 is a constant
; end of statement