编写一个c程序 实现两个数的加减计算结果的判断要求要求一用户输入的是一个等式 只有加或减两种可能 例如3+2=5,要求2程序对算式的结果进行正确与否的判断 要求3 若用户输入的等式结果正确 则显示perfect。若错误则显示era。
时间: 2024-05-06 08:20:29 浏览: 15
```c
#include <stdio.h>
int main() {
int a, b, c;
char op;
printf("请输入一个等式,格式为 a + b 或 a - b:\n");
scanf("%d %c %d", &a, &op, &b);
if (op == '+') {
c = a + b;
} else if (op == '-') {
c = a - b;
} else {
printf("输入错误!\n");
return 0;
}
int result;
printf("请输入计算结果:\n");
scanf("%d", &result);
if (result == c) {
printf("perfect\n");
} else {
printf("era\n");
}
return 0;
}
```
相关问题
用户输入一个等式(只有加或减两种可能)
抱歉,作为AI语言模型,我不能够直接和用户进行交互,也无法获取用户的输入。但是,如果您想要对一个等式进行加减运算,您可以使用Python编程语言来实现。例如,以下代码实现了对一个等式进行加减运算:
```
equation = "3 + 5 - 2" # 用户输入的等式
result = eval(equation) # 使用eval函数计算结果
print(result) # 输出结果
```
输出结果为6,即3+5-2的结果。当然,这只是一个简单的例子,您可以根据自己的需求进行修改和扩展。
用c语言写一个判断输入的加减等式是否正确的程序
可以使用栈来实现该程序。首先定义一个栈结构体,包含栈顶指针和栈大小。然后定义一个字符数组来存储输入的加减等式,通过循环读取每个字符,如果是数字或运算符,则将其入栈;如果是等于号,则弹出两个栈顶元素进行运算,并将结果入栈。最后判断栈是否为空,如果为空则说明输入的加减等式正确,否则不正确。
以下是伪代码:
typedef struct {
int top;
int size;
int *data;
} Stack;
Stack *init_stack(int size) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->top = -1;
stack->size = size;
stack->data = (int *)malloc(sizeof(int) * size);
return stack;
}
void push(Stack *stack, int value) {
if (stack->top == stack->size - 1) {
// 栈满了
return;
}
stack->top++;
stack->data[stack->top] = value;
}
int pop(Stack *stack) {
if (stack->top == -1) {
// 栈为空
return -1;
}
int value = stack->data[stack->top];
stack->top--;
return value;
}
int is_empty(Stack *stack) {
return stack->top == -1;
}
int is_correct_equation(char *equation) {
Stack *stack = init_stack(strlen(equation));
for (int i = 0; i < strlen(equation); i++) {
char ch = equation[i];
if (ch >= '0' && ch <= '9') {
push(stack, ch - '0');
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
if (is_empty(stack)) {
return 0;
}
int b = pop(stack);
if (is_empty(stack)) {
return 0;
}
int a = pop(stack);
int result;
switch (ch) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
push(stack, result);
} else if (ch == '=') {
if (stack->top != 0) {
// 栈中还有多余的元素,说明输入的加减等式不正确
return 0;
}
pop(stack);
} else {
// 非法的字符
return 0;
}
}
return 1;
}
int main() {
char equation[100];
scanf("%s", equation);
int result = is_correct_equation(equation);
if (result) {
printf("输入的加减等式正确\n");
} else {
printf("输入的加减等式不正确\n");
}
return 0;
}
相关推荐
![](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)