辅导Clist 列表、线性表程序、辅导C编程

- 首页 >> C/C++编程

For many, Easter is a time for receiving chocolate eggs. Large ones, small ones, solid

ones, hollow ones, filled ones. Dark chocolate ones, milk chocolate ones, white

chocolate ones. Stripy wrapped ones, spotty wrapped ones, plain wrapped ones.

Chocolate eggs! How many did you get? Which did you get the most of? Which did

you get the least of?

The Easter Bunny has asked you to develop an app to manage my collection of Easter

eggs, to analyse the data and illustrate various distributions. You will need to show

the number of eggs per chocolatier, the total weight of chocolate per chocolatier, and

these values for a specified type of chocolate.

Eggs are characterised by their maker, i.e. Lindt, Cadbury, Pink Lady, Darrell Lea,

Kinder, Ferrero, or Mars. Only eggs from these seven chocolatiers will be stored. All

others should be discarded. All eggs of the same chocolatier should be stored in the

computerised collection together for ease of access.

2/10

Each egg should have its volume in cubic centimetres (a double), weight in grams

(an int), filling (an enumeration), wrapper style (an enumeration), and blend of

chocolate (an enumeration) stored. The collection of eggs for each chocolatier should

be stored in increasing order of volume (smallest first).

A text file exists with the basic data of the Easter eggs I received. I will give you

code to read this into your program. You will need to illustrate (using a bar chart

and/or some print outs) various information.

a Which underlying data structure (array or linked-list) will you use as a basis to

model the collection of eggs for each of the seven chocolatiers? In two–three

sentences, justify your answer.

b Which kind of abstract data type (binary tree, general tree, array, stack, priority

queue, double-ended queue, set, list, etc.) would you use to model the collection

of eggs I received for each of the chocolatiers? In two–three sentences, justify

your answer by indicating the functionality required.

c Which underlying data structure (array or linked-list) will you use as a basis to

model the collection of chocolatiers? In two–three sentences, justify your

answer.

d Which kind of abstract data type (binary tree, general tree, array, stack, priority

queue, double-ended queue, set, list, etc.) would you use to model the collection

of chocolatiers? In two–three sentences, justify your answer by indicating the

functionality required.

The types required to implement this include the following:

typedef struct {

char *name;

collection eggs;

} chocolatier;

typedef chocolatier chocolatiers;

#define NUM_CHOCOLATIERS 7

const char *MAKERS[] = { "Lindt", "Cadbury", "Pink Lady",

"Darrell Lea", "Kinder", "Ferrero", "Mars" };

chocolatiers basket;

In other words, you must create a struct (called chocolatier in the above

example) which combines the chocolatier name with your answer to (a) above to

represent the collection of eggs for that particular maker, and then use that as a basis

for another type (used to create a variable called basket in the above example)

which uses your answer to (c) above to create a collection of chocolatiers.

An egg is defined as follows:

3/10

struct egg_int {

double volume;

int weight;

chocolate choc;

wrapping wrap;

filling fill;

};

typedef struct egg_int *egg;

and you may assume the existence of those types and the following types and

functions:

typedef enum {dark, milk, white} chocolate;

typedef enum {stripy, spotty, plain} wrapping;

typedef enum {solid, hollow, filled} filling;

void init_egg(egg *e, double v, int *w, chocolate c,

wrapping r, filling f);

double get_volume(egg e);

int get_weight(egg e);

chocolate get_choc(egg e);

wrapping *get_wrap(egg e);

filling *get_fill(egg e);

void set_volume(egg e, double v);

void get_weight(egg e, int w);

void set_choc(egg e, chocolate c);

void set_wrap(egg e, wrapping r);

void set_fill(egg e, filling f);

char *to_string(egg e);

A Visual Studio project is available on MyLO for you to download and use as a

starting point. This comprises the following files:

 egg.h and egg.c — the Egg ADT as specified above. These files are

complete;

 assig_two118.c — the file which contains the main() function and

other functions which implement the required task (including reading eggs

from the text file).

e You must complete assig_two118.c

Start by adding the collection and chocolatiers types from above.

You may add other types, constants and variables as necessary.

Then, complete the following functions:

 get_chocolatier() — which takes a chocolatier name, locates it

in the MAKERS array and returns its index (or –1 if it is not present);

 add_existing() — which takes the address of a non-empty

collection of eggs for a particular chocolatier and an egg to add to that

4/10

chocolatier’s collection and which adds the egg to the collection in

increasing order of volume;

 add_egg() — which takes the name of a chocolatier and an egg and

which either stores the name and egg in basket if there are none for

that chocolatier yet, or which calls add_existing() to do so if there

are already eggs for that chocolatier;

 process_category() — which takes a collection of eggs for a

particular chocolatier, a letter indicating which category should be

processed, and an int indicating which value in the category the

processing should occur for. Possible letters, values and actions are:

o 't' — calculate the total number of eggs in the collection;

o 'w' — calculate the total weight of eggs in the collection;

o 'v' — calculate the total volume of eggs in the collection;

o 'c' — calculate the number of eggs of that particular chocolate

value (dark, milk, or white);

o 'r' — calculate the number of eggs of that particular wrap

value (stripy, spotty, or plain); and

o 'f' — calculate the number of eggs of that particular fill value

(solid, hollow, filled).

The answer (a double) should be returned.

 show_graph() — which takes a letter (see above), a string description

of the output to be displayed (see sample output), and a value for the

category (see above) or –1 if the category is total, weight, or volume, and

which displays the histogram of titles, asterisks (unless weight or volume),

and totals (which can be obtained using process_category() for

each chocolatier); and

 main() — which should initialize basket, call read_in_data(),

and call show_graph().

If your answer to (a) or (c) above is a linked-list then you will also need to write

node.h and node.c and add them to the project.

The project also contains the data file. This is just a text file which can be opened and

read with most applications. It contains details of eggs that I received over Easter, but

sometimes my little sister created some false data — these have a chocolatier name

which is not one of the valid seven stated above.

Program specification

First you must obtain the eggs from a text file. The data must be stored in appropriate

collections. At the top level there should be 7 chocolatiers and for each there should

be a collection of eggs (stored in ascending order of volume).

As an egg is read from the file, the details should be checked for validity. If invalid, it

should be counted, but discarded. If valid, its data should be stored in the appropriate

collection (depending upon the chocolatier).

5/10

Once the data have been read in and stored in the collections, output should be

provided. This could be the total number of eggs, the total volume, the total weight,

the number of eggs per different chocolate type (dark, milk, white), the number of

eggs per different wrapping (stripy, spotty, plain), or the number of eggs per fill type

(solid, hollow, filled). Each of these should be calculated per chocolatier (see below).

A histogram illustrating the results per chocolatier should be displayed with one

asterisk printed for every 25 eggs (other than for volume and weight — see sample

output below).

Each histogram should state the count of ‘invalid’ eggs beneath it.

The output of your program should look something like the following (with the bold

added in this document for emphasis only):

Easter Eggs -- total number

Lindt | ************************************************************************** 1859

Cadbury | **************************************************************************** 1911

Pink Lady | ************************************************************************* 1846

Darrell Lea | ************************************************************************** 1867

Kinder | ************************************************************************** 1867

Ferrero | ************************************************************************ 1822

Mars | ***************************************************************************** 1942

Invalid eggs: 1886

And the most common chocolatier for total number eggs is......Mars!

Easter Eggs -- summed volume

Lindt | 3887245.910

Cadbury | 4024133.045

Pink Lady | 3876976.102

Darrell Lea | 3940421.478

Kinder | 3853535.410

Ferrero | 3989328.427

Mars | 3962688.109

Invalid eggs: 1886

And the most common chocolatier for summed volume eggs is......Cadbury!

Easter Eggs -- summed weight

Lindt | 7676788

Cadbury | 8127868

Pink Lady | 7892665

Darrell Lea | 7941233

Kinder | 7726915

Ferrero | 8010267

Mars | 7820819

Invalid eggs: 1886

And the most common chocolatier for summed weight eggs is......Cadbury!

Easter Eggs -- dark chocolate

Lindt | ************************* 631

Cadbury | ************************ 611

Pink Lady | ********************** 571

Darrell Lea | ************************* 644

Kinder | ************************* 645

Ferrero | ************************ 613

Mars | *************************** 678

6/10

Invalid eggs: 1886

And the most common chocolatier for dark chocolate eggs is......Mars!

Easter Eggs -- milk chocolate

Lindt | ************************* 637

Cadbury | ************************** 665

Pink Lady | ************************** 650

Darrell Lea | ************************ 619

Kinder | ************************ 608

Ferrero | ************************* 640

Mars | ************************** 657

Invalid eggs: 1886

And the most common chocolatier for milk chocolate eggs is......Cadbury!

Easter Eggs -- white chocolate

Lindt | *********************** 591

Cadbury | ************************* 635

Pink Lady | ************************* 625

Darrell Lea | ************************ 604

Kinder | ************************ 614

Ferrero | ********************** 569

Mars | ************************ 607

Invalid eggs: 1886

And the most common chocolatier for white chocolate eggs is......Cadbury!

Easter Eggs -- stripy wrap

Lindt | ************************** 663

Cadbury | ************************* 639

Pink Lady | ************************* 629

Darrell Lea | ************************* 641

Kinder | ************************ 611

Ferrero | *********************** 587

Mars | ************************** 673

Invalid eggs: 1886

And the most common chocolatier for stripy wrap eggs is......Mars!

Easter Eggs -- plain wrap

Lindt | ************************ 611

Cadbury | ************************* 629

Pink Lady | ************************ 614

Darrell Lea | ************************ 609

Kinder | ************************ 610

Ferrero | ************************ 624

Mars | ************************* 640

Invalid eggs: 1886

And the most common chocolatier for plain wrap eggs is......Mars!

Easter Eggs -- spotty wrap

Lindt | *********************** 585

Cadbury | ************************* 643

Pink Lady | ************************ 603

Darrell Lea | ************************ 617

Kinder | ************************* 646

Ferrero | ************************ 611

Mars | ************************* 629

Invalid eggs: 1886

7/10

And the most common chocolatier for spotty wrap eggs is......Kinder!

Easter Eggs -- solid

Lindt | *********************** 598

Cadbury | ************************* 649

Pink Lady | ************************* 625

Darrell Lea | ************************* 643

Kinder | ************************ 624

Ferrero | ************************ 623

Mars | ************************* 625

Invalid eggs: 1886

And the most common chocolatier for solid eggs is......Cadbury!

Easter Eggs -- filled

Lindt | ************************* 630

Cadbury | ************************ 612

Pink Lady | ************************ 610

Darrell Lea | *********************** 587

Kinder | ************************ 616

Ferrero | *********************** 597

Mars | ************************** 662

Invalid eggs: 1886

And the most common chocolatier for filled eggs is......Mars!

Easter Eggs -- hollow

Lindt | ************************* 631

Cadbury | ************************** 650

Pink Lady | ************************ 611

Darrell Lea | ************************* 637

Kinder | ************************* 627

Ferrero | ************************ 602

Mars | ************************** 655

Invalid eggs: 1886

And the most common chocolatier for hollow eggs is......Mars!

Program Style

The program you write for this assignment must be contained in five files as follows:

 egg.h and egg.c for managing a chocolate egg;

 node.h and node.c for nodes of linked lists (if appropriate);

 assig_two118.c for the main algorithm which controls the reading of the

data, the management of the collections, the calculation of the results, and the

display of the histogram. No function should be longer than about half a

screen-full or so of code.

Your program should follow the following coding conventions:

 const variable identifiers should be used as much as possible, should be

written all in upper case and should be declared before all other variables;

 #defined symbols should only be used for constant values if const is

inappropriate, for example the size of an array.

 global variables should be used sparingly with parameter lists used to pass

information in and out of functions.

8/10

 local variables should only be defined at the beginning of functions and their

identifiers should start with a lower case letter;

 every if and if-else statement should have a block of code (i.e.

collections of lines surrounded by { and }) for both the if part and

the else part (if used)

 every do, for, and while loop should have a block of code (i.e. {}s)

 the keyword continue should not be used;

 the keyword break should only be used as part of a switch statement;

 opening and closing braces of a block should be aligned;

 all code within a block should be aligned and indented 1 tab stop (or 4 spaces)

from the braces marking this block;

 commenting:

o There should be a block of header comment which includes at least

 file name

 student name

 student identity number

 a statement of the purpose of the program

 date

o Each variable declaration should be commented

o There should be a comment identifying groups of statements that do

various parts of the task

o Comments should describe the strategy of the code and should not

simply translate the C into English

What and how to submit

What to submit

You must make both a paper and an electronic submission.

Paper submission

 A paper cover page (blanks can be collected from the Information and

Communications Technology Discipline Help Desk).

 Written answers to parts (a)–(d) above.

 A landscape oriented print-out of assign_two118.c and any other

program files you have added to the solution. Your assignment will not be fully

marked unless these are present.

Electronic submission

 You should submit the entire Visual Studio project folder compressed as a ZIP

file.

How to submit

Paper submission

 Firmly staple together all of the required documents (with the signed cover

page on top) and place them in the appropriate submissions box near the ICT

Help Desk.

9/10

Electronic submission

 You should ZIP the Visual Studio project folder and then submit it to the

“Assignment 2” assignment drop-box on KIT107’s MyLO site.

Marking scheme

Task/Topic Maximum

mark

Design

ADTs chosen wisely 3

Data structures correct and justified 3

Program operates as specified

node.c correctly completed (if required, or array equivalent) 6

main() function shows abstraction of algorithm 2

Chocolatier index successfully found (get_chocolatier()) 2

Egg added —

 As the first for each chocolatier (add_egg()) 2

 For an existing chocolatier (add_existing()) 2

 In increasing order by volume (add_existing()) 3

 Discarding but counting invalid eggs (add_egg()) 1

Totals correctly calculated for each of six categories (process_category()) 4

Histogram displayed (show_graph()) —

 With title 1

 With chocolatier name 1

 With asterisks (when appropriate) 2

 With total 1

 Number of discarded eggs displayed 1

 With ‘winning’ details 2

Program Style

Does not unnecessarily repeat tests or have other redundant/confusing code 4

Uses correctly the C naming conventions 4

Alignment of code and use of white space makes code readable 4

Always uses blocks in branch and loop constructs 4

Meaningful identifiers 4

Variables defined at the start of functions only 4

Header comments for the program (author, date etc) 4

Each variable declaration is commented 4

Comments within the code indicate the purpose of sections of code (but DO NOT just duplicate

what the code says)

4

Plagiarism and Cheating:

Practical assignments are used in the ICT discipline for students to both reinforce and

demonstrate their understanding of material which has been presented in class. They

have a role both for assessment and for learning. It is a requirement that work you

hand in for assessment is substantially your own.

Working with others

One effective way to grasp principles and concepts is to discuss the issues with your

peers and/or friends. You are encouraged to do this. We also encourage you to discuss

aspects of practical assignments with others. However, once you have clarified the

principles, you must express them in writing or electronically entirely by yourself. In

10/10

other words you must develop the algorithm to solve the problem and write the

program which implements this algorithm alone and with the help of no one else

(other than staff). You can discuss the problem with other students, but not how to

solve it.

Cheating

 Cheating occurs if you claim work as your own when it is substantially the

work of someone else.

 Cheating is an offence under the Ordinance of Student Discipline within the

University. Furthermore, the ICT profession has ethical standards in which

cheating has no place.

 Cheating involves two or more parties.

o If you allow written work, computer listings, or electronic version of

your code to be borrowed or copied by another student you are an

equal partner in the act of cheating.

o You should be careful to ensure that your work is not left in a situation

where it may be stolen by others.

 Where there is a reasonable cause to believe that a case of cheating has

occurred, this will be brought to the attention of the unit lecturer. If the

lecturer considers that there is evidence of cheating, then no marks will be

given to any of the students involved. The case will be referred to the Head of

School for consideration of further action.


站长地图