Download presentation
Presentation is loading. Please wait.
Published by미주 부 Modified over 5 years ago
1
Structure Problem Solving & Program Design in C Eighth Edition
Jeri R. Hanly & Elliot B. Koffman © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
2
Name Addr Content Lecture Lecture++;
3
Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members can be of different types.
4
Structure Definition struct name { variable declaration; . };
the keyword struct announces that this is a structure definition
5
Struct Basics Definition of a structure: Example: The “Date” structure
struct <struct-type>{ <type> <identifier_list>; ... } ; Example: struct Date { int day; int month; int year; Each identifier defines a member of the structure. The “Date” structure has 3 members, day, month & year.
6
Struct Examples Example: The “StudentGrade” structure has 5 members of
struct StudentInfo{ int Id; int age; char Gender; double CGA; }; struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; The “StudentInfo” structure has 4 members of different types. The “StudentGrade” structure has 5 members of different array types.
7
Struct Examples Example: The “StudentRecord” structure has 4
struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; struct StudentRecord{ int Id; char Dept[5]; char Gender; The “BankAcount” structure has simple, array and structure types as members. The “StudentRecord” structure has 4 members.
8
Initializing Structures
struct Rect { double x; double y; char color; double width; double height; }; struct Rect r1 = {0,0,’r’,5,10}; x y r color 5.0 width 10.0 height
9
Accessing Data Members
To access a data member use Varname.membername struct Rect r1 = {0,0,’r’,5,10}; r1.x r1.y r1.color r1.width r1.height x y r color 5 width 10 height
10
Assignment Operator Assignment operator is defined for structure of the same type. struct rect { double x; double y; char color; double width; double height; }; struct rect r1, r2 r1.x = 10.5; r1.y = 18.99; r1.width = 24.2; r1.height = 15.9; r1.color = 'r'; /* Copy all data from r1 to r2. */ r2 = r1;
11
Nested Structures We can nest structures inside structures. Examples:
struct point{ double x, y; }; point P; struct line{ point p1, p2; }; line L; struct triangle{ point p1, p2, p3; }; triangle T; (P.x, P.y) (L.p2.x, L.p2.y) (L.p1.x, L.p1.y) (T.p2.x, T.p2.y) (T.p3.x, T.p3.y) (T.p1.x, T.p1.y)
12
Nested Structures point P; line L; triangle T; L.p1.x = 2; T.p1.x = 2;
P.y = 11; (4, 11) (10, 9) L.p1.x = 2; L.p1.y = 7; L.p2.x = 10; L.p2.y = 9; (2, 7) (6, 5) T.p1.x = 2; T.p1.y = 0; T.p2.x = 6; T.p2.y = 5; T.p3.x = 8; T.p3.y = 3; (8, 3) (2, 0)
13
Nested Structs struct address { char city[21]; char state[3];
char zip[6]; }; city state zip struct address default; strcpy(default.city, “Hammond”);
14
Nested Structs Given: struct address { char city[21]; char state[3];
char zip[6]; }; struct family_rec { int id_num; char name[15]; struct address addr; double income; } family1, family2;
15
Nested Structs family1 id_num name addr city (struct address) state
zip income
16
Nested Structs - Accessing Members
family1.id_num = 5; strcpy(family1.name,”Jones”); strcpy(family1.addr.city,”Gary”); strcpy(family1.addr.state,”IN”); strcpy(family1.addr.zip,”46403”); family1.income = ;
17
} HIRE_DATE, TERM_DATE, CURRENT_DATE; struct pay_period {
struct employee { char name[MAXNAME]; int age; } emp; struct date { int month, day, year; } HIRE_DATE, TERM_DATE, CURRENT_DATE; struct pay_period { struct date start; struct date end; } Emp_Worked, Emp_Schedule; struct employee_days_worked { struct employee person struct pay_period work_schedule; } EMPLOYEE; strcpy(EMPLOYEE.person.name,“Sherlock Holmes”); EMPLOYEE.person.age = 27; EMPLOYEE.work_schedule.start.month = 4; EMPLOYEE.work_schedule.start.day = 5; EMPLOYEE.work_schedule.start.year = 95;
18
Arrays of structures An ordinary array: One type of data
An array of structs: Multiple types of data in each array element. … …
19
Arrays of Structures We often use arrays of structures.
Example: StudentRecord Class[100]; strcpy(Class[98].Name, "Chan Tai Man"); Class[98].Id = 12345; strcpy(Class[98].Dept, "COMP"); Class[98].gender = 'M'; Class[0] = Class[98]; Chan Tai Man M COMP . . . …
20
Arrays Inside Structures
We can use arrays inside structures. Example: struct square{ point vertex[4]; }; square Sq; Assign values to Sq using the given square (4, 3) (10, 3) (4, 1) (10, 1) x y x y x y x y
21
Arrays of Structures ……….......
Arrays of structures may be declared in the same way as other C data types. struct rect rectangles[20]; rectangles[0] references first structure of rectangles array. rectangles[0].color = ‘r’; x y ……… color width height 1 2 3 19
22
Structures as Arguments to Functions
When a structure is passed as an argument to a function, it is a call-by-value reference. Changes made to the formal parameters do not change the argument. A pointer to a structure may also be passed as an argument to a function. Changes made to the formal parameters also change the argument.
23
Call by Value Example s s1 Updated s struct simple { int ival;
double dval; }; struct simple fun1(struct simple s) struct simple temp1; temp1.ival = s.ival+1; temp1.dval = s.dval + 2; return temp1; } int main(void) struct simple temp2, s1 = {10, 1.5}; temp2=fun1(s1); printf(“%i %lf\n”, temp2.ival , temp2.dval ); return 0; s s1 10 10 ival 1.5 1.5 dval Updated s 11 3.5
24
Exercise (a,b)+(c,d)=(a+c,b+d) (a,b)*(c,d)=(a*c,b*d)
Write a program using structures that manipulates pairs. Addition and multiplication of pairs is defined below. (a,b)+(c,d)=(a+c,b+d) (a,b)*(c,d)=(a*c,b*d)
25
Pair Structure Store two integers to represent the first and second number of pair struct pair { int first; int second; };
26
Addition struct pair add(struct pair p1, struct pair p2) {
struct pair temp; temp.first = p1.first + p2.first; temp.second = p1.second + p2.second; return temp; }
27
Multiplication struct pair multiply(struct pair p1, struct pair p2) {
struct pair temp; temp.first = p1.first * p2.first; temp.second = p1.second * p2.second; return temp; }
28
How to use the functions
struct pair mp1,mp2,mp3,mp4; printf("Enter first pair\n"); scanf("%d %d",&mp1.first, &mp1.second); printf("Enter second pair\n"); scanf("%d %d",&mp2.first, &mp2.second); mp3 = add(mp1, mp2); printf("Addition result = (%d,%d)\n",mp3.first,mp3.second); mp4 = multiply(mp1,mp2); printf("Multiplication result = (%d,%d)\n",mp4.first,mp4.second);
29
Exercise Update the program to support the following on pairs
c*(a,b) = (c*a,c*b) (a,b)^c = (a^c,b^c)
30
Nested Structures Structure definitions may contain data members that are other structures: struct Card { char suit; int rank; }; struct Deck struct Card cards[52]; int next_card = 0; Card suit rank 1 51 …... cards …… next_card
31
Initialize the Deck for (i=0; i<13; i++) for (i=26; i<39; i++) {
d1.cards[i].suit = 'h'; d1.cards[i].rank = (i-26)+1; } for (i=39; i<52; i++) d1.cards[i].suit = 's'; d1.cards[i].rank = (i-39)+1; for (i=0; i<13; i++) { d1.cards[i].suit = 'c'; d1.cards[i].rank = i+1; } for (i=13; i<26; i++) d1.cards[i].suit = 'd'; d1.cards[i].rank = (i-13)+1;
32
Print the Deck void print(struct Deck d1) { int i;
for (i=0; i<52; i++) printf("%c %d\n", d1.cards[i].suit, d1.cards[i].rank); return; }
33
How to Shuffle the Deck? for (i=0; i<52; i++) {
1 2 3 51 ………………………… for (i=0; i<52; i++) { // pick a random card x from 0-51 // swap card x and card i }
34
BlackJack Shuffle the deck Deal first 2 cars to user
Print both cards on screen Next two cards to dealer Print only the first on screen Ask user whether he/she wants to continue Highest total <= 21 wins Jack, Queen, King are 10 points Ace is 11 points Other cards represented by their number
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.