C++语言知识计算器源代码.doc

上传人:一*** 文档编号:2737978 上传时间:2020-05-02 格式:DOC 页数:14 大小:55.50KB
返回 下载 相关 举报
C++语言知识计算器源代码.doc_第1页
第1页 / 共14页
C++语言知识计算器源代码.doc_第2页
第2页 / 共14页
点击查看更多>>
资源描述

《C++语言知识计算器源代码.doc》由会员分享,可在线阅读,更多相关《C++语言知识计算器源代码.doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、/ C+语言编写。#include#include#includeusing namespace std;const double pi = 3.14159265;const double e = 2.718281828459;const int SIZE = 1000;typedef struct node/为了处理符号而建立的链表(如: 1+(-2)char data;node *next;node;typedef struct stack_num/存储 数 的栈double *top;double *base;stack_num;typedef struct stack_char/存储

2、运算符号 的栈char *top;char *base;stack_char;stack_num S_num;/定义stack_char S_char;/定义char fu18 = n, ), +, -, *, /, %, , Q, L, C, S, T, c, s, t, (;int compare1000;/表现出各运算符号的优先级double shu1000;/存储 数 的数组double dai_result;/运算的结果,是为了处理 M 运算(简介函数里有M的定义)int biao = 0;/和dia_result 一样,为了处理 M 运算char lineSIZE;/输入的所要计算

3、的表达式void init()/初始化comparefu0 = -2;/用数字的大小表现出符号的优先级comparefu1 = -1;comparefu2 = 2;comparefu3 = 2;comparefu4 = 4;comparefu5 = 4;comparefu6 = 4;comparefu7 = 5;for(int i = 8; i = 15; i+)comparefui = 6;comparefu16 = 7;S_num.base = (double*)malloc(sizeof(double)*SIZE);/为栈开辟空间S_char.base = (char*)malloc(s

4、izeof(char)*SIZE);/同上S_num.top = S_num.base;S_char.top = S_char.base;void push_num(double n)/数字进栈* +S_num.top = n;void push_char(char c)/运算符号进栈* +S_char.top = c;double pop_num()/数字出栈double m = *S_num.top;S_num.top-;return m;char pop_char()/运算符号出栈char cc = *S_char.top;S_char.top-;return cc;char get_t

5、op_char()/得到运算符号的栈中最顶端的运算符号return *S_char.top;double operate(double y, char c, double x)/对两个数计算(含是双目运算符:如 *, / 等等)double r;if(c = -)r = x - y;else if(c = +)r = x + y;else if(c = / & y != 0)r = x / y;else if(c = *)r = x * y;else if(c = )r = 1;for(int i = 1; i = y; i+)r *= x;else if(c = %)int r0 = (in

6、t)x % (int)y;r = double(r0);return r;double operate_one(double one, char cc)/对一个数运算(含单目运算符:如log(L), sin(S) 等等)double r;if(cc = Q)r = sqrt(one);else if(cc = C)r = cos(one);else if(cc = S)r = sin(one);else if(cc = T)r = tan(one);else if(cc = c)r = acos(one);else if(cc = s)r = asin(one);else if(cc = t)

7、r = atan(one);return r;double operate_L(double a, double b, char dian)/求对数的值double r = log(b) / log(a);return r;double compute()/对整个表达式的计算char c;/表示运算符号int p = 0;/用于shu+p, 先初始化int i, j;init();/进行初始化push_char(n);linestrlen(line) = n;linestrlen(line)+1 = 0;if(biao)push_num(dai_result);/把运算的结果先进栈, 在这个结

8、果的基础上继续进行运算biao = 0;for(i = 0; linei != 0;)/把表达式中的数字字符串转化成可计算的数字int flag = 0;int flag1 = 1;/标记是否是运算符号/int flag2 = 1;/标记是否出现_;double h = 0;int ge;/位数int biao_dian = 0;/是否是小数的类型while(1)flag1 = 1;for(j = 0; j = 16; j+)if(linei = fuj)flag1 = 0;break;if(linei = _)break;if(linei = .)i+;ge = 0;biao_dian =

9、1;if(linei = P)shu+p = pi;i+;break;if(linei = E)shu+p = e;i+;break;if(flag1)h = h * 10 + (linei - 0);flag = 1;i+;if(biao_dian)ge+;elsebreak;if(flag)if(biao_dian)int r = 1;for(int k = 1; k = 0)push_num(m);i+;elseif(m = -1)c = +;else if(m = -2)c = -;else if(m = -3)c = *;else if(m = -4)c = /;else if(m

10、= -5)c = %;else if(m = -6)c = ;else if(m = -7)c = Q;else if(m = -8)c = L;else if(m = -9)c = C;else if(m = -10)c = S;else if(m = -11)c = T;else if(m = -12)c = c;else if(m = -13)c = s;else if(m = -14)c = t;else if(m = -15)c = (;else if(m = -16)c = );else if(m = -17)c = n;char ch = get_top_char();/得到最顶

11、端运算符号if(comparech = comparec & ch != ( & ch != n)if(ch = Q | ch = C | ch = S| ch = T| ch = c | ch = s | ch = t)double one = pop_num();char dian = pop_char();push_num(operate_one(one, dian); else if(ch = L)double one_L = pop_num();double two_L = pop_num();char dian = pop_char();push_num(operate_L(two

12、_L, one_L, dian);elsedouble x = pop_num();double y = pop_num();char dian = pop_char();if(dian = / & x = 0)/判断是否除了零cout由于您除了零,结果将是错误的= 3)return 0;else return 1;void output(double result)/打出结果printf(所得结果是: );coutresultendl;void check()/检查表达式是否合法void introduce();char cc;/决定计算器按哪种功能进行计算double result;/结果

13、void input();/定义if( check_kuohao() & check_char() )/看是否合法, 合法则计算result = compute();output(result);cout输入一个字符M或D或F, 决定是否继续: cc)if(cc = M)system(cls);introduce();printf(您上次所得结果为: );coutresultendl;cout在上次计算结果的基础上, 请继续输入想计算的表达式endl;dai_result = result;biao = 1;input();/输入表达式break;else if(cc = D)system(c

14、ls);introduce();cout计算器已清零, 请输入您所要计算的表达式endl;input();/输入表达式break;else if(cc = F)system(cls);cout计算器关闭, 谢谢使用!endl;break;elsecout所输入字符无效, 请输入一个字符M或D或F!endl;continue;else/不合法,分两种不合法if(check_kuohao() = 0 & check_char() = 1)cout您所输入的表达式括号不匹配, 请重新输入:endl;input();/输入表达式elsecout您所输入的表达式不合法, 请重新输入:next = NUL

15、L;int i;for(i = 0; linei != 0; i+)/建立链表p = new node;p-data = linei;p-next = head-next;head-next = p;head = p;/ delete p;q = (node*)malloc(sizeof(node);head = root;if(root-next-data = + | root-next-data = -)/处理第一个字符p = new node;p-data = 0;p-next = head-next;head-next = p;if(root-next != NULL)for(q =

16、root-next; q; q = q-next)if(q-data = ( & (q-next-data = - | q-next-data = +)p = new node;p-data = 0;p-next = q-next;q-next = p;/ delete q;p1 = new node;int qi = -1;for(p1 = root-next; p1; p1 = p1-next)line+qi = p1-data;line+qi = 0;void input()/输入cinline;if(biao = 0)tackle_fuhao();/处理负号check();/检查表达式

17、是否合法void introduce()/对计算器的符号功能的简要介绍 cout计算器简要介绍endl;coutC(cos) S(sin) T(tan) a(arccos) c(arcsin) endl;cout7 8 9 / on t(arctan) endl;cout4 5 6 * % L(log)endl;cout1 2 3 - M(M+) Q(sqrt) endl;cout0 . + (乘方) F(off) Enter(=) endl;cout对于对数输入 L2_5 表示以2为底5的对数endl;coutM(在前面结果的基础上继续计算,如:上次结果为10,现输入+10.5*2)endl

18、;coutD(清零并继续输入)endl;coutF(计算机关闭)endl;cout输入 P 就代表输入圆周率, 输入 E 代表输入自然对数endlendl;void print()system(color 2);cout 欢迎使用本计算器endl;cout输入一个字符串 on, 计算器开始启动start)if(start != on)cout您所输入的字符无效, 请按照介绍的继续输入:endl;continue;elsebreak;if(start = on) system(color 5);/颜色的处理system(cls);/刷屏introduce();/对计算器的简要介绍cout现在,请输入您所要计算的表达式endl;input();/输入所要计算的表达式int main()if_start();/调用是否启动计算器函数return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁