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

Urgent help needed with C++ program ! Thanx http://www2.cs.uidaho.edu/~bruceb/cs

ID: 3865712 • Letter: U

Question

Urgent help needed with C++ program ! Thanx

http://www2.cs.uidaho.edu/~bruceb/cs120/Assignments/states2000.dat

Objectives Implement a program that uses dynamic memory and uses an insertion sort to add items to a singly linked list. Modify the sorted listed by deleting elements as specified below. Program Description Create a sorted list (by state) of nodes containing all the state information (state name, capital, population). Display your list. Remove states whose population is less than four (4) million. Display the resulting list after the states have been removed from the list. Create a sorted list (by population) of nodes containing all the state information. Display your list. Remove states whose population is greater than 10 million. Display the resulting list after the states have been removed from the list. Data file - do not modify (estimated state populations as of 2000): http: //www2.cs.uidaho.edu/ bruceb/cs120/Assignments/states2000.dat Deliverables A complete functional program with output. A program design sheet. Describe all functions necessary to implement your program. Programming Log: Record the time required to design and implement your program. Record of things you encountered/learned while implementing your program.

Explanation / Answer

//main.cpp

#include <iostream>

using namespace std;

#include <fstream>

#include <string>

#include <cmath>

#include "tree.h"

#include "functions.h"

int main()

{

ifstream fin;

Tree tree1, tree2, tree3;

string state = "", capital = "", spopulation;

int ipopulation;

  

cout << "Opening states2000.dat... ";

fin.open("states2000.dat");

if(fin.fail())

{

cerr << "ERROR: could not open 'states2000.dat'. Exiting... ";

exit(1);

}

  

//////////////////////////////////////

//1. Add to tree by State order (lexographically)

//////////////////////////////////////

  

//filter out header...

string goodHeader =

" State Capital Population ";

string stemp = "";

do

{

getline(fin, stemp);

}

while(stemp != goodHeader);

//and following white spaces.

do

{

fin >> state;

}

while(state == "");

//state is now loaded with the first state name

fin >> capital;

fin >> spopulation;   

ipopulation = strtoi(spopulation);

cout << " Inserting ---> " << state << " " << capital

<< " " << ipopulation

<< ' '

;   

InsertStateOrder(tree1, state, capital, ipopulation);

  

fin >> state;

while(state != "Total")

{

fin >> stemp;

if(stemp == "Dakota"

|| stemp == "Carolina"

|| stemp == "Hampshire"

|| stemp == "Virginia"

|| stemp == "Jersey"

|| stemp == "York"

|| stemp == "Island"

|| stemp == "Mexico")

{

state += stemp;

fin >> stemp;

}

if(stemp == "Little"

|| stemp == "Baton"

|| stemp == "Des"

|| stemp == "Oklahoma"

|| stemp == "Carson"

|| stemp == "Santa"

|| stemp == "St.")

{

fin >> capital;

stemp += capital;

capital = stemp;

}

if(stemp == "Salt")

{

fin >> capital;

stemp += capital;

fin >> capital;

stemp += capital;

capital = stemp;   

}

else capital = stemp;

fin >> spopulation;

ipopulation = strtoi(spopulation);   

cout << "Inserting ---> " << state << " " << capital

<< " " << ipopulation

<< ' '

;

InsertStateOrder(tree1, state, capital, ipopulation);   

fin >> state;

}

  

cout << "Printing Tree1: ";

PrintTreeStateOrder(tree1);

wait(2);

  

//////////////////////////////////////

//2. Add to tree by Capital order (lexographically)

//////////////////////////////////////

  

//Resetting the file reading position...

fin.clear();

fin.seekg (0, ios::beg);

  

//filter out header...

stemp = "";

do

{

getline(fin, stemp);

}

while(stemp != goodHeader);

//and following white spaces.

do

{

fin >> state;

}

while(state == "");

//state is now loaded with the first state name

fin >> capital;

fin >> spopulation;   

ipopulation = strtoi(spopulation);

cout << " Inserting ---> " << state << " " << capital

<< " " << ipopulation

<< ' '

;   

InsertCapitalOrder(tree2, state, capital, ipopulation);

  

fin >> state;

while(state != "Total")

{

fin >> stemp;

if(stemp == "Dakota"

|| stemp == "Carolina"

|| stemp == "Hampshire"

|| stemp == "Virginia"

|| stemp == "Jersey"

|| stemp == "York"

|| stemp == "Island"

|| stemp == "Mexico")

{

state += stemp;

fin >> stemp;

}

if(stemp == "Little"

|| stemp == "Baton"

|| stemp == "Des"

|| stemp == "Oklahoma"

|| stemp == "Carson"

|| stemp == "Santa"

|| stemp == "St.")

{

fin >> capital;

stemp += capital;

capital = stemp;

}

if(stemp == "Salt")

{

fin >> capital;

stemp += capital;

fin >> capital;

stemp += capital;

capital = stemp;   

}

else capital = stemp;

fin >> spopulation;

ipopulation = strtoi(spopulation);   

cout << "Inserting ---> " << state << " " << capital

<< " " << ipopulation

<< ' '

;

InsertCapitalOrder(tree2, state, capital, ipopulation);   

fin >> state;

}

  

cout << "Printing Tree2: ";

PrintTreeCapitalOrder(tree2);

wait(2);

//////////////////////////////////////

//3. Add to tree by Population order

//////////////////////////////////////

  

//Resetting the file reading position...

fin.clear();

fin.seekg (0, ios::beg);

  

//filter out header...

stemp = "";

do

{

getline(fin, stemp);

}

while(stemp != goodHeader);

//and following white spaces.

do

{

fin >> state;

}

while(state == "");

//state is now loaded with the first state name

fin >> capital;

fin >> spopulation;   

ipopulation = strtoi(spopulation);

cout << " Inserting ---> " << state << " "

<< capital << " " << ipopulation

<< ' '

;   

InsertPopulationOrder(tree3, state, capital, ipopulation);

  

fin >> state;

while(state != "Total")

{

fin >> stemp;

if(stemp == "Dakota"

|| stemp == "Carolina"

|| stemp == "Hampshire"

|| stemp == "Virginia"

|| stemp == "Jersey"

|| stemp == "York"

|| stemp == "Island"

|| stemp == "Mexico")

{

state += stemp;

fin >> stemp;

}

if(stemp == "Little"

|| stemp == "Baton"

|| stemp == "Des"

|| stemp == "Oklahoma"

|| stemp == "Carson"

|| stemp == "Santa"

|| stemp == "St.")

{

fin >> capital;

stemp += capital;

capital = stemp;

}

if(stemp == "Salt")

{

fin >> capital;

stemp += capital;

fin >> capital;

stemp += capital;

capital = stemp;   

}

else capital = stemp;

fin >> spopulation;

ipopulation = strtoi(spopulation);   

cout << "Inserting ---> " << state << " " << capital

<< " " << ipopulation

<< ' '

;

InsertPopulationOrder(tree3, state, capital, ipopulation);   

fin >> state;

}

  

cout << "Printing Tree3: ";

PrintTreePopulationOrder(tree3);

wait(2);

  

cout << " 4. Printing less than 5 Million: ";

PrintTreeLess5Mil(tree3);

cout << " Printing more than 10 Million: ";

PrintTreeMore10Mil(tree3);

cout << "Done. ";

//EXTRA - adding on Node removal abilities

cout << "Now entering Tree editing tool. First option: removal. "

<< "Enter the state name of the state you would like to "

<< "delete: "

;

cin >> stemp;

tree1.DeleteNode(stemp);

wait(120);

fin.close();

}

===========================================================================

//functions.h

#ifndef _functions_h_included
#define _functions_h_included
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}

int strtoi(string s1)
{
int output = 0;
int is1 = 0;
int power = 0;
for(int i = s1.size() - 1; i >= 0; --i)
{
is1 = static_cast<int>(s1[i]);
//cout << "char val = " << is1 << ' ';
//This if statement filters out commas in the string number
if(is1 != 44)
{
output += ( (is1 - 48) * pow(10.0, power ));
++power;
}
}
return(output);
}

/////////////////////////////////////////////////////
//1. Sort by State Names
void InsertStateOrder(Tree &tree1, string state, string capital, int population)
{
tree1.InsertNodeStateOrder(state, capital, population);
}

void PrintTreeStateOrder(Tree tree1)
{
tree1.PrintStateOrder();
}
///////////////////////////////////////////////////////

/////////////////////////////////////////////////////
//2. Sort by Capital Names
void InsertCapitalOrder(Tree &tree2, string state, string capital, int population)
{
tree2.InsertNodeCapitalOrder(state, capital, population);
}


void PrintTreeCapitalOrder(Tree tree2)
{
tree2.PrintCapitalOrder();
}
///////////////////////////////////////////////////////


/////////////////////////////////////////////////////
//3. Sort by Population
void InsertPopulationOrder(Tree &tree3, string state, string capital, int population)
{
tree3.InsertNodePopulationOrder(state, capital, population);
}


void PrintTreePopulationOrder(Tree tree3)
{
tree3.PrintPopulationOrder();
}
///////////////////////////////////////////////////////

//4. Print Certain Populations
void PrintTreeLess5Mil(Tree tree3)
{
tree3.PrintLess5Mil();
}

void PrintTreeMore10Mil(Tree tree3)
{
tree3.PrintMore10Mil();
}

#endif

================================================================================

//tree.h

#ifndef _tree_h_included

#define _tree_h_included

#include <iomanip>

//In Order Tree Class

//Customer Data types for the Node for CS121 Assignment 05

class Tree

{

private:

static const int MAX_SIZE = 40;

struct Node

{

string m_state;

string m_capital;

int m_population;

Node *m_left;

Node *m_right;

};

Node *head, *pI, *pTail;

public:

Tree()

:head(NULL), pI(NULL), pTail(NULL)

{}

bool IsLeaf(Node *pCurrent)

{

return(pCurrent->m_left == NULL && pCurrent->m_right == NULL);   

}

bool empty()

{

return(head == NULL);   

}

/*

//should be passed 'head' by calling function to start from

// the head of the tree

Tree Seek(Tree pSeek, string state)

{

if(pSeek == NULL) return(NULL);

  

Seek(pSeek->left, state);

if(pSeek->m_state == state) return(pSeek);

Seek(pSeek->right, state);

  

return

}

*/

void DeleteNode(string state)

{

//traverse until it is found

//delete the found node

//

}

/////////////////////////////////////////////////////

//1. Sort by State Names

void InsertNodeStateOrder(string state, string capital, int population)

{

//This Add function was originally designed by: Prof. Bruce Bolden,

//CS Dept. U of I. I just couldn't get my own add function to work,

//I will have to resolve troublshooting on this later.

Node *newPtr = new Node;

  

newPtr->m_state = state;

newPtr->m_capital = capital;

newPtr->m_population = population;

newPtr->m_left = NULL;

newPtr->m_right = NULL;

  

if( head == NULL )

{

head = newPtr;

}

else   

{

Node *treePtr = head;

Node *targetNodePtr;

while( treePtr != NULL )

{

targetNodePtr = treePtr;

if(newPtr->m_state < treePtr->m_state)

treePtr = treePtr->m_left;

else   

treePtr = treePtr->m_right;

}

if(newPtr->m_state < targetNodePtr->m_state)

targetNodePtr->m_left = newPtr;

else

targetNodePtr->m_right = newPtr;

}

}

void PrintStateOrder()

{

if(empty())

{

cout << "(empty) ";

return;

}

PrintStateOrderRecursively(head);

}

void PrintStateOrderRecursively(Node *pCurrent)

{

if(pCurrent == NULL) return;

if(IsLeaf(pCurrent))

{

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

return;

}

PrintStateOrderRecursively(pCurrent->m_left);

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

PrintStateOrderRecursively(pCurrent->m_right);

}

/////////////////////////////////////////////////////

/////////////////////////////////////////////////////

//2. Sort by Capital Names

void InsertNodeCapitalOrder(string state, string capital, int population)

{

//This Add function was originally designed by: Prof. Bruce Bolden,

//CS Dept. U of I. I just couldn't get my own add finction to work,

//I will have to resolve troublshooting on this later.

Node *newPtr = new Node;

  

newPtr->m_state = state;

newPtr->m_capital = capital;

newPtr->m_population = population;

newPtr->m_left = NULL;

newPtr->m_right = NULL;

  

if( head == NULL )

{

head = newPtr;

}

else   

{

Node *treePtr = head;

Node *targetNodePtr;

while( treePtr != NULL )

{

targetNodePtr = treePtr;

if(newPtr->m_capital < treePtr->m_capital)

treePtr = treePtr->m_left;

else   

treePtr = treePtr->m_right;

}

if(newPtr->m_capital < targetNodePtr->m_capital)

targetNodePtr->m_left = newPtr;

else

targetNodePtr->m_right = newPtr;

}

}

void PrintCapitalOrder()

{

if(empty())

{

cout << "(empty) ";

return;

}

PrintCapitalOrderRecursively(head);

}

void PrintCapitalOrderRecursively(Node *pCurrent)

{

if(pCurrent == NULL) return;

if(IsLeaf(pCurrent))

{

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

return;

}

PrintCapitalOrderRecursively(pCurrent->m_left);

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

PrintCapitalOrderRecursively(pCurrent->m_right);

}

/////////////////////////////////////////////////////

/////////////////////////////////////////////////////

//3. Sort by Population Names

void InsertNodePopulationOrder(string state, string capital, int population)

{

//This Add function was originally designed by: Prof. Bruce Bolden,

//CS Dept. U of I. I just couldn't get my own add finction to work,

//I will have to resolve troublshooting on this later.

Node *newPtr = new Node;

  

newPtr->m_state = state;

newPtr->m_capital = capital;

newPtr->m_population = population;

newPtr->m_left = NULL;

newPtr->m_right = NULL;

  

if( head == NULL )

{

head = newPtr;

}

else   

{

Node *treePtr = head;

Node *targetNodePtr;

while( treePtr != NULL )

{

targetNodePtr = treePtr;

if(newPtr->m_population < treePtr->m_population)

treePtr = treePtr->m_left;

else   

treePtr = treePtr->m_right;

}

if(newPtr->m_population < targetNodePtr->m_population)

targetNodePtr->m_left = newPtr;

else

targetNodePtr->m_right = newPtr;

}

}

void PrintPopulationOrder()

{

if(empty())

{

cout << "(empty) ";

return;

}

PrintPopulationOrderRecursively(head);

}

void PrintPopulationOrderRecursively(Node *pCurrent)

{

if(pCurrent == NULL) return;

if(IsLeaf(pCurrent))

{

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

return;

}

PrintPopulationOrderRecursively(pCurrent->m_left);

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

PrintPopulationOrderRecursively(pCurrent->m_right);

}

/////////////////////////////////////////////////////

/////////////////////////////////////////////////////

//4.

void PrintLess5Mil()

{

if(empty())

{

cout << "(empty) ";

return;

}

PrintLess5MilRecursively(head);

}

void PrintLess5MilRecursively(Node *pCurrent)

{

if(pCurrent == NULL) return;

if(pCurrent->m_population > 5000000) return;

if(IsLeaf(pCurrent))

{

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

return;

}

PrintLess5MilRecursively(pCurrent->m_left);

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

PrintLess5MilRecursively(pCurrent->m_right);

}

void PrintMore10Mil()

{

if(empty())

{

cout << "(empty) ";

return;

}

PrintMore10MilRecursively(head);

}

void PrintMore10MilRecursively(Node *pCurrent)

{

if(pCurrent == NULL) return;

if(pCurrent->m_population < 1000000) return;

if(IsLeaf(pCurrent))

{

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

return;

}

PrintMore10MilRecursively(pCurrent->m_left);

  

cout << setw(26) << pCurrent->m_state << " "

<< setw(26) << pCurrent->m_capital << " "

<< setw(26) << pCurrent->m_population << ' '

;

PrintMore10MilRecursively(pCurrent->m_right);

}

  

};

#endif

===================================================================================

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