#include <iostream>
#include <stack>
#include <string>
using namespace std;
int check(char c)
{
switch(c)
{
case ';':return(0);
case '+':return(1);
case '-':return(1);
case '*':return(2);
case '/':return(2);
case '(':return(3);
case ')':return(3);
case '#':return(4);
default: cout<<"輸入的運算符有錯誤!"<<endl;
exit(-1);
}
}
int cmp(char a, char b)
return(check(a)-check(b));
double oper(char c,double a,double b)
case '+':return(a+b);
case '-':return(a-b);
case '*':return(a*b);
case '/':return(a/b);
default:cout<<"出現錯誤!"<<endl;
void empty(char *a)
for(int i=0;i<20;i++)
a[i]='\0';
bool data_check(char a)
if(a!='1'&&a!='2'&&a!='3'&&a!='4'&&a!='5'&&a!='6'&&a!='7'&&a!='8'&&a!='9'&&a!='0')
return 0;
else
return 1;
double fuc(string &ch)
char a[20];
int i=0,count=0; //count 用于括号計數
double temp;
stack<char>f; //運算符棧
stack<double>d; //資料棧
f.push('#'); //運算符棧頂設定基準
string::iterator p;
p=ch.begin();
while(*p!=';')
if(*p=='(') //碰到左括号
{
f.push('(');
cout<<"#0"<<f.top()<<endl;
p++;
count++;
while( *p!='(' && count!=0) //沒有碰到右括号前 先進行括号内部運算 并有幾個括号進行幾次循環
{
while(*p!=')' && *p!='(')
{
if((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-') //不是運算符 将數值壓入棧
{
if(!data_check(*p)) //資料檢測
{
cout<<"輸入有誤!"<<endl;
exit(-1);
}
i=0;
empty(a);
do
a[i]=*p;
p++;
i++;
} while ((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-'&&(*p)!=')'&&(*p)!=';'); //碰到運算符 跳出
temp=atoi(a);
d.push(temp); //數值進棧
cout<<"0@"<<d.top()<<endl;
}
else //針對括号内運算符進行運算
if(cmp(*p,f.top())<=0) //先判斷運算優先序
if(cmp(f.top(),'(')!=0) //對于括号運算完後再對前一個運算符進行判斷 符合條件就進行運算
{
cout<<"here1"<<endl;
temp=d.top();
d.pop();
temp=oper(f.top(),d.top(),temp);
f.pop();
d.push(temp);
cout<<temp<<endl;
}
else{ //否則運算符進棧
f.push(*p);
cout<<"#1"<<f.top()<<endl;
p++;
else //後一級運算順序高 進行運算
f.push(*p);
cout<<"#2"<<f.top()<<endl;
if((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-' &&(*p)!='(') //不是運算符 将數值壓入棧
if(!data_check(*p))
{
cout<<"輸入有誤!"<<endl;
exit(-1);
}
i=0;
empty(a);
do
a[i]=*p;
p++;
i++;
} while ((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-'&&(*p)!=')'&&(*p)!=';'); //碰到運算符 跳出
temp=atoi(a);
temp=oper(f.top(),d.top(),temp); //計算
d.pop(); //舊數值出棧
d.push(temp); //新數值進棧
cout<<"1@"<<d.top()<<endl;
cout<<"#3"<<f.top()<<endl;//舊運算符出棧
}
if(*p!='(') //處理括号内剩餘的資料
while(f.top()!='(')
{temp=d.top();
d.pop();
temp=oper(f.top(),d.top(),temp);
d.push(temp);
cout<<"2@"<<d.top()<<endl;
f.pop();
cout<<"#4"<<f.top()<<endl;
cout<<"#5"<<f.top()<<endl;
p++;
count--;
}
}
}
else //沒遇到括号
if((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-') //不是運算符 将數值壓入棧
{
if(!data_check(*p))
cout<<"輸入有誤!"<<endl;
exit(-1);
i=0;
empty(a);
do
a[i]=*p;
i++;
} while ((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-'&&(*p)!=')'&&(*p)!=';'); //碰到運算符 跳出
temp=atoi(a);
d.push(temp);
cout<<"3@"<<d.top()<<endl;
else
if(cmp(*p,f.top())<=0) //進行運算
if((cmp(f.top(),'(')!=0) && f.top()!='#') //對前一級運算進行分析 符合條件就進行計算
cout<<"here2"<<endl;
temp=d.top();
d.pop();
temp=oper(f.top(),d.top(),temp);
f.pop();
d.push(temp);
else{
cout<<"#6"<<f.top()<<endl;
else
f.push(*p);
cout<<"#7"<<f.top()<<endl;
if((*p)!='*' && (*p)!='/'&& (*p)!='+' && (*p)!='-'&&(*p)!='(') //不是運算符 将數值壓入棧
if(!data_check(*p))
temp=oper(f.top(),d.top(),temp); //計算
d.pop(); //舊數值出棧
d.push(temp); //新數值進棧
cout<<"4@"<<d.top()<<endl;
f.pop();
cout<<"#8"<<f.top()<<endl;//舊運算符出棧
//最後對整個式子的遺留部分進行運算
while(f.top()!='#')
temp=d.top();
d.pop();
temp=oper(f.top(),d.top(),temp);
d.push(temp);
cout<<"5@"<<d.top()<<endl;
f.pop();
cout<<"#9"<<f.top()<<endl;
return(d.top());
void main()
string ch;
cout<<"請輸入表達式:"<<endl;
cin>>ch;
cout<<"計算結果為:"<<fuc(ch)<<endl;
getchar();getchar();