nput
输入一个小C语言源程序,源程序长度不超过2000个字符,保证输入合法。
Output
按照源程序中单词出现顺序输出,输出二元组形式的单词串。
(单词种类,单词值)
单词一共5个种类:
关键字:用keyword表示
自定义标识符:用identifier表示
整数:用integer表示
界符:用boundary表示
运算符:用operator表示
每种单词值用该单词的符号串表示。#include
#include
using namespace std;
string S[5]= {"keyword","identifier","integer","boundary","operator"};
string T[6]= {"main","if","else","for","while","int"};
void panduan(string s)
{
if(s[0]>='0'&&s[0]<='9') //开头是数字肯定就为数字
{
cout<
}
else
{
int f=1;
for(int i=0; i<6; i++)
{
if(s==T[i])
{
f=0;
cout<
break;
}
}
if(f==1)
{
cout<
}
}
}
int main()
{
string s;
while(cin>>s)
{
int len=s.length();
string temp="";
for(int i=0; i
{
//操作符
if(s[i] == '=' || s[i] == '+' || s[i] == '-'||s[i] == '*'|| s[i] == '/' || s[i] == '' || s[i] == '!')
{
if(temp.length())
{
panduan(temp);
}
temp="";
if(i+1
{
cout<
i++;
}
else
{
cout<
}
}
//界符
else if(s[i] == '(' || s[i] == ')' || s[i] == '{'||s[i] == '}'|| s[i] == ',' || s[i] ==';')
{
if(temp.length())
{
panduan(temp);
}
temp="";
cout<
}
//不是界符也不是操作符,就存到临时字符串里面,等待判断
else
{
temp=temp+s[i];
}
}
if(temp.length())
{
panduan(temp);
}
}
return 0;
}