Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class romanType {
- public:
- string r;
- int d;
- romanType(string rn) {
- r = rn;
- }
- int convert() {
- int res = 0;
- for (int i = 0; i < r.length(); i++) {
- int s1 = value(r[i]);
- if (i + 1 < r.length()) {
- int s2 = value(r[i + 1]);
- if (s1 >= s2) {
- res = res + s1;
- } else {
- res = res + s2 - s1;
- i++;
- }
- } else {
- res = res + s1;
- }
- }
- d = res;
- }
- int value(char r) {
- if (r == 'I')
- return 1;
- if (r == 'V')
- return 5;
- if (r == 'X')
- return 10;
- if (r == 'L')
- return 50;
- if (r == 'C')
- return 100;
- if (r == 'D')
- return 500;
- if (r == 'M')
- return 1000;
- return -1;
- }
- void display(string which) {
- if (which == "r")
- cout << "Roman numeral: " << r << endl;
- else
- cout << "Decimal: " << d << endl;
- }
- };
- int main() {
- romanType rn("MCXIV");
- rn.convert();
- rn.display("r");
- rn.display("d");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement