Background information

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.

Task 1

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:

  • Create Number one
    • Without initial value;
    • Display one as an integer;
    • Display one in US style;
    • Display one in EURO style;
  • Set one to 1111
    • Set one to 1111
    • Display one in US style;
    • Display one in EURO style;
  • Set one to 99
    • Display one as an integer;
    • Display one in US style;
    • Display one in EURO style;
  • Create Number two
    • With initial value 22;
    • Display two as an integer;
    • Display two in US style;
    • Display two in EURO style;
  • Set two to 2022
    • Display two as an integer;
    • Display two in US style;
    • Display two in EURO style;
  • Create Number three
    • With initial value 3333;
    • Display three as an integer;
    • Display three in US style;
    • Display three in EURO style;

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
*/

Task 2a

Using the Number class as the base class, derive a Roman class:

  • The Roman class is the same as Number class except it also stores the integer as a Roman numeral.
  • If a Roman is created without an initial value, then the Roman numeral is "INVALID".
  • You are not allowed to use arrays, atoi(), atol(), atof(), printf(), sprintf(), fprintf(), vfprintf(), vprintf(), vsprintf(), scanf(), sscanf(), fscanf(). Feel free to use any other library functions provided by MinGW.

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:

  • Create Roman one.
    • Without initial value;
    • Display one as an integer;
    • Display one in US style;
    • Display one in EURO style;
    • Display one as a Roman numeral;
  • Set one to 2014
    • Display one as an integer;
    • Display one in US style;
    • Display one in EURO style;
    • Display one as a Roman numeral;
  • Create Roman two
    • With initial value 22;
    • Display two as an integer;
    • Display two in US style;
    • Display two in EURO style;
    • Display two as a Roman numeral;
  • Create Roman three
    • With initial value 2402;
    • Display three as an integer;
    • Display three in US style;
    • Display three in EURO style;
    • Display three as a Roman numeral;

Note:

  • We will use MinGW 4.7.1 to compile programs if needed; executables will be run in Windows.
  • Testing: Test your program by using different integers and comparing its calculating results to the website Roman numeral converter: https://www.calculateme.com/cRomanNumerals/ArabicNumeralsToRoman.htm
/*
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
*/

Task 2b

Using the Roman class as the base class, derive a Binary class:

  • The Binary class is the same as Roman class except it also stores the integer as a binary string.
  • If a Binary is created without an initial value, then BINARY "0".
  • You are not allowed to use arrays, atoi(), atol(), atof(), printf(), sprintf(), fprintf(), vfprintf(), vprintf(), vsprintf(), scanf(), sscanf(), fscanf(). Feel free to use any other library functions provided by MinGW.

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:

  • Create Binary one
    • Without initial value;
    • Display one as an integer;
    • Display one in US style;
    • Display one in EURO style;
    • Display one as a Roman numeral;
    • Display one as a binary string;
  • Set one to 1024
    • Display one as an integer;
    • Display one in US style;
    • Display one in EURO style;
    • Display one as a Roman numeral;
    • Display one as a binary string;
  • Create Roman two
    • With initial value 22;
    • Display two as an integer;
    • Display two in US style;
    • Display two in EURO style;
    • Display two as a Roman numeral;
  • Create Number three
    • With initial value 2402;
    • Display three as an integer;
    • Display three in US style;
    • Display three in EURO style;

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
*/

Task 2c

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:

  • displays the following prompt:
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
  • When user enters < 1>, < 2>, < 3> or < 4>, the program display the prompt:
Enter a positive integer smaller than 4000:
  • After entering a positive integer less than 4000, the program will create a Binary class object and display the requested format of the integer. The application will display prompt for another input.
  • If the user enters any character other than a digit, then the program will display a history of all the results and exit.

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>.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.