Question

In: Computer Science

I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================...

I need the following code completed:

#include <iostream>
#include <time.h>

#include "CSVparser.hpp"

using namespace std;

//============================================================================
// Global definitions visible to all methods and classes
//============================================================================

// forward declarations
double strToDouble(string str, char ch);

// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};

// FIXME (1): Internal structure for tree node
struct Node {
   Bid data; Node *left; Node *right;
};

//============================================================================
// Binary Search Tree class definition
//============================================================================

/**
* Define a class containing data members and methods to
* implement a binary search tree
*/
class BinarySearchTree {

private:
Node* root;

void addNode(Node* node, Bid bid);
void inOrder(Node* node);
Node* removeNode(Node* node, string bidId);

public:
BinarySearchTree();
virtual ~BinarySearchTree();
void InOrder();
void Insert(Bid bid);
void Remove(string bidId);
Bid Search(string bidId);
};

/**
* Default constructor
*/
BinarySearchTree::BinarySearchTree() {
// initialize housekeeping variables
}

/**
* Destructor
*/
BinarySearchTree::~BinarySearchTree() {
// recurse from root deleting every node
}

/**
* Traverse the tree in order
*/
void BinarySearchTree::InOrder() {
}
/**
* Insert a bid
*/
void BinarySearchTree::Insert(Bid bid) {
   Node p = Insert(root, bid);
   p->data.bidId = bid.bidId;
   p->data.title = bid.title;
   p->data.fund = bid.fund;
   p->data.amount = bid.amount;
}

/**
* Remove a bid
*/
void BinarySearchTree::Remove(string bidId) {
   deleteNode(root,bidid);
}

/**
* Search for a bid
*/
Bid BinarySearchTree::Search(string bidId) {
// FIXME (3) Implement searching the tree for a bid

   Bid bid;
   if (root == NULL || root->data.bidId == bidId) {
   bid.bidId = bidId;
   bid.title = root->data.title;
   bid.fund = root->data.fund;
   bid.amount = root->data.amount;
   return bid;
   }

   if (root->data.bidId < bidId)
   return search(root->right, bidId);

   return search(root->left, bidId);
return bid;
}

/**
* Add a bid to some node (recursive)
*
* @param node Current node in tree
* @param bid Bid to be added
*/
void BinarySearchTree::addNode(Node* node, Bid bid) {
// FIXME (2b) Implement inserting a bid into the tree
   Node p = insertNode(node, bid);
   p->data.bidId = bid.bidId;
   p->data.title = bid.title;
   p->data.fund = bid.fund;
   p->data.amount = bid.amount;
}
void BinarySearchTree::inOrder(Node* node) {
}
//============================================================================
// Static methods used for testing
//============================================================================

/**
* Display the bid information to the console (std::out)
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " << bid.title << " | " << bid.amount << " | "
<< bid.fund << endl;
return;
}

/**
* Load a CSV file containing bids into a container
*
* @param csvPath the path to the CSV file to load
* @return a container holding all the bids read
*/
void loadBids(string csvPath, BinarySearchTree* bst) {
cout << "Loading CSV file " << csvPath << endl;

// initialize the CSV Parser using the given path
csv::Parser file = csv::Parser(csvPath);

// read and display header row - optional
vector<string> header = file.getHeader();
for (auto const& c : header) {
cout << c << " | ";
}
cout << "" << endl;

try {
// loop to read rows of a CSV file
for (unsigned int i = 0; i < file.rowCount(); i++) {

// Create a data structure and add to the collection of bids
Bid bid;
bid.bidId = file[i][1];
bid.title = file[i][0];
bid.fund = file[i][8];
bid.amount = strToDouble(file[i][4], '$');

//cout << "Item: " << bid.title << ", Fund: " << bid.fund << ", Amount: " << bid.amount << endl;

// push this bid to the end
bst->Insert(bid);
}
} catch (csv::Error &e) {
std::cerr << e.what() << std::endl;
}
}

/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}

/**
* The one and only main() method
*/
int main(int argc, char* argv[]) {

// process command line arguments
string csvPath, bidKey;
switch (argc) {
case 2:
csvPath = argv[1];
bidKey = "98109";
break;
case 3:
csvPath = argv[1];
bidKey = argv[2];
break;
default:
csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
bidKey = "98109";
}

// Define a timer variable
clock_t ticks;

// Define a binary search tree to hold all bids
BinarySearchTree* bst;

Bid bid;

int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << " 1. Load Bids" << endl;
cout << " 2. Display All Bids" << endl;
cout << " 3. Find Bid" << endl;
cout << " 4. Remove Bid" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;

switch (choice) {

case 1:
bst = new BinarySearchTree();

// Initialize a timer variable before loading bids
ticks = clock();

// Complete the method call to load the bids
loadBids(csvPath, bst);

//cout << bst->Size() << " bids read" << endl;

// Calculate elapsed time and display result
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;

case 2:
bst->InOrder();
break;

case 3:
ticks = clock();

bid = bst->Search(bidKey);

ticks = clock() - ticks; // current clock ticks minus starting clock ticks

if (!bid.bidId.empty()) {
displayBid(bid);
} else {
   cout << "Bid Id " << bidKey << " not found." << endl;
}

cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;

break;

case 4:
bst->Remove(bidKey);
break;
}
}

cout << "Good bye." << endl;

   return 0;
}

Solutions

Expert Solution

Code:-

main.cpp

#include <algorithm>
#include <iostream>
#include <time.h>
#include <string>

#include "CSVparser.hpp"

using namespace std;

//============================================================================
// Global definitions visible to all methods and classes
//============================================================================

// forward declarations
double strToDouble(string str, char ch);

// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};

// FIXME (1): Internal structure for tree node
struct Node {
Bid bid;
Node* parentPtr;
Node* leftPtr;
Node* rightPtr;

};

//============================================================================
// Binary Search Tree class definition
//============================================================================

/**
* Define a class containing data members and methods to
* implement a binary search tree
*/
class BinarySearchTree {

private:
Node* root;

void addNode(Node* node, Bid bid);
void inOrder(Node* node);
Node* removeNode(Node* node, string bidId);

public:
BinarySearchTree();
virtual ~BinarySearchTree();
void InOrder();
void Insert(Bid bid);
void Remove(string bidId);
Bid Search(string bidId);
Node* SearchNode(Node* node, string bidId);
};

/**
* Default constructor
*/
BinarySearchTree::BinarySearchTree() {
// initialize housekeeping variables
root = nullptr;
}

/**
* Destructor
*/
BinarySearchTree::~BinarySearchTree() {
// recurse from root deleting every node
}

/**
* Traverse the tree in order
*/
void BinarySearchTree::InOrder() {
}
/**
* Insert a bid
*/
void BinarySearchTree::Insert(Bid bid) {

if (root == nullptr) {
root = new Node;
root->bid = bid;
root->leftPtr = nullptr;
root->rightPtr = nullptr;
root->parentPtr = nullptr;
} else {
addNode(root, bid);
}

}

/**
* Remove a bid
*/
void BinarySearchTree::Remove(string bidId) {
// FIXME (4a) Implement removing a bid from the tree
Node* nodePtr = SearchNode(root, bidId);
if (nodePtr == nullptr) {
return;
} else {
nodePtr->parentPtr->leftPtr = nodePtr->leftPtr;
nodePtr->parentPtr->rightPtr = nodePtr->rightPtr;
nodePtr->leftPtr->parentPtr = nodePtr->parentPtr;
nodePtr->rightPtr->parentPtr = nodePtr->parentPtr;
delete nodePtr;
}
}

/**
* Search for a bid
*/
Bid BinarySearchTree::Search(string bidId) {
// FIXME (3) Implement searching the tree for a bid
Node* nodePtr = SearchNode(root, bidId);
if (nodePtr == nullptr) {
Bid bid;
return bid;
} else {
return nodePtr->bid;
}
Bid bid;
return bid;
}

/**
* New function to search recursively for bidId/key within search tree
*/

Node* BinarySearchTree::SearchNode (Node* node, string bidId) {
if (node == nullptr) {
return nullptr;
} else {
if (node->bid.bidId == bidId) {
return node;
} else {
int curKey = atoi(node->bid.bidId.c_str());
int key = atoi(bidId.c_str());
if (key > curKey) {
return SearchNode(node->rightPtr, bidId);
} else {
return SearchNode(node->leftPtr, bidId);
}
}
}
return nullptr;
}

/**
* Add a bid to some node (recursive)
*
* @param node Current node in tree
* @param bid Bid to be added
*/
void BinarySearchTree::addNode(Node* node, Bid bid) {
// FIXME (2b) Implement inserting a bid into the tree
int curKey = atoi(node->bid.bidId.c_str());
int key = atoi(bid.bidId.c_str());

cout << "Current key is: " << curKey << endl;
cout << "Search key is: " << key << endl;

if (key > curKey) {
if (node->rightPtr == nullptr) {
node->rightPtr = new Node;
node->rightPtr->bid = bid;
node->rightPtr->rightPtr = nullptr;
node->rightPtr->leftPtr = nullptr;
node->rightPtr->parentPtr = node;
} else {
addNode(node->rightPtr, bid);
}
} else {
if (node->leftPtr == nullptr) {
node->leftPtr = new Node;
node->leftPtr->bid = bid;
node->leftPtr->rightPtr = nullptr;
node->leftPtr->leftPtr = nullptr;
node->leftPtr->parentPtr = node;

cout << "leftPtr is nullptr" << endl;
} else {

cout << "leftPtr is NOT nullptr" << endl;
addNode(node->leftPtr, bid);
}
}
}
void BinarySearchTree::inOrder(Node* node) {
}
//============================================================================
// Static methods used for testing
//============================================================================

/**
* Display the bid information to the console (std::out)
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " << bid.title << " | " << bid.amount << " | "
<< bid.fund << endl;
return;
}

/**
* Load a CSV file containing bids into a container
*
* @param csvPath the path to the CSV file to load
* @return a container holding all the bids read
*/
void loadBids(string csvPath, BinarySearchTree* bst) {
cout << "Loading CSV file " << csvPath << endl;

// initialize the CSV Parser using the given path
csv::Parser file = csv::Parser(csvPath);

// read and display header row - optional
vector<string> header = file.getHeader();
for (auto const& c : header) {
cout << c << " | ";
}
cout << "" << endl;

try {
// loop to read rows of a CSV file
for (unsigned int i = 0; i < file.rowCount(); i++) {

// Create a data structure and add to the collection of bids
Bid bid;
bid.bidId = file[i][1];
bid.title = file[i][0];
bid.fund = file[i][8];
bid.amount = strToDouble(file[i][4], '$');

//cout << "Item: " << bid.title << ", Fund: " << bid.fund << ", Amount: " << bid.amount << endl;

// push this bid to the end
bst->Insert(bid);
}
} catch (csv::Error &e) {
std::cerr << e.what() << std::endl;
}
}

double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}


int main(int argc, char* argv[]) {

// process command line arguments
string csvPath, bidKey;
switch (argc) {
case 2:
csvPath = argv[1];
bidKey = "98105";
break;
case 3:
csvPath = argv[1];
bidKey = argv[2];
break;
default:
csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
bidKey = "98105";
}

// Define a timer variable
clock_t ticks;

// Define a binary search tree to hold all bids
BinarySearchTree* bst;

Bid bid;

int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << " 1. Load Bids" << endl;
cout << " 2. Display All Bids" << endl;
cout << " 3. Find Bid" << endl;
cout << " 4. Remove Bid" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;

switch (choice) {

case 1:
bst = new BinarySearchTree();

// Initialize a timer variable before loading bids
ticks = clock();

// Complete the method call to load the bids
loadBids(csvPath, bst);

//cout << bst->Size() << " bids read" << endl;

// Calculate elapsed time and display result
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;

case 2:
bst->InOrder();
break;

case 3:
ticks = clock();

bid = bst->Search(bidKey);

ticks = clock() - ticks; // current clock ticks minus starting clock ticks

if (!bid.bidId.empty()) {
displayBid(bid);
} else {
cout << "Bid Id " << bidKey << " not found." << endl;
}

cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;

break;

case 4:
bst->Remove(bidKey);
break;
}
}

cout << "Good bye." << endl;

return 0;
}

P.s - save the main.cpp , CSVparser.hpp and eBid_Monthly_Sales_Dec_2016.csv in same folder or it will give errors.

if u like my answer then please give a thums up..!!


Related Solutions

Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void...
Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void magic (int &a, int b, int& c) {   a *= 2;    b = b+2;    c = c-2; } int main () { int x=1, y=3, z=7;    magic (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z;    magic (z, y, x); cout << "x=" << x << ", y=" << y <<...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT