c++要求读入2个整数A和B,然后分别输出它们的和、差、积、商、模的算式和结果
时间: 2024-09-22 21:01:15 浏览: 69
在C++中,你可以使用标准输入流`cin`来读取两个整数A和B,并通过控制台输出相应的算术运算表达式和计算结果。下面是一个简单的示例:
```cpp
#include <iostream>
int main() {
int A, B;
// 读取两个整数
std::cout << "请输入第一个整数 A: ";
std::cin >> A;
std::cout << "请输入第二个整数 B: ";
std::cin >> B;
// 计算并输出算式和结果
int sum = A + B;
int difference = A - B;
int product = A * B;
if (B != 0) { // 防止除数为零
float quotient = static_cast<float>(A) / B;
} else {
quotient = 0; // 或者抛出异常处理除零错误
}
int modulus = A % B;
// 输出算式和结果
std::cout << "和: " << A << " + " << B << " = " << sum << std::endl;
std::cout << "差: " << A << " - " << B << " = " << difference << std::endl;
std::cout << "积: " << A << " * " << B << " = " << product << std::endl;
std::cout << "商: " << A << " / " << B << " (若B非零) = " << quotient << std::endl;
std::cout << "模: " << A << " % " << B << " = " << modulus << std::endl;
return 0;
}
```
阅读全文