Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve this C++ problem. Help me with the full code Write a program that manages the power grid for a city. First, create a struct

Solve this C++ problem. Help me with the full code
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Write a program that manages the power grid for a city. First, create a struct named district (all lowercase) that represents each district in the power grid. Each district struct will have: - name: a string that holds the name of the district (note that names can consist of multiple words!) - type: a string that holds the type of the district, there are three types: "RESIDENTIAL", "COMMERCIAL" and "HOSPITAL". Note that district type is always in all caps. - num_resident : a number for how many residents are in that district. - power: how much power the district is currently consuming from the power grid. - has_power: a boolean that indicates if the district is currently supplied with power or not. - num_technician : a number for how many residents are in that district. - technicians: a string array holding the names of all the technicians assigned to that district - has_power: a boolean that indicates if the district is currently supplied with power or not. - num_technician : a number for how many residents are in that district. - technicians: a string array holding the names of all the technicians assigned to that district The data on each district is stored inside a single file. Write a load function to read in all the districts information from this file. This function should first take in a string for the filename of the file to load, an array of district structs (that will be updated with the corresponding districts after the function finish loading), the number of districts (as this value should reflect the number of districts read from the file, it should be passed in as reference), and the current power output of all powerplants (as an integer, again as this value is read from the file, it should also be passed as reference). The function declaration should look like: void load(string fname, district districts[], int \&num, int \&power) (Note, to pass the unit tests, make sure the parameter types of all functions to be in correct order, and the function name must be exactly the same!). Next, read in the file with that name. The file will be in this format: Where the is the combined power output of all power plants. Here is an example input file: with prioritizing districts, sort them in ascending order based on each district's number of residents! The next function to be implemented is the save () function. This function should have the same parameters as the load ( ) function, except none of the parameters should be passed in as reference. The function will take in the string file name of the save file, an array of district structs, the number of districts, and the current combined power output of all power plants (in megawatts MW), then it saves all the information into the save file in the same format provided above. void save (string fname, district districts[], int \&num, int \&power) The third function is print_all( ) which will print out the information of each district in the following format: The current power supply is > MW. DISTRICT 1 'S NAME> District type: DISTRICT 1 's TYPE> Number of residents: DISTRICT 1 's NUMBER OF RESIDENTS> Power use: MW Has power: DISTRICT 2 's NAME An example output is: If the district has 0 technicians then do not display the Technicians: line for that district If the current power output is lower than the total power demand of all districts, print the following message: Critical Error! The current power supply of MW is insufficient to supply > MW of power demand. Please shut off one or more districts. supply of MW is insufficient to supply MW of power demand. Please shut off one or more districts. Next write the assign_technician () which accepts a string for the name of the technician, a string for the name of the district to assign them to, the district array, and the number of districts in the array. It tries to find the district with the district name in the array and if found, add the technician's name to that district's technicians array and print out "Technician assigned!". If no district with that name is found, print "Error! The district with that name doesn't exist." The function should be declared as: void assign_technician (string tech_name, string district_name, district districts[], int num_district) Lastly write the main ( ) function as follows: - First ask the user to enter the name of the file to load: "Please enter the name of the file to load:" - Get the file name from cin and load the file - Print the information of all districts - Then start a menu loop. This menu loop should repeat indefinitely until the user selects the exit option: file to load:" - Get the file name from cin and load the file - Print the information of all districts - Then start a menu loop. This menu loop should repeat indefinitely until the user selects the exit option: Main Menu 1 - Status 2- Set Power Supply 3- Shutoff Power 4- Restore Power 5- Assign Technician 6- Save 7- Load 8- Exit - If the user entered an invalid option, print "Invalid option!" - Option 1 will print all district information - Option 2 will set the value of the current total power supply of all powerplants - Option 3 will first ask the user for the name of the district "Enter the name of the district:" - Then it search for the district and if found, it will set the district's has_power status to false and print "The district's power has been successfully turned off." - If the district with that name is not found, then the program will instead print "Error! The district with that name doesn't exist." tullieu 01:. - If the district with that name is not found, then the program will instead print "Error! The district with that name doesn't exist." - Option 4 will first ask the user for the name of the district "Enter the name of the district:" - Then it search for the district and if found, it will set the district's has_power status to true and print "The district's power has been successfully turned back on." - If the district with that name is not found, then the program will instead print "Error! The district with that name doesn't exist." - Option 5 will prompt the user to enter to "Enter the name of the technician:" then "Enter the name of the district to assign them to:" - Option 6 will prompt the user to "Enter the file name to save to:" and save everything to that file - Option 7 will prompt the user to "Enter the file name to load from: " and load everything from that file - Option 8 will terminate the program Additional notes: - Don't forget to close files after you finish reading/writing them! - Make sure to use cin. ignore( ) or file.ignore () as needed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions