| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 785 人关注过本帖
标题:用顺序栈实现的四则运算表达式求值,编译没错误,就是只能算一位数的,求指 ...
只看楼主 加入收藏
爱编聪聪
Rank: 2
等 级:论坛游民
帖 子:26
专家分:20
注 册:2013-9-16
结帖率:100%
收藏
 问题点数:0 回复次数:0 
用顺序栈实现的四则运算表达式求值,编译没错误,就是只能算一位数的,求指点

在两位数及两位数以上进行运算时,中缀表达式也不能正确转化为后缀表达式,我觉得是以下红字部分的问题,不知道怎么改
用顺序栈实现的
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 1024
typedef char elemtype;
typedef struct sequenstack
{
    elemtype data[MAXSIZE];
    int top;
}sequenstack;
sequenstack*init_sequenstack();
int sequenstack_full(sequenstack *s);
int sequenstack_empty(sequenstack *s);
int push_sequenstack(sequenstack *s,elemtype x);
int pop_sequenstack(sequenstack *s,elemtype*x);
int gettop_sequenstack(sequenstack *s,elemtype *x);
int arithmetic(int operand1,int operand2,char operators);
void calculate(sequenstack *s,char operators);
int changechartodata(char c);
char changedatatochar(int x);
char *transform(char *express);
int evaluatepostfix(char *express);
void main()
{
    char choose;
    char *infix;
    char *postfix;
    int result=0;
    while(1)
    {
        infix=(char*)malloc(sizeof(char));
        printf("\n\n\n\n\n\n\t\t\t请输入要计算的表达式:");
        gets(infix);
        postfix=transform(infix);
        printf("转化后的后缀表达式:%s",postfix);
        result=evaluatepostfix(postfix);
        printf("\n计算结果:%d\n",result);
        printf("\n是否继续计算?( 按任意键继续计算 or N):");
        fflush(stdin);
        choose=getchar();
        fflush(stdin);
        if(choose=='N')
            break;
        system("cls");
    }
}
sequenstack * init_sequenstack()
{
    sequenstack *s;
    s=(sequenstack *)malloc(sizeof(sequenstack));
    s->top=-1;
    return s;
}
int sequenstack_empty(sequenstack *s)      /*判空*/
{
    if(s->top==-1)
        return 1;
    else
        return 0;
}
int sequenstack_full(sequenstack *s)            /*判满*/
{
    if(s->top+1==MAXSIZE)
        return 1;
    else
        return 0;
}
int push_sequenstack(sequenstack *s,elemtype x)       /*入栈*/
{
    if(s->top>=MAXSIZE-1)
        return 0;
    s->top++;
    s->data[s->top]=x;
    return 1;
}
int pop_sequenstack(sequenstack *s,elemtype *x)        /*出栈*/
{
    if(s->top==-1)
        return 0;
    else
    {
        s->top--;
        *x=s->data[s->top+1];
        return 1;
    }
}
int gettop_sequenstack(sequenstack *s,elemtype *x)      /*读取栈顶元素*/
{
    if(s->top==-1)
    {
        printf("栈为空!");
        return 0;
    }
    else
    {
        *x=s->data[s->top];
        return 1;
    }
}
int evaluatepostfix(char *postfix)
{
    char operand;
    sequenstack *s;
    char result;
    int ii;
    s=init_sequenstack();
    for(ii=0;postfix[ii]!='\0';ii++)
    {
        operand=postfix[ii];
        if(operand!='+'&&operand!='-'&&operand!='*'&&operand!='/')
            push_sequenstack(s,operand);
        else
            calculate(s,operand);
    }
    pop_sequenstack(s,&result);
    return changechartodata(result);
}
int changechartodata(char c)
{
    return c-'0';
}
char changedatatochar(int x)
{
    return x+48;
}
void calculate(sequenstack*s,char operators)
{
    char operand1,operand2;
    int result;
    pop_sequenstack(s,&operand2);
    pop_sequenstack(s,&operand1);
    result=arithmetic(changechartodata(operand1),changechartodata(operand2),operators);
    push_sequenstack(s,changedatatochar(result));
}
int arithmetic(int operand1,int operand2,char operators)
{
    int result;
    switch(operators)
    {
        case '+':result=operand1+operand2;break;
        case '-':result=operand1-operand2;break;
        case '*':result=operand1*operand2;break;
        case '/':result=operand1/operand2;break;
    }
    return result;
}
char *transform(char *express)                  /*将输入的中缀表达式转化为后缀表达式*/
{
    char oprand,operators,ca[20];
    sequenstack *s;
    char *postfix;
    int offset=0,ia[20];
    int ii,it,ij,ip,is,isum;
    s=init_sequenstack();
    postfix=(char*)malloc(sizeof(char));
    for(ii=0;express[ii]!='\0';ii++)
    {
        oprand=express[ii];
        switch(oprand)
        {
            case '+':
            case '-':
                while(!sequenstack_empty(s))
                {
                    gettop_sequenstack(s,&operators);
                    if(operators!='(')
                    {
                        pop_sequenstack(s,&operators);
                        postfix[offset++]=operators;
                    }
                    else
                        break;
                }
                push_sequenstack(s,oprand);
                break;
            case '*':
            case '/':
                while(!sequenstack_empty(s))
                {
                    gettop_sequenstack(s,&operators);
                    if(operators=='*'||operators=='/')
                    {
                        pop_sequenstack(s,&operators);
                        postfix[offset++]=operators;
                    }
                    else
                        break;
                }
                push_sequenstack(s,oprand);
                break;
            case '(':
                push_sequenstack(s,oprand);
                break;
            case ')':
                while(!sequenstack_empty(s))
                {
                    gettop_sequenstack(s,&operators);
                    if(operators!='(')
                    {
                        pop_sequenstack(s,&operators);
                        postfix[offset++]=operators;
                    }
                    else
                    {
                        pop_sequenstack(s,&operators);
                        break;
                    }
                }
                break;
            default:
                it=ij=isum=0;
                while(express[ii]>'0'&&express[ii]<'9')
                {
                    ca[it]=express[ii];
                    ii++;
                    ij++;
                }
                for(ip=0;ip<ij;ip++)
                    ia[ip]=changechartodata(ca[ip]);
                for(is=1,ip=ij-1;is<ij*10,ip>=0;is=is*10,ip--)
                    isum=isum+ia[ip]*is;
                ii--;
                postfix[offset++]=changedatatochar(isum);
        }

    }
    while(!sequenstack_empty(s))
    {
        pop_sequenstack(s,&operators);
        postfix[offset++]=operators;
    }
    postfix[offset]='\0';
    return postfix;
}












































搜索更多相关主题的帖子: include 表达式 
2013-10-27 21:04
快速回复:用顺序栈实现的四则运算表达式求值,编译没错误,就是只能算一位数的, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016664 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved