《C语言计算器源代码.doc》由会员分享,可在线阅读,更多相关《C语言计算器源代码.doc(20页珍藏版)》请在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.7;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/存储 运算符号 的栈char
2、*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;/输入的所要计算的表达式void ini
3、t()/初始化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(sizeof(char)*
4、SIZE);/同上S_num = S_num.base;S_char = S_char.base;void push_num(double n)/数字进栈* +S_num = n;void push_char(char c)/运算符号进栈* +S_char = c;double pop_num()/数字出栈double m = *S_num;S_num-;return m;char pop_char()/运算符号出栈char cc = *S_char;S_char-;return cc;char get_top_char()/得到运算符号的栈中最顶端的运算符号return *S_char;do
5、uble 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 = (int)x % (int)y;r = double(r0);return r;double oper
6、ate_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)r = atan(one);return r;double operate_L(double a
7、, 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);/把运算的结果先进栈, 在这个结果的基础上继续进行运算biao = 0;for(i = 0; linei != 0;)/把表达式
8、中的数字字符串转化成可计算的数字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 = 1;if(linei = P)shu+p = pi;i+;break;if(linei = E)
9、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 = -5)c = %;else if(m = -6)c = ;else if(m = -7)c
10、= 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();/得到最顶端运算符号if(comparech = comparec & ch != ( & ch != n
11、)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_L, one_L, dian);elsedouble x = pop_num();double
12、 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;/结果void input();/定义if( check_kuohao() & check_char(
13、) )/看是否合法, 合法则计算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(cls);introduce();cout计算器已清零, 请输入您所要计算的表达式endl;inp
14、ut();/输入表达式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 = NULL;int i;for(i = 0; linei != 0; i+)/建立链表p = new n
15、ode;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 = root-next; q; q = q-next)if(q-data = ( & (q-next
16、-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();/检查表达式是否合法void introduce()/对计算器的符号功能的简要介绍cout计算器简要介绍en
17、dl;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;coutD(清零并继续输入)endl;coutF(计算机关闭)endl;cout输入 P 就代表
18、输入圆周率, 输入 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;第 20 页