📄 a.c
字号:
#include <stdio.h>
#include <string.h>
#include <process.h>
#define MAX 50
void main()
{ FILE *in,*out;/*存放输入字符串和输出单词串的文件*/
char arr[MAX];/*arr数组存放单词符号*/
char currentchar;/*currentchar存放当前输入字符*/
int i=0;/*arr数组的一个指针*/
/*以读方式打开输入文件*/
if((in=fopen("infile.txt","r"))==NULL)
{printf("cannont open infile\n"); exit(0); }
/*以写方式打开输出文件*/
if((out=fopen("outfile.txt","w"))==NULL)
{printf("cannout open outfile\n"); exit(1); }
/*过滤掉开始的空格*/
currentchar=fgetc(in);
while(currentchar==' ')
currentchar=fgetc(in);
/*判断常数部分*/
if(currentchar>='0'&¤tchar<='9')
{ arr[i++]=currentchar; currentchar=fgetc(in);
/*再读入下一个字符*/
while(currentchar>='0'&¤tchar<='9')
{ arr[i++]=currentchar; currentchar=fgetc(in); }
if(currentchar!='.')/*如果当前读入的符号不为小数点,则常数读完毕,输出此常数*/
{ fprintf(out,"%s,%6d,%s\n","常数",2,arr);/*格式化输出到目标文件, 2代表类别
arr为内码值,规定用数字本身代表*/ }
else/*当前字符为小数点则继续向下读入*/
{ arr[i++]=currentchar; currentchar=fgetc(in);
while(currentchar>='0'&¤tchar<='9')
{ arr[i++]=currentchar; currentchar=fgetc(in); }
fprintf(out,"%s,%6d,%s\n","常数",2,arr);
}
} /*判断标识符和关键字部分*/
else if(currentchar>='a'&¤tchar<='z')
{ i=0;/*清空arr字符数组*/
while((currentchar>='a'&¤tchar<='z')||(currentchar>='0'&¤tchar<='9'))
{ arr[i++]=currentchar; currentchar=fgetc(in); } /*把字符数组arr和关键字表比较,判断单词串是关键字还是标识符*/ arr[i++]='\0'; /*关键字的类别编码从3开始个不一样,所以没必要再用内码值标识*/ if(strcmp(arr,"program")==0) fprintf(out,"%s,%6d\n",arr,3); else if(strcmp(arr,"begin")==0) fprintf(out,"%s,%6d\n",arr,4); else if(strcmp(arr,"end")==0) fprintf(out,"%s,%6d\n",arr,5); else if(strcmp(arr,"if")==0) fprintf(out,"%s,%6d\n",arr,6); else if(strcmp(arr,"then")==0) fprintf(out,"%s,%6d\n",arr,7); else if(strcmp(arr,"else")==0) fprintf(out,"%s,%6d\n",arr,8); else fprintf(out,"s%,%6d,%s\n","标识符",1,arr);/*标识符的类别编码为1, 内码值用字符串本身表*/
} else if(currentchar=='+')
{ fprintf(out,"%c,%6d\n",currentchar,9);
currentchar=fgetc(in); }
else if(currentchar=='-')
{ fprintf(out,"%c,%6d\n",currentchar,10);
currentchar=fgetc(in); }
else if(currentchar=='*')
{ fprintf(out,"%c,%6d\n",currentchar,11);
currentchar=fgetc(in); }
else if(currentchar=='/')
{ fprintf(out,"%c,%6d\n",currentchar,12);
currentchar=fgetc(in); }
else if(currentchar==';')
{ fprintf(out,"%c,%6d\n",currentchar,13);
currentchar=fgetc(in); }
else if(currentchar==':')
{ i=0; arr[i++]=currentchar;
currentchar=fgetc(in);
if(currentchar=='=')
{ arr[i++]=currentchar;
fprintf(out,"%s,%6d\n",arr,14); }
else
fprintf(out,"%s,%6d\n",arr,15);}
else if(currentchar=='=')
{ i=0; arr[i++]=currentchar;
currentchar=fgetc(in);
if(currentchar=='=')
{ arr[i++]=currentchar;
fprintf(out,"%s,%6d\n",arr,16); }
else
fprintf(out,"%s,%6d\n",arr,17); }
else if(currentchar=='>')
{ i=0; arr[i++]=currentchar;
currentchar=fgetc(in);
if(currentchar=='=')
{ arr[i++]=currentchar;
fprintf(out,"%s,%6d\n",arr,18); }
else fprintf(out,"%s,%6d\n",arr,19); }
else if(currentchar=='<')
{ i=0; arr[i++]=currentchar;
currentchar=fgetc(in);
if(currentchar=='=')
{ arr[i++]=currentchar;
fprintf(out,"%s,%6d\n",arr,20); }
else if(currentchar=='>')
{ arr[i++]=currentchar;
fprintf(out,"%s,%6d\n",arr,21); }
else fprintf(out,"%s,%6d\n",arr,22); }
else if(currentchar=='#') exit(2);
else printf("the %c is not a correct letter\n",currentchar);
fclose(in);
fclose(out);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -