C + + exercises with answers

Job 1 (Section2 to Section5)

Write a C + + program to convert binary numbers to decimal numbers and convert decimal numbers to binary numbers. The program prompts the user to enter a number. You must read the number as a string. If the number is binary, convert it to decimal. If the number is decimal, convert it to binary. If the number is invalid, the corresponding error message must be displayed. Use the following example to
(a) See what you need and
(b) View possible output types

Notes:

1. Decimal number must be between 0 and 255 (you must check this)
2. The binary number cannot exceed 9 binary numbers (you must check this)
3. A binary number must always enter leading zero (the first number you enter must be 0)
4. Decimal numbers cannot have leading zeros (check this)
5. Decimal numbers cannot be displayed with leading zeros
6. Binary numbers must always be displayed as 8 bits with a space after 4 bits (see example)
7. No spaces are used when entering binary numbers -- you don't have to check this
8. There are several different error messages - see examples 4 to 7
9. Do not use spaces to convert binary to decimal library functions - write your own functions
10. Don't use library functions to convert decimal to binary - write your own functions 11. You must contain at least two (2) functions (these may be 9 and 10 above)

Your display must be the same as the example shown below.
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:

There is always more than one possible solution. Here's a possible solution - it's not the only solution.

#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;
}

Keywords: C++

Added by sspatel82 on Sat, 06 Nov 2021 05:12:45 +0200