首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

后缀表达式求值算法。栈的应用解决方案

2012-04-17 
后缀表达式求值算法。。。栈的应用#includestdio.h#includestdlib.h#define StackMaxSize 30typedef int

后缀表达式求值算法。。。栈的应用
#include<stdio.h>
#include<stdlib.h>
#define StackMaxSize 30

typedef int ElemType;
struct Stack
{
ElemType elem[StackMaxSize];
int top;
}s;

void Push(ElemType x)
{
if(s.top = StackMaxSize - 1)
printf("stack is full!\n");
else
s.elem[++s.top] = x;
}

ElemType Pop()
{
ElemType e;
if(s.top == -1)
printf("stack is empty!\n");
else
e = s.elem[s.top--];
return e;
}

void ExprCompute(char *str)
{

char ch;
int i = 0, x;
ch = str[i++];
while(ch != '@')
{
switch(ch)
{
case '+':
x = Pop() + Pop();
break;
case '-':
x = Pop();
x = Pop() - x;
break;
case '*':
x = Pop() * Pop();
break;
case '/':
x = Pop();
if(x != 0)
x = Pop() / x;
else
{
printf("Divided by 0 !\n");
exit(1);
}
break;
default:
x = ch - 30;
break;
while(str[i] >= '0' && str[i] <= '9')
{
x = x * 10 + str[i] - 30;
i++;
}
}
Push(x);
ch = str[i++];
}

if(s.top > -1)
{
x = Pop();
if(s.top == -1)
return ;
else
{
printf("expression error !\n");
exit(1);
}
}
else
{
printf("Stack is empty!\n");
exit(1);
}
}

void main()
{
s.top = -1;
char str[20];
printf("please input a string ended with a @:");
gets(str);
for(int i = 0;str[i] != '@'; i++)
{
ExprCompute(str);
}
}

出啥错了?、求助

[解决办法]

C/C++ code
#include<stdio.h>#include<stdlib.h>#define StackMaxSize 30typedef int ElemType;struct Stack{    ElemType elem[StackMaxSize];    int top;}s;void Push(ElemType x){    if(s.top ==StackMaxSize - 1)  //判断错误         printf("stack is full!\n");    else        s.elem[s.top++] = x;      //}ElemType Pop(){    ElemType e;    if(s.top == -1)        printf("stack is empty!\n");    else        e = s.elem[s.top--];    return e;}void ExprCompute(char *str){            char ch;    int i = 0, x;    ch = str[i++];    while(ch != '@')    {        switch(ch)        {        case '+':            x = Pop() + Pop();            break;        case '-':            x = Pop();            x = Pop() - x;            break;        case '*':            x = Pop() * Pop();            break;        case '/':            x = Pop();            if(x != 0)                x = Pop() / x;            else            {                printf("Divided by 0 !\n");                exit(1);            }            break;        default:            x = ch - 48;   //            break;            while(str[i] >= '0' && str[i] <= '9')            {                x = x * 10 + str[i] - 48;             //                i++;            }        }        Push(x);        ch = str[i++];    }        if(s.top > -1)    {        x = Pop();        if(s.top == -1)            return ;        else        {            printf("expression error !\n");            exit(1);        }    }    else    {        printf("Stack is empty!\n");        exit(1);    }}void main(){    s.top = -1;    char str[20];    printf("please input a string ended with a @:");    gets(str);        ExprCompute(str);} 

热点排行