| CMSC 106 | Project #7 - Magic | Fall 2002 |
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 call-by-value and call-by-reference.
For this project we are imagining that you are a powerful magician, who casts various ``spells''. Your program will keep track of an inventory of magical ingredients, which will be used up as spells are cast.
Your program begins by reading in an inventory of magical ingredients for spells. Next it will read in your Spell Book, which lists the spells you know and the ingredients that are required for casting each kind of spell. Finally, it will read in a list of spells and attempt to cast them. These spells will only be cast if you have enough of the required ingredients in inventory. Of course the amounts of ingredients in inventory must be reduced as spells are cast.
The input file will be divided into three portions: Initial ingredient inventory, Spell Book, and Spells to cast. A description of each of these sections of the input file appears below.
primary_input
file:
42 beads 11 pinches of ashes 6 ounces of camel spit 6 beads 4 cloves of garlic 6 candles 10 grams of incense 17 sheets of parchment 2 frog toes 7 dwarf lips 15 ounces of tree bark 32 grams of pixie dust 4 pinches of red pepper 7 pinches of ashes 2 frog toes 20 pounds of charcoal 5 candles 12 spider's legs 999 hours of cackling 999 hours of chanting 999 hours of babbling 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:
Love Potion 4 candles 3 hours of chanting 5 ounces of camel spit 2 candles 11111 Convert to Toad 3 ounces of bat's blood 4 sheets of parchment 2 hours of cackling 2 pounds of charcoal 3 hours of cackling 11111 Create Rain 3 spider's legs 2 grams of incense 3 grams of pixie dust 4 spider's legs 11111 Finish Project 7 12 beads 3 frog toes 4 hours of chanting 3 sheets of parchment 3 dwarf lips 11111 Levitate 6 grams of incense 3 ounces of bat's blood 2 cloves of garlic 13 candles 4 hours of babbling 11111 Reveal Truth 6 spider's legs 2 candles 13 beads 15 grams of incense 11111 Bring Luck 6 pinches of ashes 2 pinches of red pepper 12 hours of chanting 11111 11111
Notice that some of the ingredients listed may not be mentioned in the initial inventory list. Also there may be ingredients on the initial inventory list that are not mentioned in any of the spells. The spells are not sorted in any way, and the list of ingredients for each spell is also not sorted. Note that the same ingredient may appear more than once in the list for a particular spell.
Assumptions:
primary_input file:
Convert to Toad Bring Luck Remove Wart Love Potion Levitate
Notice that there may be spells listed that were not listed in the Spell Book. There may also be spells listed in the Spell Book that are not mentioned in this section. By the way, the same spell may be listed in this section repeatedly. Also, please notice that there is no limit to the number of spells 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 an IngredientList structure. Your Spell Book must be stored using an array of Spell structures.
You should write a function that accepts two parameters: An IngredientList; and an Ingredient that we will call newIngred. (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 ingredient newIngred
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 newIngred has the
name ``ounces of goop'', and the amount 15. Here is how the function
should try to add this ingredient to the list:
newIngred - i.e. set the name
of the first element to ``ounces
of goop'' and set the amount to 15.
(The function should then terminate.)
newIngred (in this case ``ounces of goop''), then you must
add the amount specified by newIngred (15)
to the amount of the ingredient of the
same type
that is already on the list. In other words, if you already have an
ingredient on the list with the name ``ounces of goop'' and the amount 4,
then you should just
increase the amount of this ingredient to 19. (The function should then
terminate.)
newIngred, then you must insert
a copy of newIngred into the list so that the names
of the ingredients in the list are always ordered alphabetically.
Since you will fill your IngredientList structures with data by repeatedly
calling this function, you may assume that the ingredients that are
already on the list are sorted alphabetically. Therefore, to find the
proper location for newIngred, you should start at the beginning
of the list
and cycle through the ingredients until you
find one that is alphabetically greater than newIngred.
This will be the spot where newIngred 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 newIngred. 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 newIngred
into the proper location. (In the event that newIngred is the
``largest'' alphabetically, you do not have to move any of the existing
members of the list - just add a copy of newIngred 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 amount 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: =================== 48 beads 11 candles 4 cloves of garlic 7 dwarf lips 4 frog toes 10 grams of incense 32 grams of pixie dust 999 hours of babbling 999 hours of cackling 999 hours of chanting 6 ounces of camel spit 15 ounces of tree bark 18 pinches of ashes 4 pinches of red pepper 20 pounds of charcoal 17 sheets of parchment 12 spider's legs Spell Book ========== Love Potion 6 candles 3 hours of chanting 5 ounces of camel spit Convert to Toad 5 hours of cackling 3 ounces of bat's blood 2 pounds of charcoal 4 sheets of parchment Create Rain 2 grams of incense 3 grams of pixie dust 7 spider's legs Finish Project 7 12 beads 3 dwarf lips 3 frog toes 4 hours of chanting 3 sheets of parchment Levitate 13 candles 2 cloves of garlic 6 grams of incense 4 hours of babbling 3 ounces of bat's blood Reveal Truth 13 beads 2 candles 15 grams of incense 6 spider's legs Bring Luck 12 hours of chanting 6 pinches of ashes 2 pinches of red pepper Time for spell casting! ======================= Attempting to cast Convert to Toad I do not carry ounces of bat's blood Attempting to cast Bring Luck Spell Successful! New inventory: 48 beads 11 candles 4 cloves of garlic 7 dwarf lips 4 frog toes 10 grams of incense 32 grams of pixie dust 999 hours of babbling 999 hours of cackling 987 hours of chanting 6 ounces of camel spit 15 ounces of tree bark 12 pinches of ashes 2 pinches of red pepper 20 pounds of charcoal 17 sheets of parchment 12 spider's legs Attempting to cast Remove Wart I do not know this spell. Attempting to cast Love Potion Spell Successful! New inventory: 48 beads 5 candles 4 cloves of garlic 7 dwarf lips 4 frog toes 10 grams of incense 32 grams of pixie dust 999 hours of babbling 999 hours of cackling 984 hours of chanting 1 ounces of camel spit 15 ounces of tree bark 12 pinches of ashes 2 pinches of red pepper 20 pounds of charcoal 17 sheets of parchment 12 spider's legs Attempting to cast Levitate I do not have enough candles I do not carry ounces of bat's blood
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 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.
Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!