#include<iostream>
using namespace std;
#include <stdio>
#include <math>
#include<string>
#include<stack>
using std::stack;
#include<fstream.h>
#define n 5
char op[6]={{'**'},{'/'},{'*'},{'+'},{'-'},{';'}};
int co[6][6]={{1,0,0,0,0,1},{0,0,0,1,1,1},{0,0,0,1,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1}};
bool isoperate(char s)
{
for(int k=0;k<5;k++)
if(s==op[k])
return(1);
return(0);
}
int compare(char ch1,char ch2)
{
int m,i,k;
for(i=0;i<5;i++)
{
if(ch1==op[i])
m=i;
}
for(i=0;i<5;i++)
if(ch2==op[i])
k=i;
return co[m][n];
}
bool result(double x,double y,char op,double &z)
{
switch(op)
{
case '+': z = x+y;break ;
case '-': z = x-y;break ;
case '*': z = x*y;break ;
case '/': z=x/y;break;
case '**': z=pow(x,y);break;
default:
{
cout<<"出错!"<<endl;
return false;
}
}
return true;
}
void main()
{
stack<char> OS[100];
stack<double> NS[100];
int t,j;
double x,y,z,k;
char s[100],w,q;
cout<<"请输入运算符与操作数"<<endl;
for(int i=0;i<n;i++)
cin>>s[i];
t=0;
i=0;j=0;
OS[i].push(";");
while(t!=2)
{
if(t==0)
w=s[i];i++;
if(!isoperate(w))
{
k=w-'0';
NS[j].push(k);
}
else
{
q=OS.top();
if(compare(w,q))
{
OS.push(w);
t=0;
}
else if(q==';'&&w==';')
{
z=NS.pop();
t=2;}
else
{
x=NS.top();
NS.pop();
y=NS.top();
NS.pop();
q=OS.top();
OS.pop();
if(result(x,y,q,z))
NS.push(z);t=1;
}
}
}
cout<<"结果为"<<z<<endl;
}
using namespace std;
#include <stdio>
#include <math>
#include<string>
#include<stack>
using std::stack;
#include<fstream.h>
#define n 5
char op[6]={{'**'},{'/'},{'*'},{'+'},{'-'},{';'}};
int co[6][6]={{1,0,0,0,0,1},{0,0,0,1,1,1},{0,0,0,1,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1}};
bool isoperate(char s)
{
for(int k=0;k<5;k++)
if(s==op[k])
return(1);
return(0);
}
int compare(char ch1,char ch2)
{
int m,i,k;
for(i=0;i<5;i++)
{
if(ch1==op[i])
m=i;
}
for(i=0;i<5;i++)
if(ch2==op[i])
k=i;
return co[m][n];
}
bool result(double x,double y,char op,double &z)
{
switch(op)
{
case '+': z = x+y;break ;
case '-': z = x-y;break ;
case '*': z = x*y;break ;
case '/': z=x/y;break;
case '**': z=pow(x,y);break;
default:
{
cout<<"出错!"<<endl;
return false;
}
}
return true;
}
void main()
{
stack<char> OS[100];
stack<double> NS[100];
int t,j;
double x,y,z,k;
char s[100],w,q;
cout<<"请输入运算符与操作数"<<endl;
for(int i=0;i<n;i++)
cin>>s[i];
t=0;
i=0;j=0;
OS[i].push(";");
while(t!=2)
{
if(t==0)
w=s[i];i++;
if(!isoperate(w))
{
k=w-'0';
NS[j].push(k);
}
else
{
q=OS.top();
if(compare(w,q))
{
OS.push(w);
t=0;
}
else if(q==';'&&w==';')
{
z=NS.pop();
t=2;}
else
{
x=NS.top();
NS.pop();
y=NS.top();
NS.pop();
q=OS.top();
OS.pop();
if(result(x,y,q,z))
NS.push(z);t=1;
}
}
}
cout<<"结果为"<<z<<endl;
}