break;
}
}
}
//
voidenter_p(inti_pt)//分屏显示操作
{
if(i_pt%21==0)
{
cout<<"NextPage";
system("Pause");
system("cls");
}
}
//
voidanaly(FILE*fp)
{
charnumber[10];//识别数字量
charanswer_0[10];
charanswer_1[10];//识别字符串
//关系运算符stringrelation[6]={">","<",">=","<=","==","!
="};
charanswer_2[100];//识别注释行
charch,ch0,ch1,ch2,ch3,ch4,ch5,ch6;
intk=0,k1=0,k2=0,k3=0,k4=0,k5=0,k6=0;
ch=fgetc(fp);//fgetc()读字符函数,如果遇到文件结束符号,函数返回文件结束标志EOF
while(ch!
=EOF)
{
if(ch=='<'||ch=='>'||ch=='!
')
{
//关系运算符
cout<";
ch3=ch;
ch=fgetc(fp);
if(ch=='=')
{
//判断<=,>=,==,!
=符号
ch4=ch;
cout<<"【关系运算符→"<ch3=ch4='0';
ch=fgetc(fp);
enter_p(i_point);
continue;
}
elseif(ch==ch3)
{
cout<<"【输入输出流符→"<ch3='0';
ch=fgetc(fp);
enter_p(i_point);
continue;
}
else
{
//判断>,<符号
cout<<"【关系运算符→"<ch3=ch4='0';
enter_p(i_point);
continue;
}
}
if(ch=='=')
{
//判断==和=符号
cout<";
ch5=ch;
ch=fgetc(fp);
if(ch==ch5)
{
cout<<"【关系运算符→"<ch5='0';
ch=fgetc(fp);
enter_p(i_point);
continue;
}
else
{
cout<<"【赋值运算符→"<ch5='0';
enter_p(i_point);
continue;
}
}
if(ch==','||ch==';'||ch=='['||ch==']'||ch=='{'||ch=='}'||ch=='('||ch==')'||ch=='#'){
//判断分界符
cout<";
analy_boundary(ch);
enter_p(i_point);
}
if(ch=='*'||ch=='/'||ch=='%'){//ch=='+'||ch=='-'||
//判断算术运算符
ch6=ch;
ch=fgetc(fp);
if(ch6=='/')
{
if(ch=='/')
{
//判断//注释
cout<";
answer_2[k5++]=ch6;
answer_2[k5++]=ch;
ch=fgetc(fp);
while(ch!
='\n')
{
answer_2[k5++]=ch;
ch=fgetc(fp);
}
answer_2[k5]='\0';
cout<<"【注释语句,"<k5=0;
ch6='0';
enter_p(i_point);
continue;
}
elseif(ch=='*')
{
//判断/*....*/注释
cout<";
answer_2[k5++]=ch6;
answer_2[k5++]=ch;
ch=fgetc(fp);
while(ch!
='/')
{
answer_2[k5++]=ch;
ch=fgetc(fp);
}
answer_2[k5++]=ch;
answer_2[k5]='\0';
cout<<"【注释语句,"<k5=0;
ch6='0';
enter_p(i_point);
continue;
}
}
else
{
cout<";
analy_operation(ch6);
enter_p(i_point);
ch6='0';
continue;
}
}
if(ch=='+'||ch=='-'){
//判断算术运算符或增减运算符
cout<";
ch0=ch;
ch=fgetc(fp);
if(ch!
=ch0)
{
if(ch>='0'&&ch<='9'||ch=='')
{
do
{
number[k6++]=ch;
ch=fgetc(fp);
}while(ch>='0'&&ch<='9'||ch=='.'||ch=='-'||ch=='e'||ch=='');
number[k6]='\0';
cout<<"【有符号数→"<enter_p(i_point);
ch0='0';
k6=0;
continue;
}
else
{
analy_operation(ch0);
ch0='0';
k6=0;
enter_p(i_point);
continue;
}
}
else
{
cout<<"【增减运算符→"<ch0='0';
ch=fgetc(fp);
enter_p(i_point);
continue;
}
}
if(ch=='&'||ch=='|')
{
//判断逻辑与逻辑或
cout<";
ch1=ch;
ch=fgetc(fp);
ch2=ch;
cout<<"【逻辑与逻辑或→"<ch1=ch2='0';
ch=fgetc(fp);
enter_p(i_point);
continue;
}
if(ch=='.')//实型常数
{
//实型常数
number[k++]='.';
ch=fgetc(fp);
while(ch>='0'&&ch<='9')
{
number[k++]=ch;
ch=fgetc(fp);
}
number[k]='\0';
k=0;
cout<<"【实型常数→"<enter_p(i_point);
continue;
}
if(ch>='0'&&ch<='9')//||ch=='.'
{
//判断整形常数和实型常数
cout<";
do
{
number[k++]=ch;
ch=fgetc(fp);
}while(ch>='0'&&ch<='9');//||ch=='.'
if(ch=='.')//实型常数
{
//实型常数
number[k++]='.';
ch=fgetc(fp);
while(ch>='0'&&ch<='9')
{
number[k++]=ch;
ch=fgetc(fp);
}
number[k]='\0';
k=0;
cout<<"【实型常数→"<enter_p(i_point);
continue;
}
elseif(ch=='e'||ch=='E')//科学计数法
{
//科学计数法如1e-5
number[k++]=ch;
//ch=fgetc(fp);
//number[k++]=ch;
ch=fgetc(fp);
do
{
number[k++]=ch;
ch=fgetc(fp);
}while(ch>='0'&&ch<='9'||ch=='e'||ch=='E'||ch=='-'||ch=='');
number[k]='\0';
k=0;
cout<<"【科学计数法→"<enter_p(i_point);
continue;
}
else//整型常数
{
//整型常数
number[k]='\0';
k=0;
cout<<"【整形常数→"<enter_p(i_point);
continue;
}
}
if(ch=='\"')//识别字符串
{
cout<";
answer_1[k2++]=ch;//把第一个"
ch=fgetc(fp);
while(ch!
='\"')
{
answer_1[k2++]=ch;//放入字符串
ch=fgetc(fp);
}
answer_1[k2++]='\"';
answer_1[k2]='\0';
k2=0;
analy_string(answer_1);
ch=fgetc(fp);
enter_p(i_point);
continue;
}
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='_')
{
//判断关键字和标示符
cout<";
answer_0[k1++]=ch;
ch=fgetc(fp);
while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='.'||ch>='0'&&ch<='9')
{
answer_0[k1++]=ch;
ch=fgetc(fp);
}
answer_0[k1]='\0';
k1=0;
analy_keyword(answer_0);
enter_p(i_point);
continue;
}
ch=fgetc(fp);
}
}
//
#include"stdio.h"//strchr,fgetc,rewind,fseek
#include"iostream.h"
#include"string.h"
#include"stdlib.h"
#definekeyword42
#definefuhao46
#definefenjie13
#defineyunsuan15
charyun[yunsuan]="+-*/=>
\&^|%:
~";
#definefenge12
charfen[fenge]=",.;{}()#[]\"";
char*a[keyword]={"auto","break","case","char","class",
"const","continue","default","delete",
"do","double","else","enum","extern",
"float","for","friend","if","inline",
"int","long","new","operator","private",
"protected","public","register","return",
"short","sizeof","static","struct","switch",
"template","this","typedef","union",
"virtual","void","while","include","char"};
char*b[fuhao]={"+","-","*","/","%","++","--",
">",">=","<","<=","==","!
=","&&","||",
"!
","<<",">>","~","|","^","&","=","+=",
"-=","*=","/=","%=",">>=","<<=","&=","^=",
"|=",",","->",".","[","]","(",")",":
:
","{","}","#",";","\""};
charch;
inti,k;
charbuff[20];
charbuff1[1];
voidoutput(int,char*);
voidzhuyao(FILE*fp){
intj;
ch=fgetc(fp);
while(ch==''||ch=='\n'||ch=='\t')
{ch=fgetc(fp);}
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){
buff[0]=ch;
i=1;
ch=fgetc(fp);
while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9')){
buff[i]=ch;
i++;
ch=fgetc(fp);//从流中读取字符
}
buff[i]='\0';/*查找保留字*/
fseek(fp,-1,1);
j=0;
while((jj++;
if(j>=keyword)
output(2,buff);
else
output(1,buff);
}
elseif(ch>='0'&&ch<='9'){
buff[0]=ch;
ch=fgetc(fp);
i=1;
while(ch>='0'&&ch<='9'){
buff[i]=ch;
i++;
ch=fgetc(fp);
}
buff1[i]=ch;
ch=fgetc(fp);
if(buff1[i]=='.'&&(ch>='0'&&ch<='9')){
while(ch>='0'&&ch<='9'){
i++;
buff[i]=ch;
ch=fgetc(fp);
}
i++;
buff[i]='\0';
}
elsebuff[i]='\0';
output(5,buff);
fseek(fp,-1,1);
}
elseif(strchr(fen,ch)>0)/*分隔符号*/
{
buff[0]=ch;
ch=fgetc(fp);
buff[1]='\0';
fseek(fp,-1,1);
k=0;
if((koutput(3,buff);
}
elseif(strchr(yun,ch)>0)/*运算符号*/
{
buff[0]=ch;
ch=fgetc(fp);
if(buff[0]=='/'&&ch=='*'){
while(ch!
='/'){
ch=fgetc(fp);
}
}
elseif(buff[0]=='/'&&ch=='/'){
while(ch!
='\n'){