关键词 > CMPT125

CMPT 125 Assignment #2

发布时间:2024-06-22

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Assignment #2     CMPT 125

Problem: Write a C program to model some aspects of running a Coin Sorter.

You may work alone or with a partner on this assignment. Groups of more than two students are not allowed.

The code you submit for this assignment will be tested automatically against a grading test plan. If your code does not compile and produce results the MAXIMUM grade you will receive is 20%.

You will be given input and output files that test some of the requirements listed below,  do not assume that requirements not tested in the provided files will not be tested when the assignment is graded.

Each student or pair will submit one file holding C code. The name of the file should be CoinSorter.c

For this assignment you will submit properly commented code that is consistent with the course coding standards posted with the assignment. You are writing a program to simulate a Coin Sorting Machine. The Coin Sorter machine separates up to five kinds of coins from a bag of assorted coins. It separates coins based on  condition, diameter, weight, and image. Any user can configure the coin sorter to sort any group of coins. An example of a group of coins would be the coins used by any nation.

The first step of using the coin sorter will be configuring the coin sorter for a particular set of coins. The information needed to configure your coin sorter is the name, weight, diameter and roll capacity of each of the types of coin. The roll capacity for a type of coin is the number of coins of that type contained in one roll of coins of that type.

Sample configuration information for Canadian coins gives the following weights and diameters

Coin

Weight

Diameter

Coins per roll OR roll capacity

Nickel

3.95g ± 0.35g

21.0mm ± 0.8mm

40

Dime

1.75g ± 0.45g

18.0mm ± 0.7mm

50

Quarter

4.40g ± 0.4g

23.7mm ± 0.9mm

40

Loonie

7.00g ± 0.5g

26.0mm ± 1.0mm

25

Twonie

7.3g± 0.55g

28.0mm ± 1.1mm

25

Contents of sample configuration file for information in the table above are shown below. There maybe any number of spaces or tabs between each of the numbers and any number of blank lines between each line

containing data.

5

Nickel 3.95 0.35 21.0 0.8 40

Dime 1.75 0.45 18.0 0.7 50

Quarter 4.40 0.4 23.7 0.9 40

Loonie 7.00 0.5 26.0 1.0 25

Twonie 7.3 0.55 28.0 1.1 25

The first row in the configuration file holds the number of distinct types of coins we can look for when sorting. This number must be between 1 and 5 inclusive (1 <= number <=5). Each of the other lines of data is the data describing one type of coin. In order the numbers and strings in each row represent

.     the name of a coin of this type

.     the weight of a coin of this type in grams (0.0

.     the variability in the weight of a coin of this type in grams (0.1g

.     the diameter of a coin of this type in mm (0.0

.     the variability in the diameter of a coin of this type in mm (0.1mm

.     the number of coins in a roll of coins of this type (5 <= number of coins <=50)

The configuration file is not the only input file. Once the coin sorter is configured then we need to open the coin input file. The coin input file holds information about each coin to be sorted. The first row in the coin input file holds the number of coins the coin sorter will be sorting. Each remaining line in the input file provides information about one coin. The information on each line (describing each coin) is

.     A double variable holding the weight of the coin in g

.     A double variable holding the diameter of the coin in mm

.     A string showing the quality of the coin. The string should hold one of two values “bent” or “usable”

.     A string  indicating the results of the visual matching done by the coin sorter. The string holds one

of the following three values, “BothSidesMatch”, “OneSideMatches” or “NoMatch” . The first value indicates that the images of both faces of the coin match images in the library of images for that coin type. The second value indicates that one face of the coin matched an image in the library of images for that type of coin. The third value indicates that no match for either side of the coin being analyzed was found.

In both files it is possible that there are more or fewer lines of data in the file than the number of lines indicated in the first line of the file. If there are more lines of data in the file than indicated in the first line of the file ignore the extra lines of data. If there are fewer lines of data than indicated in the first line of the file, then print the message indicated in the discussions below and proceed to printing the summary.

PLEASE MAKE THE SIZE OF THE CHARACTER ARRAY OR STRING HOLDING EACH OF YOUR FILENAMES >=1024 we may need to use long file names when grading your code.

Your simulation of the Coin Sorter will incorporate the following functionality:

1.     The properties of each of the coin types will be stored in an instance of a structure of type

CoinType. Define this global structure. The elements of a structure of type CoinType are the six variables whose values are given in every line in the configuration file describing a coin type.

2.     The coin sorter will be configured for a particular set of coins using a configuration file like the

example shown above. Your program will prompt the user for the name of that configuration file using the prompt below then read the supplied name. (Note that this prompt ends with one space),

Please enter the name of the configuration file:

3.     You will open the configuration file and check that the configuration file opened correctly.

4.     If the configuration file did not open correctly, you will print the first error message below then

you should repeat step 1, 2, 3 and if needed 4. These steps can be repeated up to a total of 3 times. If after 3 tries a configuration file has not been successfully opened print the second error message below and terminate the program.

NOTE:   in  the error message below FILENAME  must replace the name of the configuration file provided by the user.

ERROR: The opening of the configuration file, FILENAME, failed.

ERROR: Exceeded the maximum number of tries to open the configuration file.

5.      Read the number of coin types in the configuration file. Verify that an integer has been read and

that the value of that integer is in the range  0

ERROR: at end of file, no more data in the file.

ERROR: data corrupt, could not read any digits into the variable

ERROR: the value of number of coin types is out of range.

6.      Each remaining line of data in the configuration file contains values for each of the six variables

that describe a coin type. For each type of coin (each line of data in the configuration file), read all six values. As you read the six values in the line verify that a value is read into each of the five numeric variables, and that the value of each numeric variable read is in range. If any of these tests fail print the appropriate error message selected from the error messages below, then clean up and terminate your program.

If there are not enough lines of data in the configuration file (fewer than indicated by the number of coin types), print the appropriate message from the list below clean up and terminate the program

ERROR: Could not read the weight of the coin

ERROR: Could not read the variance of the weight of the coin

ERROR: Could not read the diameter of the coin

ERROR: Could not read the variance of the diameter of the coin

ERROR: Could not read the roll capacity

ERROR: Weight of the coin is out of range

ERROR: Variance of the weight of the coin is out of range

ERROR: Diameter of the coin is out of range

ERROR: Variance of the diameter of the coin is out of range

ERROR: Roll capacity for the coin is out of range

ERROR: Not enough rows (types) in the configuration file

7.      Place each value read from one line of the configuration file into one dynamically allocated

instance of your CoinType structure.  If the dynamic allocation of a CoinType structure fails then print the error message below, clean up and terminate the program.

ERROR: Unable to allocate memory for a CoinType structure

The CoinType structures for all coin types should be kept together in an array of CoinTypes.

8.      Prompt for and read the filename of the file containing the coin input data. You should use the

prompt below (Note that this prompt ends with one space), Type the name of the coin input file:

9.      Open the coin input file

10.   Check the coin input file is open. If the file has not opened correctly, print the first following

message then go back to step 8. Re-prompt a maximum of 2 times. (prompt a maximum of 3 times). If all three times fail print the second error message below, clean up and terminate the program.

ERROR: File FILENAME is not open for coin input

ERROR: You exceeded maximum number of tries allowed for entering the coin input file name

11.   Prompt for and read the filename of the output file. You should use the prompt below (Note that

this prompt ends with one space),

Type the name of the output file which will hold the simulation results:

12.   Open an output file to save the results of your simulation. Check if the file is open. If the file is not

open print the first following message, then go to step 11. Repeat step 11 a maximum of two additional times (three times total). If all three tries fail print the second message below, clean up and terminate the program

NOTE:  in this message FILENAME is replaced with the output filename provided by the user. ERROR: File FILENAME is not open for output

ERROR: You exceeded maximum number of tries allowed while entering the output file name

13.  Read the number of lines of data in the coin input file (the first integer in the file, alone on the first

line of the file). 1 <= number of lines of data <= 5000

14.  The value of the number of coins read from the file must be verified.

a)   If the file is empty (no number of lines of data is present in the file, no data or blank lines are in the file), print the following message then clean up and terminate your program

ERROR: Coin input file is empty

b)   If the first character cannot be part of an integer print the following message, then clean up then terminate your program

ERROR: First piece of data in the coin input file is not an integer

c)    If the number of coins is out of range (<=0 or > 5000) print the following message, then close

your open files then terminate your program    ERROR: The number of coins is out of range

REPEAT THE FOLLOWING BLOCK OF ACTIONS “NUMBER OF LINES” TIMES(number of coins times)

15.  Read one line of data from the input file. The first double will read will be the weight measurement,  the second double read will the diameter measurement, the third value read (a string) should be  “usable” or “bent”, the fourth value read (a string) should be “BothSidesMatch” , “OneSideMatches” or “NoMatch”. These four inputs should be on the same line of the input file and should be separated  by any number of spaces and/or tabs.

16.  In the data file Line 0 contains the number of lines of data, line 1 is the first line containing weight, diameter and two strings. Line N is the Nth line containing weight, diameter and two strings. Blank lines are not counted. It is possible that the data file contains more lines of data or less lines of data than is indicated by the number of lines given as the first value in the file. If there are more lines of data than the number specified on line 0, ignore the extra lines. If there are fewer lines of data than the number specified on line 0 then step 17b will be used to exit this repeated block.

17.  Each time a line of data is read it must be checked for each of the following potential problems. In all cases listed below, NNN should be replaced with the number of the line containing the problem pointed out in the message (in the data file). Only one message should be printed for a given line of data even if the line contains more than one error. If two errors occur in the same line print the message for the error that is earliest in the list below.

a)   If weight data read was not a double (did not begin with a digit or sign) print the message ERROR: Weight value read, on line NNN, is not a double

Simulation terminated early: Please correct your data file

Then ignore the remainder of the data, and leave the repeated block

b)   If no data is read, because there was no more data in the file (the last line of data was read by the previous read OR there are no lines of data, just a number of lines) print the message

ERROR: No more data

Simulation completed early before line NNN of input Then leave the repeated block.

c)    If the weight was  not within the  correct  range  of values  0.0<= weight <=40.0, print the message

ERROR: Weight out of range, ignoring line NNN in the input file

d)   If only the weight measurement was available on the present line of data (present line can be any line of data including the first or last line of data)

ERROR: Weight measurement only Ignoring line NNN of the input file

Then ignore the present line and move on to the next line

e)   If diameter value was not a double (did not begin with a digit or sign) print the message

ERROR: Diameter value read on line NNN is not a double

Simulation terminated early: Please correct your data file

Then ignore the remainder of the data, and leave the repeated block

f)    If the  diameter was  not within  the  correct range of values 0.0<= diameter<=60, print the message

ERROR: Diameter out of range, ignoring line NNN in the input file

g)        If only two double values were present on the line of data (present line can be any line of data including the first line or the last line)

ERROR: Weight and diameter measurements only Ignoring line NNN of the input file

Then ignore the present line and move on to the next line

h)       If the string giving the result of the test to  determine if the  coin is bent is not  “bent”  or “usable” print the following message

ERROR: Result of test to determine if coin is bent at line NNN is invalid Ignoring this line of data

Then ignore the present line and move on to the next line

i) If only weight, diameter, and one string are present on the line of data (present line can be any line of data including the first line and the last line)

ERROR: Weight and diameter measurements and bent string only Ignoring line NNN of the input file

Then ignore the present line and move on to the next line

j)     If the value of the image processing result is not “BothSidesMatch” or “OneSideMatches” or “NoMatch” print the following message

ERROR: image processing result at line NNN is invalid Ignoring this line of data

Then ignore the present line and move on to the next line

k)    If there is extra data at the end of the line of data

ERROR: Extra data at line NNN. Ignoring extra data

Where NNN is the line number of the out-of-range data (first line in the file is line 1). Then ignore the data just read and move on the reading and analyzing the next coin.

18.  If the data has been read with none of the problems above, or if there was extra data in the line,

then determine which type of coin the row of data represents by completing the following steps in order

a)   If the coin is bent all other data about the coin is ignored. The coin is type “bent”

b)   Check  to see if the weight and  diameter of the coin are simultaneously in the range for a configured coin type. If they are not the coin is of type “other.” If they are, go on to step c)

c)    If the image of the front side of the coin matches one of the reference images of the front side of the nth coin type, AND the image on the back side of the coin matches one of the reference images of the back side of the nth coin type the coin is identified as a coin of the nth  coin type. If both images do not match the coin is of type “other”

19.   For  each of the configured types of coins and for each coin that is identified as a coin of the nth configured coin type

a)   The coin of the nth configured coin type must be put in a wrapper for the nth   configured coin type.

b)   If the added coin fills the wrapper, then the wrapper should be removed and replaced by a new empty wrapper before the next coin is processed.

20.   Bent coins are placed in containers, not in wrappers. Other coins are also placed in containers. The

bent coin container holds up to 300g. The other coin container holds up to 200g. When you add a bent coin or an “other” coin to its container

a)   The weight of the coins in the container will be checked

b)   If the new coin weighs too much to fit into the container, the container will be replaced and the weight of coins in the new container will be updated to be the weight of the new coin

c)    Otherwise, the new coin will be added to the container

d)   The weight of coins in the container will be updated. EITHER

21.  When the coin is determined to be a coin of the nth coin type, and it is not the last of the nth coin type

in a roll print the following messages. In these messages TYPE’s must be replaced with the name of the type followed by an apostrophe s.

The Coin Sorter has sent one coin to the TYPE’s wrapper There are now NN coins in the TYPE’s wrapper

22.   When the coin is bent and adding the bent coin to the bent coin container does not make the bent

coin container overweight (print the following messages)

The Coin Sorter has sent this coin to the bent coin container     The coins in the bent coin container now weigh nnn.nn grams

23.   When the coin is not one of the coin types we are sorting into wrappers (and is not bent) then the

weight of the other container

The Coin Sorter has sent this coin to the other coin container

The coins in the other coin container now weigh nnn.nn grams OR

24.   When the coin is determined to be a of the nth coin type, and it is the last of the nth coin type in a roll

print the following messages. In these messages TYPE’s must be replaced with the name of the type followed by an apostrophe.

The Coin Sorter has sent one coin to the TYPE’s wrapper The Type wrapper is now full

The Type wrapper has now been replaced

25.   When the coin is bent and adding the bent coin to the bent coin container would make the bent coin

container overweight (print the following messages)

The Coin Sorter has sent this coin to the bent coin container

This coin does not fit in the bent coin container The bent coin container has been replaced

The coin in the new bent coin container weighs nnn.nn grams

26.   When the coin is not one of the coins we are sorting into wrappers (and is not bent) it will be placed

into the other coins container, unless adding the coin to the other coin container makes the other coin container overweight

The Coin Sorter has sent this coin to the other coin container This coin does not fit in the other coin container

The other coin container has been replaced

The coin in the other coin container now weigh nnn.nn grams END OF REPEATED BLOCK

27.   Summarize your simulation by printing the number of each kind of coin processed and the weight

of bent coins and other coins processed. Print these results with the following formats. Skip three lines before printing the summary.

SUMMARY

The Coin Sorter made nnn rolls of nickels.

There are oo nickels in the partially full roll.

The Coin Sorter made ppp rolls of dimes.

There are xx dimes in the partially full roll.

The Coin Sorter made mmm rolls of quarters.

There are yy quarters in the partially full roll.

The Coin Sorter made bbb rolls of loonies.

There are zz loonies in the partially full roll.

The Coin Sorter made ccc rolls of toonies.

There are ww toonies in the partially full roll.

The Coin Sorter processed qqqqq other coins.

The other coins filled sss containers

There were tt other coins in the partially full container

The total weight of the other coins was rrrrr.rrr grams

The Coin Sorter processed aaaaaa.aaaa g of bent coins

END OF TASK TO BE IMPLEMENTED ASA C PROGRAM

ALSO IMPORTANT

All prompts should be printed only to the screen

All output should be printed to cout and to the output file

All error messages should be printed only to cerr

sample tests will be posted later.