From decimal to binary
We can change a decimal number to binary format by repeatedly dividing the decimal number and the reminders form the binary number from right to left.
Example: Convert 2402 to binary: see image.
US style decimal number
The US style uses a comma to separate every three digits of a decimal number.
Example: 2345 in US style is 2,345
Euro style decimal number
The Euro style uses a dot to separate every three digits of a decimal number.
Example: 2345 in Euro style is 2.345
Roman numerals
The numeric system represented by Roman numerals originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the Late Middle Ages. Numbers in this system are represented by combinations of letters from the Latin alphabet. Roman numerals, as used today, are based on seven symbols:
Symbol | I | V | X | L | C | D | M |
Value | 1 | 5 | 10 | 50 | 100 | 500 | 1,000 |
Roman Numerals 1-1000 Chart is as follows:
1 = I 2 = II 3 = III 4 = IV = IIII 5 = V 6 = VI 7 = VII 8 = VIII = IIX 9 = IX = VIIII 10 = X | 11 = XI 12 = XII 13 = XIII 14 = XIV 15 = XV 16 = XVI 17 = XVII 18 = XVIII 19 = XIX 20 = XX | 21 = XXI 22 = XXII 23 = XXIII 24 = XXIV 25 = XXV 26 = XXVI 27 = XXVII 28 = XXVIII 29 = XXIX 30 = XXX | 31 = XXXI 32 = XXXII 33 = XXXIII 40 = XL 50 = L 60 = LX 70 = LXX 80 = LXXX 90 = XC 99 = XCIX | 100 = C 200 = CC 300 = CCC 400 = CD 500 = D 600 = DC 700 = DCC 800 = DCCC 900 = CM 1000 = M |
More details can be seen from the following link: https://en.wikipedia.org/wiki/Roman_numerals.
Converting integers to string
We will learn < sstream> in chapter 9. One common use of < sstream> is to convert numbers to strings. When a number is pushed to a string stream, you can get back the content as a string or as a number.
#include < iostream>
#include < sstream> // using string stream
using namespace std;
// Converting int to string
string int_to_string(int n){
ostringstream outstr;
outstr << n; // push an int to string stream
return outstr.str(); // get a string
}
// Converting string to int
int string_to_int(string s){
istringstream instr(s); // form a string stream with the string
int n;
instr >> n; // get int from string stream
return n;
}
int main(){
cout << "Integer 2402 to string: ";
cout << int_to_string(2402) << endl;
cout << "String \"2402\" to int: ";
cout << string_to_int("2402") << endl;
return 0;
}
You may also use < sstream> to covert strings to decimal numbers. You can simply replace the int used in the functions to double. See textbook section 9.4 for more details.
Always use < sstream> instead of atoi(), , sscanf(), , sprintf() functions.
Given the incomplete header file number.h:
class Number {
public:
// Default constructor without any initial value.
// Initial value will be set to 0.
// Value of integer is 0.
// Value of US is “0”.
// Value of EURO is “0”.
Number();
// Constructor with initial value.
Number(int);
// Returns int as a string in US style.
string get_US_style()const;
// Returns int as a string in Euro style.
string get_EURO_style()const;
// Returns the int.
int get_number()const;
// Sets the value of int, US, and EURO.
void set_number(int);
private:
// A decimal number less than 4000.
int number;
// A string represent number in US style.
string US;
// A string representing number in EURO style.
sting EURO;
// converting int to string in US style
void int_to_US();
// converting int to string in EURO style
void int_to_EURO();
};
Complete the header file by adding any needed included libraries and directives. You are NOT allowed to modify the content between any pair of curly braces.
Make sure that you put in measures to prevent multiple inclusion of the header file.
Implement the Number class in number.cpp. Test your implementation using task1.cpp.
Task1.cpp should:
Note: We will use MinGW 4.7.1 to compile programs if needed; executables will be run in Windows.
/*
Expected output:
Number one:
0
0
0
Number one:
1111
1,111
1.111
Number one:
99
99
99
Number two:
22
22
22
Number two:
2022
2,022
2.022
Number three:
3333
3,333
3.333
*/
Using the Number class as the base class, derive a Roman class:
Create the roman.h and implement the Roman class in roman.cpp. Make sure that you put in measures to prevent multiple inclusion of the header file. Test your implementation using task2a.cpp.
Task2a.cpp should:
Note:
/*
Expected output:
Roman one:
0
0
0
INVALID
Roman one:
2014
2,014
2.014
MMXIV
Roman two:
22
22
22
XXII
Roman three:
2402
2,402
2.402
MMCDII
*/
Using the Roman class as the base class, derive a Binary class:
Create the binary.h and implement the Binary class in binary.cpp. Make sure that you put in measures to prevent multiple inclusion of the header file. Test your implementation using task2b.cpp.
Task2b.cpp should:
Note: We will use MinGW 4.7.1 to compile programs if needed; executables will be run in Windows.
/*
Expected output:
Binary one:
0
0
0
INVALID
0
Binary one:
1024
1,024
1.024
MXXIV
10000000000
Roman two:
22
22
22
XXII
Number three:
2402
2,402
2.402
*/
It is a handy feature if a word processor can convert and display numbers in various formats. To mimic that feature, you are asked to write an application task2c.app that:
Enter 1 if you want to change from decimal to Roman numeral
Enter 2 if you want to change from decimal to Binary numeral
Enter 3 if you want to display decimal in US style
Enter 4 if you want to display decimal in Euro style
Enter a non-digit to quit
Enter a positive integer smaller than 4000:
Note 1: You must use cin.fail() to check if non-digit is entered or not.
Note 2: You must use assert() to check if the menu choice is from 1 to 4.
Note 3: You must use assert() to check if the entered input is a positive integer smaller than 4000.
Note 4: Use of arrays is not allowed.
Note 5: Run the program, use the number 2402 and 1748 for all the 4 options and finally quit with < Q>.