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
|
void DECROM()//function for conversion decimal to roman
{
int fnum,snum,dig;
bool needFnum = true;
bool needSnum= true;
bool negCheck= true;
while(negCheck)
{
while(needFnum)
{
cout<<"Enter first number: ";
try
{
cin>>fnum;
if (fnum>1500)
throw Range();
}
catch (Range)
{
cout<<"Number out of Range"<<endl;
cout<<"Please Re-enter number: "<<endl;
continue;
}
needFnum=false;
}
while(needSnum)
{
cout<<"Enter second number: ";
try
{
cin>>snum;
if (snum>1500)
throw Range();
}
catch (Range)
{
cout<<"Number out of Range"<<endl;
cout<<"Please Re-enter number: "<<endl;
continue;
}
needSnum=false;
}
dig=fnum+snum;
try
{
if (dig<=0)
throw NegativeNum();
}
catch (NegativeNum)
{
cout<<"Cannot convert to Roman Numerals"<<endl;
cout<<"Please Re-enter both numbers"<<endl;
continue;
}
cout<<endl;
cout<<intToRoman(dig);
}
negCheck=false;
}
int numcheck(int x)
{
try
{
if (x>1500)
throw Range();
}
catch (Range)
{
cout<<"Number out of Range"<<endl;
cout<<"Please Re-enter number: "<<endl;
}
return 0;
}
string intToRoman(int num)
{
string roman;
int h,th,o,t ;
char *ones[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
char *tens[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
char *hundreds[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
char *thousands[] = {"","M" , "MM", "MMM"};
if (num <=3000)
{
th=num/1000;
h=(num%1000)/100;
t=((num%1000)%100)/10;
o=(((num%1000)%100)%10)/1;
roman = roman + thousands[th] + hundreds[h] + tens[t] + ones[o];
}
else
roman = "Please enter a smaller number\n";
return roman;
}
|