前后缀表达式c语言代码
时间: 2023-10-15 20:06:03 浏览: 68
前缀表达式:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
typedef struct stack {
int top;
int data[MAX_SIZE];
} Stack;
void init(Stack *s) {
s->top = -1;
}
int is_empty(Stack *s) {
return s->top == -1;
}
int is_full(Stack *s) {
return s->top == MAX_SIZE - 1;
}
void push(Stack *s, int value) {
if (is_full(s)) {
printf("stack is full\n");
return;
}
s->data[++s->top] = value;
}
int pop(Stack *s) {
if (is_empty(s)) {
printf("stack is empty\n");
return -1;
}
return s->data[s->top--];
}
int peek(Stack *s) {
if (is_empty(s)) {
printf("stack is empty\n");
return -1;
}
return s->data[s->top];
}
int evaluate_prefix_expression(char *expression) {
Stack s;
init(&s);
int length = strlen(expression);
for (int i = length - 1; i >= 0; i--) {
if (isdigit(expression[i])) {
push(&s, expression[i] - '0');
} else {
int operand1 = pop(&s);
int operand2 = pop(&s);
switch (expression[i]) {
case '+':
push(&s, operand1 + operand2);
break;
case '-':
push(&s, operand1 - operand2);
break;
case '*':
push(&s, operand1 * operand2);
break;
case '/':
push(&s, operand1 / operand2);
break;
case '%':
push(&s, operand1 % operand2);
break;
default:
printf("invalid character %c\n", expression[i]);
return -1;
}
}
}
return pop(&s);
}
int main() {
char expression[MAX_SIZE];
printf("Enter prefix expression: ");
fgets(expression, MAX_SIZE, stdin);
expression[strlen(expression) - 1] = '\0'; // remove newline character
int result = evaluate_prefix_expression(expression);
printf("Result = %d\n", result);
return 0;
}
```
后缀表达式:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
typedef struct stack {
int top;
int data[MAX_SIZE];
} Stack;
void init(Stack *s) {
s->top = -1;
}
int is_empty(Stack *s) {
return s->top == -1;
}
int is_full(Stack *s) {
return s->top == MAX_SIZE - 1;
}
void push(Stack *s, int value) {
if (is_full(s)) {
printf("stack is full\n");
return;
}
s->data[++s->top] = value;
}
int pop(Stack *s) {
if (is_empty(s)) {
printf("stack is empty\n");
return -1;
}
return s->data[s->top--];
}
int peek(Stack *s) {
if (is_empty(s)) {
printf("stack is empty\n");
return -1;
}
return s->data[s->top];
}
int evaluate_postfix_expression(char *expression) {
Stack s;
init(&s);
int length = strlen(expression);
for (int i = 0; i < length; i++) {
if (isdigit(expression[i])) {
push(&s, expression[i] - '0');
} else {
int operand2 = pop(&s);
int operand1 = pop(&s);
switch (expression[i]) {
case '+':
push(&s, operand1 + operand2);
break;
case '-':
push(&s, operand1 - operand2);
break;
case '*':
push(&s, operand1 * operand2);
break;
case '/':
push(&s, operand1 / operand2);
break;
case '%':
push(&s, operand1 % operand2);
break;
default:
printf("invalid character %c\n", expression[i]);
return -1;
}
}
}
return pop(&s);
}
int main() {
char expression[MAX_SIZE];
printf("Enter postfix expression: ");
fgets(expression, MAX_SIZE, stdin);
expression[strlen(expression) - 1] = '\0'; // remove newline character
int result = evaluate_postfix_expression(expression);
printf("Result = %d\n", result);
return 0;
}
```