Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to implement a class (another header file and source file along with

ID: 3704867 • Letter: I

Question

I'm trying to implement a class (another header file and source file along with this named "Node.h" and "Node.cpp") and have it still work the same. I would greatly appreciate your help in doing so.

Below is the main code and what the input and output should look like.

In C++:

#include <iostream>

#include <math.h>

#include <string>

#include <cstdlib>

#include <sstream>

#include <fstream>

using namespace std;

struct Node

{

float cft;

int expn;

struct Node *next;

};

Node* insert(Node* root, double cft, int expn)

{

Node *newNode = new Node;

newNode->cft = cft;

newNode->expn = expn;

newNode->next = root;

return newNode;

}

int main()

{

ifstream infile("poly.txt");

ofstream outfile("answers.txt");

string in;

while(getline(infile, in))

{

int st = in.find_first_of('(');

int end = in.find_first_of(')');

double a = atof(in.substr(st + 1, end - st - 1).c_str());

int st_exp = in.find_first_of('=') + 2;

istringstream expr(in.substr(st_exp));

string tok;

getline(expr, tok, 'x');

Node *root = NULL;

double cft = atof(tok.compare("") ? tok.c_str() : "1");

getline(expr, tok, '^');

getline(expr, tok, ' ');

int expn = atoi(tok.c_str());

root = insert(root, cft, expn);

while(getline(expr, tok, ' '))

{

int sign = 1;

if(tok.compare("-") == 0)

{

sign = -1;

}

getline(expr, tok, 'x');

cft = atof(tok.compare("") ? tok.c_str() : "1")*sign;

getline(expr, tok, '^');

expn = getline(expr, tok, ' ') ? atoi(tok.c_str()) : 0;

root = insert(root, cft, expn);

}

double ans = 0;

while(root != NULL)

{

ans += root->cft * pow(a, root->expn);

root = root->next;

}

outfile << fixed;

outfile.precision(3);

outfile << in << " = " << ans << ' ' << endl;

}

return 0;

}

Sample Input File f (3.2)5.9x11 x*3 -3x10 f (2.75) 1.57x-47-2x^30 + x = Sample Output File f(5) = 7x^2-9 = 166 f (3.2)5.9x11 x*3 - 3x101787961.820 f (1.75) = 1.57x-47-2x^30 + x 415572768585.904

Explanation / Answer

Hi Dear,

Please find my implementation using separate files:

#### Node.h #######


struct Node

{

float cft;

int expn;

struct Node *next;

};
// function prototype
Node* insert(Node* root, double cft, int expn);

######## Node.cpp ##########


#include "Node.h"
#include <cstdlib>


Node* insert(Node* root, double cft, int expn)

{

Node *newNode = new Node;

newNode->cft = cft;

newNode->expn = expn;

newNode->next = root;

return newNode;

}

########### expression.cpp #########

#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
#include <fstream>
#include "Node.h"

using namespace std;

int main()

{

ifstream infile("poly.txt");

ofstream outfile("answers.txt");

string in;

while(getline(infile, in))

{

int st = in.find_first_of('(');

int end = in.find_first_of(')');

double a = atof(in.substr(st + 1, end - st - 1).c_str());

int st_exp = in.find_first_of('=') + 2;

istringstream expr(in.substr(st_exp));

string tok;

getline(expr, tok, 'x');

Node *root = NULL;

double cft = atof(tok.compare("") ? tok.c_str() : "1");

getline(expr, tok, '^');

getline(expr, tok, ' ');

int expn = atoi(tok.c_str());

root = insert(root, cft, expn);

while(getline(expr, tok, ' '))

{

int sign = 1;

if(tok.compare("-") == 0)

{

sign = -1;

}

getline(expr, tok, 'x');

cft = atof(tok.compare("") ? tok.c_str() : "1")*sign;

getline(expr, tok, '^');

expn = getline(expr, tok, ' ') ? atoi(tok.c_str()) : 0;

root = insert(root, cft, expn);

}

double ans = 0;

while(root != NULL)

{

ans += root->cft * pow(a, root->expn);

root = root->next;

}

outfile << fixed;

outfile.precision(3);

outfile << in << " = " << ans << ' ' << endl;

}

return 0;

}

Compile uisng command: g++ expression.cpp Node.cp

Please DONT forgot to rate my answer. We are working hard for you Guys!!