1. 创建顺序栈的结构、出栈、入栈、判栈空、栈满。 2. 实现加、减、乘、除功能。 3. 能正确计算带有圆括号的表达式。 4. 能判断用户输入的表达式输入是否合理。若输入正确的算术表达式,输出正确的结果,否则输出错误提示。
时间: 2024-05-09 08:14:31 浏览: 23
1. 创建顺序栈的结构、出栈、入栈、判栈空、栈满。
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int top;
} SqStack;
void initStack(SqStack *s) {
s->top = -1;
}
int isEmpty(SqStack *s) {
return s->top == -1;
}
int isFull(SqStack *s) {
return s->top == MAXSIZE - 1;
}
int push(SqStack *s, int x) {
if (isFull(s)) {
return 0;
}
s->data[++s->top] = x;
return 1;
}
int pop(SqStack *s, int *x) {
if (isEmpty(s)) {
return 0;
}
*x = s->data[s->top--];
return 1;
}
```
2. 实现加、减、乘、除功能。
```c
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int div(int a, int b) {
if (b == 0) {
printf("Error: division by zero\n");
exit(1);
}
return a / b;
}
```
3. 能正确计算带有圆括号的表达式。
```c
int calculate(char *exp) {
SqStack numStack;
SqStack opStack;
initStack(&numStack);
initStack(&opStack);
while (*exp != '\0') {
if (*exp >= '0' && *exp <= '9') {
int num = *exp - '0';
while (*(exp + 1) >= '0' && *(exp + 1) <= '9') {
num = num * 10 + *(++exp) - '0';
}
push(&numStack, num);
} else if (*exp == '(') {
push(&opStack, '(');
} else if (*exp == ')') {
while (!isEmpty(&opStack) && opStack.data[opStack.top] != '(') {
int b, a;
char op;
pop(&opStack, &op);
pop(&numStack, &b);
pop(&numStack, &a);
int result = 0;
switch (op) {
case '+':
result = add(a, b);
break;
case '-':
result = sub(a, b);
break;
case '*':
result = mul(a, b);
break;
case '/':
result = div(a, b);
break;
}
push(&numStack, result);
}
int tmp;
pop(&opStack, &tmp);
} else if (*exp == '+' || *exp == '-' || *exp == '*' || *exp == '/') {
while (!isEmpty(&opStack) && precedence(*exp) <= precedence(opStack.data[opStack.top])) {
int b, a;
char op;
pop(&opStack, &op);
pop(&numStack, &b);
pop(&numStack, &a);
int result = 0;
switch (op) {
case '+':
result = add(a, b);
break;
case '-':
result = sub(a, b);
break;
case '*':
result = mul(a, b);
break;
case '/':
result = div(a, b);
break;
}
push(&numStack, result);
}
push(&opStack, *exp);
}
exp++;
}
while (!isEmpty(&opStack)) {
int b, a;
char op;
pop(&opStack, &op);
pop(&numStack, &b);
pop(&numStack, &a);
int result = 0;
switch (op) {
case '+':
result = add(a, b);
break;
case '-':
result = sub(a, b);
break;
case '*':
result = mul(a, b);
break;
case '/':
result = div(a, b);
break;
}
push(&numStack, result);
}
int result;
pop(&numStack, &result);
return result;
}
int precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
```
4. 能判断用户输入的表达式输入是否合理。若输入正确的算术表达式,输出正确的结果,否则输出错误提示。
```c
#include <ctype.h>
int checkExpression(char *exp) {
int count = 0;
while (*exp != '\0') {
if (*exp == '(') {
count++;
} else if (*exp == ')') {
count--;
} else if (!isdigit(*exp) && *exp != '+' && *exp != '-' && *exp != '*' && *exp != '/') {
return 0;
}
if (count < 0) {
return 0;
}
exp++;
}
return count == 0;
}
int main() {
char exp[100];
printf("Enter an arithmetic expression: ");
scanf("%s", exp);
if (checkExpression(exp)) {
int result = calculate(exp);
printf("Result: %d\n", result);
} else {
printf("Error: invalid expression\n");
}
return 0;
}
```