最新C++语言计算器源代码.doc
《最新C++语言计算器源代码.doc》由会员分享,可在线阅读,更多相关《最新C++语言计算器源代码.doc(29页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
2、stack_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_char.base = (
4、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.top;S_char.top-;return
5、 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 i = 1; i = y; i+)r *= x;else if(c =
6、%)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(cc = c)r = acos(one);else if(cc = s)r = asin(one);e
7、lse 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(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 =
9、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+;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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最新 C+ 语言 计算器 源代码
限制150内