Lab 4 Ex 1: Apple Pies
You cannot submit for this problem because the homework's deadline is due.
Related Topics: exceptions, program arguments.
This exercise will help you better understand how try
, throw
and catch
work for exception handling. Also, you will practise using program arguments
.
You are going to simulate the process of buying ingredients for apple pies. The following are some basic rules:
To make an apple pie, you need 250 grams of flour, 1 egg and 2 apples.
There are 2 markets that you can visit:
market1
andmarket2
.market1
sells flour and eggs, andmarket2
sells apples.You should first visit
market1
and thenmarket2
. Ifmarket1
is open, you should first buy flour and then buy eggs. During the procedure, if any "exception" happens (eg:market1
is closed, eggs inmarket1
are not enough, etc), the buying process will end (exception caught). Seeex1.cpp
for details.The input will be a sequence of numbers read from program arguments (6 numbers in total): number of apple pies (integer type), status of
market1
(1 for open and 0 for closed), status ofmarket2
(1 for open and 0 for closed), amount of flour remaining inmarket1
(in grams, float type), number of eggs remaining inmarket1
(integer type), number of apples remaining inmarket2
(integer type). For example, if the program name isex1
and the arguments are "2 1 1 355 10 5
", i.e.,./ex1 2 1 1 355 10 5
then you will make 2 pies, both markets are open, 355g of flour and 10 eggs remain in
market1
, and 5 apples remain inmarket2
.Part of the program has already been implemented. Your work is to complete the program by filling the parts marked with
TODO
. Please do not change any code that is already written. Seeex1.cpp
for further instructions.Note: You do not need to round values in this exercise. We will not test you by any strange or long float numbers.
Testing
Compile ex1.cpp
to an executable file ex1
. Execute it with 6 arguments chosen by yourself. For example,
./ex1 3 1 1 1000 10 5
Then the output should be
You visit market1 first...
You've bought enough flour and eggs. Then you visit market2...
Apples in market2 are not enough. You still need 1 more.
It seems that today is not a good day for making apple pies.
Lab Four
- Status
- Finished
- Problems
- 3
- Open Since
- 2022-06-07 20:00
- DDL
- 2022-06-14 23:59
- Extension
- 72.0 hour(s)