问题遇到的现象和发生背景
C语言程序对操作符判断错误
遇到的现象和发生背景,请写出第一个错误信息
#include <stdio.h>
#include <string.h>
#define boolean int
#define true 1;
#define false 0;
char ch=' ';
char temp[40];
char* keyword[32] ={"auto","break","caes","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","unsiged","viod","volatile","while"};
//是否是关键词
boolean isKeyWord(char* temp){
for(int j=0;j<32;j++){
if(strcmp(temp,keyword[j])==0){
return true;
}
}
return false;
}
//是否是字母
boolean isLetter(char letter){
/*ascii码表中大小字母之前有其他的字符*/
if((letter>='A'&&letter<='Z')||(letter>='a'&&letter<='z')){
return true;
}
else {
return false;
}
}
//是否是数字
boolean isDigit(char digit){
if(digit>='0'&&digit<='9'){
return true;
}
else{
return false;
}
}
//是否是引号
boolean isQuote(char quote){
if(quote=='"'||quote=='\''){
return true;
}
else{
return false;
}
}
//是否是分界符
boolean isBoundary(char boundary){
switch (boundary)
{
case ',':
case ';':
case ':':
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
case '#':
{
return true;
}
default:
{
return false;
}
}
}
//是否是操作符 存在一些占双字符或三字节的运算符
boolean isOperator(char operator){
switch (operator)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '>':
case '<':
case '=':
case '&':
case '|':
case '!':
case '?':
{ return true;
}
default:
return false;
}
}
//是否是下划线
boolean isUnderline(char ul){
if(ul=='_'){
return true;
}
else{
return false;
}
}
void analyze(FILE* fp){
while((ch=fgetc(fp))!=EOF){
//读入tab,空字符,换行符等,什么也不做
if(ch==' ') { }
if(ch=='\t'){ }
if(ch=='\n'){ }
//读入数字后,拼数
else if(isDigit(ch)){
char temp[40]={'\0'};
int i=0;
while(ch=='.'||isDigit(ch)){
temp[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1L,SEEK_CUR);
printf("字面量\t\t%s\n",temp);
}
//读入引号
else if(isQuote(ch)){
if(ch=='"'){
char temp[40]={'\0'};
int i=0;
do{
temp[i]=ch;
i++;
ch=fgetc(fp);
}
while(ch!='"');
temp[i]=ch;
printf("字面量\t\t%s\n",temp);
}
else if(ch=='\''){
if(ch=='\''){
char temp[40]={'\0'};
int i=0;
do{
temp[i]=ch;
i++;
ch=fgetc(fp);
}
while(ch!='\'');
temp[i]=ch;
printf("字面量\t\t%s\n",temp);
}
}
}
//读入字母
else if(isLetter(ch)||isUnderline(ch)){
char temp[40]={'\0'};
int i=0;
while(isLetter(ch)||isDigit(ch)||isUnderline(ch)){
temp[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1L,SEEK_CUR);
if(isKeyWord(temp)){
printf("保留字\t\t%s\n",temp);
}
else {
printf("标识符\t\t%s\n",temp);
}
}
else if(isBoundary(ch)){
printf("分界符\t\t%c\n",ch);
}
else if(isOperator(ch)){
char tmep[40]={'\0'};
int i=0;
while(isOperator(ch)){
temp[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1L,SEEK_CUR);
printf("操作数\t\t%s\n",temp);
}
}
}
void main(){
FILE* fp;
fp= fopen("D:/AB.txt","r");
analyze(fp);
fclose(fp);
system("pause");
}
运行结果及详细报错内容
第一组
第二组
我的解答思路和尝试过的方法
不知道为什么第一组后面的操作符输出时会多+
请问可以从哪些角度分析呢?