Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Inside main.cpp and anywhere you have used the menu replace the creation of any Double or Integer class to use a pointer and dynamic memory.

Inside main.cpp and anywhere you have used the menu replace the creation of any Double or Integer class to use a pointer and dynamic memory. For instance:

1
Double d = new Double("123.45") 

Please make sure to check that the memory was allocated correctly. In addition don't forget you need to use the arrow operator.

Double.h

#ifndef DOUBLE

#define DOUBLE

#include "Integer.h"

#include

#include

class Double

{

private:

double data;

bool nan;

void isNan(std::string s);

public:

bool isNan() { return this->nan; }

void equals(double d);

void equals(std::string s);

Double add(const Double &d);

Double sub(const Double &d);

Double mul(const Double &d);

Double div(const Double &d);

double toDouble() const;

friend std::istream &operator>>(std::istream &is, Double& m);

// Overloaded Functions

Double add(double d);

Double sub(double d);

Double mul(double d);

Double div(double d);

// Constructors

Double();

Double(double d);

Double(const Double &d);

Double(const Integer &i);

Double(const std::string str);

//Operator Overloads

Double operator + (const Double &d);

Double operator - (const Double &d);

Double operator * (const Double &d);

Double operator / (const Double &d);

Double &operator = (const Double&d);

Double &operator = (double d);

Double &operator = (std::string s);

bool operator == (const Double &d);

bool operator == (double d);

bool operator != (const Double &d);

bool operator != (double d);

std::string toString();

};

#endif

Integer.h

#ifndef INTEGER

#define INTEGER

#include

#include

class Integer

{

private:

int data;

bool nan;

void isNaN(std::string s);

public:

bool isNaN() { return this->nan; }

void equals(int i);

void equals(std::string s);

const int& Data() const { return data; }

friend std::istream &operator>>(std::istream &is, Integer& m);

Integer add(const Integer &i);

Integer sub(const Integer &i);

Integer mul(const Integer &i);

Integer div(const Integer &i);

int toInt() const;

// Overloaded Functions

Integer add(int i);

Integer sub(int i);

Integer mul(int i);

Integer div(int i);

// Constructors

Integer();

Integer(int i);

Integer(const Integer &i);

Integer(const std::string str);

//Overloaded Operators

Integer operator + (const Integer &i);

Integer operator - (const Integer &i);

Integer operator * (const Integer &i);

Integer operator / (const Integer &i);

Integer &operator = (std::string s);

Integer &operator = (const Integer&i);

Integer &operator = (int i);

bool operator == (const Integer &i);

bool operator == (int i);

bool operator != (const Integer &i);

bool operator != (int i);

std::string toString();

};

#endif

Menu.h

#ifndef MENU

#define MENU

#include

#include

#include

#include

const int MAX_COUNT = 20;

//struct

struct menuItem

{

void(*func)();

std::string description;

};

class Menu

{

private: //data section

std::vector mi;

int count;

void runSelection();

public:

//constructors

Menu() : count(0), mi(MAX_COUNT) {};

//mutators

void addMenu(std::string description, void(*f)(void));

//accessors

void runMenu();

void waitKey();

};

#endif

Double.cpp

#include

#include

#include

#include "Double.h"

#include "Integer.h"

using namespace std;

// Constructors

Double::Double()

:nan(false)

{

this->equals(0);

}

Double::Double(double d)

: nan(false)

{

this->equals(d);

}

Double::Double(const Double &d)

: nan(false)

{

this->equals(d.toDouble());

}

Double::Double(const Integer &i)

: nan(false)

{

this->equals(static_cast(i.toInt()));

}

//Overloaded Functions

Double Double::add(double d)

{

return Double(this->toDouble() + d);

}

Double Double::sub(double d)

{

return Double(this->toDouble() - d);

}

Double Double::mul(double d)

{

return Double(this->toDouble() * d);

}

Double Double::div(double d)

{

return Double(this->toDouble() / d);

}

void Double::equals(double d)

{

this->data = d;

this->nan = false;

}

void Double::equals(std::string s)

{

this->isNan(s);

if (this->nan)

{

this->data = 0.0;

}

else

{

this->data = stod(s);

}

}

double Double::toDouble() const

{

return this->data;

}

Double Double::add(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() + d.toDouble());

return tmp;

}

Double Double::sub(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() - d.toDouble());

return tmp;

}

Double Double::mul(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() * d.toDouble());

return tmp;

}

Double Double::div(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() / d.toDouble());

return tmp;

}

istream& operator>>(istream &is, Double &m)

{

is >> m.data;

return is;

}

Double Double::operator + (const Double &d)

{

return this->add(d);

}

Double Double::operator - (const Double &d)

{

return this->sub(d);

}

Double Double::operator * (const Double &d)

{

return this->mul(d);

}

Double Double::operator / (const Double &d)

{

return this->div(d);

}

Double &Double::operator = (const Double &d)

{

this->equals(d.toDouble());

return *this;

}

Double &Double::operator = (double d)

{

this->equals(d);

return *this;

}

Double &Double::operator = (string s)

{

this->equals(s);

return *this;

}

bool Double::operator == (const Double &d)

{

return this->toDouble() == d.toDouble();

}

bool Double::operator == (double d)

{

return this->toDouble() == d;

}

bool Double::operator != (const Double &d)

{

return this->toDouble() != d.toDouble();

}

bool Double::operator != (double d)

{

return this->toDouble() != d;

}

std::string Double::toString()

{

std::stringstream ss;

ss << this->data;

return ss.str();

}

void Double::isNan(string s)

{

int pos;

this->nan = false;

pos = s.find(".", 0);

if (pos != string::npos)

{

pos = s.find(".", pos + 1);

if (pos != string::npos)

{

this->nan = true;

return;

}

}

string::iterator p;

for (p = s.begin(); p < s.end(); p++)

{

if (!isdigit(*p) && *p != '.')

{

this->nan = true;

return;

}

}

return;

}

Integer.cpp

#include

#include

#include

#include "Integer.h"

using namespace std;

//Constructors

Integer::Integer()

:nan(false)

{

this->equals(0);

}

Integer::Integer(int i)

: nan(false)

{

this->equals(i);

this->nan = false;

}

Integer::Integer(const Integer &i)

:nan(false)

{

this->equals(i.toInt());

}

//Overloaded Functions

Integer Integer::add(int i)

{

return Integer(this->toInt() + i);

}

Integer Integer::sub(int i)

{

return Integer(this->toInt() - i);

}

Integer Integer::mul(int i)

{

return Integer(this->toInt() * i);

}

Integer Integer::div(int i)

{

return Integer(this->toInt() / i);

}

void Integer::equals(int i)

{

this->data = i;

}

void Integer::equals(std::string s)

{

this->isNaN(s);

if (this->nan)

{

this->data = 0;

}

else

{

this->data = stod(s);

}

}

int Integer::toInt() const

{

return this->data;

}

Integer Integer::add(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() + i.toInt());

return tmp;

}

Integer Integer::sub(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() - i.toInt());

return tmp;

}

Integer Integer::mul(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() * i.toInt());

return tmp;

}

Integer Integer::div(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() / i.toInt());

return tmp;

}

istream& operator>>(istream& is, Integer& m)

{

is >> m.data;

return is;

}

Integer Integer::operator+(const Integer &i)

{

return this->add(i);

}

Integer Integer::operator-(const Integer &i)

{

return this->sub(i);

}

Integer Integer::operator*(const Integer &i)

{

return this->mul(i);

}

Integer Integer::operator/(const Integer &i)

{

return this->div(i);

}

Integer &Integer::operator = (const Integer &i)

{

this->equals(i.toInt());

return *this;

}

Integer &Integer::operator = (int i)

{

this->equals(i);

return *this;

}

Integer &Integer::operator = (string s)

{

this->equals(s);

return *this;

}

bool Integer::operator == (const Integer &i)

{

return this->toInt() == i.toInt();

}

bool Integer::operator == (int i)

{

return this->toInt() == i;

}

bool Integer::operator != (const Integer &i)

{

return this->toInt() != i.toInt();

}

bool Integer::operator != (int i)

{

return this->toInt() != i;

}

std::string Integer::toString()

{

std::stringstream ss;

ss << this->data;

return ss.str();

}

void Integer::isNaN(string s)

{

this->nan = false;

string::iterator p;

for (p = s.begin(); p < s.end(); p++)

{

if (!isdigit(*p) && *p != '.')

{

this->nan = true;

return;

}

}

}

main.cpp

#include

#include

#include

#include "Double.h"

#include "Integer.h"

#include "Menu.h"

using namespace std;

Menu m;

Double du;

void doubleCheck();

void doubleAdd();

void doubleSub();

void doubleMul();

void doubleDiv();

void intCheck();

void intAdd();

void intSub();

void intMul();

void intDiv();

void Exit();

int main()

{

m.addMenu("1. Add Doubles.", doubleAdd);

m.addMenu("2. Sub Doubles.", doubleSub);

m.addMenu("3. Mul Doubles.", doubleMul);

m.addMenu("4. Div Doubles.", doubleDiv);

m.addMenu("5. Add Integers.", intAdd);

m.addMenu("6. Sub Integers.", intSub);

m.addMenu("7. Mul Integers.", intMul);

m.addMenu("8. Div Integers.", intDiv);

m.addMenu("9. Check doubles.", doubleCheck);

m.addMenu("10. Check Integers.", intCheck);

m.addMenu("11. Exit.", Exit);

m.runMenu();

return 0;

}

void doubleCheck()

{

string s;

Double d;

cout << "Please enter a double." << endl;

cin >> s;

d.equals(s);

if (d.isNan())

cout << "Cannot assign a non-number to class Double" << endl;

if (d.isNan() == false)

cout << "Valid number set to " << d.toString() << endl;

m.waitKey();

}

void intCheck()

{

string s;

Integer i;

cout << "Please enter an integer." << endl;

cin >> s;

i.equals(s);

if (i.isNaN())

cout << "Cannot assign a non-number to class Integer." << endl;

if (i.isNaN() == false)

cout << "Valid number set to " << i.toString() << endl;

m.waitKey();

}

void doubleAdd()

{

Double d, d2, d3;

cout << "Enter your first double." << endl;

cin >> d;

cout << "Enter your second double." << endl;

cin >> d2;

d3 = d + d2;

cout << d3.toDouble() << endl;

m.waitKey();

}

void doubleSub()

{

Double d, d2, d3;

cout << "Enter your first double." << endl;

cin >> d;

cout << "Enter your second double." << endl;

cin >> d2;

d3 = d - d2;

cout << d3.toDouble() << endl;

m.waitKey();

}

void doubleMul()

{

Double d, d2, d3;

cout << "Enter your first double." << endl;

cin >> d;

cout << "Enter your second double." << endl;

cin >> d2;

d3 = d * d2;

cout << d3.toDouble() << endl;

m.waitKey();

}

void doubleDiv()

{

Double d, d2, d3;

cout << "Enter your first double." << endl;

cin >> d;

cout << "Enter your second double." << endl;

cin >> d2;

d3 = d / d2;

cout << d3.toDouble() << endl;

m.waitKey();

}

void intAdd()

{

Integer i, i2, i3;

cout << "Enter your first Integer." << endl;

cin >> i;

cout << "Enter your second Integer." << endl;

cin >> i2;

i3 = i + i2;

cout << i3.toInt() << endl;

m.waitKey();

}

void intSub()

{

Integer i, i2, i3;

cout << "Enter your first Integer." << endl;

cin >> i;

cout << "Enter your second Integer." << endl;

cin >> i2;

i3 = i - i2;

cout << i3.toInt() << endl;

m.waitKey();

}

void intMul()

{

Integer i, i2, i3;

cout << "Enter your first Integer." << endl;

cin >> i;

cout << "Enter your second Integer." << endl;

cin >> i2;

i3 = i * i2;

cout << i3.toInt() << endl;

m.waitKey();

}

void intDiv()

{

Integer i, i2, i3;

cout << "Enter your first Integer." << endl;

cin >> i;

cout << "Enter your second Integer." << endl;

cin >> i2;

i3 = i / i2;

cout << i3.toInt() << endl;

m.waitKey();

}

void Exit()

{

cout << "Thanks for using the program." << endl;

exit(0);

}

Menu.cpp

#include "Menu.h"

#include

#include

#include

void Menu::runSelection()

{

int select;

std::cin >> select;

if (select <= this->count)

this->mi[select - 1].func();

}

void Menu::addMenu(std::string description, void(*f)(void))

{

if (this->count < MAX_COUNT)

{

this->mi[count].func = f;

this->mi[count].description = description;

count++;

}

}

void Menu::runMenu()

{

for (;;)

{

system("CLS");

for (unsigned int pos = 0; pos < this->count; pos++)

std::cout << this->mi[pos].description << std::endl;

runSelection();

}

}

void Menu::waitKey()

{

std::cout << "Press any key to continue" << std::endl;

while (!_kbhit());

std::fflush(stdin);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

More Books

Students also viewed these Databases questions

Question

why Socrates was considered an example wisdom (the wisest ofmen)

Answered: 1 week ago

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago