注册 登录
编程论坛 数据结构与算法

这段代码switch好像没有运行到,输入(1+2)*3#最后栈里不会输出元素,为什么,谢谢告知

汐箜篌 发布于 2016-04-10 17:17, 2785 次点击
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define maxsize 20
typedef struct{
    char data[maxsize];
    int top;
}Seqstack;
Seqstack s1,s2;
int main(){
    char str[maxsize];
    int i=0;
    s1.top=s2.top=-1;
    printf("输入以#为结束标志的算式\n");
    scanf("%s",&str);
    while(str[i]!='#'){
        printf("%c",str[i]);
        switch(str[i]){
            case'(':
                s1.data[++s1.top]=str[i];break;
            case')':
                while(s1.data[s1.top]!='('){
                    s2.data[++s2.top]=s1.data[s1.top--];
                }
                s1.top--;
                break;
        }i++;
    }
    while(s1.top>-1){
        printf("%c\n",s1.data[s1.top--]);
    }
    return 0;
}
0 回复
1