Question

In: Computer Science

Copy d_state.h to your working directory and finish its implementation. Write a program: it first declares...

  1. Copy d_state.h to your working directory and finish its implementation.
  2. Write a program:
    • it first declares a set object s having 5 elements of type stateCity with its initial values such as ("Virginia", "Richmond");
    • then perform the search: input the name of a state, and use the find() function to determine where the state in the set. If the object is present, used the << operator to output the sate and the city; if it is not present, output a message to that effect.
    • Then, rather than using objects of type stateCity, you will implement the program by using a map with state name (string) as the key and the city name (string) as the data value.

d_state.h:

#ifndef STATECITY_CLASS
#define STATECITY_CLASS

#include 
#include 

using namespace std;

// object stores the state name and city in the state
class stateCity
{
        public:
                stateCity (const string& name = "", const string& city = "");

                // output the state and city name in the format
                //    cityName, stateName
                friend ostream& operator<< (ostream& ostr, const stateCity& state;
                
                // operators < and == must be defined to use with set object.
                // operators use only the stateName as the key
                friend bool operator< (const stateCity& a, const stateCity& b);
                
                friend bool operator== (const stateCity& a, const stateCity& b);
        
        private:
                string stateName, cityName;
};

#endif  // STATECITY_CLASS

Please write in C++



Sample Output:

Run 1:

Enter a state: Arizona
Phoenix, Arizona

Run 2:

Enter a state: New York
New York is not in the set

Solutions

Expert Solution

Code in C++ for main.cpp (code to copy)


#include <bits/stdc++.h>
#include "d_state.h"
using namespace std;


int main(){
        set<stateCity> s = {stateCity ("Virginia", "Richmond"), stateCity ("Arizona", "Phoenix"), stateCity ("New Hampshire", "Warner"), stateCity ("Kansas", "Scottsville"), stateCity ("Texas", "Longview")};
        cout<<"Enter a state: ";
        string state;
        cin>>state;
        auto it = s.find(stateCity(state));
        if(it==s.end()){
                cout<<state<<" is not in the set"<<endl;
        }else{
                cout<<*it<<endl;
        }
}

code in c++ for d_state.h (code to copy)

#ifndef STATECITY_CLASS
#define STATECITY_CLASS

#include<bits/stdc++.h>

using namespace std;

// object stores the state name and city in the state
class stateCity
{
        public:
                stateCity (const string& name = "", const string& city = ""){
                    stateName=name;
                    cityName=city;
                }

                // output the state and city name in the format
                //    cityName, stateName
                friend ostream& operator<< (ostream& ostr, const stateCity& state){
                    ostr<<state.cityName<<" "<<state.stateName;
                }
                
                // operators < and == must be defined to use with set object.
                // operators use only the stateName as the key
                friend bool operator< (const stateCity& a, const stateCity& b){
                    return a.stateName<b.stateName;
                }
                
                friend bool operator== (const stateCity& a, const stateCity& b){
                    return a.stateName==b.stateName;
                }
        
        private:
                string stateName, cityName;
};

#endif  // STATECITY_CLASS

Code screenshot main.cpp

Code screenshot d_state.h

Sample output screenshot 1

Sample Output screenshot 2

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of...
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of people’s names and the corresponding phone numbers. The program should read a transaction file and report the result into an output file. The transaction file can include commands such as “Add”, “Delete”, “Display”, and “Update”, and “Search”. Each command is written in one line with a few other information. The “Display” Command should simply display everyone in the directory on the screen The “Add”...
For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already...
For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already in the code) Then, modify the class so that it can store any type someone asks for, instead of only Strings. You shouldn't have to change any of the actual logic in your class to accomplish this, only type declarations (i.e. the types of parameters, return types, etc.) Note: In doing so, you may end up needing to write something like this (where T...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
Unix command lines. A) Assume that your current working directory is NOT the same as your...
Unix command lines. A) Assume that your current working directory is NOT the same as your home directory. Print all file and subdirectory names of your home directory into a file named file2. B) Copy the text from line 11 to line 20 of a given file input.txt and append the copied text at the end of the file output.txt. (Assume that the file input.txt contains more than 20 lines of text contents) C) Count and display the total number...
Write a program that declares a struct to store the data of a football player (player’s...
Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of a...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a program in ecmascript, of Maclurin series of a sine function that declares two variables:...
Write a program in ecmascript, of Maclurin series of a sine function that declares two variables: x is the value of the argument, h is the accuracy that you want test the program with x= 1; h= .00001 print out the approximation using as many terms of the Maclaurin series that you need, but no more than that, to get within the required accuracy. While you are at it you might as well also print out Sine(x) BUT do not...
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT