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

Write a C++ program to implement a menu-driven program for managing a software s

ID: 3888130 • Letter: W

Question

Write a C++ program to implement a menu-driven program for managing a software store using binary search trees. When the program starts, all information about the software packages should be stored in a file named software. The information includes the code, name, version, and price of each software package. Each field will be on a separate line.

When the program starts, it should automatically create a binary search tree with one node corresponding to one software package, and it should include the code of the package. The program should allow the file and the tree to be updated when a new software package arrives to the store and when some packages are sold. Consider the following:

New packages should be inserted to the tree and at the end of the file.

Sold packages should be deleted from the file and from the tree.

Test your program using the following example:

Start with the following content in the file software:

12

Photoshop

CS5

500

22

Office

2010

150

34

Visual Studio

2010

600

Add the following item to the file and tree:

15

Norton Security

2014

50

Delete software package with code (2) from the file and the tree.

Explanation / Answer

Given below is the C++ code and output for the question. Hope it helps. If it did, please don't forget to rate the answer. Thank you very much.

BST.h

#ifndef BST_h
#define BST_h
#include <iostream>
using std::string;
using std::ostream;

struct Node
{
//date in the node - details of a software
int code;
string name;
string version;
double price;
//left and right child of the node
struct Node* left;
struct Node* right;
};


class BST
{
private:
Node* root;
void deleteNode(Node* n); //used by destructor to delete all nodes recursively in post order
bool deleteNode(Node* n, Node* parent); //delete node from the parent
void findNode(int code, Node* &n, Node* &parent); //finds a node in the BST for hte given code
void printInOrder(ostream& out, Node* root);
public:
BST();
~BST();
void addSoftware(int code, string name, string version, double price); //add to BST with code as the key
bool removeSoftware(int code);
void print(ostream& out);//to print the BST
  
};

#endif /* BST_h */

BST.cpp


#include "BST.h"
using namespace std;
BST::BST()
{
root = NULL;
}
BST::~BST()
{
deleteNode(root);
}

//add to BST with code as the key
void BST::addSoftware(int code, string name, string version, double price)
{
Node* n = new Node();
n->code = code;
n->name = name;
n->version = version;
n->price = price;
n->left = NULL;
n->right = NULL;

if(root == NULL) //empty tree
root = n;
else
{
Node* curr = root;
while(curr != NULL)
{
if(code < curr->code) //go to left subtree
{
if(curr->left == NULL) //no left subtreee, so add as left child
{
curr->left = n;
return;
}
else
curr = curr->left;
}
else
{
if(curr->right == NULL)
{
curr->right = n;
return;
}
else
curr = curr->right;
}
}
}
}
bool BST::removeSoftware(int code)
{
Node* n = NULL;
Node* parent = NULL;
findNode(code, n, parent);

if(n == NULL) //software with code not found
return false;
else
{
return deleteNode(n, parent);
}
}

bool BST::deleteNode(Node *n, Node* parent)
{

if (n == NULL)
return false;

//node to be deleted has no children, i.e. its a leaf
if(n->left == NULL && n->right == NULL)
{
if(n == root) //deleting root?
root = NULL;
else
{
if(n == parent->left) //deleting the left child of parent?
parent->left = NULL;
else
parent->right = NULL;
}
delete n;
}
//node has only left child and no right child
else if(n->left != NULL && n->right == NULL)
{
if(n == root)
{
root = n->left;
}
else
{
//set the only child of n as correct child of parent depending on n is left / right child
if(n == parent->left) //n is left child of its parent
parent->left = n->left;
else
parent->right = n->left;
}
delete n;
}
//node has only right child and no left child
else if(n->left == NULL && n->right != NULL)
{
if(n == root)
{
root = n->right;
}
else
{
//set the only child of n as correct child of parent depending on n is left / right child
if(n == parent->left) //n is left child of its parent
parent->left = n->right;
else
parent->right = n->right;
}

delete n;
}
//n has both children
else
{
//find the successor of n in the right subtree
Node* successor = n->right;
Node* successorParent = n;
while(successor->left != NULL)
{
successorParent = successor;
successor = successor->left;
}

Node temp;
temp = *successor; //save a copy of successor

//remove the successsor from treee
deleteNode(successor, successorParent);

//now copy the values of successor into the node n
n->code = temp.code;
n->name = temp.name;
n->version = temp.version;
n->price = temp.price;

}
return true;
}

//to print the BST to the specified output stream
//can be used to print to file or to cout
void BST::print(ostream& out)
{
printInOrder(out, root);
}

void BST::printInOrder(ostream& out, Node* n)
{
if(n == NULL)
return;

printInOrder(out, n->left);

out << n->code << endl;
out << n->name << endl;
out << n->version << endl;
out << n->price << endl;

printInOrder(out, n->right);
}

void BST::deleteNode(Node* n) //used by destructor to delete all nodes recursively in post order
{
if(n == NULL)
return;
deleteNode(n->left);
deleteNode(n->right);
delete n;
}

//finds a node in the BST for hte given code. The node and its parent are returned in the reference variables
void BST::findNode(int code, Node* &n, Node* &parent)
{
n = root;
parent = NULL;
while(n != NULL)
{
  
if(code == n->code)
break;
parent = n;
if(code < n->code)
n = n->left;
else
n = n->right;
}

if(n == NULL) //required node not found
parent = NULL;
}

Main.cpp


#include <iostream>
#include <fstream>
#include "BST.h"
using namespace std;
void loadFromFile(BST& tree, string filename); //loads bst from file
void addMenu(BST& tree); //funtion to get software details and add to bst
void deleteMenu(BST& tree); //function to remove a software from bst

int main()
{
int choice = 0;
string filename = "software";
BST softwares;
  
loadFromFile(softwares, filename);
  
while(choice != 4 )
{
cout << "1. Add Software" << endl;
cout << "2. Delete Software" << endl;
cout << "3. Display all" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
addMenu(softwares);
break;
case 2:
deleteMenu(softwares);
break;
case 3:
softwares.print(cout);
break;
case 4:
break;
default:
cout << "Invalid choice!" << endl;
}
}
  
//save to file before ending the program
ofstream outfile(filename.c_str());
softwares.print(outfile);
outfile.close();
  
cout << "Updated file " << filename << endl;
}

void loadFromFile(BST& tree, string filename)
{
ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout << "Error opening file " << filename;
cout << " No data loaded into BST " << endl;
return;
}
  
int code;
double price;
string name, version;
  
while(infile >> code)
{
infile.ignore(); //flush hte newline
getline(infile, name);
getline(infile, version);
infile >> price;
tree.addSoftware(code, name, version, price);
}
  
infile.close();
}

void addMenu(BST& tree)
{
int code;
double price;
string name, version;
  
cout << "Enter code: ";
cin >> code;
cin.ignore();
  
cout << "Enter name: ";
getline(cin, name);
  
cout << "Enter version: ";
getline(cin, version);

cout << "Enter price: ";
cin >> price;
  
tree.addSoftware(code, name, version, price);
cout << "Added software with code " << code << endl << endl;
}

void deleteMenu(BST& tree)
{
int code;
  
cout << "Enter code of software to delete: ";
cin >> code;
if(tree.removeSoftware(code))
cout << "Deleted sofware with code " << code << endl << endl;
else
cout << "Not found sofware with code " << code << endl << endl;
}

Input file: software

12
Photoshop
CS5
500
22
Office
2010
150
34
Visual Studio
2010
600

output

$ g++ BST.cpp Main.cpp
$ ./a.out
1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 3
12
Photoshop
CS5
500
22
Office
2010
150
34
Visual Studio
2010
600
1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 1
Enter code: 15
Enter name: Norton Security
Enter version: 2014
Enter price: 50
Added software with code 15

1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 3
12
Photoshop
CS5
500
15
Norton Security
2014
50
22
Office
2010
150
34
Visual Studio
2010
600
1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 2
Enter code of software to delete: 2
Not found sofware with code 2

1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 2
Enter code of software to delete: 12
Deleted sofware with code 12

1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 3
15
Norton Security
2014
50
22
Office
2010
150
34
Visual Studio
2010
600
1. Add Software
2. Delete Software
3. Display all
4. Exit
Enter your choice: 4
Updated file software

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote