#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i 15
求大神指导啊#include<stdio.h>#include<string.h>#include<stdlib.h>#include<ctype.h>#include<...
求大神指导啊
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
void scanner(FILE *fp)
int IsAlpha(char c)
int IsNum(char c)
int main(void)
{struct SIGN
{int typecode;
char *Word[128];}sign;
char ch;
char in_fn[30];
FILE *fp;
printf("\n请输入源文件名(包括路径和后缀名):");
while (1)
{gets(in_fn);
scanf("%s",in_fn);
if ((fp=fopen(in_fn,"r"))!=NULL)
break;//读取文件内容,并返回文件指针,该指针指向文件的第一个字符
else printf("文件路径错误!请重新输入:");}
printf("\n*********************词法分析结果如下**********************");
do {ch=fgetc(fp);
if (ch=='\0')
break;
else fseek(fp,-1,1);
scanner(*fp);
}while(ch!="#");
system("pause");}
int IsAlpha(char c)//字母判断
{if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
return 1;
else
return 0;}
int IsNum(char c)//数字判断
{if(c>='0'&&c<='9')
return 1;
else
return 0;}
void scanner(File *fp)//扫描函数
{Struct Sign sign;
int i;
sign.typecode=0;
for (i=0;i<=128;i++)
{sign.Word[i]=0;}
char ch;
int i,c;
ch=fgetc(fp);
if (IsNum(ch)
{i=0;
while(IsNum(ch))
{sign.Word[i]=ch;
i++;
ch=fgetc(fp);}
printf("<%d,%s>",sign.typecode,sign.Word);}
else if (IsAlpha(ch))
{i=0;
sign.Word[0]=ch;
ch=fgetc(fp);
i=1;
while(IsNum(ch)||IsAlpha(ch))
{sign.Word[i]=ch;
i++;
ch=fgetc(fp);}
sign.Word[i]='\0';
fseek(fp,-1,1);
sign.typecode=2;
printf("<%d,%s>",sign.typecode,sign.Word);}
else if((ch=='+')||(ch=='*')||(ch=='(')||(ch==')'))
{switch ch
{case '+':
{sign.Word[0]=ch;
sign.typecode=3;
printf("<%d,%s>",sign.typecode,sign.Word);break;}
case '*':
{sign.Word[0]=ch;
sign.typecode=4;
printf("<%d,%s>",sign.typecode,sign.Word);break;}
case '(':
{sign.Word[0]=ch;
sign.typecode=5;
printf("<%d,%s>",sign.typecode,sign.Word);break;}
case ')':
{sign.Word[0]=ch;
sign.typecode=6;
printf("<%d,%s>",sign.typecode,sign.Word);break;}}}
else if (ch=='\n')
{sign.Word[0]='回';
sign.Word[1]='车';
sign.Word[2]='符';
sign.typecode=7;
printf("<%d,%s>",sign.typecode,sign.Word);}
else if (ch=='\0')
printf("首字符请不要是空字符,非法输入");
else
printf("对不起本词法分析器不能识别");} 展开
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
void scanner(FILE *fp)
int IsAlpha(char c)
int IsNum(char c)
int main(void)
{struct SIGN
{int typecode;
char *Word[128];}sign;
char ch;
char in_fn[30];
FILE *fp;
printf("\n请输入源文件名(包括路径和后缀名):");
while (1)
{gets(in_fn);
scanf("%s",in_fn);
if ((fp=fopen(in_fn,"r"))!=NULL)
break;//读取文件内容,并返回文件指针,该指针指向文件的第一个字符
else printf("文件路径错误!请重新输入:");}
printf("\n*********************词法分析结果如下**********************");
do {ch=fgetc(fp);
if (ch=='\0')
break;
else fseek(fp,-1,1);
scanner(*fp);
}while(ch!="#");
system("pause");}
int IsAlpha(char c)//字母判断
{if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
return 1;
else
return 0;}
int IsNum(char c)//数字判断
{if(c>='0'&&c<='9')
return 1;
else
return 0;}
void scanner(File *fp)//扫描函数
{Struct Sign sign;
int i;
sign.typecode=0;
for (i=0;i<=128;i++)
{sign.Word[i]=0;}
char ch;
int i,c;
ch=fgetc(fp);
if (IsNum(ch)
{i=0;
while(IsNum(ch))
{sign.Word[i]=ch;
i++;
ch=fgetc(fp);}
printf("<%d,%s>",sign.typecode,sign.Word);}
else if (IsAlpha(ch))
{i=0;
sign.Word[0]=ch;
ch=fgetc(fp);
i=1;
while(IsNum(ch)||IsAlpha(ch))
{sign.Word[i]=ch;
i++;
ch=fgetc(fp);}
sign.Word[i]='\0';
fseek(fp,-1,1);
sign.typecode=2;
printf("<%d,%s>",sign.typecode,sign.Word);}
else if((ch=='+')||(ch=='*')||(ch=='(')||(ch==')'))
{switch ch
{case '+':
{sign.Word[0]=ch;
sign.typecode=3;
printf("<%d,%s>",sign.typecode,sign.Word);break;}
case '*':
{sign.Word[0]=ch;
sign.typecode=4;
printf("<%d,%s>",sign.typecode,sign.Word);break;}
case '(':
{sign.Word[0]=ch;
sign.typecode=5;
printf("<%d,%s>",sign.typecode,sign.Word);break;}
case ')':
{sign.Word[0]=ch;
sign.typecode=6;
printf("<%d,%s>",sign.typecode,sign.Word);break;}}}
else if (ch=='\n')
{sign.Word[0]='回';
sign.Word[1]='车';
sign.Word[2]='符';
sign.typecode=7;
printf("<%d,%s>",sign.typecode,sign.Word);}
else if (ch=='\0')
printf("首字符请不要是空字符,非法输入");
else
printf("对不起本词法分析器不能识别");} 展开
1个回答
展开全部
%A<p>修改后的代码:(已检测,可执行)</p><pre t="code" l="cpp">#include<stdio.h>%D%A#include<string.h>%D%A#include<stdlib.h>%D%Avoid main()%D%A{ %D%A char *a; %D%A int m,n,i,j; %D%A printf("你要输入几个字母:"); %D%A scanf("%d",&n); %D%A %D%A a=(char*)malloc((n+1)*sizeof(char));//带串结束符 %D%A memset(a,0,(n+1)*sizeof(char));//初始化 %D%A //n=strlen(a); //以'\0'(也就是0)为串结束符,统计串长度 ,清零后这个长度是0 %D%A %D%A printf("请输入你想要输入的%d个字母:",n); %D%A scanf("%c",&a[0]);//去除输入n后的Enter键值%D%A %D%A for(i=0; i<n; i++) %D%A scanf("%c",&a[i]); %D%A %D%A %D%A for(i=0; i<n; i++) %D%A { %D%A if(a[i]=='y') %D%A { %D%A for(j=i; j<n; j++) %D%A a[j]=a[j+1]; %D%A i--;//移位后当前位要重新判断 %D%A }%D%A } %D%A %D%A m=strlen(a);//这个长度时串的长度 %D%A printf("去掉y后的字符串为:"); %D%A %D%A for(i=0; i<m; i++) %D%A printf("%c",a[i]);%D%A %D%A free(a); %D%A getch(); %D%A}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询