In: Computer Science
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code but still having trouble as i am unexpreinced with coding.
#include <iostream>
#include <string>
#include <fstream>
#include "ArgumentManager.h"
// DoubleLinkedList.h
struct Node {
long long num;
Node* prev;
Node* next;
Node(long long n)
{
num = n;
prev = NULL;
next = NULL;
}
};
class DoubleLinkedList {
public:
DoubleLinkedList() {
head = tail = NULL;
size = 0;
}// default construct
~DoubleLinkedList() {
freeMemory();
};
void freeMemory() {
if (!head)
return;
while (head) {
Node *p =
head->next;
delete
head;
head = p;
}
head = tail = NULL;
}// deconstruct
DoubleLinkedList(const std::string& num, int
digitsPerNode)
{ //num: "1231322432123", digitPerNode:3
int c1 = num.length() /
digitsPerNode;
int c2 = num.length() %
digitsPerNode;
// list: [12],[345],[678]
if (c2) {
string str1 =
num.substring(0, c2); //error with .substring aswell
Node* node = new
Node(stoi(str1));
Append(node);
}
for (int i = 0; i < c1; i++)
{
string str1 =
num.substring(c2+i*digitsPerNode, digitsPerNode);
Node* node = new
Node(stoi(str1));
Append(node);
}
}; // user defined construct
DoubleLinkedList(int digitsPerNode);
DoubleLinkedList(const DoubleLinkedList&
list)
{
}; // copy construct
DoubleLinkedList& operator = (const
DoubleLinkedList& list)
{
if (this != &list)
{// copy constructor?
}
return *this;
}; // assignment consturct
public:
//mandatory
DoubleLinkedList& operator + (const
DoubleLinkedList& list) const
{
};
DoubleLinkedList operator * (const
DoubleLinkedList& list) const {
};
// optional
DoubleLinkedList operator - (const
DoubleLinkedList& list) const {
};
// 10% extra
DoubleLinkedList operator / (const
DoubleLinkedList& list) const {
};
// 20% extra
DoubleLinkedList Sqrt(const DoubleLinkedList&
list) const {
};
public:
const Node* GetHead() const
{
};
const Node* GetTail() const
{
};
void Append(Node* node) // appead the tail
{
size++;
if (!tail)
{
head = tail =
node;
return;
}
tail->next = node;
node->prev = tail;
tail = node;
}
void Add(Node *node) {
size++;
if (!head) {
head = tail =
node;
return;
}
node->next = head;
head->prev = node;
head = node;
}; // add before head
void Print() const {
// list [1] [2] [3[; // 1 002
003
};
void toString(int digitsPerNode)
{
string res = "";
Node *cur = head;
if (!cur)
return res;
//head: no leading "0"
while (cur) {
int numstr =
to_string(cur->num); // res, numstr, and to_string are
errors
int l =
numstr.length();
for (int i = 0;
i < digitsPerNode - 1; i++)
{
res += "0";
// "00"
res += numstr;
// "002"
cur = cur->next;
}
return
res;
}
}
private:
int size;
Node* head;
Node* tail;
int m_digitsPerNode;
long long remainder; // for / operator
float decimal; // for sqrt() 7 valid digits.
}
// main.cpp
#include "ArgumentManager.h"
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "Usage:
infinitearithmetic
\"input=xyz.txt;digitsPerNode=<number>\"\n";
}
ArgumentManager am(argc, argv);
std::string filename = am.get("input");
int digitsPerNode =
std::stoi(am.get("digitsPerNode"));
std::ifstream ifs(filename.c_str());
std::string line;
while (getline(ifs, line)) {
std::string num1;
std::string num2;
std::string op;
// get num1 num2 and operator in
line
// ...
DoubleLinkedList l1(num1,
digitsPerNode); // DoubleLinkedList(const std::string& num, int
digitsPerNode)
DoubleLinkedList l2(num2,
digitsPerNode);
DoubleLinkedList l; //
DoubleLinkedList();
if (/* plus */) //not sure what to
enter says you need bool
{
l = l1 +
l2;
} // DoubleLinkedList operator +
(const DoubleLinkedList& list) const; DoubleLinkedList&
operator = (const DoubleLinkedList& list);
else if (/* mult */) //
DoubleLinkedList operator * (const DoubleLinkedList& list)
const;
{
l = l1 *
l2;
}
else if (/* div */)
{
l = l1 /
l2;
} // DoubleLinkedList operator /
(const DoubleLinkedList& list) const;
else if (/*subtract*/)
{//...
}
else if (/* squareroot */)
{
// ...
}
// output result
}
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include "ArgumentManager.h"
// DoubleLinkedList.h
struct Node {
long long num;
Node* prev;
Node* next;
Node(long long n)
{
num = n;
prev = NULL;
next = NULL;
}
};
class DoubleLinkedList {
public:
DoubleLinkedList() {
head = tail = NULL;
size = 0;
}// default construct
~DoubleLinkedList() {
freeMemory();
};
void freeMemory() {
if (!head)
return;
while (head) {
Node *p =
head->next;
delete
head;
head = p;
}
head = tail = NULL;
}// deconstruct
DoubleLinkedList(const std::string& num, int
digitsPerNode)
{ //num: "1231322432123", digitPerNode:3
int c1 = num.length() /
digitsPerNode;
int c2 = num.length() %
digitsPerNode;
// list: [12],[345],[678]
if (c2) {
string str1 =
num.substring(0, c2); //error with .substring aswell
Node* node = new
Node(stoi(str1));
Append(node);
}
for (int i = 0; i < c1; i++)
{
string str1 =
num.substring(c2+i*digitsPerNode, digitsPerNode);
Node* node = new
Node(stoi(str1));
Append(node);
}
}; // user defined construct
DoubleLinkedList(int digitsPerNode);
DoubleLinkedList(const DoubleLinkedList&
list)
{
}; // copy construct
DoubleLinkedList& operator = (const
DoubleLinkedList& list)
{
if (this != &list)
{// copy constructor?
}
return *this;
}; // assignment consturct
public:
//mandatory
DoubleLinkedList& operator + (const
DoubleLinkedList& list) const
{
};
DoubleLinkedList operator * (const
DoubleLinkedList& list) const {
};
// optional
DoubleLinkedList operator - (const
DoubleLinkedList& list) const {
};
// 10% extra
DoubleLinkedList operator / (const
DoubleLinkedList& list) const {
};
// 20% extra
DoubleLinkedList Sqrt(const DoubleLinkedList&
list) const {
};
public:
const Node* GetHead() const
{
};
const Node* GetTail() const
{
};
void Append(Node* node) // appead the tail
{
size++;
if (!tail)
{
head = tail =
node;
return;
}
tail->next = node;
node->prev = tail;
tail = node;
}
void Add(Node *node) {
size++;
if (!head) {
head = tail =
node;
return;
}
node->next = head;
head->prev = node;
head = node;
}; // add before head
void Print() const {
// list [1] [2] [3[; // 1 002
003
};
void toString(int digitsPerNode)
{
string res = "";
Node *cur = head;
if (!cur)
return res;
//head: no leading "0"
while (cur) {
int numstr =
to_string(cur->num); // res, numstr, and to_string are
errors
int l =
numstr.length();
for (int i = 0;
i < digitsPerNode - 1; i++)
{
res += "0";
// "00"
res += numstr;
// "002"
cur = cur->next;
}
return
res;
}
}
private:
int size;
Node* head;
Node* tail;
int m_digitsPerNode;
long long remainder; // for / operator
float decimal; // for sqrt() 7 valid digits.
}
// main.cpp
#include "ArgumentManager.h"
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "Usage:
infinitearithmetic
\"input=xyz.txt;digitsPerNode=<number>\"\n";
}
ArgumentManager am(argc, argv);
std::string filename = am.get("input");
int digitsPerNode =
std::stoi(am.get("digitsPerNode"));
std::ifstream ifs(filename.c_str());
std::string line;
while (getline(ifs, line)) {
std::string num1;
std::string num2;
std::string op;
// get num1 num2 and operator in
line
// ...
DoubleLinkedList l1(num1,
digitsPerNode); // DoubleLinkedList(const std::string& num, int
digitsPerNode)
DoubleLinkedList l2(num2,
digitsPerNode);
DoubleLinkedList l; //
DoubleLinkedList();
if (/* plus */) //not sure what to
enter says you need bool
{
l = l1 +
l2;
} // DoubleLinkedList operator +
(const DoubleLinkedList& list) const; DoubleLinkedList&
operator = (const DoubleLinkedList& list);
else if (/* mult */) //
DoubleLinkedList operator * (const DoubleLinkedList& list)
const;
{
l = l1 *
l2;
}
else if (/* div */)
{
l = l1 /
l2;
} // DoubleLinkedList operator /
(const DoubleLinkedList& list) const;
else if (/*subtract*/)
{//...
}
else if (/* squareroot */)
{
// ...
}
// output result
}
return 0;
}