Roman Numerals Math?????

I have to create a program that counts roman numerals. I'm not sure how I would do the math in it? I know if the smallest number is in front, you minus them... but I don't know how I would write that? Here is my code so far :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class romanType
{
      string numerals;
      int convert();
      void print();
};

void count();
      void displays();
string a, b;
int total = 0;

int main(int argc, char *argv[])
{
     ifstream deletefile ("romannums.out", ios::out | ios::trunc);     
deletefile.close();
     
    ifstream inFile;
    ofstream outFile;
    
    inFile.open("romannum.txt", ios::in);    
    outFile.open("romannums.out", ios::app); 
    
    int m = 1000;
    int d = 500;
    int c = 100;
    int l = 50;
    int x = 10;
    int v = 5;
    int i = 1;
    int number;
    string num;
    
    cout << "How many roman numerals do you have? : ";
    cin >> number;
    
for(int j=0; number > j; j++)
    {
    cout << "What is the letter?: " ;
    cin >> b;
    
    outFile << b;
    
    count();
    
    if(b<num)
    {cout << "hi" << endl;}
    num = b;
    } 
    
    outFile << endl;
    
    cout << endl;
    cout << "The total decimal value is : " << total << endl;
    cout << "The roman numeral is : ";
    displays();
    
    outFile.close();
    inFile.close();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void count()
{
     if(a=="M" || b == "M")
     {
     total=total+1000;
     }
     
     if(a=="D" || b == "D")
     {
     total=total+500;
     } 
     
     if(a=="C" || b == "C")
     {
     total=total+100;
     }
     
     if(a=="L" || b == "L")
     {
     total=total+50;
     }
     
     if(a=="X" || b == "X")
     {
     total=total+10;
     }
     
     if(a=="V" || b == "V")
     {
     total=total+5;
     }
     
     if(a=="I" || b == "I")
     {
     total=total+1;
     }
}

void displays() // Function to display data in customer order file
{
  string line;
  ifstream displayfile ("romannums.out");
  if (displayfile.is_open())
{while (displayfile.good())
    {
    getline (displayfile,line);
    cout << line << endl;
    }
displayfile.close();}

cout << '\n'; 
}
Your code is incorrect. Here are not anything to fix.

You need to analyze the entire line of roman numbers in string.

How to do:
http://en.wikipedia.org/wiki/Roman_numerals
Last edited on
I'm not sure how to do that.
Step 1:
Get a user input, (you've done that)
std::string roman = "MMXII";

Step 2:
Convert it to an integer (You need to define this function)
int num = RomanToInt(roman);

Step 3:
do the math with int num

Step 4:
Convert it back to roman numerals (You need to define this function too)
std::string roman_out = IntToRoman(num);
******SPOILER******

Here's a function that will turn a roman numeral string to an int. If I feel like it tonight I may try and write one that converts an int back to a roman numeral string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int RomanCharToInt(char in)
{	// Converts a single roman character to an integer
	if(in <0x60) in += 0x20; // Converts to lower-case (if applicable)
	switch (in)
	{
	case 'm':  return 1000;
	case 'd':  return 500;
	case 'c':  return 100;
	case 'l':  return 50;
	case 'x':  return 10;
	case 'v':  return 5;
	case 'i':  return 1;
	}
	return -9999999; // Very obscure number so the user knows there was a problem.
}

int RomanToInt(std::string &in)
{ // Converts a whole string of roman numerals to an integer
	int number = 0;
	int i = in.size()-1;
	
	// Get the first one
	number += RomanCharToInt(in[i--]);
	
	//Now loop for the rest
	while ( i > -1)
		if (RomanCharToInt(in[i]) < RomanCharToInt(in[i+1]))
			number -= RomanCharToInt(in[i--]);  // If it's less than the one before, subtract the value
		else 
			number += RomanCharToInt(in[i--]);  // else add the value	
	
	return number;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <string>
using namespace std;

class RomanDecimal {
	public:
		RomanDecimal(): roman_number(""), decimal_number(0), count_dec(1) {};
		~RomanDecimal();
		void add_roman(string);
		void add_decimal(int);
		string get_roman();
		int get_decimal();
		
	private:
		string roman_number;
		int decimal_number;
		void convert_to_roman();
		void convert_to_decimal();
		int count_dec;
		static const int i = 1;
		static const int v = 5;
		static const int x = 10;
		static const int l = 50;
		static const int c = 100;
		static const int d = 500;
		static const int m = 1000;
};

RomanDecimal::~RomanDecimal() {}

void RomanDecimal::add_roman(string number) {
	roman_number = number;
	convert_to_decimal();
}

void RomanDecimal::add_decimal(int number) {
	decimal_number = number;
	convert_to_roman();
}

void RomanDecimal::convert_to_decimal() {
	size_t length;
	
	length = roman_number.length();
	int * array_of_numbers = new int[length];
	
	for (unsigned int n = 0; n < length; n++){
		if (roman_number[n] == 'i' or roman_number[n] == 'I') {
			array_of_numbers[n] = i;
		}
		else if (roman_number[n] == 'v' or roman_number[n] == 'V') {
			array_of_numbers[n] = v;
		}
		else if (roman_number[n] == 'x' or roman_number[n] == 'X') {
			array_of_numbers[n] = x;
		}
		else if (roman_number[n] == 'l' or roman_number[n] == 'L') {
			array_of_numbers[n] = l;
		}
		else if (roman_number[n] == 'c' or roman_number[n] == 'C') {
			array_of_numbers[n] = c;
		}
		else if (roman_number[n] == 'd' or roman_number[n] == 'D') {
			array_of_numbers[n] = d;
		}
		else if (roman_number[n] == 'm' or roman_number[n] == 'M') {
			array_of_numbers[n] = m;
		}
		else {
			cout << "ERROR: simbol " << roman_number[n] << " are not roman number." << endl;
		}
	}
	
	decimal_number = array_of_numbers[length - 1];
	cout << decimal_number << " ";
	for (int m = length - 1; m >= 0; m--) {
		if (array_of_numbers[m] > array_of_numbers[m - 1]) {
			decimal_number -= array_of_numbers[m - 1];
		}
		else {
			decimal_number += array_of_numbers[m - 1];
		}
	}
}

void RomanDecimal::convert_to_roman() {
	if (decimal_number >= 4000 or decimal_number <= 0) {
		roman_number = "NO_NUMBER";
		return;
	}
	int temp_number = decimal_number;
	roman_number = "";
	while (temp_number) {
		if (temp_number >= 1000) {
			roman_number += "M";
			temp_number -= 1000;
		}
		else if (temp_number >= 900) {
			roman_number += "CM";
			temp_number -= 900;
		}
		else if (temp_number >= 500) {
			roman_number += "D";
			temp_number -= 500;
		}
		else if (temp_number >= 100) {
			roman_number += "C";
			temp_number -= 100;
		}
		else if (temp_number >= 90) {
			roman_number += "XC";
			temp_number -= 90;
		}
		else if (temp_number >= 50) {
			roman_number += "L";
			temp_number -= 50;
		}
		else if (temp_number >= 10) {
			roman_number += "X";
			temp_number -= 10;
		}
		else if (temp_number >= 9) {
			roman_number += "IX";
			temp_number -= 9;
		}
		else if (temp_number >= 5) {
			roman_number += "V";
			temp_number -= 5;
		}
		else if (temp_number >= 1) {
			roman_number += "I";
			temp_number -= 1;
		}
	}
}

string RomanDecimal::get_roman() {
	return roman_number;
}

int RomanDecimal::get_decimal() {
	return decimal_number;
}

int main() {
	RomanDecimal number;
	number.add_roman("MDCCCCX");  //number 1910
	cout << "Roman number " << number.get_roman() << " are " << number.get_decimal() << " in decimal." << endl;
	number.add_roman("MCMX");  //number 1910
	cout << "Roman number " << number.get_roman() << " are " << number.get_decimal() << " in decimal." << endl;
	number.add_decimal(1910);
	cout << "Roman number " << number.get_roman() << " are " << number.get_decimal() << " in decimal." << endl;
	number.add_decimal(1858);
	cout << "Roman number " << number.get_roman() << " are " << number.get_decimal() << " in decimal." << endl;
	return 0;
}
Is that your final solution or do you still have questions?
this is j123t topic not my :D
And this is my solution.
Last edited on
Sweet, glad you got it. :)
Wow thanks. Although, my user input is an int.... not a string. so i think i did it wrong? :/
Last edited on
:/
The Romans used slaves from Greece, Babylonia etc to do their math and then translate the results back to Roman at the end.

Topic archived. No new replies allowed.