主题:[讨论]表达式求解程序
//表达式求解程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define Stack_Size 100
char Operator[7]={'+','-','*','/','(',')','#'}; //用6出错!!!!
int Result,ch2;
char ch1; //定义全局变量
//*************优先关系**************************
char PriorityTable[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','o'},
{'>','>','>','>','o','>','>'},
{'<','<','<','<','<','o','='},
};
//***************运算符结构体*****************
typedef struct {
char *top;
char *base;
int stacklength;
}Sqstack1,*Pstack1;
//**************操作数结构*******************
typedef struct {
int *top;
int *base;
int stacklength;
}Sqstack2,*Pstack2;
//***************创建堆栈**********************
void InitStack1(Pstack1 S)
{
S->base=(char *)malloc(Stack_Size*sizeof(char));
if(!S->base)
exit(1);
S->top=S->base;
S->stacklength=Stack_Size;
}
void InitStack2(Pstack2 S)
{
S->base=(int *)malloc(Stack_Size*sizeof(int));
if(!S->base)
exit(1);
S->top=S->base;
S->stacklength=Stack_Size;
}
//******************入栈******************************
int PushStack1(Pstack1 S,char ch)
{
if(S->top-S->base>=Stack_Size)
exit(1);
*(S->top++)=ch;
}
void PushStack2(Pstack2 S,int ch)
{
if(S->top-S->base>=Stack_Size)
exit(1);
*(S->top++)=ch;
}
//******************出栈*********************
void PopStack1(Pstack1 S,char *ch)
{
if(S->top==S->base)
exit(1);
*ch=*(--S->top);
}
void PopStack2(Pstack2 S,int *ch)
{
if(S->top==S->base)
exit(1);
*ch=*(--S->top);
}
//**********取栈顶操作符*****************
int GetTop(Pstack1 S)
{
if(S->top==S->base)
exit(1);
return *(S->top-1);
}
//************查找优先权并返回*******************
char CheckPriority(char operator_1,char operator_2)
{
int i,j;//用来查询算符间优先关系表的下标
//返回算符优先关系表中相应值
for(i=0;i<7;i++)
if(operator_1==Operator[i]);
for(j=0;j<7;j++)
if(operator_2==Operator[j]);
return PriorityTable[i][j];
}
//**************计算中间表达式的值*********************
int Count(int a,char ch,int b)
{
switch(ch)
{
case'+':
return a+b;
break;
case'-':
return a-b;
break;
case'*':
return a*b;
break;
default:
if( b=0)
printf(" div 0 is error!\n");
else
return a/b;
}
}
//********接受字符和数字*********
int Get()
{
Sqstack1 S1;
Sqstack2 S2;
InitStack1(&S1);
InitStack2(&S2);
char Data[10];
int n=0;
char ch;
ch=getchar();
if(ch>='0'&&ch<='9')
{
Data[n++]=ch;
ch=getchar();
}
else if(ch=='\n')
ch1='#';
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')')
ch1=ch;
if(n>0)
{
Data[n]='\n';
ch2=atoi((Data));
PushStack2(&S2, ch2);
}
}
//*****************计算总表达式的值****************//有问题!!!!!
int Sum()
{
Sqstack1 S1;
Sqstack2 S2;
char Data[10];
char temp,c;
int n=0,a,b,num;
PushStack1( &S1, '#');
Get();
while(GetTop( &S1)!='#')
{
switch(CheckPriority(GetTop( &S1) ,ch1))
{
case'<':
PushStack1( &S1, ch1);
Get();
break;
case'=':
PopStack1(&S1, &temp);
Get();
break;
case'>':
PopStack2(&S2,&a);
PopStack2(&S2,&b);
PopStack1(&S1,&c);
num=Count(a,c,b);
PushStack2(&S2,num);
Get();
break;
default:
break;
}
}
Result=S2.base;
return 0;
}
//*******************主函数***************************
int main()
{
int i;
for(i=1;;i++)
{
printf("Please input Expression %d :",i);
Sum();
printf("=%d\n",Result);
}
return 0;
}
这是按照数据结构课本上的思路写的,编译虽然通过了,但产生了错误结果。
我觉得应该是“计算总表达式的值”,Sum函数这部分出了问题。
请高手指点。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define Stack_Size 100
char Operator[7]={'+','-','*','/','(',')','#'}; //用6出错!!!!
int Result,ch2;
char ch1; //定义全局变量
//*************优先关系**************************
char PriorityTable[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','o'},
{'>','>','>','>','o','>','>'},
{'<','<','<','<','<','o','='},
};
//***************运算符结构体*****************
typedef struct {
char *top;
char *base;
int stacklength;
}Sqstack1,*Pstack1;
//**************操作数结构*******************
typedef struct {
int *top;
int *base;
int stacklength;
}Sqstack2,*Pstack2;
//***************创建堆栈**********************
void InitStack1(Pstack1 S)
{
S->base=(char *)malloc(Stack_Size*sizeof(char));
if(!S->base)
exit(1);
S->top=S->base;
S->stacklength=Stack_Size;
}
void InitStack2(Pstack2 S)
{
S->base=(int *)malloc(Stack_Size*sizeof(int));
if(!S->base)
exit(1);
S->top=S->base;
S->stacklength=Stack_Size;
}
//******************入栈******************************
int PushStack1(Pstack1 S,char ch)
{
if(S->top-S->base>=Stack_Size)
exit(1);
*(S->top++)=ch;
}
void PushStack2(Pstack2 S,int ch)
{
if(S->top-S->base>=Stack_Size)
exit(1);
*(S->top++)=ch;
}
//******************出栈*********************
void PopStack1(Pstack1 S,char *ch)
{
if(S->top==S->base)
exit(1);
*ch=*(--S->top);
}
void PopStack2(Pstack2 S,int *ch)
{
if(S->top==S->base)
exit(1);
*ch=*(--S->top);
}
//**********取栈顶操作符*****************
int GetTop(Pstack1 S)
{
if(S->top==S->base)
exit(1);
return *(S->top-1);
}
//************查找优先权并返回*******************
char CheckPriority(char operator_1,char operator_2)
{
int i,j;//用来查询算符间优先关系表的下标
//返回算符优先关系表中相应值
for(i=0;i<7;i++)
if(operator_1==Operator[i]);
for(j=0;j<7;j++)
if(operator_2==Operator[j]);
return PriorityTable[i][j];
}
//**************计算中间表达式的值*********************
int Count(int a,char ch,int b)
{
switch(ch)
{
case'+':
return a+b;
break;
case'-':
return a-b;
break;
case'*':
return a*b;
break;
default:
if( b=0)
printf(" div 0 is error!\n");
else
return a/b;
}
}
//********接受字符和数字*********
int Get()
{
Sqstack1 S1;
Sqstack2 S2;
InitStack1(&S1);
InitStack2(&S2);
char Data[10];
int n=0;
char ch;
ch=getchar();
if(ch>='0'&&ch<='9')
{
Data[n++]=ch;
ch=getchar();
}
else if(ch=='\n')
ch1='#';
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')')
ch1=ch;
if(n>0)
{
Data[n]='\n';
ch2=atoi((Data));
PushStack2(&S2, ch2);
}
}
//*****************计算总表达式的值****************//有问题!!!!!
int Sum()
{
Sqstack1 S1;
Sqstack2 S2;
char Data[10];
char temp,c;
int n=0,a,b,num;
PushStack1( &S1, '#');
Get();
while(GetTop( &S1)!='#')
{
switch(CheckPriority(GetTop( &S1) ,ch1))
{
case'<':
PushStack1( &S1, ch1);
Get();
break;
case'=':
PopStack1(&S1, &temp);
Get();
break;
case'>':
PopStack2(&S2,&a);
PopStack2(&S2,&b);
PopStack1(&S1,&c);
num=Count(a,c,b);
PushStack2(&S2,num);
Get();
break;
default:
break;
}
}
Result=S2.base;
return 0;
}
//*******************主函数***************************
int main()
{
int i;
for(i=1;;i++)
{
printf("Please input Expression %d :",i);
Sum();
printf("=%d\n",Result);
}
return 0;
}
这是按照数据结构课本上的思路写的,编译虽然通过了,但产生了错误结果。
我觉得应该是“计算总表达式的值”,Sum函数这部分出了问题。
请高手指点。