Question
Basically, I need it to take the provided text file, and then export the numbers into their respective double.txt and integer.txts. It also needs to
Basically, I need it to take the provided text file, and then export the numbers into their respective double.txt and integer.txts. It also needs to use the Double and Integer classes which are linked below.
The attached Text file (http://programming.msjc.edu/cppii/labs/labsb/lab09/Numbers.txt.)Links to an external site. contains both whole and floating point numbers. Of course they are in text format and not binary. Your task is to read and parse the numbers and separate them into two text files double.txt and integer.txt.
Specifics
Open the text file and read its contents one line at a time. Determine if the line read from the file is a double or an integer. You are to place the integers in a vector called iNumbers and the doubles in a vector called dNumbers. The vector iNumbers should hold pointers to the Integer class and dNumbers should hold pointers to the Double class. When you add a value to one of the vectors you should use new and call the constructor that takes a string. For example:
iNumbers.push_back(new Integer("12.23"));
Once you have the file parsed create an overloaded writeNumbers function. One of these functions should take a vector of pointers to the Integer class, the other should take a vector of pointers to the Double class. These functions should write the contents of the vector to the appropriate text file. When you are finished you should have two files one that holds int values, the other that holds double values.
Note:
You created a toString function and added to your classes. You should use this function to write the string to the text file.
Required
You must have your name, ID, Date, Assignment number, and a brief description of what the file is in comments at the top of each page. Failure to do this will result in 10 points being deducted off the top of your grade.
Double header
#ifndef DOUBLE
#define DOUBLE
#include "Integer.h"
#include
#include
namespace Norman
{
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
Double.cpp
#include
#include
#include
#include "Double.h"
#include "Integer.h"
namespace Norman
{
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
}
//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;
}
}
#include
#include
#include
#include "Double.h"
#include "Integer.h"
namespace Norman
{
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
}
//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.h
#ifndef INTEGER
#define INTEGER
#include
#include
namespace Norman
{
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
Integer.cpp
#include
#include
#include
#include "Integer.h"
namespace Norman
{
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;
}
}
}
}
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