中南民族大学计算机科学学院本科课程设计任务书设计名称:算术表达式语法检查指导教师:下达时间: 2015-5-8学生姓名:学号:专业:一、课程设计的基本要求根据所学知识,编写指定题目的C++语言程序,并规范地完成课程设计报告。
通过课程设计,加深对《C++面向对象程序设计》课程所学知识的理解,熟练掌握和巩固C++语言的基本知识和语法规范,掌握C++语言的基础知识,理解面向对象系统的封装性、继承性和多态性;熟练使用C语言中的函数、数组、指针、链表和字符串等基本知识;掌握类的定义、标准String类和向量;理解掌握友元函数和重载操作符,动态数组;理解掌握继承和多态性;掌握模版的使用;能够进行程序调试过程中的异常处理;进一步掌握利用C++进行类的定义和操作方法;进一步掌握类的继承和派生方法;进一步理解虚函数和多态;综合利用上述知识,学习设计并编写面向对象的C++简单应用程序;培养解决复杂任务功能分解方法(自顶向下逐步求精、模块化设计、信息隐藏等)。
学会编制结构清晰、风格良好、数据结构适当的C++语言程序,从而具备利用计算机编程分析解决综合性实际问题的初步能力。
具体要求如下:1、采取模块化方式进行程序设计,要求程序的功能设计、数据结构设计及整体结构设计合理。
学生也可根据自己对题目的理解增加新的功能模块(视情况可另外加分)。
2、系统以菜单界面方式(至少采用文本菜单界面,如能采用图形菜单界面更好)工作,运行界面友好,演示程序以用户和计算机的对话方式进行。
3、程序算法说明清晰,理论分析与计算正确,运行情况良好,实验测试数据无误,容错性强(能对错误输入进行判断控制)。
4、编程风格良好(包括缩进、空行、适当注释、变量名和函数名见名知意,程序容易阅读等);5、写出规范的课程设计报告,具体要求见相关说明文档。
二、课程设计的主要内容【问题描述】算术表达式语法检查。
【功能要求】(1)键盘读入一个四则运算算术表达式,对其进行语法检查;(2)算术表达式允许嵌套,如果出错,指出出错位置;(3)不需要计算结果;(4)尽量不使用栈。
程序:(其余的你们自己写)void main() //主函数{int len;int f=1;cout<<endl<<"请输入一个算术表达式(请在一行内完成输入且每个项的长度不大于10):"<<endl;gets(str);len = strlen(str);str[len] = '^';cout<<endl;system("pause");cout<<endl;cout<<"***********************词法分析开始*****************"<<endl;f = cifa_main();if ( f == 0 ) return;cout<<endl;system("pause");cout<<endl;cout<<"***********************语法分析开始*****************"<<endl;f = yufa_main();if (f== 0) return;cout<<endl;system("pause");cout<<endl;}int F1() //F -> (E) | 标识符| 无符号整数{if ((strcmp(cifa_p->word,"(") == 0 ) ){advance();strcpy(F_name,cifa_p->word);strcpy(E_name,F_name);E1();if ((strcmp(cifa_p->word,")") == 0 ) ){advance();strcpy(F_name,E_name);return (1);}else{cout<<"ERROR"<<endl;return (0);}}else if ( cifa_p->type == 1 || cifa_p->type == 2) {strcpy(F_name,cifa_p->word);advance();return (1);}else return 0;}int T1() //T -> F*T | F/T | F{yuyi *p = new yuyi;F1();strcpy(p->op1,F_name);if (strcmp(cifa_p->word,"*") == 0) {advance();T1();p->next =NULL;p->op = '*';strcpy(p->op2,T_name);T_name[0] = 't';T_name[1] = ++count;T_name[2] = '\0';strcpy(p->result,T_name);yuyi_add(p);return(1);}else if (strcmp(cifa_p->word,"/") == 0) {advance();T1();p->next =NULL;p->op = '/';strcpy(p->op2,T_name);T_name[0] = 't';T_name[1] = ++count;T_name[2] = '\0';strcpy(p->result,T_name);yuyi_add(p);return(1);}else{strcpy(T_name,F_name);return(1);}}int E1() //E -> T+E | T-E | T {yuyi *p = new yuyi;T1();strcpy(p->op1,T_name);if (strcmp(cifa_p->word,"+") == 0) {advance();E1();p->next =NULL;p->op = '+';strcpy(p->op2,E_name);E_name[0] = 't';E_name[1] = ++count;E_name[2] = '\0';strcpy(p->result,E_name);yuyi_add(p);return (1);}else if (strcmp(cifa_p->word,"-") == 0){advance();E1();p->next =NULL;p->op = '-';strcpy(p->op2,E_name);E_name[0] = 't';E_name[1] = ++count;E_name[2] = '\0';strcpy(p->result,E_name);yuyi_add(p);return(1);}else{strcpy(E_name,T_name);return(1);}}int yufa_main() //语法分析主程序{int n;cifa *p = new cifa;strcpy(p -> word ,"#"); //对词法分析产生的结果链表进行处理p -> type =-1;p -> next = NULL;cifa_add(p);cifa_p = cifa_head;cout<<endl;yufa_zfc_disp(cifa_head->next);cout<<"的递归分析过程如下:"<<endl;cout<<endl<<"-------------------------------------------------"<<endl;cout<<'\t'<<"步骤\t"<<'\t'<<"产生式"<<endl;advance();n = E();if (n == 0){cout<<'\t'<<f<<'\t'<<'\t'<<"输入串不是该文法的一个句子!"<<endl;cout<<endl<<"------------------语法分析结束------------------"<<endl;return (0);}else if (n == 1){cout<<'\t'<<f<<'\t'<<'\t'<<"输入串是该文法的一个句子!"<<endl;cout<<endl<<"------------------语法分析结束--------------"<<endl;return (1);}}//**********************语义分析*************************************** yuyi *yuyi_add(yuyi *p) //在四元式链表末添加一个结点{yuyi_end->next = p ;yuyi_end = p;return yuyi_head;}void yuyi_sys_disp() //输出四元式链表{yuyi *p;p = yuyi_head->next;while(p!=NULL){ cout<<'('<<'\t'<<p->op<<','<<'\t'<<p->op1<<','<<'\t'<<p->op2<<','<<'\t'<<p->result<<'\t'<<')'< <endl;p = p->next;}cout<<endl;}int F() // F -> (E) | 标识符| 无符号整数子函数{int m;if ((strcmp(cifa_p->word,"(") == 0 ) ){cout<<'\t'<<f++<<'\t'<<'\t'<<"F -> (E)"<<endl;advance();m =E();if (m==0) return (0);if ((strcmp(cifa_p->word,")") == 0 ) ){advance();return (1);}else{cout<<"ERROR"<<endl;return (0);}}else if ( cifa_p->type == 1 || cifa_p->type == 2) //数字或是标识符{cout<<'\t'<<f++<<'\t'<<'\t'<<"F -> 标识符|无符号整数"<<endl; advance();return (1);}else return 0;}int S() // S -> *FS | /FS |ε子函数{int t,g;if (strcmp(cifa_p->word,"*") == 0){cout<<'\t'<<f++<<'\t'<<'\t'<<"S -> *FS"<<endl;advance();t = F();if (t== 0) return 0;g = S();if (g == 0) return 0;return(1);}else if (strcmp(cifa_p->word,"/") == 0){cout<<'\t'<<f++<<'\t'<<'\t'<<"S -> /FS"<<endl;advance();t = F();if (t== 0) return 0;g = S();if (g == 0) return 0;return(1);}else if (strcmp(cifa_p->word,"+") == 0 ||(strcmp(cifa_p->word,"-") == 0)||(strcmp(cifa_p->word,"#") == 0)||(strcmp(cifa_p->word,")") == 0)){cout<<'\t'<<f++<<'\t'<<'\t'<<"S -> ε"<<endl;return(1);}return (0);}int T() // T -> FS 子函数{int t,g;cout<<'\t'<<f++<<'\t'<<'\t'<<"T -> FS"<<endl;t = F();if (t== 0) return 0;g = S();if (g == 0) return 0;return(1);}int G() // G -〉+TG | -TG |ε子函数{int t,g;if (strcmp(cifa_p->word,"+") == 0){cout<<'\t'<<f++<<'\t'<<'\t'<<"G -> +TG"<<endl;advance();t=T();if (t == 0) return(0);g=G();if ( g== 0) return (0);return (1);}else if (strcmp(cifa_p->word,"-") == 0){cout<<'\t'<<f++<<'\t'<<'\t'<<"G -> -TG"<<endl;advance();t=T();if (t == 0) return(0);g=G();if (g == 0) return (0);return(1);}else if (strcmp(cifa_p->word,")") == 0 || strcmp(cifa_p->word,"#") == 0){cout<<'\t'<<f++<<'\t'<<'\t'<<"G -> ε"<<endl;return(1);}return (0);}int E() // E -> [+|-]TG 子函数{int t,g;if ((strcmp(cifa_p->word,"+") == 0)|| (strcmp(cifa_p->word,"-") == 0)) advance(); cout<<'\t'<<f++<<'\t'<<'\t'<<"E -> [+|-]TG"<<endl;t = T();if (t == 0) return (0);g = G();if (g == 0) return (0);else return (1);}void yufa_zfc_disp(cifa *p) //输出字符串{while(p!=NULL){cout<<p->word ;p = p->next;}// cout<<endl;}//************************语法分析部分***************************************void advance() //取词法分析产生列表中的结点作语法分析{cifa_p = cifa_p -> next;}int test(void) //识别相关符号{char temp[3];int i=0;int type;switch (ch){case ';' : //识别';'{temp[i++] = ch;GetChar();if (ch ==' ' ) temp[i++] =' ';temp[i] = '\0';type = 4;break;case '+' : //识别'+'{temp[i++] = ch;GetChar();if (ch ==' ' ) temp[i++] =' '; temp[i] = '\0';type = 3;break;}case '-' : //识别'-'{temp[i++] = ch;GetChar();if (ch ==' ' ) temp[i++] =' '; temp[i] = '\0';type = 3;break;}case '*' : //识别'*'{temp[i++] = ch;GetChar();if (ch ==' ' )temp[i++] =' ';temp[i] = '\0';type = 3;break;}case '/' : //识别'/'temp[i++] = ch; GetChar();if (ch ==' ' )temp[i++] =' '; temp[i] = '\0';type = 3;break;}case '(' : //识别'(' {temp[i++] = ch; GetChar();if (ch ==' ' )temp[i++] =' '; temp[i] = '\0';type = 4;break;}case ')' : // 识别')' {temp[i++] = ch; GetChar();if (ch ==' ' )temp[i++] =' ';temp[i] = '\0'; type = 4;break;}default :{ cout<<ch;cout<<"无法识别,出错!"<<endl;GetChar();if (ch == ' ') notock();return (0);}}if (ch == ' ') notock(); // 空格跳过cifa *p;p = new cifa;p -> next = NULL;p -> type = type;strcpy(p->word,temp);cifa_add(p);return (1);}int cifa_main() //词法分析主函数{int f;cifa_head = new cifa;cifa_head -> type = -1;cifa_head -> next = NULL;cifa_end = cifa_head;cout<<"单词种类定义如下:"<<endl<<endl;cout<<"标识符的种类编码1 :"<<endl<<endl;cout<<"常数的种类编码2 :"<<endl<<endl;cout<<"运算的种类编码3 :+ ,- ,* ,/ "<<endl<<endl; cout<<"界限符的种类编码4 : (,),;"<<endl; GetChar();notock();cout<<"--------------------------------------------------------"<<endl<<"词法分析结果如下:"<<endl;while ( nn < 100 && ch != '^'){if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )f=alph(); //字母串else if (ch >= '0' && ch <= '9') f=number(); //数字串else f=test();//其他符号if (f == 0) return (0);}cifa_disp(cifa_head);cout<<endl<<"--------------词法分析结束---------------------"<<endl;return (1);}int number(void) //识别数字{int type=2;int i=0;char temp[10];while('0'<= ch && ch <= '9'){temp[i] = ch;i++;GetChar();}temp[i]='\0';if (ch == ' ') notock();else if (ch != '^' && ch != '+' && ch != '-' && ch != ';' && ch != '*' && ch != '/' && ch != '('&& ch != ')'){cout<<temp<<"接错误后缀,出错"<<endl;return (0);}if (ch == ' ') notock();cifa *p;p = new cifa;p -> next = NULL;p -> type = type;strcpy(p->word,temp);cifa_add(p);return (1);}int alph(void) //识别标识符{int i=0;char temp[10];int type = 1;temp[i] = ch;i++;GetChar();while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')){temp[i] = ch;i++;GetChar();}temp[i] = '\0';if (ch == ' ') notock();else if (ch != '^' && ch != '+' && ch != '-' && ch != ';' &&ch != '*' && ch != '/' && ch != '('&& ch != ')'){cout<<temp<<"接错误后缀,出错"<<endl;return 0;}cifa *p;p = new cifa;p -> next = NULL;p -> type = type;strcpy(p->word,temp);cifa_add(p);return (1);}cifa *cifa_add(cifa *p) //在分析结果列表尾添加一个新接点{cifa_end -> next = p;cifa_end = cifa_end -> next;return cifa_head;}void cifa_disp(cifa *cifa_head) //输出词法分析结果{cifa *p;p = cifa_head -> next ;while ( p != NULL){cout<<'('<<'\t'<<p->type<<'\t'<<','<<'\t'<<p->word<<'\t'<<')'<<endl; p = p ->next;}}void GetChar() //取字符{ch = str[nn];nn++;}void notock() //去掉空格{if ( ch == ' ' )while ( ch == ' ' )GetChar();}char E_name[10],T_name[10],F_name[10],temp_name[10];//在求四元式的时候用来传递信息yuyi *yuyi_add(yuyi *p); //在四元式链表末添加一个结点void yuyi_sys_disp(); //输出四元式链表int E1(); //E -> T+E | T-E | Tint T1(); //T -> F*T | F/T | Fint F1(); //F -> (E) | 标识符| 无符号整数void yuyi_main(); //语义分析主函数//**********************词法分析部分*********************************//***********************语义分析部分数据结构及函数定义***************** struct yuyi //语义结构体{char op; //操作符char op1[10]; //第一个操作数char op2[10]; //第二个操作数char result[10]; //结果yuyi *next;};yuyi *yuyi_head,*yuyi_end,*yuyi_q,*yuyi_vt; //yuyi队列//***********************语法分析部分函数定义***************** void advance(); //取词法分析产生列表中的结点作语法分析int E(); // E -> [+|-]TG 子函数int G(); // G -〉+TG | -TG |ε子函数int T(); // T -> FS 子函数int S(); // S -> *FS | /FS |ε子函数int F(); // F -> (E) | 标识符| 无符号整数子函数int yufa_main(); //语法分析主函数cifa *cifa_add(cifa *p); //在分析结果列表尾添加一个新接点void cifa_disp(cifa *cifa_head); //输出词法分析结果void GetChar(); //取字符void notock(); //去掉空格int alph(void); //识别标识符int number(void); //识别数字int test(void); //识别相关符号int cifa_main(); //词法分析主函数#include <stdio.h>#include <string.h>#include <ctype.h>#include <malloc.h>#include <math.h>#include <cstdio>#include <iostream.h>#include <cstdlib>#include <fstream>#include <cmath>char str[100]; //输入的算术表达式字符串char ch;int nn=0; //字符串计数器int f=0;算术表达式语法检查实验报告char count='0'; //四元式临时变量计数器//***********************词法分析部分数据结构及函数定义*****************struct cifa //词法结构体{int type; //类型char word[10]; //字符串内容cifa *next;};cifa *cifa_head,*cifa_end,*cifa_p; //cifa队列三、课程设计的进程安排1.2015年5月8日(第9周):布置并下达课程设计题目。