
06-May-2008, 14:18
|
|
Member
|
|
Join Date: May 2008
Posts: 110
|
|
|
RomanTypr class
#include<iostream>
#include<string>
#include "romantype.h"
using namespace std;
int main()
{
romantype rom;// create an object rom from roman type class
rom.header();//display student names
rom.getRoman();//access the member getRoman
rom.convertRoman();//access the member convertRoman
rom.printRoman();//access the menber printRoman
return 0;
}
#include<iostream>
using namespace std;
class romantype// class roman type
{
public :
void header();
void getRoman(); //public members in here
void convertRoman();
void printRoman();
private: // private variabls in here
string str_roman;
int sum;
};
#include<iostream>
#include<string>
#include "romantype.h"
using namespace std;
void romantype::header()
{
cout<<"****************Team Project by*********************"<<endl;
cout<<endl;
cout<<" Heikel Khaldi and Zong Vang"<<endl;
cout<<endl;
cout<<"****************************************************"<<endl<<endl;
}
void romantype::getRoman()
{
cout<<endl;
cout<<"Please enter you Roman Number ? "<<endl<<endl;
cin >> str_roman;// get the value of str_roman
cout<<endl;
}
void romantype::convertRoman()
{
int val,val1;// two local variables to compare the 1st position from the right and the 2nd position from the right
val1=0;val=0;sum=0;// initializinghe variables
for(int i=str_roman.length()-1;i>=0;i--)// loop to select the characters positions and compares the associated
//varibles as described above
{val1=val;
switch(str_roman[i])
{
case 'i':
case'I':
val=1;
break;
case 'v':
case'V':
val=5;
break;
case 'x':
case'X':
val=10;
break;
case 'l':
case'L':
val=50;
break;
case'c':
case'C':
val=100;
break;
case'd':
case'D':
val=500;
break;
case'm':
case'M':
val=1000;
break;
default:
val=0;
}
if (val1<=val)// roman conversion algorithm
sum=sum+val;// we add when the 2nd number from right is >= to the first number from right
else
sum=sum-val;// we substract when the 2nd number from right is < to the first number from right
}
}
void romantype::printRoman()
{
int i;
cout<<endl;
cout<<"Select 1 for Roman Number. "<<endl<<endl;
cout<<endl;
cout<<"Select 2 for Decimal Number. "<<endl<<endl;
cin>>i;
if (i==1)
{
cout<<endl<<"Roman Number You entered is : "<<str_roman<<endl<<endl;// if user input 1 than the Roman number entered is the output
}
else if(i==2)
{
cout <<endl<<"Decimal Number You entered is : "<<sum<<endl<<endl;// if user input 2 than the Decimal number entered is the output
}
else if(i!=1 || i!=2)
{
cout<<endl<<"your entry is invalid please try again"<<endl<<endl;// other entry the
cin>>i;
}
}
|
|