习题5-19 修改undcl程序,使它在把文字描述转化为声明的过程中不会生成多余的圆括号
来源:互联网 发布:网络hk什么意思 编辑:程序博客网 时间:2024/06/03 13:45
错误程序:
#include <stdio.h>#include <string.h>#include <ctype.h>#define MAXTOKEN 100enum {NAME,PARENS,BRACKETS};enum {NO,YES};int gettoken(void);int tokentype;char token[MAXTOKEN];char name[MAXTOKEN];char out[1000];int prevtoken;main(){ int type; char temp[MAXTOKEN]; while(gettoken()!='\n'){ strcpy(out,token); while((type=gettoken())!=EOF) if(type==PARENS || type==BRACKETS) strcat(out,token); else if(type=='*'){ if((type==gettoken())==PARENS || type==BRACKETS) sprintf(temp,"(*%s)",out); else if(type==NAME) sprintf(temp,"*%s",out); prevtoken=YES; strcpy(out,temp); } else if(type==NAME){ sprintf(temp,"%s %s",token,out); strcpy(out,temp); } else printf("invalid input at %s\n",token); printf("%s\n",out); }}int gettoken(void){ int c,getch(void); void ungetch(int); char *p=token; if(prevtoken==YES){ prevtoken=NO; return tokentype; } while((c=getch())==' '||c=='\t') ; if(c=='('){ if((c=getch())==')'){ strcpy(token,"()"); return tokentype=PARENS; } else{ ungetch(c); return tokentype='('; } } else if(c=='['){ for(*p++=c;(*p++=getch())!=']';) ; *p='\0'; return tokentype=BRACKETS; } else if(isalpha(c)){ for(*p++=c;isalnum(c=getch());) *p++=c; *p='\0'; ungetch(c); return tokentype=NAME; } else return tokentype=c;}#define BUFSIZE 100char buf[BUFSIZE];int bufp=0;int getch(void){ return (bufp>0)? buf[--bufp]:getchar();}void ungetch(int c){ if(bufp>=BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++]=c;}
输入输出如下:
x * charchar ?﹖(\nx * charchar x char
修改:程序第27行==改成=。
0 0
- 习题5-19 修改undcl程序,使它在把文字描述转化为声明的过程中不会生成多余的圆括号
- 修改undcl函数使它在把文字转化为声明的过程中不会生成多余的圆括号
- undcl函数将文字描述转化为声明
- 在ul里面实现把多余的文本转化为“...”
- iOS中修改系统自带的软件上文字描述为中文显示
- .net中把多余的文本显示为...
- 时间戳转化为文字描述
- 把多余文字转化为省略号
- java中把对象转化为byte数组的方法
- Android中把View转化为图片的方法
- CSS 将多余的文字转化为省略号
- 100题15输入一颗二元查找树,把该树转化为它的镜像
- C++/CLI 中字符串类型的转化: 把System::String转化为std::string
- C++/CLI 中字符串类型的转化: 把std::string转化为System::String
- 把C#转化为Javascript的工具
- 把枚举转化为相应的字符串
- 把字符串转化为结合的方式
- 在实际使用它的过程中不需要再次使用 ‘
- 深度剖析之 CountdownView
- 日常小结-python2.x和python3.x之间关于String的区别
- NYOJ-1058部分和问题
- 字符串反转
- memset函数使用方法解析
- 习题5-19 修改undcl程序,使它在把文字描述转化为声明的过程中不会生成多余的圆括号
- AngularJS 事件
- NYOJ-111 分数加减法
- hdoj1181变形课
- 用Thread类创建线程
- 比DES加密更安全的算法——3DES加密算法
- ANSI,Unicode, UTF-8,GBK简单区别
- sql入门--前三章
- android异常收集-关于导入文件出现错误,运行不起来