不好用的东西呀,我把它复杂了 ,可有错呀
[此贴子已经被作者于2007-6-15 16:16:14编辑过]
[CODE]#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct{
char data[MAXSIZE];
int top;
}SeqStack,*PSeqStack;
PSeqStack Init_SeqStack();
void Destroy_SeqStack(PSeqStack *S);
int Empty_SeqStack(PSeqStack S);
int Push_SeqStack(PSeqStack S,char x);
int Pop_SeqStack(PSeqStack S,char *x);
int GetTop_SeqStack(PSeqStack S,char *x);
int IsNum(char c);
char procede(char theta1,char theta2);
int infx_exp_value(char *infixexp,char *postfixexp);
double postfix_exp(char *A);
void main()
{
char *p,*q,str[30],str1[30];
double result;
gets(str);
p=str;
q=str1;
infx_exp_value(p,q);
result=postfix_exp(q);
printf("\nThe result is:%d\n",result);
}
PSeqStack Init_SeqStack()
{
PSeqStack S=(PSeqStack)malloc(sizeof(SeqStack));
if(S) S->top=-1;
return S;
}
void Destroy_SeqStack(PSeqStack *S)
{
if(*S) free(*S);
*S=NULL;
return;
}
int Empty_SeqStack(PSeqStack S)
{
if(S->top==-1) return 1;
else return 0;
}
int Push_SeqStack(PSeqStack S,char x)
{
if(S->top==MAXSIZE) return 0;
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}
int Pop_SeqStack(PSeqStack S,char *x)
{
if(Empty_SeqStack(S)) return 0;
else
{
*x=S->data[S->top];
S->top--;
return 1;
}
}
int GetTop_SeqStack(PSeqStack S,char *x)
{
if(Empty_SeqStack(S)) return 0;
else
{
*x=S->data[S->top];
return 1;
}
}
int IsNum(char c)
{
if(c>='0'&&c<='9') return 1;
else return 0;
}
char procede(char theta1,char theta2)
{
int i,row,col;
static char CH[7]={'+','-','*','/','(',')','#'},
R[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','/'},
{'>','>','>','>','/','>','>'},
{'<','<','<','<','<','/','='}};
for(i=0;i<7;i++)
if(CH[i]==theta1)
{
row=i;break;
}
for(i=0;i<7;i++)
if(CH[i]==theta2)
{
col=i;break;
}
if(row<7&&col<7)
return (R[row][col]);
else
return ('/');
}
int infx_exp_value(char *infixexp,char *postfixexp)
{
PSeqStack S;
char w,topelement;
S=Init_SeqStack();
if(!S)
{
printf("init error!");
return 0;
}
Push_SeqStack(S,'#');
w=*infixexp;
while(!Empty_SeqStack(S))
{
if(IsNum(w))
{
*postfixexp=w;
postfixexp++;
}
else
{
GetTop_SeqStack(S,&topelement);
switch(procede(topelement,w))
{
case '>':
Pop_SeqStack(S,&topelement);
*postfixexp=topelement;
postfixexp++;
break;
case '=':
Pop_SeqStack(S,&topelement);
w=*(++infixexp);
break;
case '<':
Push_SeqStack(S,w);
w=*(++infixexp);
break;
defalut:printf("infixexp error!");
return 0;
}
}
}
*postfixexp='#';
Destroy_SeqStack(&S);
}
double postfix_exp(char *A)
{
PSeqStack S;
double result,a,b,c;
char ch;
ch=*A++;
S=Init_SeqStack();
while(ch!='#')
{
if(IsNum(ch))
Push_SeqStack(S,ch-'0');
else
{
Pop_SeqStack(S,&b);/*类型不匹配*/
Pop_SeqStack(S,&a);
switch(ch)
{
case '+':
c=a+b;break;
case '-':
c=a-b;break;
case '*':
c=a*b;break;
case '/':
c=a/b;break;
}
Push_SeqStack(S,c);
}
ch=*A++;
}
GetTop_SeqStack(S,&result);
Destroy_SeqStack(&S);
return result;
}[/CODE]
有谁帮我改一下,是计算器程序,好像有些问题,程序运行不出结果,哪位大虾帮我改一下,谢谢!
[此贴子已经被作者于2007-6-14 11:50:46编辑过]