📄 新建 文本文档.txt
字号:
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int isKey(char *str);
/****************************保留字集合***************************/
char* keyword[]={"_asm", "auto", "bad_cast", "bad_typeid","bool",
"break" ,"case" ,"catch","char","class",
"const","const_cast","continue" ,"default" ,"delete" ,
"do","double", "dynamic_cast", "else", "enum",
"except" ,"explicit" ,"extern" ,"false","finally",
"float", "for", "friend","goto", "if",
"include","inline", "int","long", "mutable",
"namespace", "new","operator", "private", "protected",
"public","register", "reinterpret_cast", "return", "short",
"signed", "sizeof", "static", "static_cast","struct",
"switch", "template", "this","throw","true",
"try", "type_info","typedef", "typeid", "typename",
"union","unsigned", "using", "virtual","void",
"volatile", "while","main","iostream"
};
/*****************************是不是标志符*******************************/
int identf(char *str)
{
int flag=0;
char *p=str;
/*判断第一个字符是否符合*/
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_')
{
flag=1;
str++;
}
/*从第二个字符开始遍历判断*/
while(*str && flag)
{
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_'
|| (*str>='0' && *str<='9'))
{
flag=1;
str++;
}
else
flag=0;
}
/*判断是不是关键字*/
if(flag)
flag=isKey(p);
return flag;
}
/*****************************是不是关键字*******************************/
int isKey(char *str)
{
int i, flag=1;
for(i=0; i<69; i++)
{
if(strcmp(str, keyword[i])==0)
{
flag=0;
break;
}
}
return flag;
}
/*****************************主函数*******************************/
int main (int)
{
ifstream inf;
ofstream outf;
char c;
char *buf=new char[256];
int i;
inf.open("in.txt");
outf.open("out.txt");
inf >> noskipws;
while(inf >>c)
{
i=0;
/****************************换行***************************/
if (c == '\n'){
//outf << "\n";
//cout << "\n";
continue;
}
/*****************************符号处理**********************/
if(c>='!'&&c<='/' || c>=':'&&c<='@' || c>='['&&c<='`' || c>='{'&&c<='~')
{
/*****************************判断注释**********************/
if(c=='/')
{
}
cout<<c;
outf<<c;
cout<<" 特殊符号"<<endl;
outf<<" 特殊符号"<<endl;
}
/*****************************符号处理**********************/
else
{
while((c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'
|| (c>='0' && c<='9'))
{
buf[i++]=c;
inf>>c;
}
buf[i]='\0';
if(!isKey(buf))
{
for(int j=0;j<i;j++)
{
cout << buf[j];
outf<< buf[j];
}
cout<<" 关键字"<<endl;
outf<<" 关键字"<<endl;
}
else if(identf(buf))
{
for(int j=0;j<i;j++)
{
cout << buf[j];
outf<< buf[j];
}
cout<<" 标识符"<<endl;
outf<<" 标识符"<<endl;
}
//outf << c;
}
}
inf.close();
outf.close();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -