1 Structs within structs typedef struct short int

  • Slides: 21
Download presentation
1 Structs within structs typedef struct{ short int year; unsigned int month; /* 1.

1 Structs within structs typedef struct{ short int year; unsigned int month; /* 1. . 12 */ unsigned int day; /* 1. . 31 */ }DATE; DATE birthday = {1963, 1, 4}; 2007 Pearson Education, Inc. All rights reserved.

2 if( birthday. year < 2000 ){ printf(" Born last century!n”); } 2007 Pearson

2 if( birthday. year < 2000 ){ printf(" Born last century!n”); } 2007 Pearson Education, Inc. All rights reserved.

3 typedef struct{ short int year; unsigned char month; /* 1. . 12 */

3 typedef struct{ short int year; unsigned char month; /* 1. . 12 */ unsigned char day; /* 1. . 31 */ }DATE; typedef struct{ DATE birthday; char gnd; float weight; }FRIEND; FRIEND esam; FRIEND zayed ={ {1990, 12, 31}, ’M’, 61. 5} /*FRIEND zayed ={ 1990, 12, 31, ’M’, 61. 5}*/ 2007 Pearson Education, Inc. All rights reserved.

4 printf( "Date of birth is %2 u/%4 u n“, zayed. birthday. day, zayed.

4 printf( "Date of birth is %2 u/%4 u n“, zayed. birthday. day, zayed. birthday. month, zayed. birthday. year ); %d decimal (29, -12) %u unsigned value (29, 12) 2007 Pearson Education, Inc. All rights reserved.

5 § Create a struct Appointment – First what is an appointment 2007 Pearson

5 § Create a struct Appointment – First what is an appointment 2007 Pearson Education, Inc. All rights reserved.

6 § Appointment = Date + Time + description § What is Date and

6 § Appointment = Date + Time + description § What is Date and what is Time 2007 Pearson Education, Inc. All rights reserved.

7 § Date = year + month + day § Time = hour +

7 § Date = year + month + day § Time = hour + minuets + seconds 2007 Pearson Education, Inc. All rights reserved.

8 typedef struct Date{ int y, m, d; } DATE; typedef struct Time{ int

8 typedef struct Date{ int y, m, d; } DATE; typedef struct Time{ int h, m, s; } TIME; typedef struct appointment{ DATE d; TIME t; char description[100]; } APPOINTMENT; create a variable of APPOINTMENT with initial values 2007 Pearson Education, Inc. All rights reserved.

9 APPOINMENT review = { {2010, 11, 28}, {5 , 30, 0}, “to review

9 APPOINMENT review = { {2010, 11, 28}, {5 , 30, 0}, “to review programming” }; APPOINMENT review = {2010, 11, 28, 5 , 30, 0, “to review programming”}; 2007 Pearson Education, Inc. All rights reserved.

10 § Create a struct name it circle which has a point 2007 Pearson

10 § Create a struct name it circle which has a point 2007 Pearson Education, Inc. All rights reserved.

11 § Create a struct name it circle which has a point – First

11 § Create a struct name it circle which has a point – First what is a circle 2007 Pearson Education, Inc. All rights reserved.

12 § Create a struct name it circle which has a point – First

12 § Create a struct name it circle which has a point – First what is a circle – Circle = ( center ) point + radius – What is a point 2007 Pearson Education, Inc. All rights reserved.

13 § Create a struct name it circle which has a point – First

13 § Create a struct name it circle which has a point – First what is a circle – Circle = point + radius – What is a point – Point = x-axis + y-axis 2007 Pearson Education, Inc. All rights reserved.

14 typedef struct { int, x, y; } POINT; typedef struct{ POINT centre; double

14 typedef struct { int, x, y; } POINT; typedef struct{ POINT centre; double rds; } CIRCLE; Typedef struct{ int x, y; }POINT; Typedef struct{ POINT centre; double rds; }CIRCLE; /*create a point variable */ 2007 Pearson Education, Inc. All rights reserved.

15 POINT startpoint; POINT startpoint = {10, 10}; create a circle its centre is

15 POINT startpoint; POINT startpoint = {10, 10}; create a circle its centre is the original point 2007 Pearson Education, Inc. All rights reserved.

16 CIRCLE crl 1 = {0, 0, 3. 5}; Create a circle and then

16 CIRCLE crl 1 = {0, 0, 3. 5}; Create a circle and then assign its center startpoint and 2. 4 to the radius 2007 Pearson Education, Inc. All rights reserved.

17 CIRCLE crl 2 ; crl 2. c. x = startpoint. x; crl 2.

17 CIRCLE crl 2 ; crl 2. c. x = startpoint. x; crl 2. c. y= startpoint. y; crl 2. rds = 2. 4; crl 2. c = startpoint; crl 2. rds=2. 4; 2007 Pearson Education, Inc. All rights reserved.

18 § Create a struct name it person where each person has a name,

18 § Create a struct name it person where each person has a name, address, and birth day 2007 Pearson Education, Inc. All rights reserved.

19 typedef struct { char first[20]; char father[20]; char family[20]; }NAME; typedef struct{ char

19 typedef struct { char first[20]; char father[20]; char family[20]; }NAME; typedef struct{ char country[20]; char city[20]; char street[2]; int bulding; int flat; } ADDRESS; typedef struct{ int y, m, d; }DATE; typedef struct { NAME pname; ADDRESS padd; DATE pbd; }PERSON; 2007 Pearson Education, Inc. All rights reserved.

20 ASS § 1 - Create a variable from each with initial values §

20 ASS § 1 - Create a variable from each with initial values § 2 - Create a variable from PERSON and then show this variable can be assigned values from the variables created at 1 2007 Pearson Education, Inc. All rights reserved.

21 ASS § Recreate the STUDENT struct with a birth day member and then

21 ASS § Recreate the STUDENT struct with a birth day member and then create two variables one initial values and the second allow the user to input the values 2007 Pearson Education, Inc. All rights reserved.