C语言实现整数四则运算,含括号
时间: 2024-03-29 19:37:23 浏览: 14
好的,我可以为您介绍一下 C 语言实现整数四则运算,含括号的方法。
首先,我们可以使用栈来实现表达式的计算。具体实现步骤如下:
1. 创建两个栈,一个用于存储操作符,一个用于存储操作数。
2. 从左到右遍历表达式,遇到数字时,将其压入操作数栈;遇到运算符时,分以下几种情况处理:
(1)若当前操作符的优先级比栈顶操作符的优先级高,直接将该操作符压入操作符栈。
(2)若当前操作符的优先级比栈顶操作符的优先级低或相等,将栈顶操作符弹出并进行计算,直到当前操作符的优先级比栈顶操作符的优先级高为止,然后将当前操作符压入操作符栈。
(3)若当前操作符为左括号,则直接将其压入操作符栈。
(4)若当前操作符为右括号,则将操作符栈中的操作符弹出并进行计算,直到遇到左括号为止,左括号弹出丢弃。
3. 当表达式遍历完成后,将操作符栈中的操作符依次弹出并进行计算,直到操作符栈为空。
4. 最终,操作数栈中剩下的数字即为表达式的计算结果。
例如,对于表达式 "3 + 5 * ( 4 - 2 ) / 2",按照上述步骤进行计算,得到的结果为 8。
希望这个回答对您有所帮助。
相关问题
c语言栈实现四则运算
栈是一种可以进行后进先出(Last In First Out,LIFO)操作的数据结构,可以用来实现四则运算的求值。以下是使用C语言实现栈来实现四则运算的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
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 val) {
if (is_full(s)) {
printf("Stack overflow\n");
exit(1);
}
s->data[++s->top] = val;
}
// 出栈操作
int pop(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow\n");
exit(1);
}
return s->data[s->top--];
}
// 获取栈顶元素
int top(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow\n");
exit(1);
}
return s->data[s->top];
}
// 判断字符是否为数字
int is_digit(char c) {
return c >= '0' && c <= '9';
}
// 计算两个数的结果
int calculate(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
printf("Invalid operator\n");
exit(1);
}
}
// 计算表达式的值
int evaluate(char *expr) {
Stack operand_stack, operator_stack;
init(&operand_stack);
init(&operator_stack);
while (*expr != '\0') {
if (is_digit(*expr)) {
int num = 0;
while (is_digit(*expr)) {
num = num * 10 + (*expr - '0');
expr++;
}
push(&operand_stack, num);
} else if (*expr == '(') {
push(&operator_stack, '(');
expr++;
} else if (*expr == ')') {
while (top(&operator_stack) != '(') {
char op = pop(&operator_stack);
int b = pop(&operand_stack);
int a = pop(&operand_stack);
int result = calculate(a, b, op);
push(&operand_stack, result);
}
pop(&operator_stack);
expr++;
} else if (*expr == '+' || *expr == '-') {
while (!is_empty(&operator_stack) && top(&operator_stack) != '(') {
char op = pop(&operator_stack);
int b = pop(&operand_stack);
int a = pop(&operand_stack);
int result = calculate(a, b, op);
push(&operand_stack, result);
}
push(&operator_stack, *expr);
expr++;
} else if (*expr == '*' || *expr == '/') {
while (!is_empty(&operator_stack) && (top(&operator_stack) == '*' || top(&operator_stack) == '/')) {
char op = pop(&operator_stack);
int b = pop(&operand_stack);
int a = pop(&operand_stack);
int result = calculate(a, b, op);
push(&operand_stack, result);
}
push(&operator_stack, *expr);
expr++;
} else {
printf("Invalid character\n");
exit(1);
}
}
while (!is_empty(&operator_stack)) {
char op = pop(&operator_stack);
int b = pop(&operand_stack);
int a = pop(&operand_stack);
int result = calculate(a, b, op);
push(&operand_stack, result);
}
return top(&operand_stack);
}
int main() {
char expr[MAX_SIZE];
printf("Enter an expression: ");
scanf("%s", expr);
int result = evaluate(expr);
printf("Result: %d\n", result);
return 0;
}
```
在以上代码中,我们使用了两个栈来实现四则运算的求值。其中,`operand_stack` 用于存储操作数,`operator_stack` 用于存储运算符。具体的求值过程如下:
1. 从左到右遍历表达式的每个字符。
2. 如果字符是数字,则将其解析为整数,并将其压入 `operand_stack` 中。
3. 如果字符是左括号,则将其压入 `operator_stack` 中。
4. 如果字符是右括号,则不断弹出 `operator_stack` 的栈顶元素,直到遇到左括号为止。对于每个弹出的运算符,都从 `operand_stack` 中弹出两个操作数进行计算,将结果压入 `operand_stack` 中。
5. 如果字符是加号或减号,则不断弹出 `operator_stack` 的栈顶元素,直到遇到左括号或空栈为止。对于每个弹出的运算符,都从 `operand_stack` 中弹出两个操作数进行计算,将结果压入 `operand_stack` 中。最后将当前字符压入 `operator_stack` 中。
6. 如果字符是乘号或除号,则不断弹出 `operator_stack` 的栈顶元素,直到遇到加号、减号、左括号或空栈为止。对于每个弹出的运算符,都从 `operand_stack` 中弹出两个操作数进行计算,将结果压入 `operand_stack` 中。最后将当前字符压入 `operator_stack` 中。
7. 如果字符不是数字、左括号、右括号、加号、减号、乘号或除号,则报错并退出程序。
8. 遍历完整个表达式后,不断弹出 `operator_stack` 的栈顶元素,直到空栈为止。对于每个弹出的运算符,都从 `operand_stack` 中弹出两个操作数进行计算,将结果压入 `operand_stack` 中。
9. 最后,`operand_stack` 中剩下的唯一元素就是表达式的值。
以上就是使用栈来实现四则运算的示例代码。
用栈实现带括号的四则运算 C语言
以下是用栈实现带括号的四则运算的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
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 x) {
if (is_full(s)) {
printf("Error: stack is full\n");
exit(-1);
}
s->data[++s->top] = x;
}
int pop(Stack *s) {
if (is_empty(s)) {
printf("Error: stack is empty\n");
exit(-1);
}
return s->data[s->top--];
}
int peek(Stack *s) {
if (is_empty(s)) {
printf("Error: stack is empty\n");
exit(-1);
}
return s->data[s->top];
}
int is_digit(char c) {
return c >= '0' && c <= '9';
}
int is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
int priority(char op) {
if (op == '*' || op == '/') {
return 2;
} else if (op == '+' || op == '-') {
return 1;
} else {
return 0;
}
}
int calculate(int a, char op, int b) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
printf("Error: unsupported operator %c\n", op);
exit(-1);
}
}
int evaluate(char *expr) {
Stack operand_stack, operator_stack;
init(&operand_stack);
init(&operator_stack);
int i = 0;
while (expr[i] != '\0') {
if (expr[i] == '(') {
push(&operator_stack, '(');
} else if (is_digit(expr[i])) {
int num = 0;
while (is_digit(expr[i])) {
num = num * 10 + (expr[i] - '0');
i++;
}
push(&operand_stack, num);
i--;
} else if (is_operator(expr[i])) {
while (!is_empty(&operator_stack) && peek(&operator_stack) != '(' &&
priority(expr[i]) <= priority(peek(&operator_stack))) {
int b = pop(&operand_stack);
int a = pop(&operand_stack);
char op = pop(&operator_stack);
push(&operand_stack, calculate(a, op, b));
}
push(&operator_stack, expr[i]);
} else if (expr[i] == ')') {
while (!is_empty(&operator_stack) && peek(&operator_stack) != '(') {
int b = pop(&operand_stack);
int a = pop(&operand_stack);
char op = pop(&operator_stack);
push(&operand_stack, calculate(a, op, b));
}
pop(&operator_stack);
}
i++;
}
while (!is_empty(&operator_stack)) {
int b = pop(&operand_stack);
int a = pop(&operand_stack);
char op = pop(&operator_stack);
push(&operand_stack, calculate(a, op, b));
}
return pop(&operand_stack);
}
int main() {
char expr[MAX_SIZE];
printf("Enter an expression: ");
fgets(expr, MAX_SIZE, stdin);
printf("Result: %d\n", evaluate(expr));
return 0;
}
```
程序首先定义了一个栈结构体,包括栈顶指针和数据数组。然后定义了一些基本栈操作函数,如初始化栈、判断栈是否为空、判断栈是否已满、入栈、出栈和查看栈顶元素等。
接下来定义了一些辅助函数,如判断字符是否为数字、判断字符是否为运算符、获取运算符的优先级和计算两个操作数之间的结果。
最后实现了主函数,该函数接收用户输入的表达式,并使用栈来计算表达式的值。程序遍历输入的表达式中的每个字符,如果遇到左括号,则将其压入运算符栈中。如果遇到数字,则将其解析为整数并压入操作数栈中。如果遇到运算符,则将该运算符与运算符栈中的运算符进行比较,如果该运算符的优先级不高于栈顶运算符,则将栈顶运算符弹出,从操作数栈中弹出两个操作数进行运算,并将结果压入操作数栈中。如果遇到右括号,则弹出运算符栈中的运算符,并从操作数栈中弹出两个操作数进行运算,并将结果压入操作数栈中。最后,如果运算符栈不为空,则继续弹出栈顶运算符,并从操作数栈中弹出两个操作数进行运算,直到最终得到表达式的结果。