📄 2.cpp
字号:
#include "prediction.h"
#include "func.h"
const char *Operator[]={
"," , "?" , ":" ,
"||" , "&&" , "|" , "^" , "&" , "!" , "<" , ">" , "~" ,
"+" , "-" , "*" , "/" , "=" , "%" ,
"[" , "]" , "(" , ")"
};
int isstatic(str)
{
const char *StrToken[]=
{"auto","break","case","char","const","continue","default","do",
"double","else","enum","extern","float","for","goto","if",
"int","long","register","return","short","signed","sizeof","static",
"struct","switch","typedef","union","unsigned","void","volatile","while"
};
int i;
for(i=0;i<32;i++)
if(strcmp(str,StrToken[i])==0) return (i+1);
return(0);
}
void SaveResult(char str[],char message[])
{
if(fputs(str,fpw)!=0) exit(0);
if(fputc('<',fpw)==EOF) exit(0);
for(j=0;j<5;j++)
if(fputc('-',fpw)==EOF) exit(0);
if(fputc('>',fpw)==EOF) exit(0);
if(fputs(message,fpw)!=0) exit(0);
if(fputc("\t",fpw)==EOF) exit(0);
}
//////////////////////////////////////////////////////////////////////////////////
void GetMessage()
{
char nowch;
char str[100],message[20];
int i,j;
while(fgetc(fpr)!=EOF)
{
i=0;
nowch=fgetc(fpr);
str[i++]=nowch;
switch(nowch)
{ //跳过空格、换行等
if(isspace(nowch)==1) continue;
//头文件符号#
case '#' : str[i++]='\0';
strcpy(message,"#号");
break;
//字符串
case '"' : while((nowch=fgetc(fpr))!='"')
str[i++]=nowch;
str[i++]='"';str[i++]='\0'
strcpy(message,"字符串");
break;
//注释,/,/=
case '/' : nowch=fgetc(fpr);
if(nowch=='*')
{ str[i++]='*';
strcpy(message,"左解释符");
printf("%s<----->%s\t",str,message);
SaveResult(str,message);
//继续分析至注释结束
while(1)
{
nowch=fgetc(fpr);
if((nowch=fgetc(fpr))=='*')
if((nowch=fgetc(fpr))=='/')
{
strcpy(str,"*/");
strcpy(message,"右解释符");
break;
}
}
}
else
{
while(isspace(nowch)==1) nowch=fgetc(fpr);
if(nowch=='=')
{ str[i++]=nowch;str[i++]='\0';
strcpy(message,"组合运算符\=");
}
else
{
str[i++]='\0';
fseek(fpr,-sizeof(char),1);
strcpy(message,"除法运算符");
}
}
break;
//*,*=
case '*' : nowch=fgetc(fpr);
while(isspace(nowch)==1) nowch=fgetc(fpr);
if(nowch=='=')
{ str[i++]=nowch;str[i++]='\0';
strcpy(message,"组合运算符*=");
}
else
{
str[i++]='\0';
fseek(fpr,-sizeof(char),1);
strcpy(message,"乘法运算符");
}
break;
//+=,++,+
case '+' :
case '-' :
case '<' :
case '>' :
case '=' :
}
//打印分析结果并保存
printf("%s<----->%s\t",str,message);
SaveResult(str,message);
if((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))//字符是字母
}
}
/*
i=0;
while(isspace(nowch)==1)
nowch=fgetc(fpr);
while(islower(nowch)||isupper(nowch))
{
str[i++]=nowch;
nowch=fgetc(fpr);
}
str[i++]='\0';
if(isstatic(str)) strcpy(message,"关键字");
else
printf("%s<----->%s\t",str,message);
SaveResult(str,message);
//头文件名
i=0;
fseek(fpr,-sizeof(char),1);
nowch=fgetc(fpr);
while(isspace(nowch)==1)
nowch=fgetc(fpr);
if(nowch=='<')
{
}
case '^':
cch=GetC();
if(cch=='=')cout<<"29"<<"^=";
else
{
Pushback(cch);cout<<"30"<<"^";
};break;
case '|':
cch=GetC();
switch(cch)
{
case'|':cout<<"31"<<"||";break;
case'=':cout<<"32"<<"|=";break;
default:Pushback(cch);cout<<"33"<<"|";
};break;
case '(':cout<<"34"<<"(";break;
case ')':cout<<"35"<<")";break;
case '[':cout<<"36"<<"[";break;
case ']':cout<<"37"<<"]";break;
case '{':cout<<"38"<<"{";break;
case '}':cout<<"39"<<"}";break;
case '~':cout<<"40"<<"~";break;
case '.':cout<<"41"<<".";break;
case ';':cout<<"42"<<";";break;
case ',':cout<<"43"<<",";break;
//case '#':cout<<"#";break;
case '?':
case '+':
{
cch=GetC();
switch(cch)
{
case '+':cout<<"1"<<"++";break;
case '=':cout<<"2"<<"+=";break;
default:Pushback(cch);cout<<"3"<<"+";break;
}
};break;
case '-':
{
cch=GetC();
switch(cch)
{
case '-':cout<<"4"<<"--";break;
case '=':cout<<"5"<<"-=";break;
case '>':cout<<"6"<<"->";break;
default :Pushback(cch);
cout<<"7"<<"-";
break;
}
};break;
case '*':
{
cch=GetC();
if(cch=='=') cout<<"8"<<"*=";
else
{
Pushback(cch);
cout<<"9(注释):"<<"*";
}
};break;
case '/':
{
cch=GetC();
if(cch=='=') cout<<"10"<<"/=";
else
{
Pushback(cch);
cout<<"11(字符是):"<<"/";
}
};break;
case '<':
{
cch=GetC();
switch(cch)
{
case'=':cout<<"12"<<"<=";break;
case'<':cch=GetC();
if(cch=='=')cout<<"13"<<"<<=";
else
{
Pushback(cch);
cout<<"14"<<"<<";
};
default:Pushback(cch);cout<<"15"<<"<";
}
};break;
case '>':
{
cch=GetC();
switch(cch)
{
case'=':cout<<"16"<<">=";break;
case'>':cch=GetC();
if(cch=='=')cout<<"17"<<">>=";
else
{
Pushback(cch);
cout<<"18"<<">>";break;}
default:Pushback(cch);cout<<"19"<<">";
}
};break;
////////////////////////////////////////////////////////////////////////
case '=':
{
cch=GetC();
if(cch=='=') cout<<"20"<<"==";
else
{
Pushback(cch);
cout<<"21"<<"=";
}
};break;
case '%':
{
cch=GetC();
if(cch=='=') cout<<"22"<<"%=";
else
{
Pushback(cch);
cout<<"23"<<"%";
}
};break;
case '!':
{
cch=GetC();
if(cch=='=') cout<<"24"<<"!=";
else
{
Pushback(cch);
cout<<"25"<<"!";
}
};break;
case '&':
{
cch=GetC();
switch(cch)
{
case '&':cout<<"26"<<"&&";break;
case '=':cout<<"27"<<"&=";break;
default:Pushback(cch);cout<<"28"<<"&";
}
};break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -