1、C语言计算器源代码 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 st
2、ack_char/存储 运算符号 的栈 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 lin
3、eSIZE;/输入的所要计算的表达式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_
4、char.base = (char*)malloc(sizeof(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.t
5、op; S_char.top-; return cc;char get_top_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
6、i = 1; i = y; i+) r *= x; else if(c = %) int r0 = (int)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(
7、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, 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
8、(line) = n; linestrlen(line)+1 = 0; if(biao) push_num(dai_result);/把运算的结果先进栈, 在这个结果的基础上继续进行运算 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
9、= 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) shu+p = e; i+; break; if(flag1) h = h * 10 + (linei - 0); flag = 1; i+; if(biao_dian) ge+; else break; if(flag) if(biao_dian) int r = 1; for(in
10、t k = 1; k = 0) push_num(m); i+; else if(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 = 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 = -
11、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) if(ch = Q | ch = C | ch = S| ch = T | ch = c | ch = s | ch = t) double one = pop_num();
12、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); else double x = pop_num(); double y = pop_num(); char dian = pop_char(); if(dian = / & x = 0)/判断是否除了零 cout由于您除了
13、零,结果将是错误的= 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() )/看是否合法, 合法则计算 result = compute(); output(result); cout输入一个字符M或D
14、或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; input();/输入表达式 break; else if(cc = F) system(cls);
15、cout计算器关闭, 谢谢使用!endl; break; else cout所输入字符无效, 请输入一个字符M或D或F!endl; continue; else/不合法,分两种不合法 if(check_kuohao() = 0 & check_char() = 1) cout您所输入的表达式括号不匹配, 请重新输入:endl; input();/输入表达式 else cout您所输入的表达式不合法, 请重新输入:next = NULL; int i; for(i = 0; linei != 0; i+)/建立链表 p = new node; p-data = linei; p-next = h
16、ead-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-data = - | q-ne
17、xt-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计算器简要介绍end
18、l; 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 就代表输入圆周率, 输入 E 代表输入自然对数endlendl;void print() system(color 2); cout 欢迎使用本计算器endl; cout输入一个字符串 on, 计算器开始启动start) if(start != on) cout您所输入的字符无效, 请按照介绍的继续输入:endl;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1