输入两个整数a和b及+-中的任意一字符。根据输入字符对整数a和b做相应的算术运算如输入+程序就给出a与b之和输入-就给出a和b之差输入就给出a和b的乘积输入就给出a与b的商。若输入其他字符则提示输入错误。使用c++并且不需要写出主函数
#include <iostream>
int main() {
int a, b;
char op;
std::cin >> a >> b >> op;
int result;
switch (op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b != 0) {
result = a / b;
} else {
std::cout << "Error: Division by zero is not allowed." << std::endl;
return 0;
}
break;
default:
std::cout << "Error: Invalid operator." << std::endl;
return 0;
}
std::cout << "Result: " << result << std::endl;
return 0;
}
注意:这段代码是一个完整的C++程序,包含了一个主函数。如果你想要在这段代码的基础上进行修改,可以将主函数的部分删除。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/i11C 著作权归作者所有。请勿转载和采集!