NumberConversions - Northeastern University

advertisement
Exercise Set 9: Representation of numbers, conversions, formatted and textfile output
Conversions between representations of numbers:
1. Convert the following (decimal) integers to binary:
123
312
213
265
2. Convert the following binary numbers to their decimal equivalent:
1011011
11100010101
11001010
1010101
3. Write the following hexadecimal numbers first in their binary form then as decimal
integers:
1A2
2B3
FFF
DED
4. Write the function BinaryStringToDecimal that will convert a string of digits 0 and 1
into a corresponding integer. Write a function MyRequestBinary that will read a
string typed in by the user and will verify that all characters are only 0's or 1's. If
there is an error, print a message and ask the user to type the string again. Model
this function after RequestInt function. Write a program that asks the user to type in
a binary string and prints its decimal equivalent.
5. Modify the exercise above to define functions DecimalStringToDecimal and
MyRequestDigits. Test it by asking the user to type in several numbers and printing
the number, 3 times that number and 1/2 of that number. Format the output nicely.
6. Write a function UnsignedIntToBinary that will convert a positive integer to a string
of 0's and 1's. Test this function.
7. Write a function that will convert a signed number in the range from -128 to 128 into
an unsigned char using casting. Use this function and the function
UnsignedIntToBinary form Exercise 6 to print a table of binary representations of
numbers from -8 to + 7.
Formatted output and output to a file:
Copyright 2000 Northeastern University
1
2/15/16 4:26 PM
Exercise Set 9: Representation of numbers, conversions, formatted and textfile output
8. Print a multiplication table for numbers from 1 to 10. Format it nicely!
9. Write a program that processes an invoice.
 Read from Inventory file a list of part numbers and their prices into an array. Use
the part number as the array index and make the array value to be the
corresponding price. Assume that item numbers 10 through 99 are used. Make
this a separate function.
 Read from an Order file a list of parts ordered and the quantities ordered.
 Print to an Invoice file a list consisting of part number, the number ordered, price
per item, total price per this quantity and after a blank line, the total price of the
whole purchase. (Print each line as soon as you read from the Order file, then
print the totals at the end.)
 Print out the output file from a WordPad.
Sample Inventory file:
11
12
17
23
25
54.30
10.29
12.50
89.90
14.50
Sample Order file:
11
12
23
3
8
1
Corresponding Invoice file:
11
12
23
Total due:
3
8
1
54.30
10.29
89.90
162.90
82.32
89.90
327.54
Assignment: Do Exercises 1, 2, and 3 by hand. Write the program and run Exercise 5.
Copyright 2000 Northeastern University
2
2/15/16 4:26 PM
Exercise Set 9: Representation of numbers, conversions, formatted and textfile output
Exercise 1:
Show 1/2
123
61
30
15
7
3
1
0
Show the remainder
|
|
|
|
|
|
|
|
1
1
0
1
1
1
1
0
The binary number is: 0111 1011
Exercise 2:
1011011 =
= 1 + 1 * 2 + 0 * 4 + 1 * 8 + 1 * 16 + 0 * 32 + 1 * 64 =
= 1 + 2
+
8
+
16
+
64
= 91
Exercise 3:
Hexadecimal:
1
A
2
Binary:
0001
1010
0010
Decimal value of digits:
1
10
2
Decimal from hex:
1 * 256 + 10 * 16 + 2 = 256 + 160 + 2 = 418
Decimal from binary: 0 + 1 * 2 + 0 * 4 + 0 * 8 + 0 * 16 + 1 * 32 + 0 * 64 + 1 * 128 + 1 * 256 =
0 + 2
+
32
+
128
+ 256 = =
418
Copyright 2000 Northeastern University
3
2/15/16 4:26 PM
Download