C++进制转换题
159.102 Instructions for Assignment 1
Assignment 1 starts in Week 2 and is due in Week 5(Thursday Sep. 22,2022 at 7pm)(China Time).
You should plan to complete the assignment about a week early. Then you still have time for revisions ifsomething unexpected happens to slow you down.
lt is a good idea to put your name and lD number in a comment at the top of your program.
Write a C++ program that converts binary numbers to decimal and decimal numbers to binary. Theprogram prompts the user to enter a number. You must read the number as a string. If the number isbinary, convert it to decimal.If the number is decimal, convert it to binary.If the number is not valid,
then an appropriate error message must be displayed.Use the examples below to (a) see what is requiredand (b) see the type of output that can occur.
Notes:
- Decimal numbers must be in the range 0 to 255 (you must check for this)
- Binary numbers must not be more than 9 binary digits (you must check for this)
- A binary number must always be entered with a leading zero (the first digit you enter must be 0)
- A decimal number must never be entered with a leading zero (check for this)
- Decimal numbers must never be displayed with leading zeros
- Binary numbers must always be displayed as 8 bits with a space after 4 bits (see the examples)
- The space will not be used when entering binary numbers - you do not have to check for this
- There are several different error messages - see examples 4 to 7
- Do not use a library function to convert binary to decimal - write your own function
- Do not use a library function to convert decimal to binary - write your own function
- You must include at least two (2) functions (these could be 9 and 10 above)
Your display must look the same as the examples shown below.
Example 1:
Enter a number: 8
Converting decimal to binary. The result is 0000 1000
Example 2:
Enter a number: 010
Converting binary to decimal. The result is 2
Example 3:
Enter a number: 10
Converting decimal to binary. The result is 0000 1010
Example 4:
Enter a number: hello
This is not a valid number.
Example 5:
Enter a number: 027
This is not a valid binary number.
Example 6:
Enter a number: 256
This decimal number is outside the range 0 to 255.
Example 7:
Enter a number: 00101001010
This binary number has more than 9 binary digits.
源码传送门
传送门:https://pan.baidu.com/s/1JJs9vbZahUCB6cQvXLgAVg?pwd=1111
标签:binary,转换,进制,decimal,number,C++,numbers,Example,must From: https://www.cnblogs.com/codewriter/p/16710082.html