\"CS270 Data Structures\" Object-Oriented Design (OOD) and C++ (Textbook: Malik,
ID: 3572358 • Letter: #
Question
"CS270 Data Structures"
Object-Oriented Design (OOD) and C++
(Textbook: Malik, D.S. (2009). Data Structures Using C++ 2nd edition. Boston, MA: Course Technology (Cengage Learning).
ISBN: 9780324782011)
Pages 57 and 124
Chapter one (complete Programming problems only): 1, 2, 3
Chapter two (complete Programming problems only): 2, 6, 20
You will need to submit .cpp files or if you're using Visual Studio you can submit the entire contents of the project (which includes the .cpp file(s)). You can download the source code for the problems within the chapter so that you don’t have to type everything out via the “Supplemental Material” link in the course shell. It’s important to note that all programs must run. If a program does not compile and run, it will receive a grade of 0. See my announcement on how to submit assignments, such as what naming convention to use for your programs. For each program/problem, you should submit a screenshot of your output that shows you compiled and tested your program (show the results).
Explanation / Answer
//CHAPTER 1-Programming Prob:1
//The following program converts given Roman number to decimal number as per specifications provided in the //refference.
//Program.cpp
#include <iostream>
#include <string>
using namespace std;
class romanToDec{
public:
romanToDec();
romanToDec(string s);
~romanToDec();
void setRom(string); //Set the Roman numeral from user entries.
int rtd(); //Convert the Roman numeral(string) to Decimal value.
void displayDecimal(); //Display the decimal value.
void displayRoman(); //Display the Roman numeral value.
private:
string rNum;
int decimalNum = 0;
};
romanToDec::romanToDec()
{
rNum = 1;
}
romanToDec::~romanToDec()
{
}
void romanToDec::setRom(string troll)
{
rNum = troll;
}
int romanToDec::rtd()
{
for (int i = 0; i < rNum.length(); i++)
{
if (rNum[i] == 'I')
decimalNum++;
if (rNum[i] == 'V')
{
if (i > 0 && rNum[i - 1] == 'I')
decimalNum -= 2;
decimalNum += 5;
}
if (rNum[i] == 'X')
{
if (i > 0 && rNum[i - 1] == 'I')
decimalNum -= 2;
decimalNum += 10;
}
if (rNum[i] == 'L')
{
if (i > 0 && rNum[i - 1] == 'X')
decimalNum -= 20;
decimalNum += 50;
}
}
cout << decimalNum << endl;
return decimalNum;
}
int main()
{
string numeral;
char choice;
do{
cout << "Please enter the roman numeral: ";
cin >> numeral;
romanToDec Rom;
Rom.setRom(numeral);
Rom.rtd();
cout<<" Do you want to convert again(Press y-yes/n-no):";
cin>>choice;
} while (choice=='y' || choice=='Y');
}
*********************************output****************************************
Please enter the roman numeral: X
10
Do you want to convert again(Press y-yes/n-no):Y
Please enter the roman numeral: XX
20
Do you want to convert again(Press y-yes/n-no):Y
Please enter the roman numeral: IX
9
Do you want to convert again(Press y-yes/n-no):Y
Please enter the roman numeral: LX
60
Do you want to convert again(Press y-yes/n-no):Y
Please enter the roman numeral: XL
40
Do you want to convert again(Press y-yes/n-no):N
--------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.