在C语言中如何输入四则运算计算式字符得结果
时间: 2024-03-25 20:39:20 浏览: 28
在C语言中,可以使用表达式求值的功能来实现输入四则运算计算式字符得结果。具体实现步骤如下:
1. 读取用户输入的四则运算计算式字符,可以使用scanf函数。
2. 将读取到的四则运算计算式字符转换成中缀表达式,并将中缀表达式转换成后缀表达式。这个过程可以使用栈来实现。
3. 对后缀表达式进行计算,也可以使用栈来实现。
下面是一个简单的示例代码,演示了如何实现输入四则运算计算式字符得结果的功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_SIZE 100
typedef struct Stack {
int top;
int data[STACK_SIZE];
} Stack;
void init_stack(Stack* s) {
s->top = -1;
}
void push(Stack* s, int x) {
if (s->top == STACK_SIZE - 1) {
printf("栈已满\n");
exit(1);
}
s->top++;
s->data[s->top] = x;
}
int pop(Stack* s) {
if (s->top == -1) {
printf("栈已空\n");
exit(1);
}
int x = s->data[s->top];
s->top--;
return x;
}
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int precedence(char c) {
if (c == '+' || c == '-') {
return 1;
}
if (c == '*' || c == '/') {
return 2;
}
return 0;
}
int infix_to_postfix(char* infix, char* postfix) {
Stack s;
init_stack(&s);
int i, j;
char c;
for (i = 0, j = 0; infix[i] != '\0'; i++) {
c = infix[i];
if (isdigit(c)) {
postfix[j++] = c;
}
else if (is_operator(c)) {
while (s.top != -1 && is_operator(s.data[s.top]) && precedence(s.data[s.top]) >= precedence(c)) {
postfix[j++] = pop(&s);
}
push(&s, c);
}
else if (c == '(') {
push(&s, c);
}
else if (c == ')') {
while (s.top != -1 && s.data[s.top] != '(') {
postfix[j++] = pop(&s);
}
if (s.top == -1) {
printf("括号不匹配\n");
return 0;
}
pop(&s);
}
else {
printf("无效的字符\n");
return 0;
}
}
while (s.top != -1) {
if (s.data[s.top] == '(') {
printf("括号不匹配\n");
return 0;
}
postfix[j++] = pop(&s);
}
postfix[j] = '\0';
return 1;
}
int evaluate_postfix(char* postfix) {
Stack s;
init_stack(&s);
int i, x, y;
char c;
for (i = 0; postfix[i] != '\0'; i++) {
c = postfix[i];
if (isdigit(c)) {
push(&s, c - '0');
}
else if (is_operator(c)) {
y = pop(&s);
x = pop(&s);
switch (c) {
case '+':
push(&s, x + y);
break;
case '-':
push(&s, x - y);
break;
case '*':
push(&s, x * y);
break;
case '/':
if (y == 0) {
printf("除数不能为0\n");
return 0;
}
push(&s, x / y);
break;
}
}
else {
printf("无效的字符\n");
return 0;
}
}
return pop(&s);
}
int main() {
char infix[STACK_SIZE], postfix[STACK_SIZE];
int result;
printf("请输入四则运算计算式:\n");
scanf("%s", infix);
if (infix_to_postfix(infix, postfix)) {
result = evaluate_postfix(postfix);
printf("计算结果:%d\n", result);
}
return 0;
}
```
在上述示例代码中,我们定义了栈的数据结构和相关操作函数。我们使用了两个字符串变量infix和postfix来存储中缀表达式和后缀表达式。我们使用了两个函数infix_to_postfix和evaluate_postfix来实现将中缀表达式转换成后缀表达式和对后缀表达式进行计算的功能。我们在main函数中读取用户输入的四则运算计算式字符,并调用上述两个函数来计算结果。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)