第一个版本:在另一个博客中看到的,那个不是封装在类里边的
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
bool isnumber(char c)
{
if(c>='0'&&c<='9')
{
return true;
}
else
{
return false;
}
}
class Calculate
{
public:
int calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c=='#')
return -2;
}
};
int Calculate::calculate(string s)
{
stack<int> number;
stack<char> operate;
char top;
int a, b;
for (unsigned int i = 0; i < s.size(); ++i) {
if (isnumber(s[i])) {
int Temp = 0;
string temp;
temp += s[i];
while (isnumber(s[++i]))
temp += s[i];
for (unsigned int j = 0; j < temp.size(); ++j) {
Temp = Temp * 10 + temp[j] - 48;
}
number.push(Temp);
temp.clear();
}//将字符数字转换成整形数字
if (!isnumber(s[i])) {
if (operate.empty()) {
operate.push(s[i]);
}//入栈第一个符号'#'
else {
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(') {
operate.push(s[i]);
}//入栈高优先级的运算符
else {
while (priority(s[i]) <= priority(top)) {
if (top == '#'&&s[i] == '#') {
int answer;
operate.pop();
answer=number.top();
cout << "\n答案是:" << answer << endl;
number.pop();
return 0;
}//当运算符实现完全后,只剩下'#'
else if (top == '('&&s[i] == ')') {
++i;
}//当左右括号相遇时,跳过右括号,删除左括号
else {
a=number.top();
number.pop();
b=number.top();
number.pop();
}
if (top == '+') {
b += a;
number.push(b);
}
else if (top == '-') {
b -= a;
number.push(b);
}
else if (top == '*') {
b *= a;
number.push(b);
}
else if (top == '/') {
b /= a;
number.push(b);
}
operate.pop();
top=operate.top();//取前一个运算符,用于与现在扫描的运算符进行比较
}//将优先级高的运算符实现计算
operate.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈
}
}
}//扫描运算符,并判断优先级,以及运算
}//主循环
}
int main()
{
Calculate c;
char line[81];
int result;
cin.getline(line,80);
string s(line);
c.calculate(s);
}
第二个版本:老师写的
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
class calculator
{
public:
int calculate(string expression);
private:
stack<int>opdstack;
stack<char>optstack;
int priority(char opt)
{
switch(opt)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
}
int cal(int opd1,int opd2,char opt)
{
switch(opt)
{
case '+':
return opd1+opd2;
case '-':
return opd1-opd2;
case '*':
return opd1*opd2;
case '/':
return opd1/opd2;
}
}
};
int calculator::calculate(string expression)
{
istringstream strin(expression);
char opt,cur_opt;
int opd,opd1,opd2,result;
strin>>opd;
opdstack.push(opd);
while(strin>>cur_opt)
{
if(optstack.empty())
optstack.push(cur_opt);
else
{
if(priority(cur_opt)>priority(optstack.top()))
optstack.push(cur_opt);
else
{
while(!optstack.empty()&&priority(cur_opt)<=priority(optstack.top()))
{
opd2=opdstack.top();
opdstack.pop();
opd1=opdstack.top();
opdstack.pop();
opt=optstack.top();
optstack.pop();
result=cal(opd1,opd2,opt);
opdstack.push(result);
}
optstack.push(cur_opt);
}
}
strin>>opd;
opdstack.push(opd);
}
while(!optstack.empty())
{
opd2=opdstack.top();
opdstack.pop();
opd1=opdstack.top();
opdstack.pop();
opt=optstack.top();
optstack.pop();
result=cal(opd1,opd2,opt);
opdstack.push(result);
}
result=opdstack.top();
opdstack.pop();
return result;
}
int main()
{
calculator cal;
char line[81];
int result;
cin.getline(line,80);
while(line[0]!='#')
{
string expression(line);
result=cal.calculate(expression);
cout<<"="<<result<<endl;
cin.getline(line,80);
}
}
第三版本:更改第二版本,实现实数计算
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
class calculator
{
public:
double calculate(string expression);
private:
stack<double>opdstack;
stack<char>optstack;
int priority(char opt)
{
switch(opt)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
}
double cal(double opd1,double opd2,char opt)
{
switch(opt)
{
case '+':
return opd1+opd2;
case '-':
return opd1-opd2;
case '*':
return opd1*opd2;
case '/':
return opd1/opd2;
}
}
};
double calculator::calculate(string expression)
{
istringstream strin(expression);
char opt,cur_opt;
double opd,opd1,opd2,result;
strin>>opd;
opdstack.push(opd);
while(strin>>cur_opt)
{
if(optstack.empty())
optstack.push(cur_opt);
else
{
if(priority(cur_opt)>priority(optstack.top()))
optstack.push(cur_opt);
else
{
while(!optstack.empty()&&priority(cur_opt)<=priority(optstack.top()))
{
opd2=opdstack.top();
opdstack.pop();
opd1=opdstack.top();
opdstack.pop();
opt=optstack.top();
optstack.pop();
result=cal(opd1,opd2,opt);
opdstack.push(result);
}
optstack.push(cur_opt);
}
}
strin>>opd;
opdstack.push(opd);
}
while(!optstack.empty())
{
opd2=opdstack.top();
opdstack.pop();
opd1=opdstack.top();
opdstack.pop();
opt=optstack.top();
optstack.pop();
result=cal(opd1,opd2,opt);
opdstack.push(result);
}
result=opdstack.top();
opdstack.pop();
return result;
}
int main()
{
calculator cal;
char line[81];
double result;
cin.getline(line,80);
while(line[0]!='#')
{
string expression(line);
result=cal.calculate(expression);
cout<<"="<<result<<endl;
cin.getline(line,80);
}
}
第四个版本:可实现实数计算,带括号
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
class Calculate
{
public:
int calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c=='#')
return -2;
}
};
int Calculate::calculate(string s)
{
stack<double> number;
stack<char> operate;
char top;
double a, b;
for (unsigned int i = 0; i < s.size(); ++i) {
if (isnumber(s[i])) {
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
temp += s[i];
while (isnumber(s[++i]))
temp += s[i];
for (unsigned int j = 0; j < temp.size(); ++j) {
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}//将字符数字转换成整形数字
if (!isnumber(s[i])) {
if (operate.empty()) {
operate.push(s[i]);
}//入栈第一个符号'#'
else {
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(') {
operate.push(s[i]);
}//入栈高优先级的运算符
else {
while (priority(s[i]) <= priority(top)) {
if (top == '#'&&s[i] == '#') {
double answer;
operate.pop();
answer=number.top();
cout << "\n答案是:" << answer << endl;
number.pop();
return 0;
}//当运算符实现完全后,只剩下'#'
else if (top == '('&&s[i] == ')') {
++i;
}//当左右括号相遇时,跳过右括号,删除左括号
else {
a=number.top();
number.pop();
b=number.top();
number.pop();
}
if (top == '+') {
b += a;
number.push(b);
}
else if (top == '-') {
b -= a;
number.push(b);
}
else if (top == '*') {
b *= a;
number.push(b);
}
else if (top == '/') {
b /= a;
number.push(b);
}
operate.pop();
top=operate.top();//取前一个运算符,用于与现在扫描的运算符进行比较
}//将优先级高的运算符实现计算
operate.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈
}
}
}//扫描运算符,并判断优先级,以及运算
}//主循环
}
int main()
{
Calculate c;
char line[81];
double result;
cin.getline(line,80);
string s(line);
c.calculate(s);
}
第五个版本:可实现实数计算,带括号,可实现循环输入
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
class Calculate
{
public:
int calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c=='#')
return -2;
}
};
int Calculate::calculate(string s)
{
stack<double> number;
stack<char> operate;
char top;
double a, b;
for (unsigned int i = 0; i < s.size(); ++i) {
if (isnumber(s[i])) {
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
temp += s[i];
while (isnumber(s[++i]))
temp += s[i];
for (unsigned int j = 0; j < temp.size(); ++j) {
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}//将字符数字转换成整形数字
if (!isnumber(s[i])) {
if (operate.empty()) {
operate.push(s[i]);
}//入栈第一个符号'#'
else {
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(') {
operate.push(s[i]);
}//入栈高优先级的运算符
else {
while (priority(s[i]) <= priority(top)) {
if (top == '#'&&s[i] == '#') {
double answer;
operate.pop();
answer=number.top();
cout << "\n答案是:" << answer << endl;
number.pop();
return 0;
}//当运算符实现完全后,只剩下'#'
else if (top == '('&&s[i] == ')') {
++i;
}//当左右括号相遇时,跳过右括号,删除左括号
else {
a=number.top();
number.pop();
b=number.top();
number.pop();
}
if (top == '+') {
b += a;
number.push(b);
}
else if (top == '-') {
b -= a;
number.push(b);
}
else if (top == '*') {
b *= a;
number.push(b);
}
else if (top == '/') {
b /= a;
number.push(b);
}
operate.pop();
top=operate.top();//取前一个运算符,用于与现在扫描的运算符进行比较
}//将优先级高的运算符实现计算
operate.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈
}
}
}//扫描运算符,并判断优先级,以及运算
}//主循环
}
int main()
{
Calculate c;
char line[81];
double result;
int i,f=0;
cout<<"请输入开头和结尾为'#'的表达式"<<endl;
cin.getline(line,80);
while(f!=2)
{
string s(line);
for(i=0;i<80;i++)
{
if(s[i]=='#')
{
f++;
}
}
c.calculate(s);
cin.getline(line,80);
f=0;
}
}
第五个版本:带乘方的
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
class Calculate
{
public:
int calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='^')
return 2;
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c=='#')
return -2;
}
};
int Calculate::calculate(string s)
{
stack<double> number;
stack<char> operate;
char top;
double a, b;
for (unsigned int i = 0; i < s.size(); ++i) {
if (isnumber(s[i])) {
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
temp += s[i];
while (isnumber(s[++i]))
temp += s[i];
for (unsigned int j = 0; j < temp.size(); ++j) {
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}//将字符数字转换成整形数字
if (!isnumber(s[i])) {
if (operate.empty()) {
operate.push(s[i]);
}//入栈第一个符号'#'
else {
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(') {
operate.push(s[i]);
}//入栈高优先级的运算符
else {
while (priority(s[i]) <= priority(top)) {
if (top == '#'&&s[i] == '#') {
double answer;
operate.pop();
answer=number.top();
cout << "\n答案是:" << answer << endl;
number.pop();
return 0;
}//当运算符实现完全后,只剩下'#'
else if (top == '('&&s[i] == ')') {
++i;
}//当左右括号相遇时,跳过右括号,删除左括号
else {
a=number.top();
number.pop();
b=number.top();
number.pop();
}
if (top == '+') {
b += a;
number.push(b);
}
else if (top == '-') {
b -= a;
number.push(b);
}
else if (top == '*') {
b *= a;
number.push(b);
}
else if (top == '/') {
b /= a;
number.push(b);
}
else if (top == '^') {
b =pow(b,a);
number.push(b);
}
operate.pop();
top=operate.top();//取前一个运算符,用于与现在扫描的运算符进行比较
}//将优先级高的运算符实现计算
operate.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈
}
}
}//扫描运算符,并判断优先级,以及运算
}//主循环
}
int main()
{
Calculate c;
char line[81];
double result;
int i,f=0;
cout<<"请输入开头和结尾为'#'的表达式"<<endl;
cin.getline(line,80);
while(f!=2)
{
string s(line);
for(i=0;i<80;i++)
{
if(s[i]=='#')
{
f++;
}
}
c.calculate(s);
cin.getline(line,80);
f=0;
}
}
第六个版本:加了异常分析
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
class Exception
{
public:
Exception()
{
msg="Exception!";
}
virtual string Msg()
{
return msg;
}
protected:
string msg;
};
class OperatorInvalidException:public Exception
{
public:
OperatorInvalidException()
{
msg="Operator invalid exception!";
}
};
class DivisorIsZero:public Exception
{
public:
DivisorIsZero()
{
msg="Divisor is zero!";
}
};
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
class Calculate
{
public:
int calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='^')
return 2;
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c=='#')
return -2;
if(c!='^'&&c!='*'&&c!='/'&&c!='+'&&c!='-'&&c!='('&&c!=')'&&c!='#')
throw OperatorInvalidException();
}
};
int Calculate::calculate(string s)
{
try
{
stack<double> number;
stack<char> operate;
char top;
double a, b;
for (unsigned int i = 0; i < s.size(); ++i) {
if (isnumber(s[i])) {
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
temp += s[i];
while (isnumber(s[++i]))
temp += s[i];
for (unsigned int j = 0; j < temp.size(); ++j) {
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}//将字符数字转换成整形数字
if (!isnumber(s[i])) {
if (operate.empty()) {
operate.push(s[i]);
}//入栈第一个符号'#'
else {
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(') {
operate.push(s[i]);
}//入栈高优先级的运算符
else {
while (priority(s[i]) <= priority(top)) {
if (top == '#'&&s[i] == '#') {
double answer;
operate.pop();
answer=number.top();
cout << "\n答案是:" << answer << endl;
number.pop();
return 0;
}//当运算符实现完全后,只剩下'#'
else if (top == '('&&s[i] == ')') {
++i;
}//当左右括号相遇时,跳过右括号,删除左括号
else {
a=number.top();
number.pop();
b=number.top();
number.pop();
}
if (top == '+') {
b += a;
number.push(b);
}
else if (top == '-') {
b -= a;
number.push(b);
}
else if (top == '*') {
b *= a;
number.push(b);
}
else if (top == '/') {
if(a==0)
{
throw DivisorIsZero();
}
b /= a;
number.push(b);
}
else if (top == '^') {
b =pow(b,a);
number.push(b);
}
operate.pop();
top=operate.top();//取前一个运算符,用于与现在扫描的运算符进行比较
}//将优先级高的运算符实现计算
operate.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈
}
}
}//扫描运算符,并判断优先级,以及运算
}//主循环
}
catch(OperatorInvalidException e)
{
cout<<e.Msg()<<endl;
exit(0);
}
catch(DivisorIsZero e)
{
cout<<e.Msg()<<endl;
exit(0);
}
}
int main()
{
Calculate c;
char line[81];
double result;
int i,f=0;
cout<<"请输入开头和结尾为'#'的表达式"<<endl;
cin.getline(line,80);
while(f!=2)
{
string s(line);
for(i=0;i<80;i++)
{
if(s[i]=='#')
{
f++;
}
}
c.calculate(s);
cin.getline(line,80);
f=0;
}
}
第七个版本:去了空格
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
using namespace std;
class Exception
{
public:
Exception()
{
msg="Exception!";
}
virtual string Msg()
{
return msg;
}
protected:
string msg;
};
class OperatorInvalidException:public Exception
{
public:
OperatorInvalidException()
{
msg="Operator invalid exception!";
}
};
class DivisorIsZero:public Exception
{
public:
DivisorIsZero()
{
msg="Divisor is zero!";
}
};
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
string delspace(char str[80])
{
int i=0,j=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j++]=str[i];
}
}
str[j]='\0';
}
class Calculate
{
public:
int calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='^')
return 2;
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c=='#')
return -2;
if(c!='^'&&c!='*'&&c!='/'&&c!='+'&&c!='-'&&c!='('&&c!=')'&&c!='#')
throw OperatorInvalidException();
}
};
int Calculate::calculate(string s)
{
try
{
stack<double> number;
stack<char> operate;
char top;
double a, b;
for (unsigned int i = 0; i < s.size(); ++i) {
if (isnumber(s[i])) {
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
temp += s[i];
while (isnumber(s[++i]))
temp += s[i];
for (unsigned int j = 0; j < temp.size(); ++j) {
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}//将字符数字转换成整形数字
if (!isnumber(s[i])) {
if (operate.empty()) {
operate.push(s[i]);
}//入栈第一个符号'#'
else {
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(') {
operate.push(s[i]);
}//入栈高优先级的运算符
else {
while (priority(s[i]) <= priority(top)) {
if (top == '#'&&s[i] == '#') {
double answer;
operate.pop();
answer=number.top();
cout << "\n答案是:" << answer << endl;
number.pop();
return 0;
}//当运算符实现完全后,只剩下'#'
else if (top == '('&&s[i] == ')') {
++i;
}//当左右括号相遇时,跳过右括号,删除左括号
else {
a=number.top();
number.pop();
b=number.top();
number.pop();
}
if (top == '+') {
b += a;
number.push(b);
}
else if (top == '-') {
b -= a;
number.push(b);
}
else if (top == '*') {
b *= a;
number.push(b);
}
else if (top == '/') {
if(a==0)
{
throw DivisorIsZero();
}
b /= a;
number.push(b);
}
else if (top == '^') {
b =pow(b,a);
number.push(b);
}
operate.pop();
top=operate.top();//取前一个运算符,用于与现在扫描的运算符进行比较
}//将优先级高的运算符实现计算
operate.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈
}
}
}//扫描运算符,并判断优先级,以及运算
}//主循环
}
catch(OperatorInvalidException e)
{
cout<<e.Msg()<<endl;
exit(0);
}
catch(DivisorIsZero e)
{
cout<<e.Msg()<<endl;
exit(0);
}
}
int main()
{
Calculate c;
char line[81];
double result;
int i,f=0;
cout<<"请输入开头和结尾为'#'的表达式"<<endl;
cin.getline(line,80);
while(f!=2)
{
delspace(line);
string s(line);
//s=delspace(s);
for(i=0;i<80;i++)
{
if(s[i]=='#')
{
f++;
}
}
c.calculate(s);
cin.getline(line,80);
f=0;
}
}
第八个版本:后缀表达式
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
#include<vector>
using namespace std;
vector<char> Tmp;
class Exception
{
public:
Exception()
{
msg="Exception!";
}
virtual string Msg()
{
return msg;
}
protected:
string msg;
};
class OperatorInvalidException:public Exception
{
public:
OperatorInvalidException()
{
msg="Operator invalid exception!";
}
};
class DivisorIsZero:public Exception
{
public:
DivisorIsZero()
{
msg="Divisor is zero!";
}
};
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
string delspace(char str[80])
{
int i=0,j=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j++]=str[i];
}
}
str[j]='\0';
}
class Calculate
{
public:
double calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='^')
return 2;
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c!='^'&&c!='*'&&c!='/'&&c!='+'&&c!='-'&&c!='('&&c!=')')
throw OperatorInvalidException();
}
};
double Calculate::calculate(string s)
{
try
{
string houzhui="";
stack<double> number;
stack<char> operate;
char top;
double a, b;
int len,k=0;
double answer;
for (unsigned int i = 0; i < s.size(); ++i)
{
if (isnumber(s[i]))
{
houzhui+=s[i];
}
if (!isnumber(s[i]))
{
houzhui+=' ';
if (operate.empty())
{
operate.push(s[i]);
}
else
{
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(')
{
operate.push(s[i]);
}//入栈高优先级的运算符
else
{
if(s[i]==')')
{
while(operate.top()!='(')
{
top=operate.top();
houzhui+=top;
operate.pop();
}
operate.pop();
}
else
{
while (priority(s[i]) <= priority(top)&&s[i]!='(')
{
houzhui+=operate.top();
operate.pop();
if(operate.empty())
break;
top=operate.top();
}
operate.push(s[i]);
}
}
}
}
}
while(!operate.empty())
{
houzhui+=operate.top();
operate.pop();
}
//return houzhui;
for (unsigned int i = 0; i < houzhui.size(); ++i)
{
if (isnumber(houzhui[i]))
{
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
while (isnumber(houzhui[i]))
{
temp += houzhui[i];
i++;
}
i--;
//cout<<temp<<endl;
for (unsigned int j = 0; j < temp.size(); ++j)
{
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}
else if(houzhui[i]==' ')
continue;
else if (houzhui[i] == '+')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b += a;
number.push(b);
}
else if (houzhui[i] == '-')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b -= a;
number.push(b);
}
else if (houzhui[i] == '*')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b *= a;
number.push(b);
}
else if (houzhui[i] == '/')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
if(a==0)
{
throw DivisorIsZero();
}
b /= a;
number.push(b);
}
else if (houzhui[i] == '^')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b =pow(b,a);
number.push(b);
}
}
answer=number.top();
return answer;
}
catch(OperatorInvalidException e)
{
cout<<e.Msg()<<endl;
exit(0);
}
catch(DivisorIsZero e)
{
cout<<e.Msg()<<endl;
exit(0);
}
}
int main()
{
Calculate c;
double ans;
char line[81];
double result;
int i,f=0;
cout<<"/*....................计算器....................*/"<<endl;
while(cin.getline(line,80))
{
delspace(line);
string s(line);
ans=c.calculate(s);
cout<<ans<<endl;
}
}
/*
1.2*2+(2-1)
*/
第九个版本:可以实现负号和减号的区分
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<sstream>
#include<vector>
using namespace std;
vector<char> Tmp;
class Exception
{
public:
Exception()
{
msg="Exception!";
}
virtual string Msg()
{
return msg;
}
protected:
string msg;
};
class OperatorInvalidException:public Exception
{
public:
OperatorInvalidException()
{
msg="Operator invalid exception!";
}
};
class DivisorIsZero:public Exception
{
public:
DivisorIsZero()
{
msg="Divisor is zero!";
}
};
bool isnumber(char c)
{
if(c>='0'&&c<='9'||c=='.')
{
return true;
}
else
{
return false;
}
}
string zero(char *str)
{
char str2[80];
int i=0,j=0;
int len = strlen(str);
for(i=0; i < len;i++)
{
if((i==0&&str[i]=='-')||(str[i-1]=='('&&str[i]=='-'))
{
str2[j++]='0';
}
str2[j++]=str[i];
}
str2[j]='\0';
//str=str2;
//return str;
for(int i = 0, len = strlen(str2); i <= len; ++i) str[i] = str2[i];
//printf("%s %s\n", str, str2);
}
string delspace(char str[80])
{
int i=0,j=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j++]=str[i];
}
}
str[j]='\0';
}
class Calculate
{
public:
double calculate(string s);
private:
stack<int>number;
stack<char>operate;
int priority(char c)
{
if(c=='^')
return 2;
if(c=='*'||c=='/')
return 1;
if(c=='+'||c=='-')
return 0;
if(c=='('||c==')')
return -1;
if(c!='^'&&c!='*'&&c!='/'&&c!='+'&&c!='-'&&c!='('&&c!=')')
throw OperatorInvalidException();
}
};
double Calculate::calculate(string s)
{
try
{
string houzhui="";
stack<double> number;
stack<char> operate;
char top;
double a, b;
int len,k=0;
double answer;
for (unsigned int i = 0; i < s.size(); ++i)
{
if (isnumber(s[i]))
{
houzhui+=s[i];
}
if (!isnumber(s[i]))
{
houzhui+=' ';
if (operate.empty())
{
operate.push(s[i]);
}
else
{
top=operate.top();
if (priority(s[i])>priority(top) || s[i] == '(')
{
operate.push(s[i]);
}//入栈高优先级的运算符
else
{
if(s[i]==')')
{
while(operate.top()!='(')
{
top=operate.top();
houzhui+=top;
operate.pop();
}
operate.pop();
}
else
{
while (priority(s[i]) <= priority(top)&&s[i]!='(')
{
houzhui+=operate.top();
operate.pop();
if(operate.empty())
break;
top=operate.top();
}
operate.push(s[i]);
}
}
}
}
}
while(!operate.empty())
{
houzhui+=operate.top();
operate.pop();
}
//cout<<houzhui<<endl;
for (unsigned int i = 0; i < houzhui.size(); ++i)
{
if (isnumber(houzhui[i]))
{
double Temp = 0,Temp2 = 0;
string temp="";
int count=0,flag=0;
while (isnumber(houzhui[i]))
{
temp += houzhui[i];
i++;
}
i--;
//cout<<temp<<endl;
for (unsigned int j = 0; j < temp.size(); ++j)
{
if(flag==0)
{
if(temp[j]!='.')
{
Temp = Temp * 10 + temp[j] - 48;
}
}
if(temp[j]=='.')
{
j++;
flag=1;
}
if(flag)
{
Temp2 = Temp2 * 10 + temp[j] - 48;
count++;
}
}
while(count--)
Temp2 = Temp2 * 0.1;
Temp=Temp+Temp2;
number.push(Temp);
temp.clear();
}
else if(houzhui[i]==' ')
continue;
else if (houzhui[i] == '+')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b += a;
number.push(b);
}
else if (houzhui[i] == '-')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b -= a;
number.push(b);
}
else if (houzhui[i] == '*')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b *= a;
number.push(b);
}
else if (houzhui[i] == '/')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
if(a==0)
{
throw DivisorIsZero();
}
b /= a;
number.push(b);
}
else if (houzhui[i] == '^')
{
a=number.top();
number.pop();
b=number.top();
number.pop();
b =pow(b,a);
number.push(b);
}
}
answer=number.top();
return answer;
}
catch(OperatorInvalidException e)
{
cout<<e.Msg()<<endl;
exit(0);
}
catch(DivisorIsZero e)
{
cout<<e.Msg()<<endl;
exit(0);
}
}
int main()
{
//freopen("in.txt", "r", stdin);
Calculate c;
double ans;
char line[81];
double result;
int i,f=0;
cout<<"/*....................计算器....................*/"<<endl;
while(cin.getline(line,80))
{
delspace(line);
zero(line);
string s(line);
ans=c.calculate(s);
cout<<ans<<endl;
}
}
/*
-12.5-(-2*3+4^2)
*/