lap-trinh-c_cao-tuan-dung_week14-structure
lap-trinh-c_cao-tuan-dung_week14-structure
Introduction
week 14:Structure
om
Lecturers : Cao Tuan Dung
.c
Dept of Software Engineering
Hanoi University of
ng
Technology co
For HEDSPI Project
an
th
ng
• Structure
u
• Structure declaration
• Using typedef
• Accessing field of structure variables.
– Programming Exercises
1
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Structure
• A structure in C is a collection
of items of different types.
om
structures larger and more
complex than the ones we
.c
have discussed so far.
ng
co
an
th
ng
Defining a struct in C
o
du
struct struct-name
u
{
cu
field-type1 field-name1;
field-type2 field-name2;
field-type3 field-name3;
...
};
2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Example
• We can define a type for
representing a student who has a
name, age and a grade like this
struct student {
om
char name[20];
int age;
.c
float grade;
};
ng
co
an
th
ng
Example
o
du
struct car {
char* make;
char* model;
int year;
};
3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Variable declaration and
Initialisation
• You must use keyword struct in the
declaration
struct student s1;
struct car mycar;
om
struct student s1 = (“Nguyen Le”, 19, 8.0);
.c
struct car mycar = (“Fiat”, "Punto", 2004);
ng
co
an
th
ng
int age;
float grade;
} student_t; Now the program
has a new types -
student_t and
typedef struct car { car_t
char* make;
char* model;
int year;
}car_t;
4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Variable declaration
• With the usage of typedef, we don’t
have to write “struct student”
every time!
• For example, we can define two
om
complex numbers in the following
line:
.c
car_t mycar;
student_t excellentP;
ng
co
an
th
ng
Accessing Members of a
Structure
o
du
car_t mycar;
mycar.year = 2004;
student_t excellentp;
excellentp.age = 18;
excellentp.grade = 7.8;
5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercises 14.1
a) Create a structure named Date for storing date concerning
variables. Each date has a day, a month and a year.
b) Write a function for the input of variable of this type.
Remember to check the validation of data
c) Write a function to datecmp to compare two date which
return
-1 if the first date (parameter) is before the second
om
0 if two date are identical.
1 if the first date (parameter) is after the second
d) Write a program asking user to for two date and print out
.c
the results of the comparison.
ng
For example: 2/10/1997 is after 23/8/1997
co
an
th
ng
Exercise 14.2
o
du
– Total Rainfall
cu
– High Temperature
– Low Temperature
– Average Temperature
• The program should have an array of 12
structures to hold weather data for an entire
year. When the program run, it ask the user to
enter data for each month and then calculate and
display the average rain fall, the total rainfall of
the year, the highest and lowest temperatures for
the year.
• Input validation: Only accept temperature within
the range -40 and 50 degrees Celcius.
6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 14.3
• Write a student management program using this
structure:
typedef struct
{
char id[6];
char name[31];
float grade;
char classement
om
} student;
.c
respect to this criteria :
– from 9 to 10: A (Excellent)
– from 8 to 9: B (Good)
ng
– from 6.5 to 8: C (Medium)
– < 6.5 : D (Bad) co
an
th
ng
Exercise 14.3
o
du
7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 14.4
• Define a new type for representing
the fractions. Then use it to write a
fraction manipulation program. This
program have the following
functionalities.
om
– Input for an array of fraction
– Print the content of the fraction array
.c
– Inverse all the fraction in the array
– Compare two fraction
ng
co
an
th
ng
Exercise 14.5
o
du
8
CuuDuongThanCong.com https://fb.com/tailieudientucntt