C++实现一个简单的计算器2.0 增强稳定版
Posted Jing Sir
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现一个简单的计算器2.0 增强稳定版相关的知识,希望对你有一定的参考价值。
如下所示,这是一个简单的计算器2.0 增强稳定版:
输入算式,输出结算结果。
#include<iostream>
#include<cmath>
#include<sstream>
#include<cstdio>
using namespace std;
template <class T>
class Stack
{
private :
T data[1000];
int Top;
int f;
public :
Stack()
{
Top=0;
f=1;
}
void clear()
{
Top=0;
}
bool is_correct()
{
if(f) return true;
return false;
}
T pop()
{
if (Top<=0)
{
f=0;
return data[0];
}
Top--;
return data[Top];
}
void push(T x)
{
if(Top>=1000)
{
cout<<"栈满溢出,错误!"<<endl;
return;
}
data[Top++]=x;
}
T top()
{
if(Top<=0)
{
f=0;
return data[0];
}
return data[Top-1];
}
bool is_empty()
{
if(Top<=0) return true;
return false;
}
};
class Calculator
{
private :
string postfixExp;
string infixExp;
double ans;
int f ;
public :
Calculator(string s="")
{
infixExp=s;
ans=0;
f=1;
}
void display()
{
if(1)
{
cout<<"表达式错误"<<endl;
return ;
}
cout<<"后缀表达式为:"<<postfixExp<<endl;
cout<<"表达式的值为:";
printf("%.2f\\n",ans);
}
double calculate(string infixExp)
{
this->infixExp= infixExp;
if (infix_to_postfix())
{
return ans=cal_postfix();
}
else
{
f=0;
cout<<"语法错误,请检查表达式"<<endl;
return 0.0;
}
}
bool infix_to_postfix()
{
f = 1;
Stack<char> stk;
int a_num_end=0,is_negative=0;
string result="#";
stk.push('#');
for (int i=0; i<infixExp.length(); i++)
{
char c = infixExp[i];
if(c=='-'&&(i==0||infixExp[i-1]=='('))
{
result+='-';
is_negative=1;
continue;
}
if((c<='9'&&c>='0')||c=='.')
{
if(a_num_end&&!is_negative)
{
result+='#';
}
is_negative=0;
result+=c;
a_num_end=0;
}
else
{
a_num_end=1;
if(c=='(') stk.push('(');
else if (c==')')
{
while (stk.top()!='(')
{
if (!stk.is_correct())
{
f = 0;
return false;
}
result+='#';
result+=stk.pop();
}
stk.pop();
if (!stk.is_correct())
{
f = 0;
return false;
}
}
else
{
int p = get_priority(c);
if (p==-1) return false;
while (p<=get_priority(stk.top()))
{
if (!stk.is_correct())
{
f = 0;
return false;
}
result+='#';
result+=stk.pop();
}
stk.push(c);
}
}
}
while (stk.top()!='#')
{
if (!stk.is_correct())
{
f = 0;
return false;
}
result+='#';
result+=stk.pop();
}
result+='#';
postfixExp = result;
return true;
}
double cal_postfix()
{
int l = postfixExp.length();
Stack<double> stk1;
for (int i=1; i<l; i++)
{
char c = postfixExp[i];
if (c=='#') continue;
if((c=='-'&&postfixExp[i+1]!='#')||(c>='0'&&c<='9'))
stk1.push(read_num(postfixExp,&i));
else
{
double y = stk1.pop();
if (!stk1.is_correct())
{
f = 0;
return false;
}
double x = stk1.pop();
if (!stk1.is_correct())
{
f = 0;
return false;
}
stk1.push(cal(x,y,c));
}
}
return stk1.pop();
}
double cal(double num1 ,double num2,char op)
{
switch(op)
{
case '+':
return num1+num2;
case '-':
return num1-num2;
case '*':
return num1*num2;
case '/':
if (num2==0)
{
f = 0;
cout<<"除数不能为零"<<endl;
return 0;
}
return num1/num2;
default:
return 0.0;
}
}
string change(double x)
{
int y = (int)(x*10000),yy = y%10000;
stringstream ss;
ss<<y/10000<<'.'<<yy/1000<<(yy%1000)/100<<(yy%100)/10<<(yy%10);
string r;
ss>>r;
int i;
for (i=r.length()-1; i>=0; i--)
if(r[i]!='0') break;
if (r[i]=='.') i--;
return r.assign(r,0,i+1);
}
int get_priority(char c)
{
switch(c)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
return 0;
case ')':
return 0;
case '#':
return -2;
default:
return -1;
}
}
double read_num(string s,int *pos)
{
double x=0;
int flag=0,w=0,h=1,i;
if(s[(*pos)]=='-')
{
(*pos)++;
h=-1;
}
for ( i=(*pos); i<s.length(); i++)
if (s[i]!='#')
if(s[i]=='.')
{
flag=1;
}
else
{
if(flag) w++;
x=x*10+s[i]-'0';
}
else break;
(*pos)=i;
return x/pow(10,w)*h;
}
};
int main()
{
Calculator c;
string formula;
while (1)
{
cout<<"输入要计算的表达式:"<<endl;
cin>>formula;
if(formula.length()==1&&formula[0]=='Q') break;
c.calculate(formula);
c.display();
}
return 0;
}
以上是关于C++实现一个简单的计算器2.0 增强稳定版的主要内容,如果未能解决你的问题,请参考以下文章