Lab 4 Ex 2: Too Many Apples

Lab 4 Ex 2: Too Many Apples

You cannot submit for this problem because the homework's deadline is due.

Related Topics: IO, compound objects, program arguments.

This exercise will help you get familiar with file streams and structs.

In this exercise, you are going to read a file. The file contains all the price information for different kinds of apples. Here is an example:

variety1 56.81 5.12
variety2 59.50 5.52
variety3 79.28 7.25

The file is composed of 3 columns. The first column is the name of the apple variety (assume there is no space in the names), the second column is the price of the box of apples (in RMB, double type), and the third column is the total weight of the box of apples (in kg, double type). Note that the number of lines of this file is not fixed. You may get a price list with a much larger number of lines.

The name of the file is not fixed as well. In your lab materials, an example file named price_list.txt is provided. However, in some test cases, the name of the file could be different. You need to read the name of the file from program argument:

./ex2 <file_name>

For this exercise, please write a program that can read in all the information from the file, and then find out the apple variety with the lowest average price (RMB/kg). If two or more kinds of apples have the same minimum average price (RMB/kg), just choose the one that appears the earliest in the list. The following are the requirements:

  • Name your file as ex2.cpp.

  • Make sure to use file streams to read the file and structs to contain the information.

  • Output: Print the name, price and weight of the apple variety with the lowest average price (RMB/kg) into stdout. Use 1 space to seperate them. Format:

    <name> <price> <weight>
    

    For consistency, you can assume that all the price and weight numbers in the file have 2 decimal places. In your output, the price and weight should have 2 decimal places as well. Hint: you may need the <iomanip> library.

  • Note: You can assume that the file can always be opened successfully in online judge. If not, then there is something wrong with your program.

Testing

Example file (suppose its name is price_list.txt):

variety1 56.81 5.12
variety2 59.50 5.52
variety3 79.28 7.25

Compile your ex2.cpp to an executable file named ex2. Execute it by

./ex2 price_list.txt

The output should be

variety2 59.50 5.52

Note that it is 59.50 instead of 59.5.

Lab Four

Not Claimed
Status
Finished
Problems
3
Open Since
2022-06-07 20:00
DDL
2022-06-14 23:59
Extension
72.0 hour(s)