A. Submission Requirements - General Requirements Submit everything in one zippe
ID: 3766410 • Letter: A
Question
A. Submission Requirements - General Requirements
Submit everything in one zipped folder. Zip File Naming Convention
Create a folder name LabAssignmentVII_LastNameFirstName
Inside the folder should be the following:
· RomanNumeral_Update_LastNameFirstName.cpp (the code)
· LabAssignmentVII_LastNameFirstName.docx (the screen shot of the output for each of the program)
then create a zip file with all the above files.
· Ex.LabAssignmentVII_RogersGregory.zip
Lab Assignment Requirements
Update Lab V
We defined a class romanType to implement Roman numbers in a program. In that exercise, we also implemented a function, romanToDecimal, to convert a Roman number into its equivalent decimal number. Modify the definition of the class romanType so that the member variables are declared as protected. Use the class newString, as designed in Programming Exercise 9, to manipulate strings. Furthermore, overload the stream insertion and stream extraction operators for easy input and output. The stream insertion operator outputs the Roman number in the Roman format.
Also, include a member function, decimalToRoman, that converts the decimal number (the decimal number must be a positive integer) to an equivalent Roman number format. Write the definition of the member function decimalToRoman.
For simplicity, we assume that only the letter I can appear in front of another letter and that it appears only in front of the letters V and X. For example, 4 is represented as IV, 9 is represented as IX, 39 is represented as XXXIX, and 49 is represented as XXXXIX. Also, 40 will be represented as XXXX, 190 will be represented as CLXXXX, and so on.
Derive a class extRomanType from the class romanType to do the following: In the class extRomanType, overload the arithmetic operators +, -, *, and/ so that arithmetic operations can be performed on Roman numbers. Also, overload the pre- and post-increment and decrement operators as member functions of the class extRomanType.
To add (subtract, multiply, or divide) Roman numbers, add (subtract, multiply, or divide, respectively) their decimal representations and then convert the result to the Roman number format. For subtraction, if the first number is smaller than the second number, output a message saying that, ‘‘Because the first number is smaller than the second, the numbers cannot be subtracted’’. Similarly, for division, the numerator must be larger than the denominator. Use similar conventions for the increment and decrement operators. c. Write the definitions of the functions to overload the operators described in part b.
Test your class extRomanType on the following program. (Include the appropriate header files.)
int main()
{
string input;
RomanType rt;
cout << "Please enter a Roman numeral : "
cin >> input;
rt.SetRomanNumeral(input);
rt.ConvertAndStoreRomanNumeral();
rt.PrintRomanNumeralDecimal();
extRomanType num1(“XXXIV”);
extRomanType num2(“XV”);
extRomanType num3;
cout << "Num1 = " << num1 << endl;
cout << "Num2 = " << num2 << endl;
cout << "Num1 + Num2 = " << num1 + num2 << endl;
cout << "Num1 * Num2 = " << num1 * num2 << endl;
cout << "Enter two numbers in Roman format: ";
cin >> num1 >> num2;
cout << endl;
cout << "Num1 = " << num1 << endl;
cout << "Num2 = " << num2 << endl;
num3 = num2 * num1;
cout << "Num3 = " << num3 << endl;
cout << "--num3: " << --num3 << endl;
cout << "++num3: " << ++num3 << endl;
system("pause");
return (0);
}
one exercise that was done:
//Header file myString.h
#ifndef H_myString
#define H_myString
#include <iostream>
using namespace std;
class newString
{
//Overload stream insertion and extraction operators
friend ostream& operator<<(ostream&, const newString&);
friend istream& operator>>(istream&, newString&);
public:
const newString& operator=(const newString&);
//overload assignment operator
//string concatenation
const newString& operator+=(const newString&);
const newString& operator+=(char ch);
newString operator+(const newString&) const;
newString operator+(char ch) const;
newString(const char *);
//Constructor; conversion from char string
newString();
//Default constructor to initialize the string to null.
newString(const newString&);
//Copy constructor
~newString();
//Destructor
int length();
//Return the length of the string
char &operator[] (int);
const char &operator[](int) const;
//overload relational operator
bool operator==(const newString&) const;
bool operator!=(const newString&) const;
bool operator<=(const newString&) const;
bool operator<(const newString&) const;
bool operator>=(const newString&) const;
bool operator>(const newString&) const;
private:
char *strPtr; //pointer to char array that holds the string
int strLength; //variable to store the length of the string
};
#endif
the other exercise that was done:
//Implementation file myString.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "myString.h"
using namespace std;
//constructor: conversion from char string to newString
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1]; //allocate memory to store char string
strcpy(strPtr, str); // copy string into strPtr.
}
newString::newString() //default constructor to store null string
{
strLength = 0;
strPtr = new char[1];
strcpy(strPtr,"");
}
newString::newString(const newString ©) //copy constructor
{
strLength = copy.strLength;
strPtr = new char[strLength + 1];
strcpy(strPtr, copy.strPtr);
}
newString::~newString() //destructor
{
delete [] strPtr;
}
//overload the assignment operator
const newString& newString::operator=(const newString &rightStr)
{
if (this != &rightStr) //avoid self copy
{
delete [] strPtr;
strLength = rightStr.strLength;
strPtr = new char [strLength + 1];
strcpy(strPtr, rightStr.strPtr);
}
return *this;
}
const newString& newString::operator+=(const newString& rightStr)
{
char *temp = strPtr;
strLength += rightStr.strLength;
strPtr = new char[strLength + 1];
strcpy(strPtr, temp);
strcat(strPtr, rightStr.strPtr);
delete []temp;
return *this;
}
const newString& newString::operator+=(char ch)
{
char *temp = strPtr;
char chStr[2];
chStr[0] = ch;
chStr[1] = '';
strLength += + 1;
strPtr = new char[strLength + 1];
strcpy(strPtr, temp);
strcat(strPtr, chStr);
delete []temp;
return *this;
}
newString newString::operator+(const newString& rightStr) const
{
newString temp;
temp.strLength = strLength + rightStr.strLength;
temp.strPtr = new char[temp.strLength + 1];
strcpy(temp.strPtr, strPtr);
strcat(temp.strPtr, rightStr.strPtr);
return temp;
}
newString newString::operator+(char ch) const
{
newString temp;
char chStr[2];
chStr[0] = ch;
chStr[1] = '';
temp.strLength = strLength + 1;
temp.strPtr = new char[temp.strLength + 1];
strcpy(temp.strPtr, strPtr);
strcat(temp.strPtr, chStr);
return temp;
}
int newString::length()
{
return strLength;
}
char& newString::operator[] (int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char & newString::operator[](int index) const
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
//overload relational operators
bool newString::operator==(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) == 0);
}
bool newString::operator<(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) < 0);
}
bool newString::operator<=(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) <= 0);
}
bool newString::operator>(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) > 0);
}
bool newString::operator>=(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) >= 0);
}
bool newString::operator!=(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) != 0);
}
//overload the stream insertion operator <<
ostream& operator<<(ostream& os, const newString &s)
{
os << s.strPtr;
return os;
}
//overload the stream extraction operator >>
istream& operator>>(istream& is, newString &s)
{
char temp[81];
is >> setw(81) >> temp;
s = temp;
return is;
}
Explanation / Answer
//Header Files
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "myString.h"
using namespace std;
//constructor: conversion from char string to newString
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1]; //allocate memory to store char string
assert(strPtr != 0);
strcpy(strPtr,str); // copy string into strPtr.
}
newString::newString() //default constructor to store null string
{
strLength = 0;
strPtr = new char[1];
assert(strPtr != 0);
strcpy(strPtr,"");
}
newString::newString(const newString ©) //copy constructor
{
strLength = copy.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != 0);
strcpy(strPtr, copy.strPtr);
}
newString::~newString() //destructor
{
delete [] strPtr;
}
//overload the assignment operator
const newString& newString::operator=(const newString &rightStr)
{
if(this != &rightStr) //avoid self copy
{
delete [] strPtr;
strLength = rightStr.strLength;
strPtr = new char [strLength + 1];
assert(strPtr != 0);
strcpy(strPtr, rightStr.strPtr);
}
return *this;
}
const newString& newString::operator+=(const newString& rightStr)
{
char *temp = strPtr;
strLength += rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != 0);
strcpy(strPtr, temp);
strcat(strPtr, rightStr.strPtr);
delete []temp;
return *this;
}
const newString& newString::operator+=(char ch)
{
char *temp = strPtr;
char chStr[2];
chStr[0] = ch;
chStr[1] = '';
strLength += + 1;
strPtr = new char[strLength + 1];
assert(strPtr != 0);
strcpy(strPtr,temp);
strcat(strPtr, chStr);
delete []temp;
return *this;
}
newString newString::operator+(const newString& rightStr) const
{
newString temp;
temp.strLength = strLength + rightStr.strLength;
temp.strPtr = new char[temp.strLength + 1];
assert(temp.strPtr != 0);
strcpy(temp.strPtr, strPtr);
strcat(temp.strPtr, rightStr.strPtr);
return temp;
}
newString newString::operator+(char ch) const
{
newString temp;
char chStr[2];
chStr[0] = ch;
chStr[1] = '';
temp.strLength = strLength + 1;
temp.strPtr = new char[temp.strLength + 1];
assert(temp.strPtr != 0);
strcpy(temp.strPtr, strPtr);
strcat(temp.strPtr, chStr);
return temp;
}
int newString::length()
{
return strLength;
}
char& newString::operator[] (int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char & newString::operator[](int index) const
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
//overload relational operators
bool newString::operator==(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) == 0);
}
bool newString::operator<(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) < 0);
}
bool newString::operator<=(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) <= 0);
}
bool newString::operator>(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) > 0);
}
bool newString::operator>=(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) >= 0);
}
bool newString::operator!=(const newString &rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) != 0);
}
//overload the stream insertion operator <<
ostream& operator << (ostream& os, const newString &s)
{
os << s.strPtr;
return os;
}
//overload the stream extraction operator >>
istream& operator >> (istream& is, newString &s)
{
char temp[81];
is >> setw(81) >> temp;
s = temp;
return is;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.