C++代写:EECS268 Cities

代写C++的Lab基础作业,实现一个菜单应用。

Overview

Infection spreads across the land. Cities are now being monitored by the CDC. The CDC is keeping a list of infected cities and applying an infection level to each city.

Requirements

  • You’ll read in from a file a list of cities (with name, population, and current infection level).
    • As your adding cities to your list, you must keep them sorted by population (descending order)
  • You’ll then prompt the user for what happens next (see below):
    • User options:
      • Increase the infection level of all cities by 1
        -Increase the infection level of a specific city by 1
      • Print the status of a city (searched by name)
      • Print all cities above a certain infection level
      • Create quarantine log
        • Save all quarantined cities information to a file named by the user
        • Format must match that of the input file
      • Repeat all option until the user wants to exit
  • Monitor the user’s actions take any needed actions based on a city’s infection level
Infection levelrequired action(s)
0None
1Remove this city from the list
1Decrease the population by 10% (round down)
1Add it back to the list (this helps keeps the list sorted by population)
2Remove this city from the list
2Decrease the population by 20% (round down)
2Add it back to the list (this helps keeps the list sorted by population)
3Take 25% (round down) of the population and form a new city (both cities should be removed and added back to the list as before)
4All hope is lost for the city
4The city is removed from the infected list and moved to the quarantined list (with it’s remaining inhabitants).
4A message “City X has been placed in quarantine” is printed to the screen
4This city can no longer be interacted with other than to be saved in the quarantine log

Notes:

  • When forming a new city…
    • the infection level will reset to zero
    • the name will always be the name of the old city with a “New” in front (e.g. New Lawrence, new Kansas City, etc.)
      • It’s okay to have a “New New New Lawrence”
  • Once a city is moved to quarantine, it does not change infection level or population
  • Both lists should be ordered by population (descending). Rank by infection level then alphabetically by name in a tie.
  • At least one city needs be quarantined before the user can make the quarantine log

Sample input file

The input will with be organized in the following way: city, population, current infection level

Lawrence,85483,3
Kansas City,1500000,2
Smallville,23,0

Notes:

  • No actions need to be taken upon reading the file. Only take action based on user interaction
  • City names may contain spaces
  • Populations will always be a whole number
  • Files will always be properly formatted
  • There is no count of entries at the top. You’ll need to read until end of file is reached.

Sample menu

This is a sample interaction with the provided sample input file.

Make a selection:
1) Increase infection level of all cities
2) Increase infection level of specific city
3) Print status of a specific city
4) Create quarantine log
5) Print all cities above an infection level
6) Exit
Choice: 2

Which city do you want to infect?: fake town
Sorry, "fake town" is not a city in the list.

Make a selection:
1) Increase infection level of all cities
2) Increase infection level of specific city
3) Print status of a specific city
4) Create quarantine log
5) Print all cities above an infection level
6) Exit
Choice: 2

Which city do you want to infect?: Lawrence
Infection level for "Lawrence" increased by 1

Make a selection:
1) Increase infection level of all cities
2) Increase infection level of specific city
3) Print status of a specific city
4) Create quarantine log
5) Print all cities above an infection level
6) Exit
Choice: 1

The infection level for all cities has been increased.
Lawrence has been placed in quarantine!

Classes to make

  • MedicalExecutive class
    • Reads in the initial list of cities
    • Maintains the infected cities list and the quarantined cities list
      • Note: The LinkedList specification is entirely positional, so this class will need to use the available list methods to maintain the required ordering by population
    • Handles User Interaction
  • City class
    • Holds the information for one city
    • Implement getters, setters, and constructors that make sense
  • List implementation
    • Use your List and Node implementations from Lab 2

Lab Write-up

  1. The file format we are using is called a “comma-separated value” file or CSV. It’s a common format for storing data in a text file. we used it in lab 01 as well. Is there any code from your lab 01 you can reuse to help handle the file reading? If not, explain. If so, what form does the code take (e.g. a block you copy and paste, a method, a class, etc.)
  2. Design a file reader class that specializes in reading CSV files. Make a list of methods you want it have. You do not have to actually implement this class, but if you start to think it would be handy, feel free to make a working implementation.
  3. Make a list of all methods that are called with the user chooses option 1 (increase infection rate of all cities). With each method name, please note the class it belongs to

Libraries You May Include

  • iostream
  • fstream
  • string

Makefile Update

In this lab we will be using a feature from c++11, the nullptr key word, and templating. Please see the Makefile tutorial for needed updates to your Makefile.

Checking for Memory Leaks

valgrind --leak-check=full ./YourProgram

It will tell you if any leaks occurred. Make sure you have the -g flag in your Makefile and you’ll get the added bonus of see what lines of code created an object that was never deleted. Science!

Issues with Valgrind

Recently, valgrind began reporting on a buffer that automatically allocated then deallocated after our main ends. This would look like you’re doing something wrong. But be aware if you run a program with an empty main, you’ll see the following:

==23799== HEAP SUMMARY:
==23799==     in use at exit: 72,704 bytes in 1 blocks
==23799==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==23799== 
==23799== LEAK SUMMARY:
==23799==    definitely lost: 0 bytes in 0 blocks
==23799==    indirectly lost: 0 bytes in 0 blocks
==23799==      possibly lost: 0 bytes in 0 blocks
==23799==    still reachable: 72,704 bytes in 1 blocks
==23799==         suppressed: 0 bytes in 0 blocks
==23799== Reachable blocks (those to which a pointer was found) are not shown.
==23799== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==23799== 
==23799== For counts of detected and suppressed errors, rerun with: -v
==23799== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Note the amount of allocations. My empty main didn’t allocate anything, but valigrind is reporting everything it can see. There is a buffer being allocated that we don’t control. It is freed, but after our main ends, so Valgrind reports on it.

Just make sure…

  • your allocs are only off by 1
  • there are no memory error
  • nothing is lost