computers and programming the 6 th lecture jiří Šebesta

21
Computers and programming The 6 th lecture Jiří Šebesta

Upload: cordelia-wilkerson

Post on 26-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computers and programming The 6 th lecture Jiří Šebesta

Computers and programming

The 6th lecture

Jiří Šebesta

Page 2: Computers and programming The 6 th lecture Jiří Šebesta

TOPIC

1. Structures2. Unions 3. Enumerative type4. Dynamic variables - introduction5. Example

Page 3: Computers and programming The 6 th lecture Jiří Šebesta

Structures (1/4)

• Structure: an array consisting of items of different types embedding into superior type

typedef struct friends // my girlfriend{ char fname[20]; // her first name

char sname[20]; // her surrname int age; // her age char phone[20]; // her phone number

} T_friend;

• Declaration

type identifier

Page 4: Computers and programming The 6 th lecture Jiří Šebesta

Structures (2/4)

int main(void){ T_friend baby;

strcpy(baby.fname, "Anna\0"); strcpy(baby.sname, "Novakova\0"); baby.age = 16; strcpy(baby.phone, "+420776151443\0");

printf(" %s %s - %d let - tel.: %s", baby.fname, baby.sname, baby.age, baby.phone);

getchar(); return 0;}

• Access to item of structure:

Source code: Ex59.c variable item

Page 5: Computers and programming The 6 th lecture Jiří Šebesta

Structures (3/4)

typedef struct date // date{ int year; // year

int month; // month int day; // day} T_date;typedef struct drivelog // drive log book{ char driver[20]; // name

char in_city[20];// initial citychar en_city[20];// ending city

T_date in_date; // initial dateT_date en_date; // ending dtaint dist; // distanceint in_tacho; // initial st. of tacho.int en_tacho; // ending st. of tacho.

} T_drivelog;

• Structure in structure:

Page 6: Computers and programming The 6 th lecture Jiří Šebesta

Structures (4/4)

T_drivelog toyota020, toyota021;

strcpy(toyota020.driver, "John");toyota020.in_date.year = 2011;toyota020.in_tacho = 53210;toyota020.en_tacho = 53372;toyota020.dist = toyota020.en_tacho-toyota020.in_tacho;

strcpy(toyota021.driver, "Judith");strcpy(toyota021.in_city, toyota020.en_city);toyota021.in_tacho = toyota020.en_tacho;toyota021.en_tacho = 53712;toyota021.dist = toyota021.en_tacho-toyota021.in_tacho;

• Manipulation with items of structure:

Source code: Ex60.c

Page 7: Computers and programming The 6 th lecture Jiří Šebesta

Unions (1/3)

• Union: it enables to save values of different types to a single variable (allocated common space in memory)

union u1 {int i; double d; char c;} U = {'u'};

name ofunion

list of union items

variable of declared type of unie

• Memory size allocated for the union is given by the size of the largest element

• STRUCTURE: int and double and char • UNION: int or double or char

Page 8: Computers and programming The 6 th lecture Jiří Šebesta

Unions (2/3)

typedef union id // ID of item{ int id_num; // numerical ID char id_str[10]; // textual ID} T_id;

int main(void){ char input_id[10];

int n, t;

printf("\n\nInsert ID: ");gets(input_id); // get string from stdin…

• Declaration

type identifier variable of declared type

Page 9: Computers and programming The 6 th lecture Jiří Šebesta

Unions (3/3)

t=0; // test if at least char is not digitfor(n=0; input_id[n]!='\0'; n++)

if (input_id[n]<'0' || input_id[n]>'9')t=1;

// t=0 input_id is a number, t=1 input_id is a stringif (t==0) // input_id is a number{

item.id_num=atol(input_id);printf("ID contents a number: %d\n", item.id_num);

}else // input_id is a string{

strcpy(item.id_str, input_id);printf("ID contents a string: %s\n", item.id_str);

}

S. code: Ex61.c

Page 10: Computers and programming The 6 th lecture Jiří Šebesta

Enumerative type (1/3)

• Enumerative type: a set of symbolic constants with strictly defined values

enum cards {seven, eight, nine, face_card, ace} set_1, set_2;

enum cards {seven=7, eight, nine, face_card=20, ace=30} set_1, set_2;

• Declaration:

type identifier variable of declared type

0 1 2

3 4

7 8 9

20 30

Page 11: Computers and programming The 6 th lecture Jiří Šebesta

Enumerative type (2/3)

• Operator for enumerative type:– only assignment

enum cards {seven=7, eight, nine, face_card=20, ace=30} set_1, set_2;

set_1 = nine;

set_2 = 8; //not possible, 8 isn’t enumerator

set_1 -=2; //not possible

int num = seven //type cast of enumerator seven to int

Page 12: Computers and programming The 6 th lecture Jiří Šebesta

Enumerative type (3/3)

enum zodiac {aries,taurus,gemini,cancer,leo,virgo, libra, scorpio, sagittarius, capricorn, …} eva;eva = virgo;

switch(eva){ case aries: printf(”She is hard-headed”); break; case taurus: printf(”She is reserved”); break; case gemini: printf(”She is friendly”); break; case cancer: printf(”She is anxious”); break; case leo: printf(”She is bossy”); break; case virgo: printf(”She is trusty”); break; case libra: printf(”She is shaky”); break; … }

Source code: Ex62.c

Page 13: Computers and programming The 6 th lecture Jiří Šebesta

Dynamic variables – intro (1/3)

• Static variable: – memory space allocation when program is starting– memory space release when program is closing

• Dynamic variable: – controlled memory space allocation when program is running by function malloc() in C– controlled memory space release when program is running by function free() in C

Page 14: Computers and programming The 6 th lecture Jiří Šebesta

Dynamic variables – intro (2/3)

• Application of dynamic variables: – a memory space can be allocated and released if program is running any time– suited for large blocks of data (e.g. large arrays), which are temporal (e.g. results, which will not be used later)– if we work with unknown size of array, if we use a static variable, we have to allocate the space for the worst case (max. size), e.g. double x[10000] , this space is permanently blocked (in given function)

Page 15: Computers and programming The 6 th lecture Jiří Šebesta

Dynamic variables – intro (3/3)

#include <stdio.h>#include <stdlib.h>

int main( void){ double *x, y; x = (double*)malloc(sizeof(double)); *x = 3.14; y = *x; free(x); printf("Ludolf's number: %f", y); printf("Ludolf's number: %f", *x); //free(x); getchar(); return 0;}

free address

free variable

void *malloc(size_t size)

type cast

space release in memory pointed by x

this space in a memory is pointed by x

allocation of space in a memory with size of double

Source code: Ex63.c

Page 16: Computers and programming The 6 th lecture Jiří Šebesta

Example (1/5)

• Create a program working as a car database. Each car is described by a type and a year of production. Data of cars are saved in dynamic variables, pointers are stored in an array. Adding a car is activated by pressing the key A, deleting by pressing D, and the program is stopped by pressing Q. In the database, at least one car has to be left.

#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct car // car record{ char type[10]; int year;} T_car; T_car *my_cars[20]; // ptrs. to cars - globalint cnt=0; // recorded cars - global

Page 17: Computers and programming The 6 th lecture Jiří Šebesta

void addcar(char *atype, int ayear){ T_car *car; // pointer to a car

car = (T_car*)malloc(sizeof(T_car)); strcpy(car->type, atype); // notation 1 (*car).year = ayear; // notation 2 my_cars[cnt++] = car;}

Example (2/5)

void showdelcar(void){ printf("type: %s\n", my_cars[cnt]->type); printf("year: %d\n", my_cars[cnt]->year);}

Function which adds a car

Function which displays erased car

Page 18: Computers and programming The 6 th lecture Jiří Šebesta

void erasecar(void){ if(cnt>1) // if at least two cars { cnt--; // one car to be removed showdelcar(); // print the removed car free(my_cars[cnt]); // delete last } else printf("All cars can’t be deleted");}

Example (3/5)

Function which deletes a car

Page 19: Computers and programming The 6 th lecture Jiří Šebesta

int main(void){ char cmd, my_type[12]; int my_year;

printf("\nA: Add, D: Delete, Q: Quit\n"); scanf("%c", &cmd); fflush( stdin); while(!(cmd == 'Q' || cmd == 'q')) { if(cmd=='A' || cmd=='a') { printf("\ntype : "); scanf("%s", &my_type); fflush(stdin); printf("\nyear : "); scanf("%d", &my_year); fflush(stdin); add(my_type, my_year); }

Example (4/5)

Main function - beginning

Page 20: Computers and programming The 6 th lecture Jiří Šebesta

if( cmd=='D' || cmd=='d') erasecar();

printf("\nA: Add, D: Delete, Q: Quit"); scanf("%c", &cmd); fflush(stdin); } return 0;}

Example (5/5)

Main function - conclusion

Source code: Ex64.c

Page 21: Computers and programming The 6 th lecture Jiří Šebesta

TOPIC OF THE NEXT LECTURE

Programming with files

THANK YOU FOR YOUR ATTENTION