Advertisement
Guest User

Untitled

a guest
Oct 10th, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class romanType {
  5. public:
  6. string r;
  7. int d;
  8.  
  9. romanType(string rn) {
  10. r = rn;
  11. }
  12.  
  13. int convert() {
  14. int res = 0;
  15.  
  16. for (int i = 0; i < r.length(); i++) {
  17. int s1 = value(r[i]);
  18. if (i + 1 < r.length()) {
  19. int s2 = value(r[i + 1]);
  20. if (s1 >= s2) {
  21. res = res + s1;
  22. } else {
  23. res = res + s2 - s1;
  24. i++;
  25. }
  26. } else {
  27. res = res + s1;
  28. }
  29. }
  30. d = res;
  31. }
  32.  
  33. int value(char r) {
  34. if (r == 'I')
  35. return 1;
  36. if (r == 'V')
  37. return 5;
  38. if (r == 'X')
  39. return 10;
  40. if (r == 'L')
  41. return 50;
  42. if (r == 'C')
  43. return 100;
  44. if (r == 'D')
  45. return 500;
  46. if (r == 'M')
  47. return 1000;
  48.  
  49. return -1;
  50. }
  51.  
  52. void display(string which) {
  53. if (which == "r")
  54. cout << "Roman numeral: " << r << endl;
  55. else
  56. cout << "Decimal: " << d << endl;
  57. }
  58. };
  59.  
  60. int main() {
  61. romanType rn("MCXIV");
  62. rn.convert();
  63. rn.display("r");
  64. rn.display("d");
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement