输入一个算术运算及比较表达式,运算符包括+、-、*、/、%五种,比较符包括=、>、<三种;如果运算符不在这 5 种操作内,输出运算符错误的信息;如果比较符不在这 3 种符号内,输出比较符错误的信息;如果式子是正确的,输出式子正确的相应信息;如果式子是错误的,输出式子错误的相应信息。假设输入的数皆为正整数,运算符和比较符都只有一个字符,且除法正好能够除尽。要求必须使用 switch 语句实现。
6条回答
我不吃辣。 2024-04-08 11:30关注#include <stdio.h> int main() { char operator, comparison; int operand1, operand2, result; // 提示用户输入算术运算及比较表达式 printf("Enter an arithmetic operation and a comparison expression: "); scanf("%d %c %d %c", &operand1, &operator, &operand2, &comparison); // 根据输入的运算符进行相应的运算 switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; case '%': result = operand1 % operand2; break; default: // 如果运算符不在规定范围内,输出错误信息 printf("Operator error: Invalid operator\n"); return 0; } // 根据输入的比较符进行相应的比较 switch (comparison) { case '=': if (operand1 == operand2) printf("Expression is correct: %d %c %d\n", operand1, comparison, operand2); else printf("Expression is incorrect: %d %c %d\n", operand1, comparison, operand2); break; case '>': if (operand1 > operand2) printf("Expression is correct: %d %c %d\n", operand1, comparison, operand2); else printf("Expression is incorrect: %d %c %d\n", operand1, comparison, operand2); break; case '<': if (operand1 < operand2) printf("Expression is correct: %d %c %d\n", operand1, comparison, operand2); else printf("Expression is incorrect: %d %c %d\n", operand1, comparison, operand2); break; default: // 如果比较符不在规定范围内,输出错误信息 printf("Comparison error: Invalid comparison operator\n"); break; } return 0; }博主你好,不知道这个程序是否是您想要的,如果是的话还望点采纳 谢谢
评论 打赏 举报解决 1无用