ECS 40 Program #3 - CS-CSIF

Transcription

ECS 40 Program #3 - CS-CSIF
ECS 40
Program #3 (50 points, my time 1.5 hours)
Spring 2015
Due: Wednesday, April 29th at 11:59pm using handin to p3 directory of cs40a.
New concepts: iostream, fstream, new, delete, and C++ .
Name of executable: simulator.out
File names: Makefile, main.cpp, city.cpp, city.h, vector.cpp, vector.h, authors.txt.
For this assignment, you will 1) adapt your code to data files that have more than one city with the same name, 2)
converting your C code of p2 to C++, and 3) you will add the ability to list all of the traffic from a specific airport.
Adapting to the duplicated city names, should only involve readAirports(), readAirport(), and isEqual(), and is fairly
straightforward for each (total 5 minutes). Converting to C++ will involve: 1) replacing stdio.h function calls with
iostream and fstream function calls; 2) replacing malloc and free with new and delete; 3) removing #include of stdio.h,
and replace #includes of other C header files with their C++ counterparts; 4) changing the Vector struct to a Vector class;
and 5) changing the City struct to a City class. Listing airport traffic involves adding a getChoic() function to main.cpp
and a new method to Vector.
You may use either your own p1 code, or my p1 code from ~ssdavis/40/p1/SeansSrc (available Thursday morning) as
your starting point for the assignment. There is no problem with plagiarism if you use my code. You will find my
executable, and the two data files in ~ssdavis/40/p3. As usual, the format and values of your program must match mine.
To get traffic value into a more reasonable range you will now divide by 2500000000U instead of 250000000U.
Here is a road map to making the transitions. After each of the five steps your program should compile with no
warnings, and run perfectly. The road map is based on my own program, so you may wish to have my program handy
even if you are modifying your own p1.
1. Replacing stdio.h function calls with iostream and fstream function calls. (10 minutes)
1.1. Replace printf with cout statements.
1.2. Replace scanf with cin statements.
1.3. Replace FILE* declarations and assignments with ifstream declarations.
1.3.1. An ifstream may only be passed by reference.
1.4. Replace fgets with getline. Note that getline does not place the '\n' in the array.
1.5. Don't forget to add "using namespace std;" to any files with C++ header files.
2. Replacing malloc and free with new and delete. (5 minutes)
2.1. Remember to use delete [] to deallocate arrays.
3. Change all input/output to C++, and update standard header file #includes to C++ format. (15 minutes)
3.1. Remove stdio.h #includes, and add iostream and fstream includes only where appropriate.
3.2. Remember to add “using namespace std;” below the #include lists.
3.3. Change fopen() to an ifstream constructor, printf() to cout <<, scanf to cin >>, and fgets() to getline().
3.4. Change all remaining #includes involving < > from <filename.h> to <cfilename>, e.g. <string.h> to <cstring>.
4. Changing the Vector struct to a Vector class (10 minutes.)
4.1. In vector.h
4.1.1. Change the typedef struct declaration to a class, and move the closing "};" until the line after the last
function prototype in the file.
4.1.2. Remove the Vector* parameters from the function parameter lists. If it is "const Vector *" (e.g. in
findDistance), then the whole function should be made const, by adding "const" after the closing ")" of the
parameter list.
4.1.3. Add "public:" to the class just below the variable declarations. Move the prototypes of any function(s) that
are only called from Vector functions above “public” because they should be private.
4.1.4. Make initialize() the constructor for the class.
4.1.5. Make deallocate() the destructor for the class.
4.2. In vector.cpp
4.2.1. Remove the Vector* parameters from the function parameter lists, and make functions const that are listed
as such in vector.h.
4.2.2. Make each function a member of the Vector class using the scope operator, "::".
4.2.3. Make initialize() the constructor. Note that constructors have no return value.
4.2.4. Make deallocate() the destructor for the class. Note that destructors have no return value.
4.2.5. Delete all "cities->", or whatever you used for the Vector pointer identifier. This can be a simple search
and replace!
4.2.6. You will need to add just “::” in front of the call to City calcDistance() from Vector calcDistance() so that
the compiler does not raise errors trying to make it a recursive call.
4.3. In main.cpp
4.3.1. Remove the Vector parameters from function calls of the Vector class, use the Vector object to call the
functions instead, e.g. readCities(&cities) becomes cities.readCities();
4.3.2. Eliminate the calls to initialize() and deallocate(). They will be called implicitly.
5. Changing the City struct to a City class. (25 minutes)
5.1. In city.h, follow the same steps as vector.h for the Vector class change.
5.1.1. Note that you should only remove the first City parameter from functions with two City parameters.
5.1.2. You will have to retain the deallocate() function for readAirports(), but make a copy of it for the destructor.
5.1.3. In order for "temp[i] = cityArray[i];" to work properly, you will need to have an overloaded assignment
operator for the City class.
5.2. In city.cpp, follow the same steps as vector.cpp for the Vector class change.
5.2.1. Note that when a parameter has the same name as a City data member you will have to change the name of
the parameter.
5.2.2. You will need to add the operator= code.
5.3. In vector.cpp, follow the same steps as main.cpp for the Vector class change.
5.3.1. There is no longer a need for the destructor to explicitly call City::deallocate because the City destructor
will be called automatically.
5.3.2. There is no longer a need to call initialize(), since the constructor is called automatically.
5.3.3. The “::” in front of the call to City::calcDistance() should be replaced with “cityArray[index1].”
5.3.4. Note that your overloaded operator= should eliminate the need for cleanCities() to call deallocate().
6. Adding the ability to list all of the traffic from a specific airport. (25 minutes)
6.1. Add a function, getChoice(), to print a menu, and range checks the user's answer to main.cpp. You should use
cin.ignore(…) after you read their choice to eat up the '\n' that remains in the keyboard buffer.
6.2. run() will now need to call specific functions to deal with the two searching tasks.
6.3. Write Vector::calcAirportTraffic().
6.4. Note that the order of the cities need not match mine, but number of cities, and their passenger volume must.
[@lect1 p3]$ simulator.out
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two airports.
2. Determine all traffic from one airport.
Your choice (0 - 2): 3
Your choice must be between 0 and 2.
Your choice (0 - 2): 2
Please try again.
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two airports.
2. Determine all traffic from one airport.
Your choice (0 - 2): -1
Your choice must be between 0 and 2.
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two
airports.
2. Determine all traffic from one airport.
Please try again.
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two airports.
2. Determine all traffic from one airport.
Your choice (0 - 2): 1
Please enter two airport abbreviations (XXX XXX): SFO ECS
ECS is not a valid airport
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two airports.
2. Determine all traffic from one airport.
Your choice (0 - 2): 1
Please enter two airport abbreviations (XXX XXX): SFO NYC
2531 passengers fly the 2570 miles from
San Francisco, California to New York City, New York.
Please enter an airport abbreviation (XXX): ECS
ECS is not a valid airport
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two
airports.
2. Determine all traffic from one airport.
Your choice (0 - 2): 2
Please enter an airport abbreviation (XXX): SFO
New York City, New York: 2531
Los Angeles, California: 1173
Chicago, Illinois: 867
Houston, Texas: 675
Phoenix, Arizona: 474
Philadelphia, Pennsylvania: 443
San Antonio, Texas: 406
San Diego, California: 387
Dallas, Texas: 379
// edited by Sean
Rockford, Illinois: 47
Dayton, Ohio: 47
Springfield, Missouri: 47
Palmdale, California: 43
Salem, Oregon: 46
Sioux Falls, South Dakota: 46
Torrance, California: 43
Eugene, Oregon: 45
Lancaster, California: 43
Total passengers: 18885
Flight Simulator Menu
0. Done.
1. Determine distance and passengers between two airports.
2. Determine all traffic from one airport.
Your choice (0 - 2): 0
[@lect1 p3]$