栈实现简易计算器

计算数学表达式的值。 输入数学表达式,输出表达式的计算结果。数学表达式由单个数字和运算符+、-、、/、(、)构成,
例如 2+3
(4+5)–6/4。

#include<iostream>
#include<iomanip>
#include<string>
#include<cstdio>
using namespace std;
double result;
int tag;
template<class T>
class arrayStack
{
   public:
       arrayStack();
       ~arrayStack() {delete [] stack;}
       bool empty() const {return stackTop == -1;}
       int size() const
          {return stackTop + 1;}
       T& top(){return stack[stackTop];}
       void pop(){stack[stackTop--].~T();}  // T的析构 
       void push(const T& theElement){stack[++stackTop] = theElement;}
      
   private:
      int stackTop;         // 当前栈顶 
      int arrayLength;      // 栈容量
      T *stack;           // 元素数组 
};
template<class T>//构造函数 
arrayStack<T>::arrayStack()
{
   arrayLength = 2000;
   stack = new T[2000];
   stackTop = -1;
}
arrayStack<double> num;//数字 
arrayStack<char> sign;//运算符

int priority(char a)
{
 if(a=='(')
    return 0;
 else if(a == '+')
    return 1;
 else if(a =='-')
  return 2;
 else if(a == '*')
  return 3;
 else if(a == '/')
  return 3;
 return 0;
}

bool judge(char e)//判断是运算符还是数字 
{
 bool judge=false; 
 if(e>='0' && e<='9')//不能用'0'<=e<='9' 
 {
  judge=true;
 }
 return judge;
}

void pop_caculate()
{
 double l=0,r=0,w=0;
 char ssign=sign.top();
 r=num.top();
 num.pop();
 l=num.top();
 num.pop();
 sign.pop();
 if(ssign == '+') w=l+r;
 else if(ssign == '-') w=l-r;
 else if(ssign == '*') w=l*r;
 else if(ssign == '/') w=l/r;
 num.push(w);
}
void caculate(string expr)//计算函数 
{
 double n=0;
 string::size_type size=0;
 for(string::size_type i=0;i<expr.size();)
 { 
  if(tag==0 && judge(expr[i])==true)
  {
  n=(double)(expr[i]-48);
  if(sign.top()=='-')
  {
   n=(-1.00)*n;
   sign.pop();
   sign.push('+');
  }
  num.push(n);
  i++;
  tag=1;
  } 
  else{
   tag=0;
   if(sign.empty())
   {sign.push(expr[i]);}
   else if(expr[i]=='(')
   {sign.push(expr[i]);}
   else if(expr[i]==')')
   {
    while(sign.top()!='(')
    {
     pop_caculate();
    }
    sign.pop();
    if(sign.top()=='-')
    {
     num.top()=(-1.00)*num.top();
     sign.pop();
     sign.push('+');
    }   
   }
   else if(priority(expr[i])<=priority(sign.top()))
   {
    pop_caculate();
    sign.push(expr[i]);
   }
   else  sign.push(expr[i]);
   ++i;
  }
 }
 while(!sign.empty())
 {
  pop_caculate();
 }
 result=num.top();
 //cout.setf(ios::fixed,ios::floatfield);
 //cout<<result<<endl;
 printf("%.2f",result);
 cout<<endl;
}
int main()
{
 //freopen("a.txt","r",stdin);
 int n;
 cin>>n;
 for(int i=0;i<n;i++)
 {
  string x;
  cin>>x;
  tag=0;
  caculate(x);
 }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的 stack<char> opStack; // 存储运算符的 string expression; // 表达式字符串 cin >> expression; int len = expression.length(); for (int i = 0; i < len; i++) { char ch = expression[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,入数字 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符为空或者当前运算符是左括号,直接入 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符中的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符是加减乘除 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.push(ch); } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ``` 这个程序可以处理包含加减乘除和括号的表达式,但是没有考虑负数的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值