C++程序设计一(进制转换)

文章目录

任务 1

编写一个 C++ 程序,将二进制数转换为十进制数 并将十进制数转换为二进制数。 该程序会提示用户输入一个数字。 您必须将数字作为字符串读取。 如果数字是二进制数,则将其转换为十进制数。 如果数字是十进制,则将其转换为二进制。 如果数字无效,则必须显示相应的错误消息。使用下面的示例来
(a) 查看需要什么以及
(b) 查看可能发生的输出类型

Notes:

1.十进制数必须在 0 到 255 之间(你必须检查这个)
2.二进制数不能超过 9 个二进制数字(你必须检查这个)
3.一个二进制数必须始终输入 前导零(您输入的第一个数字必须是 0)
4. 十进制数不能带前导零(检查这个)
5. 十进制数不能用前导零显示
6. 二进制数必须始终显示为 8 位 4 位后有一个空格(参见示例)
7. 输入二进制数时不会使用空格——您不必检查这一点
8. 有几种不同的错误消息——参见示例 4 到 7
9.不要使用空格 将二进制转换为十进制的库函数——编写你自己的函数
10.不要使用库函数将十进制转换为二进制——编写你自己的函数11.你必须至少包含两(2)个函数(这些可能是上面的 9 和 10)

您的显示必须与下面显示的示例相同。
Example 1:
Enter a number: 8Converting decimal to binary. The result is 00001000

Example 2:
Enter a number: 010Converting binary to decimal. The result is 2

Example 3:
Enter a number: 10Converting decimal to binary. The result is 00001010

Example 4:
Enter a number: helloThis is not a valid number.

Example 5:
Enter a number: 027This is not a valid binary number.

Example 6:
Enter a number: 256Thisdecimal number is outside the range 0 to 255.

Example 7:
Enter a number: 00101001010This binary number has more than 9 binary digits.

Answer:

总是有不止一种可能的解决方案。以下是一种可能的解决方案——它不是唯一的解决方案。

#include <iostream>
#include <cstdlib> // need this for exit
using namespace std;
void binarytodecimal(string s);
void decimaltobinary(string s);
string convert(int num);
int main() {
 string str;
 cout << "Enter a number ";
 cin >> str;
 if (str[0] == '0') {
 binarytodecimal(str);
 } else {
 decimaltobinary(str);
 }
}
void binarytodecimal(string s) {
 int value, len, i;
 value = 0;
 len = s.length();
 if (len > 9) {
 cout << "This binary number has more than 9 binary digits.\n";
 exit(20);
 }
 for (i = 0; i < len; i++) {
 if ((s[i] == '0') || (s[i] == '1')) {
 value = value * 2 + s[i] - 48;
 } else {
 cout << "This is not a valid binary number.\n";
 exit(22);
 }
 }
 cout << "Converting binary to decimal. The result is ";
 cout << value << endl;
}
void decimaltobinary(string s) {
 string binary;
 int value, len, i;
 value = 0;
 len = s.length();
 for (i = 0; i < len; i++) {
 if ((s[i] >= '0') && (s[i] <= '9')) {
 value = value * 10 + s[i] - 48;
 } else {
 cout << "This is not a valid number.\n";
 exit(24);
 }
 }
 if ((value < 0) || (value > 255)) {
 cout << "This decimal number is outside the range 0 to 255.\n";
 exit(26);
 }
 binary = convert(value);
 cout << "Converting decimal to binary. The result is ";
 cout << binary << endl;
}
string convert(int num) {
// convert num to binary and display the result
 int len, i;
 char digit;
 string temp, result, display;
 i = 0;
 temp = "";
 while (num > 0) {
 digit = (num % 2) + 48;
 temp = temp + digit;
 num = num / 2;
 }
 // include extra zeros to get to 8 binary digits
 len = temp.length();
 while (len < 8) {
 temp = temp + "0";
 len = temp.length();
 }
 // reverse temp to get the result
 result = "";
 len = temp.length();
 for (i = len - 1; i >= 0; i--) {
 result = result + temp[i];
 }
 // place a space after first 4 digits
 display = "";
 for (i = 0; i < 4; i++) {
 display = display + result[i];
 }
 display = display + " ";
 for (i = 4; i < 8; i++) {
 display = display + result[i];
 }
 return display;
}
第一个任务只是预热,后面还有第二第三个任务
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值