DOC PREVIEW
UIUC CS 101 - lect2223

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

22&23-2 • Understand how structure data types are implemented in C. • Use “.” operator to access members of a structure. • See how to declare pointers to structure variables. • Use “ -> ” operator to access members of a structure when using pointers. • Related Chapter: ABC 9.1 – 9.622&23-3 1. Problem Definition Write a program that keeps an inventory for an auto parts store. Previously, the information for each auto part was kept on an index card. Each index card had the following fields: id --- a unique identifier for your parts name ---- a category name for the part, like “air filter” quantity --- the quantity in stock price ---- retail price The auto parts store can have as many as 30,000 different parts. Your program should define a new C data type named “part”. You will declare 30,000 variables of data type “part”. Each variable (like an index card) should hold the values for all of the fields listed above.22&23-4 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) We first define a new data type “part”. Then in main we will declare an array of 30,000 variables each of data type “part”. Your program will display a menu with the following choices: 0. Exit 1. Display the parts. 2. Find a part. 3. Enter information for a new part. For each menu item we will write a corresponding function to carry out that menu option. Input = A choice from the menu. For menu choice 2. the user must supply the part “id”. For choice 3. the user must supply all the information for the new auto part. Output= For choice 1. all the fields of all the parts will be displayed on the computer screen.#include <stdio.h> #include <stdlib.h> #define MAX_SIZE 30000 typedef struct { /* user defined data-type called part */ int id; /* this is a definition*/ char name[32]; /* no variables are declared */ int quantity; /* location is outside any block */ float price; } part; int menu(void) { int choice; /* C merges strings */ printf("\n0. Exit \n" "1. Display the parts.\n" "2. Find a part.\n" "3. Enter information for a new part.\n" "choice: "); scanf("%i",&choice); return choice; } /* end of menu */ /* continued on next slide */void display_one_part(part p) { printf(“%-9i %-31s %-5i $%.2f \n”, p.id , p.name , p.quantity , p.price); } /* end of display_one_part */ int display_parts(part parts[],int count) { int i; for(i = 0; i < count; ++i) display_one_part(parts[i]); } /* end of display_parts */ /* continued on next slide */int find_part(part parts[], int count) { int i, id_sought; /* return the index of the array for given id */ /* return the value -1 if id_sought not found */ printf("Enter an id number: "); scanf("%i",&id_sought); for(i= 0; i < count; ++i) if (id_sought == parts[i].id) return i; return -1; } /* end of find_part */ /* continued on next slide */int add_part(part parts[],int count, int max_size) { int i; if (count == max_size) { printf(“insufficient space to add another part\n”); return count; } /* use C regular expression [ ] to read a string with blanks*/ /* ^ means NOT */ printf("Enter one part in format \n" printf(" id; part name ; quantity; price \n"); scanf(" %i; %[^;]; %i; %f", &parts[count].id , parts[count].name , &parts[count].quantity , &parts[count].price); return count+1; } /* end of add_part */ /* continued on next slide */void main(void) { int choice, index; part parts[MAX_SIZE]; /* MAX_SIZE is a constant = 30000 */ int count = 0; /* actual number of parts in array */ while(1) /* infinite loop */ { choice = menu(); switch(choice) { case 0: printf("Bye bye!\n"); return; case 1: display_parts(parts,count); break; case 2: index = find_part(parts,count); if ( index != -1) display_one_part(parts[index]); else printf("Part not found!\n"); break; case 3: count = add_part(parts,count,MAX_SIZE); } /* end of switch */ } /* end of while */ } /* end of main */22&23-10 (see demo in lecture)22&23-11 To set up a structure in C, we need to (1) Define a structure type. There are several ways to do this in C. In general, we will use the typedef mechanism to do this. (2) Declare variables to be of that type. Note: The built-in data types (e.g. int, float, char,…) can be used to declare variables in the C language. These data types don’t have to be defined by the user. Structures provide the user with the means to define custom designed data types.22&23-12 You can use the typedef mechanism in C to create an alias for a structure. The code above names “part” as a alias for, typedef struct { int id; char name[32]; int quantity; float price; } part; struct { int id; char name[32]; int quantity; float price; };22&23-13 Structure definitions do not reserve storage, they are used to define a new data type. For example, the code above doesn’t allocate any storage. It doesn’t reserve memory for id, name, quantity or price --- the fields or members of the structure. Also, part is not a variable its like int, char ,float, double. typedef struct { int id; char name[32]; int quantity; float price; } part;22&23-14 Once you have defined a new data type, you can declare (and initialize) a variable of this data type as in, The variables p is declared to be of data type “part” where “part” is a synonym for the structure, part p ={1001,”Air Filter”, 100, 12.49}; Structure variables, like p above, are initialized in the same way you initialize an array. struct { int id; char name[32]; int quantity; float price; };22&23-15 p .id 1001 12.49 “Air Filter” part p ={1001,”Air Filter”,100, 12.49}; After the declaration of p, the picture of memory looks like, .name 100 .quantity .price 1000 1040 1004 1036 typedef struct { int id; char name[32]; int quantity; float price; } part;22&23-16 part parts[30000]; We can even declare an array of structure data type. typedef struct { int id; char name[32]; int quantity;


View Full Document

UIUC CS 101 - lect2223

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect2223
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect2223 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect2223 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?