loading
open in
main.cpp
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
157
158
159
160
161
162
163
164
/*
 Problem:
 Write a program that converts a number entered in Roman numerals to a
 positive integer. Your program should consist of a class, say, romanType.
 An object of type romanType should do the following:
 a. Store the number as a Roman numeral.
 b. Convert and store the number as a positive integer.
 c. Print the number as a Roman numeral or positive integer as requested
 by the user.
 
 The integer values of the Roman numerals are:
 M 1000
 D 500
 C 100
 L 50
 X 10
 V 5
 I 1
 d. Test your program using the following Roman numerals: MCXIV,
 CCCLIX, MDCLXVI.
 
 */



//////////////////////////////////////////


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;

//Class Definition
class romanType
{
public:
    string romanNumeral;
    int negInt;
    int romanIntArray[];
    void getRomanNumeral(string&);
    void spartanConverter(string&);
    
private:
    
};



//Main Function
int main() {
    
    //Description Header
    cout << "================================================" << endl;
    cout << "Description: Roman Numeral Project" << endl;
    cout << "Author: Christian Araya" << endl;
    cout << "================================================\n\n\n" << endl;
    
    
    
    //Declarations & Initializations
    romanType myRomanNumeral;
    string romanNumeral;
    
    
    
    
    //Output
    cout << "Welcome to the Spartan converter! Magically transform any roman numeral into a positive integer as if touched by the gods themselves!\n\n";
    
    for (int k = 0; k < 3; k++)
    {
        cout << "Enter the Roman Numeral you wish to convert here:";
        myRomanNumeral.getRomanNumeral(romanNumeral);
        myRomanNumeral.spartanConverter(romanNumeral);
        romanNumeral = "";
    }
    
    return 0;
}



//Function Definitions
void romanType::getRomanNumeral(string& romanNumeral)
{
    
    cin >> romanNumeral;
    cout << "The Roman numeral entered and stored is: " << romanNumeral << endl;
    
}

void romanType::spartanConverter(string& romanNumeral)
{
    unsigned short int n = romanNumeral.length();
    int sum = 0;
    int total;
    for (int i = 0; i < n; i++)
    {
        if (romanNumeral[i] == 'I')
        {
            romanIntArray[i] = 1;
        }
        else if (romanNumeral[i] == 'V')
        {
            romanIntArray[i] = 5;
            if (i > 0 && romanNumeral[i - 1] == 'I')
            {
                negInt= -2;
            }
        }
        else if (romanNumeral[i] == 'X')
        {
            romanIntArray[i] = 10;
            if (i > 0 && romanNumeral[i - 1] == 'I')
            {
                negInt= -2;
            }
        }
        else if (romanNumeral[i] == 'L')
        {
            romanIntArray[i] = 50;
            if (i > 0 && romanNumeral[i - 1] == 'X')
            {
                negInt= -20;
            }
        }
        else if (romanNumeral[i] == 'C')
        {
            romanIntArray[i] = 100;
            if (i > 0 && romanNumeral[i - 1] == 'X')
            {
                negInt= -20;
            }
        }
        else if (romanNumeral[i] == 'D')
        {
            romanIntArray[i] = 500;
            if (i > 0 && romanNumeral[i - 1] == 'C')
            {
                negInt= -200;
            }
        }
        else if (romanNumeral[i] == 'M')
        {
            romanIntArray[i] = 1000;
        }
        
    
        sum = sum + romanIntArray[i];
        
        
    }
    total = sum + negInt;
    cout << "The decimal equivalent is: " << total << endl << endl;
    negInt = 0;
    
}



gcc version 4.6.3