小说天幕红尘:HELP~~往各位精通C++的大虾帮忙~!!!
来源:百度文库 编辑:查人人中国名人网 时间:2024/05/13 14:00:48
#include<cstddef>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#define norw 13
#define al 10
string word[norw]={"begin","call", "const", "do",
"end", "if", "odd","procedure",
"read", "then", "var","while",
"write"};
int num;
int pos;
int l;
int stringlength;
string lowerCase( string ss)
{
string lower(ss);
for(int i=0;i<ss.length();i++)
lower[i]=tolower(lower[i]);
return lower;
}
void getsym(string ss)
{
int k;
char ch;
char next;
int i;
char a[100]={0};
string s(ss);
i=0;
ch=s[i];
while(ch==' ')
{
if(pos==stringlength-1)
exit(1);
ch=s[++i];
pos++;
}
if ((ch>='a'&&ch<'=z')||ch=='_')
{
k=-1;
do
{
if(k<al)
{
k=k+1;
a[k]=ch;
}
else
{
cout<<"太长的标识符! ";
system("PAUSE");
exit(1);
}
ch=s[++i];
pos++;
}while((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'));
for(l=0;l<norw;l++)
{
if(word[l]==a)
break;
}
if (l<norw)
{
cout<<word[l].c_str()<<"------- 关键字"<<endl;
}
else
{
cout<<a<<"---------标识符"<<endl;
}
return;
}
else
if(ch>='0'&&ch<='9')
{
k=0;
num=0;
// a[k]=ch;
do
{ a[k]=ch;
k=k+1;
num=10*num+(int(ch)-int('0'));
ch=s[++i];
pos++;
next=s[i];
// k=k+1;
// a[k]=ch;
}while(ch>='0'&&ch<='9');
if (num>32767)
{
cout<<"数字太长"<<endl;
exit(1);
}
if(next>='a'&&next<='z')
{
do{
a[k]=next;
next=s[++i];
k++;
pos++;
}while((next>='a'&&next<='z')||(next>='0'&&next<='9'));
cout<<a<<"-------------错误的标识符!"<<endl;
system("PAUSE");
}
else
{
cout<<a<<"--------------数字";
cout<<endl;
}
return;
}
else
if(ch==':')
{
ch=s[++i];
pos++;
if(ch=='=')
{
ch=s[++i];
pos++;
cout<<":= -------赋值符号"<<endl;
}
else
{
cout<<": ---------冒号"<<endl;
}
return;
}
else
if(ch=='<')
{
ch=s[++i];
pos++;
if(ch=='=')
{
ch=s[++i];
pos++;
cout<<"<= -------小于等于"<<endl;
}
else
{
cout<<"< ---------小于"<<endl;
}
return;
}
else
{
if(ch=='>')
{
ch=s[++i];
pos++;
if(ch=='=')
{
ch=s[++i];
pos++;
cout<<">= --------大于等于"<<endl;
}
else
{
cout<<"> --------大于"<<endl;
}
return;
}
else
{ switch(ch)
{
case '+':cout<<ch<<" ----------"<<"加号"<<endl;break;
case '-':cout<<ch<<" ----------"<<"减号"<<endl;break;
case '*':cout<<ch<<" ----------"<<"乘号"<<endl;break;
case '/':cout<<ch<<" ----------"<<"斜线"<<endl;break;
case '(':cout<<ch<<" ----------"<<"左括号"<<endl;break;
case ')':cout<<ch<<" ----------"<<"右括号"<<endl;break;
case '=':cout<<ch<<" ----------"<<"等号"<<endl;break;
case ',':cout<<ch<<" ----------"<<"逗号"<<endl;break;
case '.':cout<<ch<<" ----------"<<"点号"<<endl;break;
case ';':cout<<ch<<" ----------"<<"分号"<<endl;break;
case '#':cout<<ch<<" ----------"<<"不等号"<<endl;break;
default: cout<<"错误字符:"<<ch<<endl;
}
ch=s[++i];
pos++;
}
}
return;
}
int main(int argc,char *argv[])
{
string ss;
char chin;
ifstream inf(argv[1]);
while(inf)
{
inf.get(chin);
if(chin=='\n')
chin=' ';
ss+=chin;
}
inf.close();
string ss2(ss);
stringlength=ss2.length();
ss2=lowerCase(ss2);
pos=0;
while(pos<ss2.length())
{
getsym(ss2.substr(pos,ss2.length()));
}
return 0;