82.4k views
3 votes
C++

13. 5 lab: zip code and population (class templates)

define a class statepair with two template types (t1 and t2), constructors, mutators, accessors, and a printinfo() method. three vectors have been pre-filled with statepair data in main():

vector> zipcodestate: zip code - state abbreviation pairs

vector> abbrevstate: state abbreviation - state name pairs

vector> statepopulation: state name - population pairs

complete main() to use an input zip code to retrieve the correct state abbreviation from the vector zipcodestate. then use the state abbreviation to retrieve the state name from the vector abbrevstate. lastly, use the state name to retrieve the correct state name/population pair from the vector statepopulation and output the pair.

ex: if the input is:

21044

the output is:

maryland: 6079602

main cpp :

#include

#include

#include

#include

#include "statepair. h"

using namespace std;

int main()

{

User Baswell
by
3.5k points

2 Answers

4 votes

Below is the complete code for the main.cpp file:

C++

#include <iostream>

#include <vector>

#include <string>

#include "statepair.h"

using namespace std;

// Vectors of zip code - state abbreviation pairs, state abbreviation - state name pairs, and state name - population pairs

vector<StatePair<int, string>> zipcodestate;

vector<StatePair<string, string>> abbrevstate;

vector<StatePair<string, int>> statepopulation;

// Function to search for a state abbreviation given a zip code

string getAbbreviation(int zipCode) {

for (StatePair<int, string> pair : zipcodestate) {

if (pair.first == zipCode) {

return pair.second;

}

}

return "";

}

// Function to search for a state name given a state abbreviation

string getName(string abbrev) {

for (StatePair<string, string> pair : abbrevstate) {

if (pair.first == abbrev) {

return pair.second;

}

}

return "";

}

// Function to search for a population given a state name

int getPopulation(string stateName) {

for (StatePair<string, int> pair : statepopulation) {

if (pair.first == stateName) {

return pair.second;

}

}

return 0;

}

int main() {

// Read the zip code from the user

int zipCode;

cin >> zipCode;

// Get the state abbreviation from the vector zipcodestate

string stateAbbreviation = getAbbreviation(zipCode);

// Get the state name from the vector abbrevstate

string stateName = getName(stateAbbreviation);

// Get the population from the vector statepopulation

int population = getPopulation(stateName);

// Output the result

cout << stateName << ": " << population << endl;

return 0;

}

So, Before running the code, one need to create the statepair.h file and define the StatePair class template. Below is the code for the statepair.h file:

C++

template <class T1, class T2>

class StatePair

{

private:

T1 first;

T2 second;

public:

StatePair(T1 firstValue, T2 secondValue) : first(firstValue), second(secondValue) {}

T1 getFirst() const { return first; }

void setFirst(T1 firstValue) { first = firstValue; }

T2 getSecond() const { return second; }

void setSecond(T2 secondValue) { second = secondValue; }

void printInfo() {

cout << "(" << first << ", " << second << ")" << endl;

}

};

User Abiratsis
by
2.8k points
1 vote

#include<iostream>

#include <fstream>

#include <vector>

#include <string>

#include "StatePair.h"

using namespace std;

int main() {

ifstream inFS;

int zip;

int population;

string abbrev;

string state;

unsigned int i;

vector<StatePair <int, string>> zipCodeState;

vector<StatePair<string, string>> abbrevState;

vector<StatePair<string, int>> statePopulation;

inFS.open("zip_code_state.txt");

if (!inFS.is_open()) {

cout << "Could not open file zip_code_state.txt." << endl;

return 1;

}

while (!inFS.eof()) {

StatePair <int, string> temp;

inFS >> zip;

if (!inFS.fail()) {

temp.SetKey(zip);

}

inFS >> abbrev;

if (!inFS.fail()) {

temp.SetValue(abbrev);

}

zipCodeState.push_back(temp);

}

inFS.close();

inFS.open("abbreviation_state.txt");

if (!inFS.is_open()) {

cout << "Could not open file abbreviation_state.txt." << endl;

return 1;

}

while (!inFS.eof()) {

StatePair <string, string> temp;

inFS >> abbrev;

if (!inFS.fail()) {

temp.SetKey(abbrev);

}

getline(inFS, state);

getline(inFS, state);

state = state.substr(0, state.size()-1);

if (!inFS.fail()) {

temp.SetValue(state);

}

abbrevState.push_back(temp);

}

inFS.close();

inFS.open("state_population.txt");

if (!inFS.is_open()) {

cout << "Could not open file state_population.txt." << endl;

return 1;

}

while (!inFS.eof()) {

StatePair <string, int> temp;

getline(inFS, state);

state = state.substr(0, state.size()-1);

if (!inFS.fail()) {

temp.SetKey(state);

}

inFS >> population;

if (!inFS.fail()) {

temp.SetValue(population);

}

getline(inFS, state);

statePopulation.push_back(temp);

}

inFS.close();

cin >> zip;

for (i = 0; i < zipCodeState.size(); ++i) {

}

for (i = 0; i < abbrevState.size(); ++i) {

}

for (i = 0; i < statePopulation.size(); ++i) {

}

}

Statepair.h

#ifndef STATEPAIR

#define STATEPAIR

#include <iostream>

using namespace std;

template<typename T1, typename T2>

class StatePair {

private:

T1 key;

T2 value;

public:

StatePair()

{}

void SetKey(T1 key)

{

this->key = key;

}

void SetValue(T2 value)

{

this->value = value;

}

T1 GetKey() { return key;}

T2 GetValue() { return value;}

void PrintInfo()

{

cout<<key<<" : "<<value<<endl;

}

};

C++ 13. 5 lab: zip code and population (class templates) define a class statepair-example-1
C++ 13. 5 lab: zip code and population (class templates) define a class statepair-example-2
User Tom Jonckheere
by
3.1k points