#include<stdio.h> #include<string.h> char prog[80],token[8],ch; int syn,p,m,n,sum; char rwtab[6]={"begin","if","then","while","do","end"}; void scaner(); int main() { p=0; printf("\n please input a string(end with '#'):\n"); do{ scanf("%c",&ch); prog[p++]=ch; }while(ch!='#'); p=0; do{ scaner(); switch(syn) { case 11:printf("(%-10d%5d)\n",sum,syn); break; case -1:printf("you have input a wrong string:\n"); getchar(); return 0; default:printf("(%-10s%5d)\n",token,syn); break;} }while(syn!=0); getchar(); return 0; } void scaner(){ sum=0; for(m=0;m<8;m++) token[m++]=NULL; ch=prog[p++]; m=0; while((ch==' ')||(ch=='\n')) ch=prog[p++]; if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))) {while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))||((ch>='0')&&(ch<='9'))) {token[m++]=ch; ch=prog[p++];} p--; syn=10; for(n=0;n<6;n++) if(strcmp(token,rwtab[n])==0) {syn=n+1; break;} } else if((ch>='0')&&(ch<='9')) {while((ch>='0')&&(ch<='9')) { sum=sum10+ch-'0'; ch=prog[p++]; } p--; syn=11; } else switch(ch) { case '<':token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=22; token[m++]=ch; } else{ syn=20; p--; } break; case '>':token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=24; token[m++]=ch; } else{ syn=23; p--; } break; case '+':token[m++]=ch; ch=prog[p++]; if(ch=='+') { syn=17; token[m++]=ch; } else{ syn=13; p--; } break; case '-':token[m++]=ch; ch=prog[p++]; if(ch=='-') { syn=29; token[m++]=ch; } else{ syn=14; p--; } break; case '!':ch=prog[p++]; if(ch=='=') { syn=21; token[m++]=ch; } else{ syn=3; p--; } break; case '=':token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=25; token[m++]=ch; } else{ syn=18; p--; } break; case '*':syn=15; token[m++]=ch; break; case '/':syn=16; token[m++]=ch; break; case '(':syn=27; token[m++]=ch; break; case ')':syn=28; token[m++]=ch; break; case '{':syn=5; token[m++]=ch; break; case '}':syn=6; token[m++]=ch; break; case ';':syn=26; token[m++]=ch; break; case '"':syn=30; token[m++]=ch; break; case '#':syn=0; token[m++]=ch; break; case ':':syn=17; token[m++]=ch; break; default:syn=-1; break; } token[m++]='\0'; }怎么让输出结果value在前和code在后
时间: 2023-10-15 22:06:56 浏览: 82
在switch语句中,根据不同的情况,可以将value和code分别存储到两个不同的数组中,最后遍历两个数组,分别输出value和code即可。以下是修改后的代码:
```
#include<stdio.h>
#include<string.h>
char prog[80],token[8],ch;
int syn,p,m,n,sum;
char rwtab[6]={"begin","if","then","while","do","end"};
void scaner();
int main() {
p=0;
printf("\n please input a string(end with '#'):\n");
do{
scanf("%c",&ch); prog[p++]=ch;
}while(ch!='#');
p=0;
do{
scaner();
switch(syn) {
case 11: printf("(%-10d%5d)\n",sum,syn); break;
case -1: printf("you have input a wrong string:\n");
getchar(); return 0;
default: break;
}
}while(syn!=0);
// 存储value和code
char value_arr[100][10];
char code_arr[100][10];
int value_index = 0;
int code_index = 0;
p = 0;
do{
scaner();
switch(syn) {
case 11: sprintf(value_arr[value_index], "%d", sum);
strcpy(code_arr[code_index], "value");
value_index++;
code_index++;
break;
case -1: printf("you have input a wrong string:\n");
getchar(); return 0;
default: strcpy(value_arr[value_index], token);
strcpy(code_arr[code_index], "code");
value_index++;
code_index++;
break;
}
}while(syn!=0);
// 输出value和code
for (int i = 0; i < value_index; i++) {
if (strcmp(code_arr[i], "value") == 0) {
printf("(%-10s%5s)\n", value_arr[i], "");
} else {
printf("(%-10s%5s)\n", "", code_arr[i]);
}
}
getchar();
return 0;
}
void scaner(){
sum=0;
for(m=0;m<8;m++) token[m++]=NULL;
ch=prog[p++];
m=0;
while((ch==' ')||(ch=='\n')) ch=prog[p++];
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))) {
while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))||((ch>='0')&&(ch<='9'))) {
token[m++]=ch;
ch=prog[p++];
}
p--;
syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0) {
syn=n+1; break;}
}
else if((ch>='0')&&(ch<='9')) {
while((ch>='0')&&(ch<='9')) {
sum=sum*10+ch-'0';
ch=prog[p++];
}
p--;
syn=11;
}
else
switch(ch) {
case '<':token[m++]=ch; ch=prog[p++];
if(ch=='=') { syn=22; token[m++]=ch; }
else{ syn=20; p--; }
break;
case '>':token[m++]=ch; ch=prog[p++];
if(ch=='=') { syn=24; token[m++]=ch; }
else{ syn=23; p--; }
break;
case '+':token[m++]=ch; ch=prog[p++];
if(ch=='+') { syn=17; token[m++]=ch; }
else{ syn=13; p--; }
break;
case '-':token[m++]=ch; ch=prog[p++];
if(ch=='-') { syn=29; token[m++]=ch; }
else{ syn=14; p--; }
break;
case '!':ch=prog[p++];
if(ch=='=') { syn=21; token[m++]=ch; }
else{ syn=3; p--; }
break;
case '=':token[m++]=ch; ch=prog[p++];
if(ch=='=') { syn=25; token[m++]=ch; }
else{ syn=18; p--; }
break;
case '*':syn=15; token[m++]=ch; break;
case '/':syn=16; token[m++]=ch; break;
case '(':syn=27; token[m++]=ch; break;
case ')':syn=28; token[m++]=ch; break;
case '{':syn=5; token[m++]=ch; break;
case '}':syn=6; token[m++]=ch; break;
case ';':syn=26; token[m++]=ch; break;
case '"':syn=30; token[m++]=ch; break;
case '#':syn=0; token[m++]=ch; break;
case ':':syn=17; token[m++]=ch; break;
default:syn=-1; break;
}
token[m++]='\0';
}
```
在上述代码中,我们新建了两个数组`value_arr`和`code_arr`,分别用来存储value和code。在第一次遍历中,如果当前`syn`的值为11,则说明当前token是一个数字,我们就将其存入`value_arr`中,并将`code_arr`对应位置的值设为"value"。如果当前不是数字,则说明当前token是一个代码标识符,我们将其存入`value_arr`中,并将`code_arr`对应位置的值设为"code"。在第二次遍历中,我们就可以遍历两个数组,分别输出value和code了。
阅读全文