首页 技术 正文
技术 2022年11月10日
0 收藏 541 点赞 3,100 浏览 1603 个字

#include <stdio.h>
#include <string.h> char string[],word[],ch;
int type,p,i,n,sum;
char keyword[][]={"begin","if","then","while","do","end"}; //关键字数组void scaner(void); main()
{
p=;
printf("\n\n请输入一个程序段(以'#'结束):\n");
do{
scanf("%c",&ch);
string[p++]=ch;
}while(ch!='#');
printf("单词符号\t种别码 \n");
p=;
do{
scaner();
switch(type)
{
case :
printf(" %-10d%5d\n",sum,type);
break;
case -:
printf("有无法识别的字符\n");
return ;
break;
default:
printf(" %-10s%5d \n",word,type);
break;
}
}while(type!=);
} void scaner(void)
{
sum=;
for(i=;i<;i++)
word[i++]= NULL; ch=string[p++];
i=; while((ch==' ')||(ch=='\n'))
ch=string[p++]; if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
{
while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='')&&(ch<='')))
{
word[i++]=ch;
ch=string[p++];
} p--;
type=; //先将以字母开头的字符识别为标识符 for(n=;n<;n++)
if(strcmp(word,keyword[n])==) //关键字数组比较,相同则表示为关键字,种别码即为下标值加一
{
type=n+;
break;
}
} else if((ch>='')&&(ch<=''))
{
while((ch>='')&&(ch<=''))
{
sum=sum*+ch-''; //将字符型转化为整形sum
ch=string[p++];
}
p--;
type=;
} else //else语句内识别除关键字、标识符、数字以外的其他字符
{
switch(ch)
{
case '+':
type=;
word[i++]=ch;
break; case '-':
type=;
word[i++]=ch;
break; case '*':
type=;
word[i++]=ch;
break; case '/':
type=;
word[i++]=ch;
break; case ':':
word[i++]=ch;
ch=string[p++];
if(ch=='=')
{
type=;
word[i++]=ch;
}
else
{
type=;
p--;
}
break; case '<':
word[i++]=ch;
ch=string[p++];
if(ch=='=')
{
type=;
word[i++]=ch;
}
else
{
type=;
p--;
}
break; case '>':
word[i++]=ch;
ch=string[p++];
if(ch=='=')
{
type=;
word[i++]=ch;
}
else
{
type=;
p--;
}
break; case '=':
type=;
word[i++]=ch;
break; case ';':
type=;
word[i++]=ch;
break; case '(':
type=;
word[i++]=ch;
break; case ')':
type=;
word[i++]=ch;
break; case '#':
type=;
word[i++]=ch;
break; default:
type=-;
break;
}
}
word[i++]='\0'; //word字符数组变字符串
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898