In this project you will write a program using structures and structures which contain other structures as members, arrays of structures, and functions with structure parameters, using both pass-by-value and pass-by-reference.
For this project we are imagining that you run a car repair shop. By the way, I am not a mechanic and this scenario is completely fictitious, so please don't bother to tell me how unrealistic and inaccurate my examples are! Your program will keep track of an inventory of parts, which will be used up as repair jobs are performed.
Your program begins by reading in an inventory of the parts you have in stock. Next it will read in your Repair Manual, which lists the repair jobs that you know how to perform, and specifies the parts that are required to complete each kind of job. Finally, it will read in a list of jobs that you are supposed to perform this day, and it will attempt to perform them. These jobs will only be performed if you have enough of the required parts in inventory. Of course the number of parts in your inventory must be reduced as each repair job is performed.
The input file will be divided into three portions: Initial parts inventory, Repair Manual, and Jobs to perform. A description of each of these sections of the input file appears below.
primary_input
file:
2 engine 4 pcv valve 7 thermostat 8 valve cover gasket 2 engine 4 tire 4 throttle body inlet 6 starter 8 fuel pump 14 spark plug 7 intake manifold 6 pcv valve 2 clutch 3 exhaust manifold 4 valve cover gasket 4 radiator 15 fan belt 15 wheel bearing 4 tire 5 fuel filter 11 wheel weight 7 cv joint 23 cv boot 6 transmission mount 4 transmission filter 4 radiator 3 clutch 47 bolt 134 nut 51 washer 37 ignition wire 20 spark plug 14 brake caliper 60 washer 7 rotor 17 brake pad 3 master cylinder 4 compressor 3 evaporator core 2 expansion valve 17 oil filter 6 drain pan plug 23 oil, quart 17 grease, ounce 28 radiator fluid, gallon 33 brake fluid, quart 19 transmission fluid, quart 11111
Please notice that many items appear more than once on the list. Also notice that this list is not sorted in any way.
Assumptions:
primary_input
file:
Oil Change 1 oil filter 1 drain pan plug 5 oil, quart 11111 Replace Transmission 1 transmission 1 transmission mount 12 bolt 6 transmission fluid, quart 11111 Replace Tires 4 tire 4 wheel weight 11111 Replace Fan Belt 1 fan belt 11111 Replace Engine 1 engine 12 bolt 5 oil, quart 1 pcv valve 1 throttle body inlet 4 bolt 4 washer 4 nut 4 grease, ounce 2 valve cover gasket 12 bolt 16 washer 8 nut 11111 Fix Brakes 4 rotor 4 brake caliper 8 brake pad 1 master cylinder 6 brake fluid, quart 16 bolt 16 nut 4 washer 11111 Tune Up 1 oil filter 1 drain pan plug 2 valve cover gasket 5 oil, quart 1 pcv valve 1 fuel filter 4 ignition wire 4 spark plug 6 transmission fluid, quart 3 radiator fluid, gallon 11111 11111
Notice that some of the parts listed may not be mentioned in the initial inventory list. Also there may be parts on the initial inventory list that are not mentioned in any of the jobs in the repair manual. The jobs listed are not sorted in any way, and the list of parts for each job is also not sorted. Note that the same part may appear more than once in the list for a particular job.
Assumptions:
primary_input file:
Oil Change Tune Up Replace Transmission Oil Change Fix Brakes Replace Engine Oil Change Replace Tires Overhaul Engine Replace TransmissionNotice that there may be jobs listed that were not listed in the repair manual. There may also be jobs listed in the repair manual that are not mentioned in this section. By the way, the same job may be listed in this section repeatedly. Also, please notice that there is no limit to the number of jobs that may be listed in this section, so you must process them one at a time rather than trying to read them all into an array and then process them. This section does not terminate with ``11111''.
Assumptions:
Important: Your inventory must be stored using a PartsList structure. Your Repair Manual must be stored using an array of Job structures.
You should write a function that accepts two parameters: A PartsList, and a Part that we will call newPart. (You must decide whether to pass these parameters by value, or to pass them with a pointer.)
The role of the function is to add the part newPart
to the list. We will do this in such a way that the elements in the
list are always ordered alphabetically by name, and no name will appear
more than once on the list.
As an example, imagine that newPart has the
name ``brake caliper'', and the quantity 15. Here is how the function
should try to add this part to the list:
newPart - i.e. set the name
of the first element to ``brake caliper'' and set the quantity to 15.
(The function should then terminate.)
newPart (in this case ``brake caliper''), then you must
add the quantity specified by newPart (15)
to the quantity of the part of the
same type
that is already on the list. In other words, if you already have a
part on the list with the name ``brake caliper'' and with quantity 4,
then you should just
increase the quantity of this part to 19. (The function should then
terminate.)
newPart, then you must insert
a copy of newPart into the list so that the names
of the parts in the list are always ordered alphabetically.
Since you will fill your PartsList structures with data by repeatedly
calling this function, you may assume that the parts that are
already on the list are sorted alphabetically. Therefore, to find the
proper location for newPart, you should start at the beginning
of the list
and cycle through the parts until you
find one that is alphabetically greater than newPart.
This will be the spot where newPart must be inserted.
All of the items on the list from this point to the end of the list must
be moved over to make room for newPart. To move these
elements over, first move the last one, then the next to
the last one, and so on. After you have made room, copy newPart
into the proper location. (In the event that newPart is the
``largest'' alphabetically, you do not have to move any of the existing
members of the list - just add a copy of newPart to the end
of the list.)
You may need to write some ``helper'' functions that will be called by this function. Don't forget that we still have that rule about having a maximum of 30 statements per function!
primary_output file, below, for the
precise formatting.
primary_output file, below, for the precise formatting.
primary_output file, below, for the precise formatting.)
primary_output for the exact formatting requirements.
In the
event that the quantity of an item is reduced to exactly zero, you
should no longer display that item when listing the inventory.
primary_output. This will give
you an idea about how your output must be formatted. As usual, it is
important for you to check your output against ours using the diff
command.
Starting Inventory: =================== 47 bolt 14 brake caliper 33 brake fluid, quart 17 brake pad 5 clutch 4 compressor 23 cv boot 7 cv joint 6 drain pan plug 4 engine 3 evaporator core 3 exhaust manifold 2 expansion valve 15 fan belt 5 fuel filter 8 fuel pump 17 grease, ounce 37 ignition wire 7 intake manifold 3 master cylinder 134 nut 17 oil filter 23 oil, quart 10 pcv valve 8 radiator 28 radiator fluid, gallon 7 rotor 34 spark plug 6 starter 7 thermostat 4 throttle body inlet 8 tire 4 transmission filter 19 transmission fluid, quart 6 transmission mount 12 valve cover gasket 111 washer 15 wheel bearing 11 wheel weight Repair Manual ============= Oil Change 1 drain pan plug 1 oil filter 5 oil, quart Replace Transmission 12 bolt 1 transmission 6 transmission fluid, quart 1 transmission mount Replace Tires 4 tire 4 wheel weight Replace Fan Belt 1 fan belt Replace Engine 28 bolt 1 engine 4 grease, ounce 12 nut 5 oil, quart 1 pcv valve 1 throttle body inlet 2 valve cover gasket 20 washer Fix Brakes 16 bolt 4 brake caliper 6 brake fluid, quart 8 brake pad 1 master cylinder 16 nut 4 rotor 4 washer Tune Up 1 drain pan plug 1 fuel filter 4 ignition wire 1 oil filter 5 oil, quart 1 pcv valve 3 radiator fluid, gallon 4 spark plug 6 transmission fluid, quart 2 valve cover gasket Time to fix some cars! ====================== Attempting job: Oil Change Job Done! Attempting job: Tune Up Job Done! Attempting job: Replace Transmission I do not carry this item: transmission Attempting job: Oil Change Job Done! Attempting job: Fix Brakes Job Done! Attempting job: Replace Engine Job Done! Attempting job: Oil Change I do not have enough of this item: oil, quart Attempting job: Replace Tires Job Done! Attempting job: Overhaul Engine I do not know how to do this job. Attempting job: Replace Transmission I do not have enough of this item: bolt I do not carry this item: transmission Final Inventory: ================ 3 bolt 10 brake caliper 27 brake fluid, quart 9 brake pad 5 clutch 4 compressor 23 cv boot 7 cv joint 3 drain pan plug 3 engine 3 evaporator core 3 exhaust manifold 2 expansion valve 15 fan belt 4 fuel filter 8 fuel pump 13 grease, ounce 33 ignition wire 7 intake manifold 2 master cylinder 106 nut 14 oil filter 3 oil, quart 8 pcv valve 8 radiator 25 radiator fluid, gallon 3 rotor 30 spark plug 6 starter 7 thermostat 3 throttle body inlet 4 tire 4 transmission filter 13 transmission fluid, quart 6 transmission mount 8 valve cover gasket 87 washer 15 wheel bearing 7 wheel weight
All your C programs in this course should be written in ANSI C, which
means they must compile and run correctly with cc -std1 -trapuv
on the OIT
UNIX Class Cluster. You will lose credit if your program generates any
warning messages when it is compiled. Prototypes must appear for all
functions used, listed at the top of the program file.
You may use any ANSI C language features we have discussed in the course so far, plus anything we will discuss between now and the end of the semester, with the following exceptions:
goto nor the continue statement may be
used, and the break statement may not be used in any loop.
exit() library function may not be used at all, and at
most one return statement may be used in any function,
including main.
mem can be used
Using any of these disallowed C features will result in losing credit.
Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. In addition, you must have a comment before each function, explaining its action and operation. Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:
return statements in any function
You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.
The more you write individual functions to perform various manipulations on structures, the easier your code becomes to test. It is really recommended you use functions for each separate, discrete operation to be performed on any structure.
Here are some suggestions for possible functions. You do not have to use these, and you may use as many other functions as you want:
Do not use a global structure variable or array, like your book does in its example in Chapter 16! You will lose substantial credit if you use global variables in your project.
If you get an error from the first line of your first function involving a particular structure type, which identifies a syntax error right after the name of the structure type, you probably have declared the structure type after this function which uses it. All structure type definitions must precede the first use of a variable or parameter of that structure type.
Here are several other common compilation errors having to do with structures produced by the cc compiler on our class machines and what they mean:
Structure fields cannot be initialized in a structure type definition. See your class notes or text for the proper way to declare a structure variable with an initializer.
(where X is the name of a structure variable)
You are using the -> operator with something which is a
structure, not a pointer to a structure. The -> operator
only operates upon a pointer to a structure. Carefully draw a
picture of your structure variables, and look at how the statement
with the error is referencing them.
(where X is the name of a pointer variable)
This is the opposite of the above problem. You are using the . operator with something which is a pointer to a structure, not a structure.
You are using the . operator directly in front of an array
subscript (i.e., x.[y]). The . operator selects a
field of a structure, so it must be followed by a field name, not a
subscript. Perhaps you are trying to refer to an element within an
array field, but you forgot the array field name after the .
and before the subscript.
(where X is the name of an array variable)
You are trying to assign something to the name of an array.
Perhaps you are trying to copy one character string to another, but
you have to use strcpy since you cannot assign arrays.
(where X is the name of a variable being used as a function's return value)
You are probably trying to return an int from a function whose return type is a structure, not a number.
(where X is the name of a structure variable)
This is the opposite of the above problem. You are probably trying to return a structure from a function whose return type is int, not a structure.
(where X is the name of a structure variable)
You are trying to pass a structure to a call-by-reference structure function, which is expecting an argument which is a pointer to a structure.
(where X is the name of a structure variable)
This is the opposite of the above problem. You are trying to pass a pointer to a structure to a call-by-value structure function, which is expecting an argument which is a structure, not a pointer to a structure.
printf statements if your code isn't
working, to find out where! You need to know this before you can
find out why the problem is occurring.
Frequently save backup copies of your program under different names or in different subdirectories, so even if you inadvertently delete your file or change things which you realize you shouldn't have, it's easy to recover a recent version.
Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.
VIOLATIONS OF ACADEMIC HONESTY INCLUDE:
|
IT IS THE RESPONSIBILITY, UNDER THE UNIVERSITY HONOR POLICY, OF ANY STUDENT WHO LEARNS OF AN INCIDENT OF ACADEMIC DISHONESTY TO REPORT IT TO THEIR INSTRUCTOR.
Your project must be electronically submitted by the date above, to avoid losing credit as described on the syllabus. No projects more than two days late will be accepted for credit without prior permission or a valid medical excuse, as described on your syllabus. Only the project which you electronically submit, according to the procedures provided, can be graded; it is your responsibility to test your program and verify that it works properly before submitting. Lost passwords or other system problems do not constitute valid justifications for late projects, so do not put off working on your program or wait to submit it at the last minute!
Turn in your assignment using the ``submit'' program as before, except using ``7'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named ``p7.c'', submit would be run as
submit 7 p7.c.
This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.61)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -show_section_numbers -split 0 -no_navigation -no_footnode project7
The translation was initiated by Fawzi Emad on 2003-12-01