求用C语言编写一个可以进行 加 减 乘 除 平方 开方 的程序,并且要考虑优先级

求用C语言编写一个可以进行 加 减 乘 除 平方 开方 的程序,并且要考虑优先级,比如输入2+3*5,得到的结果应为17,而不是25。。。急求啊~高分悬赏啊~~~

#include "stdafx.h"

#define STACK_INIT_SIZE 10
#define STACKINCRESIZE 5

typedef struct
{
SElemType *base;
SElemType *top;
int stack_size;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stack_size=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
if((S.top-S.base)>=STACK_INIT_SIZE){
S.base=(ElemType*)realloc(S.base,(S.stack_size+STACKINCRESIZE)*(sizeof(SqStack)));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stack_size;
S.stack_size+=STACK_INIT_SIZE;

}
*(S.top)++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;

e=*--S.top;

return OK;
}
Status StackEmpty(SqStack &S)
{
if(S.top==S.base)
return TRUE;
else return FALSE;
}
int GetTop(SqStack S,ElemType &e)
{
if(S.top >S.base )
{
e=*(S.top-1) ;
return OK;
}
else return ERROR;
}
/*#include "stdafx.h"
#include "MyStack.h"

int InitStack(SqStack &S)
{
S.base =(ElemType *)malloc(STACKSIZE*sizeof(ElemType));
S.top =S.base ;
S.stacksize =STACKSIZE;
return OK;
}

int GetTop(SqStack S,ElemType &e)
{
if(S.top >S.base )
{
e=*(S.top-1) ;
return OK;
}
else return ERROR;
}
int Push(SqStack &S,Elemtype e)
{
if(S.top -S.base >=S.stacksize )
{
S.base=(ElemType *)realloc(S.base ,(S.stacksize +ADDSIZE)sizeof(ElemType));
S.top =S.stacksize +S.base ;
S.stacksize =S.stacksize +ADDSIZE;
}
*(S.top)++=e;
return OK;
}
int Pop(SqStack &S,ElemType &e)
{
if(S.base !=S.top )
{
e=*(--S.top );
return OK;
}
else return ERROR;
}*/

#include "stdafx.h"
#include "stdafx.h"
#include "MyStack.h"
//#include "MyStack.cpp"

ElemType Precede(ElemType t1,ElemType t2)
{
ElemType f='0';
switch(t2)
{
case '+':
case '-':
if(t1=='('||t1=='=')
f='<';
else f='>';
break;
case '*':
case '/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
break;
case '(':
if(t1==')')
{
cout<<"ERROR"<<endl;
exit(ERROR);
}
else f='<';
break;
case ')':switch(t1)
{
case '(':f='=';
break;
case '=':printf("ERROR2\n");
exit(ERROR);
default: f='>';
}
break;
case'=':switch(t1)
{
case '=':f='=';
break;
case '(':cout<<"ERROR"<<endl;
default:f='>';
}
break;
}
return f;
}

int In(ElemType e)
{
switch(e)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'=':return TRUE;
default:return FALSE;
}
}
ElemType Operate(ElemType a,ElemType theta,ElemType b)
{
ElemType re=0;
switch(theta)
{
case'+':re=a+b;
break;
case'-':re=a-b;
break;
case'*':re=a*b;
break;
case'/':re=a/b;
break;
}
return re;
}
ElemType EvaluateExpression()
{
ElemType x,a,b,theta,d;
char c;
char z[6];
SqStack OPTR,OPND;
InitStack(OPTR);Push(OPTR,'=');
InitStack(OPND);
c=getchar();
GetTop(OPTR,x);
while(c!='='||x!='=')
{
if(In(c)) // 是7种运算符之一
switch(Precede(x,c))
{
case'<':Push(OPTR,c); // 栈顶元素优先权低
c=getchar();
break;
case'=':Pop(OPTR,x); // 脱括号并接收下一字符
c=getchar();
break;
case'>':Pop(OPTR,theta); // 退栈并将运算结果入栈
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
else if(c>='0'&&c<='9') // c是操作数
{
int i=0;
do
{
z[i]=c;
i++;
c=getchar();

}while(c>='0'&&c<='9');
z[i]=0;
d=atoi(z);
Push(OPND,d);
}
else // c是非法字符
{
printf("ERROR4\n");
exit(ERROR);
}
GetTop(OPTR,x);
}
GetTop(OPND,x);
return x;
}

// Calculator.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Calculator.h"

int _tmain(int argc, _TCHAR* argv[])
{
printf("请输入算术表达式,负数要用(0-正数)表示,并以=结束\n");
printf("%d\n",EvaluateExpression());

getchar();
system("pause");
return 0;
}
如果需要源码的话,827222866 ,
温馨提示:答案为网友推荐,仅供参考
相似回答