问题描述
这里限定的表达式求值问题是: 用户输入一个包含“+”、“-”、“*”、“/”、正整数和圆括号的合法数学表达式,计算该表达式的运算结果。
算术表达式求值过程是: STEP 1:先将算术表达式转换成后缀表达式。
STEP 2:然后对该后缀表达式求值。
首先分析一下实现过程
中缀表达式转后缀表达式
首先从键盘输入中缀表达式,想要将中缀转为后缀需要用到两个栈,分别是数字栈与符号栈,依次遍历中缀表达式,如是数字直接压入数字栈,如符号栈为空或者当前符合是“(”直接压入字符栈,如当前符合优先级大于栈顶符号,直接将当前符号压入字符栈,如当前符号小于或等于栈顶符号,那就把符号栈里所有符号依次退栈并压入数字栈中,如当前符号是“)”,即说明匹配上了,则将符号栈依次退栈压入数字栈,直到遇见“(”为止,最后将数字栈输出即是后缀表达式。
后缀表达式求值
创建一个求和栈,然后从0索引遍历数字栈中的数组,遇到数字直接入求和栈,如果是符号那就将求和栈的栈顶元素和次顶元素做运算,将结果放进求和栈,循环结束求和栈里的值就是结果。
代码实现
#include <stdio.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
typedef char SElemType;
typedef int Status;
typedef struct Stack{
SElemType data[MAXSIZE];
int top;
}Stack;
//初始化栈
void InitStack(Stack *S){
S->top = -1;
}
//压栈
Status Push(Stack *S, SElemType e){
if(S->top == MAXSIZE - 1){
return ERROR;
}
S->top++;
S->data[S->top] = e;
return OK;
}
//出栈
Status Pop(Stack *S, SElemType *e){
if(S->top == -1){
return ERROR;
}
*e = S->data[S->top];
S->top--;
return OK;
}
//拿到栈顶元素
SElemType getTop(Stack *S){
return S->data[S->top];
}
//判断字符是否为数字
Status isNum(SElemType ch){
if(ch >= 48 && ch <= 57){
return OK;
}else{
return ERROR;
}
}
//符号对比
char compare(char c1, char c2){
int num1, num2;
char result;
switch(c1){
case '(':
num1 = 0;
break;
case '+':
case '-':
num1 = 1;
break;
case '*':
case '/':
num1 = 2;
break;
}
switch(c2){
case '(':
num2 = 0;
break;
case '+':
case '-':
num2 = 1;
break;
case '*':
case '/':
num2 = 2;
break;
}
if(num1 > num2){
result = '>';
}else if(num1 <= num2){
result ='<';
}
return result;
}
int charToInt(char c){
int num = c - 48;
return num;
}
char intToChar(int i){
char c = i + 48;
return c;
}
int main(){
Stack num;
InitStack(&num);
Stack opera;
InitStack(&opera);
SElemType elem;
SElemType express[100];
printf("中缀表达式为:");
scanf("%s", express);
SElemType *p = express;
SElemType c;
while(*p != '\0'){
//数字直接进数字栈
if(isNum(*p)){
Push(&num, *p);
}else{
//当前栈为空或者栈顶元素为'('符号进符号栈
if(opera.top == -1 || *p == '('){
Push(&opera, *p);
}else{
//当前符号大于栈顶符号,直接进符号栈
if(compare(*p, getTop(&opera)) == '>' && *p != ')'){
Push(&opera, *p);
/*
当前符号小于栈顶符号,将栈内所有元素依次出符号栈并压入数字栈,
并当当前符号压入符号栈
*/
}else if(compare(*p, getTop(&opera)) == '<' && *p != ')'){
while(opera.top != -1){
Pop(&opera, &c);
Push(&num, c);
}
Push(&opera, *p);
/*
若当前符号是')',那就依次将符号栈出栈,并把符号依次压入数字栈
同时干掉'('和')'
*/
}else if(*p == ')'){
Pop(&opera, &c);
while(c != '('){
Push(&num, c);
Pop(&opera, &c);
}
}
}
}
p++;
}
//若中缀表达式遍历完了之后,符号栈内还有元素,那就依次将符号栈内元素压入数字栈
if(opera.top != -1){
while(opera.top != -1){
Pop(&opera, &c);
Push(&num, c);
}
}
Push(&num, '\0');
printf("\n后缀表达式为:");
printf("%s", num.data);
Stack sum;
InitStack(&sum);
SElemType *p2 = num.data;
SElemType top1, top2, top3;
int num1, num2 ,num3;
while(*p2 != '\0'){
if(isNum(*p2)){
Push(&sum, *p2);
}else if(*p2 == '+'){
Pop(&sum, &top1);
Pop(&sum, &top2);
num1 = charToInt(top1);
num2 = charToInt(top2);
num3 = num2 + num1;
top3 = intToChar(num3);
Push(&sum, top3);
}else if(*p2 == '-'){
Pop(&sum, &top1);
Pop(&sum, &top2);
num1 = charToInt(top1);
num2 = charToInt(top2);
num3 = num2 - num1;
top3 = intToChar(num3);
Push(&sum, top3);
}else if(*p2 == '*'){
Pop(&sum, &top1);
Pop(&sum, &top2);
num1 = charToInt(top1);
num2 = charToInt(top2);
num3 = num2 * num1;
top3 = intToChar(num3);
Push(&sum, top3);
}else if(*p2 == '/'){
Pop(&sum, &top1);
Pop(&sum, &top2);
num1 = charToInt(top1);
num2 = charToInt(top2);
num3 = num2 / num1;
top3 = intToChar(num3);
Push(&sum, top3);
}
p2++;
}
printf("\n\n结果为:");
printf("%c", sum.data[0]);
return 0;
}
结果与分析