使用c语言设计一个程序,演示用算符优先法对算术表达式求值的过程。
时间: 2024-01-24 19:17:31 浏览: 94
设计一个程序,演示用算符优先法对算术表达式求值的过程
3星 · 编辑精心推荐
以下是使用C语言设计的用算符优先法对算术表达式求值的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
typedef enum {
ADD, SUB, MUL, DIV, LEFT_PAREN, RIGHT_PAREN, OPERAND
} TokenType;
typedef struct {
TokenType type;
int precedence;
int value;
} Token;
Token stack[MAX_STACK_SIZE];
int top = -1;
void push(Token token) {
if (top >= MAX_STACK_SIZE - 1) {
fprintf(stderr, "Stack overflow\n");
exit(EXIT_FAILURE);
}
stack[++top] = token;
}
Token pop() {
if (top < 0) {
fprintf(stderr, "Stack underflow\n");
exit(EXIT_FAILURE);
}
return stack[top--];
}
Token get_token(char *expr, int *index) {
Token token;
char ch = expr[*index];
switch (ch) {
case '+':
token.type = ADD;
token.precedence = 1;
break;
case '-':
token.type = SUB;
token.precedence = 1;
break;
case '*':
token.type = MUL;
token.precedence = 2;
break;
case '/':
token.type = DIV;
token.precedence = 2;
break;
case '(':
token.type = LEFT_PAREN;
token.precedence = 0;
break;
case ')':
token.type = RIGHT_PAREN;
token.precedence = 0;
break;
default:
if (isdigit(ch)) {
token.type = OPERAND;
token.precedence = 0;
token.value = ch - '0';
} else {
fprintf(stderr, "Invalid character '%c'\n", ch);
exit(EXIT_FAILURE);
}
break;
}
(*index)++;
return token;
}
int evaluate(Token operator, Token operand1, Token operand2) {
switch (operator.type) {
case ADD:
return operand1.value + operand2.value;
case SUB:
return operand1.value - operand2.value;
case MUL:
return operand1.value * operand2.value;
case DIV:
if (operand2.value == 0) {
fprintf(stderr, "Division by zero\n");
exit(EXIT_FAILURE);
}
return operand1.value / operand2.value;
default:
fprintf(stderr, "Invalid operator type\n");
exit(EXIT_FAILURE);
}
}
int main() {
char expr[100];
printf("Enter an arithmetic expression: ");
scanf("%s", expr);
int index = 0;
while (expr[index] != '\0') {
Token token = get_token(expr, &index);
if (token.type == OPERAND) {
push(token);
} else if (token.type == ADD || token.type == SUB || token.type == MUL || token.type == DIV) {
while (top >= 0 && stack[top].type != LEFT_PAREN && stack[top].precedence >= token.precedence) {
Token operator = pop();
Token operand2 = pop();
Token operand1 = pop();
int result = evaluate(operator, operand1, operand2);
Token result_token = {OPERAND, 0, result};
push(result_token);
}
push(token);
} else if (token.type == LEFT_PAREN) {
push(token);
} else if (token.type == RIGHT_PAREN) {
while (top >= 0 && stack[top].type != LEFT_PAREN) {
Token operator = pop();
Token operand2 = pop();
Token operand1 = pop();
int result = evaluate(operator, operand1, operand2);
Token result_token = {OPERAND, 0, result};
push(result_token);
}
if (top < 0 || stack[top].type != LEFT_PAREN) {
fprintf(stderr, "Unbalanced parentheses\n");
exit(EXIT_FAILURE);
}
pop();
} else {
fprintf(stderr, "Invalid token type\n");
exit(EXIT_FAILURE);
}
}
while (top >= 0) {
Token operator = pop();
if (operator.type == LEFT_PAREN) {
fprintf(stderr, "Unbalanced parentheses\n");
exit(EXIT_FAILURE);
}
Token operand2 = pop();
Token operand1 = pop();
int result = evaluate(operator, operand1, operand2);
Token result_token = {OPERAND, 0, result};
push(result_token);
}
if (top != 0) {
fprintf(stderr, "Invalid expression\n");
exit(EXIT_FAILURE);
}
Token result_token = pop();
printf("Result: %d\n", result_token.value);
return 0;
}
```
程序首先通过 `get_token` 函数将输入的表达式解析成一个个 Token,然后使用栈来维护运算符和操作数,按照算符优先级的顺序计算表达式的值。最终栈中只剩下一个操作数,即为表达式的结果。
阅读全文