c++编程题: 编写程序实现功能:输入两个数,第一个数为被除数,第二个数为除数,如果除数为 0,则抛出一个异常并要求重新输入两个数;如果除数不为 0,则计算并显示结果。
时间: 2023-05-28 14:05:50 浏览: 49
```c
#include <stdio.h>
int main() {
int dividend, divisor, result;
while (1) {
printf("Enter dividend and divisor: ");
scanf("%d%d", ÷nd, &divisor);
if (divisor == 0) { // 如果除数为 0,抛出异常并重新输入
printf("Divisor can not be zero! Please enter again.\n");
continue;
}
result = dividend / divisor; // 计算结果
printf("%d / %d = %d\n", dividend, divisor, result);
break;
}
return 0;
}
```
相关问题
编写c++程序实现功能:输入两个数,第一个数为被除数,第二个数为除数,如果除数为 0,则抛出一个异常并要求重新输入两个数;如果除数不为 0,则计算并显示结果。
```
#include <stdio.h>
#include <stdlib.h>
int main() {
int dividend, divisor, result;
while (1) {
printf("Please enter two numbers: ");
scanf("%d %d", ÷nd, &divisor);
if (divisor == 0) {
printf("The divisor can not be 0, please enter again.\n");
continue;
}
result = dividend / divisor;
printf("%d / %d = %d\n", dividend, divisor, result);
break;
}
return 0;
}
```
c++编程实现:将1个十进制整数转换为二进制数。
以下是将十进制整数转换为二进制数的C++代码:
```c++
#include <iostream>
#include <stack>
using namespace std;
void decimalToBinary(int decimal) {
stack<int> binaryStack; // 创建一个栈,用于存储二进制位
// 将十进制整数转换为二进制数
while (decimal > 0) {
int remainder = decimal % 2;
binaryStack.push(remainder);
decimal /= 2;
}
// 从栈中依次取出二进制位并输出
cout << "Binary: ";
while (!binaryStack.empty()) {
cout << binaryStack.top();
binaryStack.pop();
}
}
int main() {
int decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
decimalToBinary(decimal);
return 0;
}
```
运行结果:
```
Enter a decimal number: 15
Binary: 1111
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)