文档库 最新最全的文档下载
当前位置:文档库 › 浮点数四则运算代码(C++)

浮点数四则运算代码(C++)

#include
#include
#include
using namespace std;

double Operate(double a,char pre_op,double b,int n);
double Expression_Eval();
double Expression_Eval1();
int Precede(char Operator1,char Operator2);
int GetOperatorID(char Operator);
char OperatorPrecede[8][8]={
'>','>','<','<','<','>','>','<',
'>','>','<','<','<','>','>','<',
'>','>','>','>','<','>','>','<',
'>','>','>','>','<','>','>','<',
'<','<','<','<','<','=','E','<',
'>','>','>','>','E','>','>','<',
'<','<','<','<','<','E','=','<',
'>','>','>','>','>','>','>','>',
};

template
class SeqStack
{
T data[MaxSize];
int top;
public:
SeqStack();
void Push(T x);
T Pop();
T Top();
bool Empty();

};

template
SeqStack::SeqStack()
{
top=-1;
}

template
void SeqStack::Push(T x)
{
if(top==MaxSize){
cerr<<"上溢";exit(1);
}
top++;
data[top]=x;
}

template
T SeqStack::Pop()
{
if(top==-1) {
cerr<<"下溢";exit(1);
}
T x=data[top];
top--;
return x;
}

template
T SeqStack::Top()
{
if(top==-1){
cerr<<"下溢";exit(1);
}
return data[top];
}

template
bool SeqStack::Empty()
{
return top=-1;
}


int GetOperatorID(char Operator)
{
int retCode;
retCode=-1;
switch (Operator)
{
case '+': retCode=0;break;
case '-': retCode=1;break;
case '*': retCode=2;break;
case '/': retCode=3;break;
case '(': retCode=4;break;
case ')': retCode=5;break;
case '@':retCode=6;break;
case '.': retCode=7;break;
}
return (retCode);
}


int Precede(char Operator1,char Operator2)
{
int OperatorID1,OperatorID2;
OperatorID1=GetOperatorID(Operator1);
OperatorID2=GetOperatorID(Operator2);
if(OperatorID1<0||OperatorID1>7||OperatorID2<0||OperatorID2>7)
return 'E';
return OperatorPrecede[OperatorID1][OperatorID2];
}


double Operate(double a,char pre_op,double b,int n)
{
switch (pre_op)
{
case '+':return a+b;break;
case '-': return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
case '.':return (a+b*pow(10,-n));break;
default:exit(1);
}
}


double Expression_Eval() //用于计算单位数的四则运算
{
SeqStack OPTR;
SeqStack OPND;
double m;
int n=0;
int i=0;
OPTR.Push('@');
char ch=getchar();
while(ch!='@'||OPTR.Top()!='@')
{
if(ch>=48&&ch<=57)
{m=ch-48;OPND.Push(m);ch=getchar();i++;}
else
{
char pre_op=OPTR.Top();
switch (Precede(pre_op,ch))
{
case '<':OPTR.Push(ch);ch=getchar();break;
case '=':OPTR.Pop();ch=getchar();break;
case '>':double b=OPND.Pop();double a=OPND.Pop();pre_op=OPTR.Pop();
OPND.Push(Operate(a,pre_op,b,n));break;
}
}

}
return OPND.Top();
}

double Expression_Eval1() //用于计算多位数的四则运算

{
SeqStack OPTR;
SeqStack OPND;
double a[100];
double m;
int n;
OPTR.Push('@');
char ch=getchar();
while(ch!='@'||OPTR.Top()!='@')
{
while(ch>=48&&ch<=57)
{
int i=0;double b=0;
while(ch>=48&&ch<=57)
{
m=ch-48; //将字符型转化为整型数
a[i]=m;
i++;
ch=getchar();
}
n=i;
for(int j=0;j{
b+=a[j]*pow(10,n-j-1);
}
OPND.Push(b);
}


//
char pre_op=OPTR.Top();
switch (Precede(pre_op,ch))
{
case '<':OPTR.Push(ch);ch=getchar();break;
case '=':OPTR.Pop();ch=getchar();break;
case '>':double b=OPND.Pop();double a=OPND.Pop();pre_op=OPTR.Pop();
OPND.Push(Operate(a,pre_op,b,n));break;
}


}
cout<cout<<"运算结果为:"<return OPND.Top();
}
void main()
{
cout<<"请输入四则运算式(以“=”结尾巴):"<cout<//048字符对整型,getchar()的返回值是AscII码,如:输入1,得到AscII码49,赋值给char,输出1,赋值给int,输出49
}

相关文档
相关文档 最新文档