Guest User

Untitled

a guest
Aug 3rd, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. //main******************************************************************************************************
  2. //cplusplus.com/forum/beginner/75558
  3. #include <iostream>
  4. #include "RomanInt.h"
  5.  
  6. int main()
  7. {
  8.     RomanInt RInt;
  9.  
  10.     do
  11.     {
  12.         std::cout << "Enter a roman numeral: ";
  13.         std::cin >> RInt;
  14.     }
  15.     while (true);
  16.  
  17. }
  18. //RomanInt.h************************************************************************************************
  19. #ifndef ROMANINT_H
  20. #define ROMANINT_H
  21. #include <string>
  22. #include <iostream>
  23. #include <cstdlib>
  24. #include <algorithm>
  25.  
  26. class RomanInt
  27. {
  28. public:
  29.     RomanInt (std::string RNum = "");
  30.     void PrintRoman(int);
  31. private:
  32.     void Convert (std::string RNum);
  33.     int ValueOf (char RChar);
  34.     bool IsValid (std::string RNum);
  35.  
  36.     friend std::istream& operator>> (std::istream& is, RomanInt& RInt);
  37.  
  38.     std::string RomanNumeral;
  39.     int DecimalNumeral;
  40. };
  41. #endif
  42. //RomanInt.cpp**********************************************************************************************
  43. #include "RomanInt.h"
  44.  
  45. RomanInt::RomanInt (std::string RNum) :
  46. RomanNumeral (RNum)
  47. {
  48.     if (RNum != "") Convert (RNum);
  49. }
  50.  
  51. void RomanInt::Convert (std::string RNum)
  52. {
  53.     int DecimalNumeral = 0;
  54.     bool isOdd = 0;
  55.     int ValueL;
  56.     int ValueR;
  57.  
  58.     for (unsigned int i = 0; i < RNum.length(); i++)
  59.     {
  60.         if (RNum.length() % 2 && i == 0) // is odd and first number
  61.         {
  62.             isOdd = 1;
  63.             DecimalNumeral += ValueOf (RNum[0]);
  64.         }
  65.         else
  66.         {
  67.             ValueL = ValueOf (RNum[RNum.length() - i - 2 + isOdd]); //shortcut and easier on the eyes
  68.             ValueR = ValueOf (RNum[RNum.length() - i - 1 + isOdd]);
  69.  
  70.             if (ValueL < ValueR) DecimalNumeral += (ValueR - ValueL);
  71.             else DecimalNumeral += (ValueL + ValueR);
  72.  
  73.             i++; //do it in groups of 2
  74.         }
  75.     }
  76.     std::cout << RNum << " = " << DecimalNumeral << std::endl;
  77. }
  78.  
  79. int RomanInt::ValueOf (char RChar)
  80. {
  81.     switch (RChar)
  82.     {
  83.     case 'I': return 1;
  84.     case 'V': return 5;
  85.     case 'X': return 10;
  86.     case 'L': return 50;
  87.     case 'C': return 100;
  88.     case 'D': return 500;
  89.     case 'M': return 1000;
  90.     default:
  91.         std::cout << RChar << " should not be in the ValueOf function, something broke" << std::endl;
  92.         exit (1); //terminate
  93.     }
  94.  
  95.     return -10000; //shouldn't ever be called, but just in case some weird shit goes on.
  96. }
  97.  
  98. void RomanInt::PrintRoman(int var)
  99. {
  100.     if (var == 0) std::cout << RomanNumeral << std::endl; //why? don't ask me, part of the exercise
  101.     else std::cout << DecimalNumeral << std::endl; //why? don't ask me, part of the exercise
  102. }
  103.  
  104. bool RomanInt::IsValid (std::string RNum)
  105. {
  106.     for (unsigned int i = 0; i < RNum.length(); i++)
  107.     {
  108.         if ( (RNum[i] != 'I' && RNum[i] != 'V' && RNum[i] != 'X' && RNum[i] != 'L' && RNum[i] != 'C' && RNum[i] != 'D' && RNum[i] != 'M'))
  109.             return false; // only accept IVXLCDM
  110.         else if ( (RNum.length() >= 4 && i <= RNum.length() - 4) && (RNum[i] == RNum[i+1]) && (RNum[i+1] == RNum[i+2]) && (RNum[i+2] == RNum[i+3]))
  111.             return false; // no more than 3 of the same character in a row
  112.     }
  113.  
  114.     return true;
  115. }
  116.  
  117. std::istream& operator>> (std::istream& is, RomanInt& RInt)
  118. {
  119.     std::string RNum;
  120.     is >> RNum; //get input
  121.     std::transform (RNum.begin(), RNum.end(), RNum.begin(), ::toupper); //fix lowercase
  122.  
  123.     if (RInt.IsValid (RNum)) //check we can convert it
  124.     {
  125.         RInt.RomanNumeral = RNum;
  126.         RInt.Convert (RNum);
  127.     }
  128.     else std::cout << "Invalid Numeral " << RNum << " entered\n" << std::endl;
  129.  
  130.     return is;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment