《编写一个简易计算器的源代码》由会员分享,可在线阅读,更多相关《编写一个简易计算器的源代码(12页珍藏版)》请在金锄头文库上搜索。
1、AStack.h#ifndef ASTACK_HEADER#define ASTACK_HEADER#include using namespace std;template class AStackprivate:int size;int top;Elem* listArray;public:AStack() size = 100; top = 0; listArray = new Elem100; AStack() delete listArray; void clear() top = 0; bool push(Elem& item)if (top = size) return fals
2、e;else listArraytop+ = item; return true; bool pop(Elem& it)if (top = 0) return false;else it = listArray-top; return true; bool topValue(Elem& it) constif (top = 0) return false;else it = listArraytop - 1; return true; int length() const return top; ;#endifFunction.cpp#include function.h#include AS
3、tack.h#include #include void calUserInfo()cout= 48) & (int)ch #include #include using namespace std;int main()char str100 = 0;AStack opnd; /用于存放数据AStack optr; /用于存放操作符char now_ch = =; /用于记录当前字符char prior_ch = 0; /用于记录前一个字符double now_dig = 0; /用于记录当前一个数字double prior_dig = 0; /用于记录前一个数字double value =
4、0; /用于存放计算后的值bool point = false; /用于判断是否有小数点int point_num = 1; /用于记录数字后面的小数点位数char topValue; /用于记录 opnd 的 top 上的值char option = Y; /用于判断是否要继续运算doprior_dig = 0; /在 opnd 中提前放一个 0now_ch = =;opnd.push(prior_dig);optr.push(now_ch);system(cls);calUserInfo();cout str;bool exitPoint = false;/*对每个字符的处理*/for(i
5、nt i = 0; i osp(now_ch)if(now_ch = ) & topValue = ()break;dodouble x = 1,y = 0;opnd.pop(x);opnd.pop(y);if( cal(topValue,y,x,value) )opnd.push(value);if(!optr.pop(topValue)break;while(isp(topValue) osp(now_ch);/ if(topValue != ()if (now_ch != )optr.push(topValue);optr.push(now_ch);prior_ch = now_ch;/*其他情况报错*/elsebreak;/*其他情况报错*/elsecout option;/*清空数字栈和操作符栈*/opnd.clear();optr.clear();while(option = Y);return 0;