hhhh还好还好 发表于 2018-10-7 00:20:47

后缀表达式求值

//代码如下,问题在代码里有描述



#include <stdio.h>
#include <stdlib.h>
#define max_size 100


typedef struct
{       
        char a;
        int top;
}wang,*WANG;                         //栈的数据结构定义



void initwang(WANG L)
{
        L->top=-1;                   //栈的初始化
}







void Cruzhan(WANG L,char ch)
{
        if(L->top>max_size-1)
        {
                printf("超出范围.\n");         //入栈函数
                exit(-1);
        }
        L->top++;
        L->a=ch;
}






void Cchuzhan(WANG L)
{
        if(L->top==-1)
        {
                printf("空栈.\n");          //出栈函数
                exit(-1);
        }
        L->top--;
}






char Cshow(WANG L)
{
        char x;
        if(L->top==-1)
        {
                printf("空栈,无法显示元素.\n");
                exit(-1);                                                    //显示栈顶元素函数
        }
        x=L->a;
        return x;
}





int main()
{
        WANG L1;
        char ch;
        char k3;
        int i=0;
        int k1,k2;
        L1=(WANG)malloc(sizeof(wang));
//        L2=(WANG)malloc(sizeof(wang));
        initwang(L1);
//        initwang(L2);
        printf("请输入字符串:");
        scanf("%s",ch);
       
        while(ch!='\0')
        {
                switch(ch)
                {
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                        {
                                Cruzhan(L1,ch);
                        }
                        break;
                case '+':
                case '*':
                        {
                                if(ch=='+')
                                {
                                        k1=(int)Cshow(L1);             //问题如下,我想将字符型的1和2先强制转化为整形,之后把他们相加,然后再转化为字符型入栈,但是好像不行。。。希望能给个别的思路,根据我这个代码最后输出的是空的。
                                        Cchuzhan(L1);
                                        k2=(int)Cshow(L1);
                                        Cchuzhan(L1);
                                        k3=(char)(k1+k2);
                                        Cruzhan(L1,k3);
                                }
                                if(ch=='*')
                                {
                                        k1=(int)Cshow(L1);
                                        Cchuzhan(L1);
                                        k2=(int)Cshow(L1);
                                        Cchuzhan(L1);
                                        k3=(char)(k2*k1);
                                        Cruzhan(L1,k3);
                                }
                                /*if(L1->top==0)
                                {
                                        printf("最终结果为:");
                                        printf("%c \n",L1->a);
                                }*/
                        }
                        break;
                }
                i++;
        }
        printf("最终结果为:");
        printf("%c \n",L1->a);
}

claws0n 发表于 2018-10-7 00:27:14

栈的应用是把中缀表达式编程后缀表达式而已吧?求值的话应该需要逆波兰

hhhh还好还好 发表于 2018-10-7 22:22:57

claws0n 发表于 2018-10-7 00:27
栈的应用是把中缀表达式编程后缀表达式而已吧?求值的话应该需要逆波兰

不不不,我这个书上的练习题写的就是求后缀表达式的值。。。。关键在于我问的问题,有什么解决方案吗?
页: [1]
查看完整版本: 后缀表达式求值