天天看点

中缀表达式转化成后缀表达式并计算结果

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 1024

void print(char *postexp){
    while(*postexp != '\0'){
        printf("%c",*postexp);
        postexp++;
    }
    printf("\n");
}
void change(char* exp,char postexp[]){
    //定义栈 
    char st[MaxSize];
    int top = -1;
    int i = 0,k = 0;
	//规则:栈顶元素比当前处理元素优先级高时出栈,之后当前元素进栈。
    while(*exp != '\0'){
        switch(*exp){
            case '('://左括号直接进栈
                st[++top] = *exp;
                exp++;
				break;
            case ')':
				//右括号时出栈元素直到左括号,左括号也出栈
                while(top != -1 || st[top] != '('){
                    postexp[k++] = st[top--];
                }
                exp++;
				break;
            case '+':
            case '-':
				//栈不空且栈顶不为左括号时出栈
				while(top != -1 && st[top] != '('){
					postexp[k++] = st[top--];
					
				}
				//进栈
                st[++top] = *exp;
                exp++;
				break;
            case '*':
            case '/':
				//栈不空且栈顶为‘*’或‘/’时出栈
                while(top != -1 && (st[top] == '*' || st[top] == '/')){
                    postexp[k++] = st[top--];
                }
				st[++top] = *exp;
                exp++;
				break;
            default:
				//获取整数并进栈,整数以‘#’结尾
                while(*exp >= '0' && *exp <= '9'){
                    postexp[k++] = *exp;
                    exp++;
                }
                postexp[k++] = '#';
        }
    }
	//栈不为空时进栈
    while(top != -1){
        postexp[k++] = st[top--];
    }
	postexp[k] = '\0';
}

int value(char* postexp){
    int st[MaxSize];
    int top = -1;
    
	int sum = 0;
    int firstA = 0,secondA = 0;
    
    while(*postexp != '\0'){
        switch(*postexp){
            case '+':
                firstA = st[top--];
                secondA = st[top--];
                secondA += firstA;
                st[++top] = secondA;
                break;
            case '-':
                firstA = st[top--];
                secondA = st[top--];
                secondA -= firstA;
                st[++top] = secondA;
                break;
            case '*':
                firstA = st[top--]; 
                secondA = st[top--];
                secondA *= firstA;
                st[++top] = secondA;
                break;
            case '/':
                firstA = st[top--];
                secondA = st[top--];
                if(firstA == 0){
                    printf("除零错误!!!");
                    exit(0);
                }
                secondA /= firstA;
                st[++top] = secondA;
                break;
            default:
                while(*postexp >= '0' && *postexp <= '9'){
                    sum += *postexp - '0';
                    postexp++;
                }
                st[++top] = sum;
        }
        postexp++;
    }
    return st[top];
}

int main(){
    char exp[18] = "2+3*(432-2)/10-86";
    char postexp[MaxSize];
    change(exp,postexp);
	printf("中缀表达式:%s",exp);
	printf("后缀表达式:%s",postexp);
    print(postexp);
	printf("表达式结果值:%d",value(postexp));
    
    return 1;
}
           

继续阅读