Discover the answers you need at Westonci.ca, where experts provide clear and concise information on various topics. Get expert answers to your questions quickly and accurately from our dedicated community of professionals. Get precise and detailed answers to your questions from a knowledgeable community of experts on our Q&A platform.
Sagot :
Answer:
Here.Hope this helps and do read carefully ,you need to make 1 .cpp file and 1 .h file
Explanation:
#include<iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;
int main() {
ifstream inFS; // File input stream
int zip;
int population;
string abbrev;
string state;
unsigned int i;
// ZIP code - state abbrev. pairs
vector<StatePair <int, string>> zipCodeState;
// state abbrev. - state name pairs
vector<StatePair<string, string>> abbrevState;
// state name - population pairs
vector<StatePair<string, int>> statePopulation;
// Fill the three ArrayLists
// Try to open zip_code_state.txt file
inFS.open("zip_code_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file zip_code_state.txt." << endl;
return 1; // 1 indicates error
}
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();
// Try to open abbreviation_state.txt file
inFS.open("abbreviation_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file abbreviation_state.txt." << endl;
return 1; // 1 indicates error
}
while (!inFS.eof()) {
StatePair <string, string> temp;
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetKey(abbrev);
}
getline(inFS, state); //flushes endline
getline(inFS, state);
state = state.substr(0, state.size()-1);
if (!inFS.fail()) {
temp.SetValue(state);
}
abbrevState.push_back(temp);
}
inFS.close();
// Try to open state_population.txt file
inFS.open("state_population.txt");
if (!inFS.is_open()) {
cout << "Could not open file state_population.txt." << endl;
return 1; // 1 indicates error
}
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); //flushes endline
statePopulation.push_back(temp);
}
inFS.close();
cin >> zip;
for (i = 0; i < zipCodeState.size(); ++i) {
// TODO: Using ZIP code, find state abbreviation
}
for (i = 0; i < abbrevState.size(); ++i) {
// TODO: Using state abbreviation, find state name
}
for (i = 0; i < statePopulation.size(); ++i) {
// TODO: Using state name, find population. Print pair info.
}
}
Statepair.h
#ifndef STATEPAIR
#define STATEPAIR
#include <iostream>
using namespace std;
template<typename T1, typename T2>
class StatePair {
private:
T1 key;
T2 value;
public:
// Define a constructor, mutators, and accessors
// for StatePair
StatePair() //default constructor
{}
// set the key
void SetKey(T1 key)
{
this->key = key;
}
// set the value
void SetValue(T2 value)
{
this->value = value;
}
// return key
T1 GetKey() { return key;}
// return value
T2 GetValue() { return value;}
// Define PrintInfo() method
// display key and value in the format key : value
void PrintInfo()
{
cout<<key<<" : "<<value<<endl;
}
};
#endif
In this exercise we have to use the knowledge in computational language in C++ to write the following code:
We have the code can be found in the attached image.
So in an easier way we have that the code is
#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;
}
};
See more about C code at brainly.com/question/19705654


Thank you for your visit. We are dedicated to helping you find the information you need, whenever you need it. Thank you for your visit. We're committed to providing you with the best information available. Return anytime for more. Thank you for using Westonci.ca. Come back for more in-depth answers to all your queries.