我的练习一

#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)
{
switch(c)
{
case '+':return(a+b);
case '-':return(a-b);
case '*':return(a*b);
case '/':return(a/b);
default:cout<<"出现错误!"<<endl;
exit(-1);
}
}

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.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;
p++;
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;
f.pop();
cout<<"#3"<<f.top()<<endl;//旧运算符出栈
}

}
}

}
if(*p!='(') //处理括号内剩余的数据
{
while(f.top()!='(')
{temp=d.top();
d.pop();
temp=oper(f.top(),d.top(),temp);
d.pop();
d.push(temp);
cout<<"2@"<<d.top()<<endl;
f.pop();
cout<<"#4"<<f.top()<<endl;
}
f.pop();
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;
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.pop();
d.push(temp);
}
else{
f.push(*p);
cout<<"#6"<<f.top()<<endl;
p++;
}
}
else
{
f.push(*p);
cout<<"#7"<<f.top()<<endl;
p++;
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<<"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.pop();
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();
}

原文地址:https://www.cnblogs.com/chaney/p/4378932.html