写一段C语言程序,输入一个中缀表达式,判断表达式是否合理,如果合理则输出他的后缀表达式并输出运算结果不合理则指出哪里出现了错误,要求输入可以是多位整数,小数,负数,并且存在菜单,选择1输入中缀表达式,2判断并转换,3进行下一道中缀表达式的输入
时间: 2024-03-23 10:42:36 浏览: 54
中缀表达式运算
好的,这里是一个简单的程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define STACK_SIZE 100
typedef struct {
int top;
char data[STACK_SIZE];
} Stack;
void init_stack(Stack *s) {
s->top = -1;
}
void push(Stack *s, char c) {
if (s->top == STACK_SIZE - 1) {
fprintf(stderr, "Stack overflow\n");
exit(EXIT_FAILURE);
}
s->data[++s->top] = c;
}
char pop(Stack *s) {
if (s->top == -1) {
fprintf(stderr, "Stack underflow\n");
exit(EXIT_FAILURE);
}
return s->data[s->top--];
}
char peek(Stack *s) {
if (s->top == -1) {
fprintf(stderr, "Stack underflow\n");
exit(EXIT_FAILURE);
}
return s->data[s->top];
}
int is_empty(Stack *s) {
return s->top == -1;
}
int is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
int priority(char c) {
switch (c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
void infix_to_postfix(char *infix, char *postfix) {
Stack s;
init_stack(&s);
int i, j;
for (i = 0, j = 0; infix[i] != '\0'; i++) {
if (isdigit(infix[i]) || infix[i] == '.') {
postfix[j++] = infix[i];
} else if (is_operator(infix[i])) {
while (!is_empty(&s) && priority(peek(&s)) >= priority(infix[i])) {
postfix[j++] = pop(&s);
}
push(&s, infix[i]);
} else if (infix[i] == '(') {
push(&s, infix[i]);
} else if (infix[i] == ')') {
while (!is_empty(&s) && peek(&s) != '(') {
postfix[j++] = pop(&s);
}
if (is_empty(&s)) {
fprintf(stderr, "Mismatched parentheses\n");
exit(EXIT_FAILURE);
}
pop(&s);
} else if (isspace(infix[i])) {
continue;
} else {
fprintf(stderr, "Invalid character: %c\n", infix[i]);
exit(EXIT_FAILURE);
}
}
while (!is_empty(&s)) {
if (peek(&s) == '(') {
fprintf(stderr, "Mismatched parentheses\n");
exit(EXIT_FAILURE);
}
postfix[j++] = pop(&s);
}
postfix[j] = '\0';
}
double evaluate_postfix(char *postfix) {
Stack s;
init_stack(&s);
int i;
for (i = 0; postfix[i] != '\0'; i++) {
if (isdigit(postfix[i])) {
double num = 0;
while (isdigit(postfix[i]) || postfix[i] == '.') {
if (postfix[i] == '.') {
i++;
break;
}
num = num * 10 + (postfix[i] - '0');
i++;
}
i--;
push(&s, num);
} else if (is_operator(postfix[i])) {
double b = pop(&s);
double a = pop(&s);
switch (postfix[i]) {
case '+':
push(&s, a + b);
break;
case '-':
push(&s, a - b);
break;
case '*':
push(&s, a * b);
break;
case '/':
push(&s, a / b);
break;
}
} else {
fprintf(stderr, "Invalid character: %c\n", postfix[i]);
exit(EXIT_FAILURE);
}
}
if (s.top != 0) {
fprintf(stderr, "Too many operands\n");
exit(EXIT_FAILURE);
}
return pop(&s);
}
int main() {
char infix[100], postfix[100];
int choice;
do {
printf("Menu:\n");
printf("1. Enter infix expression\n");
printf("2. Convert and evaluate\n");
printf("3. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter infix expression: ");
scanf(" %[^\n]s", infix);
break;
case 2:
infix_to_postfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
printf("Result: %f\n", evaluate_postfix(postfix));
break;
case 3:
break;
default:
printf("Invalid choice\n");
break;
}
} while (choice != 3);
return 0;
}
```
这个程序使用栈来实现将中缀表达式转换为后缀表达式,并计算后缀表达式的值。其中包括了菜单,可以让用户输入中缀表达式,转换为后缀表达式并计算结果,或者退出程序。注意,这个程序假设输入的表达式是合理的,因此没有进行错误处理的情况下不一定能正确处理所有输入。
阅读全文