1、中南大学C+课程设计设计报告题 目 C+课程设计 学生姓名 学 号 专业班级 数学类 0901 指导老师 袁修贵 数学科学与计算技术学院2010 年 6 月一、实验目的、内容1.用户任意输入一个年份以及该年的 1 月 1 日是星期几,而后再输入该年的任意一个月份,由程序负责在屏幕上按照你所设计的格式显示出这一个月的月历。思考:利用元年元月元日(即 1 年 1 月 1 日)是星期一的已知事实,可对程序进行改造,让用户仅输入任意一个年份和一个月份,则程序就应按格式显示出该年那一个月的月历。2. 编写程序,从键盘读入一个文本文件名字(可带路径) ,为该文件中的所有单词建立一个词汇索引。按字母顺序显示
2、所有单词(仅一次) ,后面紧跟着它们所在的行号。大写与小写字母被认为是相同的。例如,对于下列的输入文件:To be ornot to be,that is the question.产生的词汇索引如下:be 1 2is 3not 2or 1question 3that 3the 3to 1 23.模拟计算器程序问题描述设计一个程序来模拟一个简单的手持计算器。程序支持算术运算+、-、*、/、= 、以及(清除) 、A(全清除)操作。基本要求程序运行时,显示一个窗口,等待用户输入,用户可以从键盘输入要计算的表达式,输入的表达式显示在窗口中,用户键入= 符号后,窗口显示出结果。测试数据程序输入不少于
3、5 种不同的表达式进行测试。实现提示可定义一个计算器类,该类包括两个组件对象,一个计算引擎和一个用户接口,用户接口对象处理接受的键盘输入信息,并显示答案,计算引擎对象对给出的数据执行相应操作,并存储操作的结果。二、实验程序设计及结构1、 需求分析对需求的信息进行模糊查询 1模拟电子辞典 2模拟计算器 32、 设计结构(1)#include #include int a13 = 0,31,28,31,30,31,30,31,31,30,31,30,31;int main()int i,n,s = 0,r = 0,year,month,days = 0,day,date = 1;int b42 =
4、1,2,3,4,5,6,7; /日历中的星期puts(“Please input the weekday of date month 1/day 1:“);scanf(“%d“,puts(“Please input the date you want:“);scanf(“%d%d%d“,if(year % 4 = 0 for(i=1;i= 7)s = (s + n - 1) % 7;elses = s + n - 1;printf(“weekday:“);switch(s)case 0: puts(“7n“);break;case 1: puts(“1n“);break;case 2: put
5、s(“2n“);break;case 3: puts(“3n“);break;case 4: puts(“4n“);break;case 5: puts(“5n“);break;case 6: puts(“6n“);break;if(days +n)= 7)r = (days + n)%7;elser = days +n;for(i = 0;i 5return 0;(2)#include #include #include #include using namespace std;typedef struct _unitstring word;int line;unit;int main()v
6、ector list;ifstream in;string fileName;coutfileName;in.open(fileName.c_str();if (in.fail()couttemp.word;temp.line = lineNumber;list.push_back(temp);if (in.peek()=n)lineNumber+;in.close();bool isIn = false;vector copy;for (unsigned int i=0; i lineNo;for (unsigned int m=0; m#include #include #include
7、#include #include #include using namespace std; istream* input; / pointer to input stream int no_of_errors; / note: default initialized to 0 double error(const char* s) no_of_errors+; cerr get(ch) return curr_tok = END; while (ch!=n switch (ch) case ;: case n: return curr_tok=PRINT; case *: case /:
8、case +: case -: case (: case ): case =: return curr_tok=Token_value(ch); case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case .: input-putback(ch); *input number_value; return curr_tok=NUMBER; default: / NAME, NAME=, or error if (isalpha(ch) string_value = ch; while (
9、input-get(ch) / string_value.push_back(ch); / to work around library bug input-putback(ch); return curr_tok=NAME; error(“bad token“); return curr_tok=PRINT; map table; double expr(bool); / cannot do without double prim(bool get) / handle primaries if (get) get_token(); switch (curr_tok) case NUMBER:
10、 / floating-point constant double v = number_value; get_token(); return v; case NAME: double if (get_token() = ASSIGN) v = expr(true); return v; case MINUS: / unary minus return -prim(true); case LP: double e = expr(true); if (curr_tok != RP) return error(“) expected“); get_token(); / eat ) return e
11、; default: return error(“primary expected“); double term(bool get) / multiply and divide double left = prim(get); for (;) switch (curr_tok) case MUL: left *= prim(true); break; case DIV: if (double d = prim(true) left /= d; break; return error(“divide by 0“); default: return left; double expr(bool g
12、et) / add and subtract double left = term(get); for (;) / forever switch (curr_tok) case PLUS: left += term(true); break; case MINUS: left -= term(true); break; default: return left; int main(int argc, char* argv) switch (argc) case 1: / read from standard input input = break; case 2: / read argumen
13、t string / input = new istringstream(argv1); input = new istrstream(argv1); break; default: error(“too many arguments“); return 1; table“pi“ = 3.1415926535897932385; / insert predefined names table“e“ = 2.7182818284590452354; while (*input) get_token(); if (curr_tok = END) break; if (curr_tok = PRIN
14、T) continue; cout #include int a13 = 0,31,28,31,30,31,30,31,31,30,31,30,31;int main()int i,n,s = 0,r = 0,year,month,days = 0,day,date = 1;int b42 =1,2,3,4,5,6,7; /日历中的星期puts(“Please input the weekday of date month 1/day 1:“);scanf(“%d“,puts(“Please input the date you want:“);scanf(“%d%d%d“,if(year %
15、 4 = 0 for(i=1;i= 7)s = (s + n - 1) % 7;elses = s + n - 1;printf(“weekday:“);switch(s)case 0: puts(“7n“);break;case 1: puts(“1n“);break;case 2: puts(“2n“);break;case 3: puts(“3n“);break;case 4: puts(“4n“);break;case 5: puts(“5n“);break;case 6: puts(“6n“);break;if(days +n)= 7)r = (days + n)%7;elser =
16、 days +n;for(i = 0;i 5return 0;2. #include #include #include #include using namespace std;typedef struct _unitstring word;int line;unit;int main()vector list;ifstream in;string fileName;coutfileName;in.open(fileName.c_str();if (in.fail()couttemp.word;temp.line = lineNumber;list.push_back(temp);if (i
17、n.peek()=n)lineNumber+;in.close();bool isIn = false;vector copy;for (unsigned int i=0; i lineNo;for (unsigned int m=0; m#include #include #include #include #include #include using namespace std; istream* input; / pointer to input stream int no_of_errors; / note: default initialized to 0 double error
18、(const char* s) no_of_errors+; cerr get(ch) return curr_tok = END; while (ch!=n switch (ch) case ;: case n: return curr_tok=PRINT; case *: case /: case +: case -: case (: case ): case =: return curr_tok=Token_value(ch); case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
19、case .: input-putback(ch); *input number_value; return curr_tok=NUMBER; default: / NAME, NAME=, or error if (isalpha(ch) string_value = ch; while (input-get(ch) / string_value.push_back(ch); / to work around library bug input-putback(ch); return curr_tok=NAME; error(“bad token“); return curr_tok=PRI
20、NT; map table; double expr(bool); / cannot do without double prim(bool get) / handle primaries if (get) get_token(); switch (curr_tok) case NUMBER: / floating-point constant double v = number_value; get_token(); return v; case NAME: double if (get_token() = ASSIGN) v = expr(true); return v; case MIN
21、US: / unary minus return -prim(true); case LP: double e = expr(true); if (curr_tok != RP) return error(“) expected“); get_token(); / eat ) return e; default: return error(“primary expected“); double term(bool get) / multiply and divide double left = prim(get); for (;) switch (curr_tok) case MUL: lef
22、t *= prim(true); break; case DIV: if (double d = prim(true) left /= d; break; return error(“divide by 0“); default: return left; double expr(bool get) / add and subtract double left = term(get); for (;) / forever switch (curr_tok) case PLUS: left += term(true); break; case MINUS: left -= term(true);
23、 break; default: return left; int main(int argc, char* argv) switch (argc) case 1: / read from standard input input = break; case 2: / read argument string / input = new istringstream(argv1); input = new istrstream(argv1); break; default: error(“too many arguments“); return 1; table“pi“ = 3.1415926535897932385; / insert predefined names table“e“ = 2.7182818284590452354; while (*input) get_token(); if (curr_tok = END) break; if (curr_tok = PRINT) continue; cout expr(false) n; if (input != return no_of_errors;d 教师评语及计分