您的位置:首页 > 编程语言 > C语言/C++

C语言用后序求表达式的结果

2015-10-05 15:41 423 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
# define MaxSize 50
#define chartonumber(x) (x-'0')
#define numbertochar(x) (x+'0')
double op;

typedef struct SqStack{
double data[MaxSize];
int top;
}SqStack;

void InitStack(SqStack &S){
S.top=-1;
}

bool StackEmpty(SqStack S){
if(S.top==-1)return true;
else return false;
}

bool Push(SqStack &S,double x){
if(S.top==MaxSize-1)return false;
else
S.data[++S.top]=x;
return true;
}

bool Pop(SqStack &S,double &x){
if(S.top==-1)return false;
x=S.data[S.top--];
return true;
}

bool GetTop(SqStack S,double &x){
if(S.top==-1)return false;
x=S.data[S.top];
return true;
}

typedef struct LinkNode{
double data;
int flag;
struct LinkNode *next;
}LinkNode;

typedef struct LinkQueue{
LinkNode *front,*rear;
}LinkQueue;

void InitQueue(LinkQueue &Q){
Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}

bool IsEmpty(LinkQueue Q){
if(Q.front==Q.rear)return true;
else return false;
}

void EnQueue(LinkQueue &Q,double x,int y){
LinkNode *s;
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=x;
s->flag=y;
s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}

bool DeQueue(LinkQueue &Q,double &x,int &y){
if(Q.front==Q.rear)return false;
LinkNode *p;
p=Q.front->next;
x=p->data;
y=p->flag;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
return true;
}

void calculate(LinkQueue Q){
SqStack S;
InitStack(S);
double num=0.0,arg=0.0;;int flag=0;double fx;int fy;
while(DeQueue(Q,fx,fy)){
if(fy==0){
if(flag==0){num=fx;flag=1;}
else if(flag==1){arg=fx;flag=2;}
else if(flag==2){
Push(S,num);
num=arg;
arg=fx;
flag=2;
}
}
else
if(fy==1){
if(flag==1){
arg=num;
Pop(S,num);
flag==2;
}
switch((int)fx){
case '+':num+=arg;flag=1;break;
case '-':num-=arg;flag=1;break;
case '*':num*=arg;flag=1;break;
case '/':num/=arg;flag=1;break;
default:printf("%s","请重新输入计算式");break;
}
}
}
printf("%.3f\n",num);
}

LinkQueue midtoend(char string[],int length){
LinkQueue Q;SqStack S;
InitQueue(Q);InitStack(S);
char c;
for(int i=0;i<length;i++){
double num=0.0;
c=string[i];
int numflag=chartonumber(c);
if(numflag>=0 && numflag<=9){
while((numflag>=0 && numflag<=9)){
num=num*10+numflag;
c=string[++i];
if(c=='.'){
c=string[++i];
numflag=chartonumber(c);int point=1;
while((numflag>=0 && numflag<=9)){
double dn=numflag;
while(point--){
dn=dn/10;
}
num+=dn;
point++;
}
}else
numflag=chartonumber(c);
}
EnQueue(Q,num,0);
}
switch(c){
case '(':
Push(S,'(');
break;
case '=':
while(Pop(S,op)){
EnQueue(Q,op,1);
}
break;
case ')':
Pop(S,op);
while(((int)op)!='('){
EnQueue(Q,op,1);;
Pop(S,op);
}
break;
case '+':
while(GetTop(S,op)){
if(((int)op)!='('){Pop(S,op);EnQueue(Q,op,1);}else break;
}
Push(S,'+');
break;
case '-':
while(GetTop(S,op)){
if(((int)op)!='('){Pop(S,op);EnQueue(Q,op,1);}else break;
}
Push(S,'-');
break;
case '*':
while(GetTop(S,op)){
if(((int)op)=='*'||((int)op)=='/'){Pop(S,op);EnQueue(Q,op,1);}else break;
}
Push(S,'*');
break;
case '/':
if(GetTop(S,op)){
if(((int)op)=='*'||((int)op)=='/'){Pop(S,op);EnQueue(Q,op,1);}
}
Push(S,'/');
break;
default : printf("%s","不是正确的表达式"); break;
}
}
return Q;
}

int main(){
while(true){
char string[100];
int i=0;
gets(string);
while(string[i]!='\0'){
i++;
}
string[i++]='=';
int length=i;
calculate(midtoend(string,length));
}
return 0;
}
</pre><pre name="code" class="plain">可以计算double型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 堆栈 后续