Question: c programming language split the code into two files: project.c and project.h Olympics Implement a system that tracks the olympic medals of different nations. A
c programming language
split the code into two files: project.c and project.h
Olympics
Implement a system that tracks the olympic medals of different nations. A nation is represented with the following data.
name is the name of the nation, which is a string that can have arbitrary length.
gold is the number of gold medals, which is a nonnegative integer.
silver is the number of silver medal, which is a nonnegative integer.
bronze is the number of bronze medals, which is a nonnegative integer.
The program should be able to support arbitrary number of nations.
System must be able to process the following commands.
Add nation command, which adds a nation to the database.
The command has the following format.
A
where
is the name name, which is an arbitrary length string.
For example, a valid add nation command is as follows.
A Finland
Initially, the nations has medals.
If the nation with the specified name is already in the database, the program must print an error message.
Add medals command, which adds the specified medals to a nation.
The command has the following format.
M
where
is the nation name.
is the number of gold medals to the country.
is the number of silver medals to the country.
is the number of bronze medals to the country.
For example, a valid add medals command is as follows.
M Finland
It shall add one silver and one bronze medal to Finlands existing medals.
Because of possible doping cases negative adjustments needs to be allowed so that the medal count arguments are signed integers. Although total medal counts cannot be negative, if the medal count argument is negative, the total medal count of a particular medal can be reduced.
If the given nation does not exist in the database, an error message should be printed.
Print database command, which displays the database content.
The command has the following format.
L
It prints the stored nations on a separate line as follows.
where the entries are the names of the nation data fields.
For example, print database command output is as follows.
China
Finland
SUCCESS
The database entries should be printed in the following order.
The nations with higher number of gold medals should be printed first.
If number of gold medals are equal, nations with higher number of silver medals should be printed above the others.
If number of gold and silver medals are equal, the nations with higher number of bronze medals should be printed before the others.
If their number of medals are the same, the nations can be printed in an arbitrary order.
The worth of this command is two points. The worth of printing all the database entries is point. If their order is also correct, you will get another point.
Save to file command, which saves the database to a text file.
The command has the following format.
W
where
is the name of the text file.
It writes the stored nations on separate lines as follows.
where the entries are the names of the nation data fields.
If an error occurs, it must print an error message.
Hint
Print and save functions can be implemented with a common print function that can be called for different streams depending on the wanted functionality.
Load from file command, which loads nations from a text file saved using Save to file command.
The command has the following format.
O
where
is the name of the text file.
It assumes the stored nations are on a separate line in the format specified above.
If an error occurs, it should print an error message.
If the file is valid and loaded correctly, the current database must be replaced with the entries loaded from the file.
Quit program command, which releases all allocated memory and exits the program.
The command has the following format.
Q
This operation must be implemented so that you can prevent memory leaks.
Below is an example of possible command sequence.
A China
A Finland
M China
M Finland
M China
M China
L
W medals
Q
After this, China has gold medals, silver, and bronze, but Finland has only one bronze. When the table is printed, China should be shown before Finland. The table will be written to the text file medals
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
