我的c\c++之旅(十一)——声明器
来源:互联网 发布:mac照片导入 编辑:程序博客网 时间:2024/05/17 07:46
声明器
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#define MAXLENGTH100#define MAXWORDLENGTH50enum Type_Tag {IDENTIFIER, QUALIFIER, TYPE};struct Words {char name[MAXWORDLENGTH];char type;};int top = -1;struct Words stack[MAXLENGTH];struct Words current;#define push(x)stack[++top] = x#define pop()stack[top--]void Dealwith_Identifier(void);void Dealwith_Declaration(void);void Get_Words(void);enum Type_Tag Classify_Word(void);void Dealwith_Array(void);void Dealwith_Function(void);void Dealwith_Pointer(void);int main(){Dealwith_Identifier();Dealwith_Declaration();return 0;}void Dealwith_Identifier(void){Get_Words();while(current.type != IDENTIFIER) {push(current);Get_Words();}printf("%s is ", current.name);}void Dealwith_Declaration(void){Get_Words();switch(current.type) {case '[':Dealwith_Array();break;case '(':Dealwith_Function();break;}Dealwith_Pointer();while(top != -1) {if(stack[top].type == '(') {pop();Dealwith_Declaration();} elseprintf("%s ", pop().name);}}void Get_Words(void){char *p = current.name;while((*p = getchar()) == ' ');if(isalnum(*p)) {while(isalnum(*++p = getchar()));ungetc(*p, stdin);*p = '\0';current.type = Classify_Word();} else if(*p == '*') {strcpy(p, "pointer to");current.type = '*';} else {current.type = *p;*++p = '\0';}return;}enum Type_Tag Classify_Word(void){char *p = current.name;if(!strcmp(p, "const")) {strcpy(p, "read-only");return QUALIFIER;}if(!strcmp(p, "volatile")) return QUALIFIER;if(!strcmp(p, "char")) return TYPE;if(!strcmp(p, "signed")) return TYPE;if(!strcmp(p, "unsigned")) return TYPE;if(!strcmp(p, "short")) return TYPE;if(!strcmp(p, "int")) return TYPE;if(!strcmp(p, "long")) return TYPE;if(!strcmp(p, "float")) return TYPE;if(!strcmp(p, "double")) return TYPE;if(!strcmp(p, "struct")) return TYPE;if(!strcmp(p, "union")) return TYPE;if(!strcmp(p, "enum")) return TYPE;return IDENTIFIER;}void Dealwith_Array(void){while(current.type == '[') {printf("array ");Get_Words();if(isdigit(current.name[0])) {printf("0...%d ", atoi(current.name)-1);Get_Words();}printf("of ");Get_Words();}}void Dealwith_Function(void){while(current.type != ')')Get_Words();printf("function return ");Get_Words();}void Dealwith_Pointer(void){while(stack[top].type == '*')printf("%s ", pop().name);}
0 0
- 我的c\c++之旅(十一)——声明器
- C专家编程学习之第三章——分析C语言的声明
- 我的C语言之旅——1
- C++——变量的声明、定义
- C语言基础知识之(十一):结构体的内存
- C专家编程学习笔记——第三章(四):C语言声明之 typedef
- C专家编程——分析C语言的声明
- Objective-C 基础知识之(十一):Block
- 水滴石穿C语言之声明的语法
- 水滴石穿C语言之声明的语法
- 水滴石穿C语言之声明的语法
- 水滴石穿C语言之声明的语法
- C 语言之三 声明的语法
- 浅显理解之c语言的声明
- NDK探究之旅《十一》—C代码调用Java代码之项目实战
- [C专家]分析C语言声明——声明是如何形成的[1]
- C语言——声明
- 【读书笔记】Effective C++—4 设计与声明(之1)
- 人脸识别CNN网络训练流程
- C# 索引器的简单理解
- c++作业五
- HTML基础总结二
- 在VC中不显示命令提示符窗口
- 我的c\c++之旅(十一)——声明器
- Java版本 学生管理系统
- 第十、十一周项目四 - 教师兼干部类
- 自定义listview的上拉加载更多
- Generator 插件开发
- MVC简易购物车项目--购物大厅
- 32.数组中的逆序对
- tomcat环境变量的配置
- 网络HTTP、TCP、UDP、Socket知识的总结