求一个c语言源程序,内容是计算机的求值问题,例如求2+5*(5+3/2)/6=,我正在学数据结构与

求一个c语言源程序,内容是计算机的求值问题,例如求2+5*(5+3/2)/6=,我正在学数据结构与算法那本书,这个应该要用到栈的,请大神帮帮忙,还有别用与这本书无关的其他函数!拜托了

第1个回答  2015-05-04
例如5+6要打5+6#
#include<cstdio>
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;

SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
}
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;

}
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
char GetTop(SNode *top){

return (top->next)->date;
}
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';

}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();
PushOptr(top[0],'#');
top[1]=InitStack();
c=getchar();
while(c!='#'||(GetTop(top[0]))!='#'){
if(!In(c)){
PushOpnd(top[1],c);
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);
switch(r){
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}

}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",result-'0');

}追问

额,有没有分工明确的源程序,就是一个人做乘法,一个人做除法的

第2个回答  2015-05-04
#include<stdio.h>
float fen(float f,char c,int n)
{
static float s=0;
if(n)
{
s=f;
return s;
}
if(c=='*')
s*=f;
else
s/=f;
return s;
}
float add(float a,char c)
{
static float b=0;
if(c=='+')
b+=a;
else
b-=a;
return b;
}
int main()
{
float k,n,sum=0,s=0;
char c,a='+';
int i=0;
printf("欢迎使用\n");
scanf("%f",&k);
while(++i)
{
c=getchar();
if(i==1&&(c=='*'||c=='/'))
sum=fen(k,c,1);
if(i==1&&(c=='+'||c=='-'))
s=add(k,a);
if(c=='\n')
{
if(i==1)
s=add(k,a);
else
s=add(sum,a);
break;
}
scanf("%f",&n);
if(c=='*'||c=='/')
sum=fen(n,c,0);
if(c=='+'||c=='-')
{
i=0;
k=n;
s=add(sum,a);
sum=0;
a=c;
}
}
printf("%g",s);
}追答

额,这个是以前也得可以四则运算,但不支持(),不过括号原理和成原理相同你改下就好

追问

额 这个你没有用到栈啊

不过谢谢你

追答

额没事

本回答被网友采纳