📄 实数四则运算.cpp
字号:
#include <iostream.h>
#include<malloc.h>
#include<math.h>
#include<ctype.h>
#define STACK_INIT_SIZE 100
typedef struct{
float *base;
float *top;
float stacksize;
}SqStackA;
SqStackA Opnd1,Opnd2;//数据栈
typedef struct{
char *base;
char *top;
char stacksize;
}SqStackB;//符号栈
SqStackB Optr;
char Prior[7][7] = { // 算符间的优先关系
'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=',' ',
'>','>','>','>',' ','>','>',
'<','<','<','<','<',' ','='
};
/////////函数声明/////////////
void InitStack1(SqStackA &S);
void InitStack2(SqStackB &S);
float GetTop1(SqStackA S,float &e);
char GetTop2(SqStackB S,char &e);
void Push2(SqStackB &S,char e);
void Push1(SqStackA &S,float e);
float Pop1(SqStackA &S,float &e);
char Pop2(SqStackB &S,char &e);
float Compute(float oper1,char FH,float oper2);
int lev(char fuhao);
char Compare(char x,char y);
void Order(SqStackB &S,SqStackA &P,char f);
///////////////////////////////////////
void main()
{
cout<<" By 睡着的神"<<endl;
cout<<"********************************"<<endl;
cout<<"请输入算式 "<<endl;
InitStack2(Optr);
Push2(Optr,'=');
InitStack1(Opnd1);
InitStack1(Opnd2);
char oper;
float Num=0.0,res,e,ShuZi ;
char *ShuRu;
while(Optr.base!=Optr.top)
{int i=0;ShuRu=(char *)malloc(sizeof(char));cin>>ShuRu[i];
if(int(ShuRu[i])>47&&int(ShuRu[i])<58||(ShuRu[i])==46)
{ShuZi=float(int(ShuRu[i])-48);
Push1(Opnd1,ShuZi);
}
else {oper=ShuRu[i];
int j=0,l=0;
Num=0.0;
while(Opnd1.base!=Opnd1.top){
Pop1(Opnd1,e);if(e<-1)l=j;
if(e>-1){j++;Num=Num+e*pow(10,j);
}
}
Num=Num/pow(10,l+1);
if(Num!=0)Push1(Opnd2,Num);
Order(Optr,Opnd2,oper);}
i++;
}
cout<<"答案是"<<GetTop1(Opnd2,res)<<endl;
}
///////////////构造栈//////////////
void InitStack1(SqStackA &S)
{
S.base=(float *)malloc(STACK_INIT_SIZE *sizeof(float));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}//数据
void InitStack2(SqStackB &S)
{
S.base=(char *)malloc(STACK_INIT_SIZE *sizeof(char));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}//符号
/////////取栈顶元素/////////////////
float GetTop1(SqStackA S,float &e)
{
e=*(S.top-1);
return e;
}//数据
char GetTop2(SqStackB S,char &e)
{
e=*(S.top-1);
return e;
}//符号
/////////////进栈///////////////////
void Push1(SqStackA &S,float e)
{
*S.top=e;S.top=S.top+1;
}//数据
void Push2(SqStackB &S,char e)
{
*S.top=e;S.top=S.top+1;
}//符号
//////////////////出栈///////////////////
float Pop1(SqStackA &S,float &e)
{
S.top=S.top-1;e=*S.top;
return e;
}//数据
char Pop2(SqStackB &S,char &e)
{
S.top=S.top-1;e=*S.top;
return e;
}//符号
/////////////四则运算/////////////////////
float Compute(float oper1,char FH,float oper2)
{
float ans;
switch(FH)
{
case '+':ans=oper1+oper2;break;
case '-':ans=oper1-oper2;break;
case '*':ans=oper1*oper2;break;
case '/':ans=oper1/oper2;break;
}
return ans;
}
////////////////运算级比较///////////
char Compare(char x,char y)
{
return Prior[lev(x)][lev(y)];
}
////////////////定义运算级////////////////
int lev(char fuhao)
{
int m;
switch(fuhao)
{
case '+':m=0;break;
case '-':m=1;break;
case '*':m=2;break;
case '/':m=3;break;
case '(':m=4;break;
case ')':m=5;break;
case '=':m=6;break;
}
return m;
}
///////////按运算顺序计算///////////////////////
void Order(SqStackB &S,SqStackA &P,char f)
{ char top,theta,e;
float a,b ;
switch(Compare(GetTop2(S,top),f))
{
case '>':
Pop2(S,theta);
Pop1(P,b);
Pop1(P,a);
Push1(P,Compute(a,theta ,b));
Order(S ,P, f);
break;
case '=':Pop2(S,e);break;
case '<':Push2(S,f);break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -