Question
C++ Level 2: Lab 10 - Recursion In a previous lab you created a function called isNaN that would check to see if a string
C++ Level 2: Lab 10 - Recursion
In a previous lab you created a function called isNaN that would check to see if a string contains a valid number. For this assignment create a function called recursiveNaN that uses recursion to check to see if the string contains a valid number. This function should be in the private section of the class.
So to not break the interface call recursiveNaN from the isNaN function.
Menu. h
#ifndef MENU
#define MENU
#include
#include
#include
#include
#include
using std::vector;
struct menuItem
{
std::string descript;
void(*func)();
};
class Menu
{
private:
vector
void run();
public:
Menu();
void addMenu(std::string Descript, void(*f)());
void runMenu();
void waitKey();
};
#endif
Menu.cpp
using namespace std;
Menu::Menu()
{
//Default constructor.
}
void Menu::addMenu(std::string Descript, void(*f)())
{
menuItem m;
m.descript = Descript;
m.func = f;
mI.push_back(m); // Do more research on this.
}
void Menu::run()
{
menuItem tmp;
for (;;) // Forever loop
{
for (int i = 0; i < (int)mI.size(); i++)
{
tmp = mI.at(i);
cout << tmp.descript << endl;
}
run();
}
}
void Menu::runMenu()
{
int select;
cin >> select;
if (select <= (int)mI.size())
{
mI[select - 1].func();
}
}
void Menu::waitKey()
{
cout << "Press any key to continue..." << endl;
getchar();
cin.ignore();
}
Double.h
#include "Integer.h"
#include
#ifndef DOUBLE
#define DOUBLE
class Double
{
private:
double doubleData;
bool nan;
bool isNan(std::string s);
public:
//Constructors:
Double();
Double(const Double &d);
Double(double d);
Double(const Integer &i);
Double(const std::string str);
double toDouble() const;
void equals(std::string s);
void equals(double d);
Double add(const Double &d);
Double sub(const Double &d);
Double mul(const Double &d);
Double div(const Double &d);
//Primitive Functions
Double add(double &d);
Double sub(double &d);
Double mul(double &d);
Double div(double &d);
//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);
std::string toString();
bool isNan();
};
#endif
Double.cpp
#include"Double.h"
#include"Integer.h"
#include
#include
#include
Double::Double()
:doubleData(0.0), nan(false)
{
}
Double::Double(const Double &d)
: nan(false)
{
equals(d.doubleData);
}
Double::Double(double d)
: doubleData(d), nan(false)
{
}
Double::Double(const Integer &i)
: nan(false)
{
equals((double)i.toInt());
}
Double::Double(const std::string str)
{
this->equals(str);
}
double Double::toDouble() const
{
return this->doubleData;
}
void Double::equals(std::string s)
{
this->isNan(s);
if (this->nan)
this->doubleData = 0.0;
else
this->doubleData = stod(s);
}
void Double::equals(double d)
{
doubleData = d;
}
Double Double::add(const Double &d)
{
Double ans(doubleData + d.toDouble());
return ans;
}
Double Double::sub(const Double &d)
{
Double ans(doubleData - d.toDouble());
return ans;
}
Double Double::mul(const Double &d)
{
Double ans(doubleData * d.toDouble());
return ans;
}
Double Double::div(const Double &d)
{
Double ans(doubleData / d.toDouble());
return ans;
}
//Primitive Function Definitions:
Double Double::add(double &d)
{
Double ans;
ans.equals(this->toDouble() + d);
return ans;
}
Double Double::sub(double &d)
{
Double ans;
ans.equals(this->toDouble() - d);
return ans;
}
Double Double::mul(double &d)
{
Double ans;
ans.equals(this->toDouble() * d);
return ans;
}
Double Double::div(double &d)
{
Double ans;
ans.equals(this->toDouble() / d);
return ans;
}
// Operator Overloads:
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 = (std::string s)
{
this->equals(s);
return *this;
}
bool Double::operator == (const Double &d)
{
return this->doubleData == d.doubleData;
}
bool Double::operator == (double d)
{
return this->doubleData == d;
}
std::string Double::toString()
{
std::stringstream ss;
ss << this->doubleData;
return ss.str();
}
bool Double::isNan(std::string s)
{
int pos;
this->nan = false;
pos = s.find(".", 0);
if (pos != string::npos)
{
pos = s.find(".", +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;
}
bool Double::isNan()
{
return nan;
}
Integer.h
#include
using namespace std;
#ifndef INTEGER
#define INTEGER
class Integer
{
private:
int intData;
bool nan;
void isNan(std::string s);
public:
//Constructors:
Integer();
Integer(const Integer &i);
Integer(int i);
Integer(const std::string str);
int toInt() const;
void equals(int i);
void equals(std::string s);
Integer add(const Integer &i);
Integer sub(const Integer &i);
Integer mul(const Integer &i);
Integer div(const Integer &i);
//Primitive Constructors
Integer add(int &i);
Integer sub(int &i);
Integer mul(int &i);
Integer div(int &i);
// Overloaded Operators
Integer operator + (const Integer &i);
Integer operator - (const Integer &i);
Integer operator * (const Integer &i);
Integer operator / (const Integer &i);
Integer &operator = (const Integer &i);
Integer &operator = (int i);
Integer &operator = (std::string s);
bool operator == (const Integer &i);
bool operator == (int i);
std::string toString();
bool isNan();
};
#endif
Integer.cpp
#include
#include
#include
#include "Integer.h"
Integer::Integer()
:intData(0), nan(false)
{
}
Integer::Integer(const Integer &i)
: nan(false)
{
equals(i.intData);
}
Integer::Integer(int i)
: intData(i), nan(false)
{
}
Integer::Integer(const std::string str)
{
this->equals(str);
}
int Integer::toInt() const
{
return this->intData;
}
void Integer::equals(int i)
{
intData = i;
}
void Integer::equals(std::string s)
{
this->isNan(s);
if (this->nan)
this->intData = 0;
else
this->intData = stod(s);
}
Integer Integer::add(const Integer &i)
{
Integer ans(this->intData + i.toInt());
return ans;
}
Integer Integer::sub(const Integer &i)
{
Integer ans(this->intData - i.toInt());
return ans;
}
Integer Integer::mul(const Integer &i)
{
Integer ans(this->intData - i.toInt());
return ans;
}
Integer Integer::div(const Integer &i)
{
Integer ans(this->intData - i.toInt());
return ans;
}
// Primitives
Integer Integer::add(int &i)
{
Integer ans;
ans.equals(this->toInt() + i);
return ans;
}
Integer Integer::sub(int &i)
{
Integer ans;
ans.equals(this->toInt() - i);
return ans;
}
Integer Integer::mul(int &i)
{
Integer ans;
ans.equals(this->toInt() * i);
return ans;
}
Integer Integer::div(int &i)
{
Integer ans;
ans.equals(this->toInt() / i);
return ans;
}
//Operator Overloads
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 = (std::string s)
{
this->equals(s);
return *this;
}
bool Integer::operator==(const Integer & i)
{
return this->intData == i.intData;
}
bool Integer::operator == (int i)
{
return this->intData = i;
}
std::string Integer::toString()
{
std::stringstream ss;
ss << this->intData;
return ss.str();
}
void Integer::isNan(std::string s)
{
int pos;
this->nan = false;
pos = s.find(".", 0);
if (pos != string::npos)
{
pos = s.find(".", +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;
}
bool Integer::isNan()
{
return nan;
}
main.cpp
#include
#include
#include
#include
#include
#include "Double.h"
#include "Integer.h"
#include "Menu.h"
using namespace std;
void intAdd();
void intSub();
void intMul();
void intDiv();
void doubleAdd();
void doubleSub();
void doubleMul();
void doubleDiv();
void Exit();
string toString(int num);
string toString(float num);
int writeToFile(vector
int writeToFile(vector
Menu m;
int main()
{
vector
vector
char data[100];
fstream ifile;
ifile.open("numbers.txt");
if (!ifile)
{
cout << "Cannot open output file. ";
return 1;
}
while (!ifile.eof())
{
ifile.getline(data, 100);
if (strstr(data, "."))
{
dNumbers.push_back(new float(atof(data)));
}
else
{
iNumbers.push_back(new int(atoi(data)));
}
}
writeToFile(iNumbers);
writeToFile(dNumbers);
ifile.close();
system("pause");
cout << "Welcome to the Double and Integer Menu. Select one from the menu: " << endl;
cout << " 1. Add Integer. 2. Subract Integer. 3. Multiply Integer. 4. Divide Integer. 5. Add Double. 6. Subtract Double 7. Multiply Double. 8. Divide Double 9. Exit" << endl;
m.addMenu("1. Add Integer ", intAdd);
m.addMenu("2. Subtract Integer", intSub);
m.addMenu("3. Multiply Integer ", intMul);
m.addMenu("4. Divide Integer ", intDiv);
m.addMenu("5. Add Double ", doubleAdd);
m.addMenu("6. Subtract Double ", doubleSub);
m.addMenu("7. Multiply Double ", doubleMul);
m.addMenu("8. Divide Double ", doubleDiv);
m.addMenu("9. Exit ", Exit);
m.runMenu();
return 0;
}
/******************
Integer Menu Items:
*******************/
void intAdd()
{
int i, i2;
cout << "Enter two Integers: " << endl;
cin >> i >> i2;
Integer *x = new Integer(i);
Integer *y = new Integer(i2);
Integer z = *x + *y;
delete x, y;
cout << "Sum = " << z.toInt() << endl;
m.waitKey();
}
void intSub()
{
int i, i2;
cout << "Enter two Integers: " << endl;
cin >> i >> i2;
Integer *x = new Integer(i);
Integer *y = new Integer(i2);
Integer z = *x - *y;
delete x, y;
cout << "Difference = " << z.toInt() << endl;
m.waitKey();
}
void intMul()
{
int i, i2;
cout << "Enter two Integers: " << endl;
cin >> i >> i2;
Integer *x = new Integer(i);
Integer *y = new Integer(i2);
Integer z = *x * *y;
delete x, y;
cout << "Product = " << z.toInt() << endl;
m.waitKey();
}
void intDiv()
{
int i, i2;
cout << "Enter two Integers: " << endl;
cin >> i >> i2;
Integer *x = new Integer(i);
Integer *y = new Integer(i2);
Integer z = *x / *y;
delete x, y;
cout << "Quotient = " << z.toInt() << endl;
m.waitKey();
}
/*****************
Double Menu Items:
******************/
void doubleAdd()
{
double d, d2;
cout << "Enter two Doubles: " << endl;
cin >> d >> d2;
Double *x = new Double(d);
Double *y = new Double(d2);
Double z = *x + *y;
delete x, y;
cout << "Sum = " << z.toDouble() << endl;
m.waitKey();
}
void doubleSub()
{
double d, d2;
cout << "Enter two Doubles: " << endl;
cin >> d >> d2;
Double *x = new Double(d);
Double *y = new Double(d2);
Double z = *x - *y;
delete x, y;
cout << "Difference = " << z.toDouble() << endl;
m.waitKey();
}
void doubleMul()
{
double d, d2;
cout << "Enter two Doubles: " << endl;
cin >> d >> d2;
Double *x = new Double(d);
Double *y = new Double(d2);
Double z = *x * *y;
delete x, y;
cout << "Product = " << z.toDouble() << endl;
m.waitKey();
}
void doubleDiv()
{
double d, d2;
cout << "Enter two Doubles: " << endl;
cin >> d >> d2;
Double *x = new Double(d);
Double *y = new Double(d2);
Double z = *x / *y;
delete x, y;
cout << "Quotient = " << z.toDouble() << endl;
m.waitKey();
}
void Exit()
{
cout << "Come again! " << endl;
exit(0);
}
/**************
File IO Items:
***************/
string toString(int num)
{
stringstream ss;
ss << num;
return ss.str();
}
string toString(float num)
{
stringstream ss;
ss << num;
return ss.str();
}
int writeToFile(vector
{
ofstream outFile;
outFile.open("double.txt");
if (!outFile)
{
cout << "Cannot open output file. " << endl;
return 1;
}
for (vector
{
outFile << toString(**iter) << endl;
}
outFile.close();
return 0;
}
int writeToFile(vector
{
ofstream outFile2;
outFile2.open("integer.txt");
if (!outFile2)
{
cout << "Cannot open output file. " << endl;
return 1;
}
for (vector
{
outFile2 << toString(**iter) << endl;
}
outFile2.close();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started