תרגול 3 מבנים טענות נכונות (assert) לי-טל משיח litalma@cs.technion.ac.illitalma@cs.technion.ac.il נערך ע''י ודים אייזנברג

  • Published on
    19-Dec-2015

  • View
    222

  • Download
    7

Embed Size (px)

Transcript

  • Slide 1
  • 3 (assert) - litalma@cs.technion.ac.illitalma@cs.technion.ac.il ''
  • Slide 2 ; -> ; : struct Date_t d; struct Date_t* p = &d; d.day = 7; p->day=12; /* or (*p).day=12; */ 6
  • Slide 7 year = 197"> year = 1971 ; printf ("The year is %d\n",date.year); } 7"> year = 197" title=" - Structures : int main() { struct Date_t date, *pdate; date.day = 21; strcpy(date.month, "NOV"); pdate = &date ; pdate->year = 197">
  • - Structures : int main() { struct Date_t date, *pdate; date.day = 21; strcpy(date.month, "NOV"); pdate = &date ; pdate->year = 1971 ; printf ("The year is %d\n",date.year); } 7
  • Slide 8
  • struct Date_t struct , , Date , : int i; char c; Date date; -C '' typedef 8 Date -struct Date_t
  • Slide 9
  • typedef typedef typedef ; : typedef int ID; typedef int * Element;... ID i; Element element; 9 -: int i; int * element;
  • Slide 10
  • typedef typedef typedef struct Date_t Date ; : typedef struct Date_t * pDate; typedef Date* pDate; : typedef struct Date_t { int day; char month[4]; int year; } Date; typedef Date* pDate; 10
  • Slide 11
  • typedef struct Date_t { int day; char month[4]; int year; } Date ; typedef struct Person_t { char* name; int height; Date birth; } Person ; : Person p ; Person* pp=&p; p.birth.year = 1994 pp->birth.year = 1994; pp->birth->year ? 11
  • Slide 12
  • birth Date Date typedef struct Person_t { char* name; int height; Date* birth; } Person ; : Person p ; Person* pp=&p; p.birth->year = 1994 pp->birth->year = 1994; : p Person Date ! ? 12
  • Slide 13 year); } 13">
  • : typedef struct Date_t { int day; char month[4]; int year; struct Date_t* next; } Date ; : int main() { Date dt1, dt2, *pdt ; dt1.day = 31 ; dt1.year = 1992 ; strcpy(dt1.month,DEC); dt2.day = 1 ; dt2.year = 1993 ; strcpy(dt2.month,JAN); pdt = &dt2; dt1.next = &dt2; printf ("Year in dt1: %d\n Year in dt2: %d\n ",dt1.year, (dt1.next)->year); } 13
  • Slide 14
  • year dt2 : dt2.year = 1994; pdt year = 1994; (dt1.next) year = 1994; next -dt2 ? 14 pdt 31 DEC 1992 1 JAN 1993 ? dt1 dt2
  • Slide 15
  • : , : 15
  • Slide 16 day = day; tmp->year = year; strcpy(tmp->month,month); tmp->next = top ; top = tmp; } print_date_list(top); return 0; } 16 ? ?">
  • int main() { Date *top = NULL, *tmp = NULL ; int day, year ; char month[4]; while (scanf("%d %s %d", &day,month, &year) != EOF){ tmp = (Date*) malloc(sizeof(Date)); if (tmp == NULL) { free_date_list(top); exit(0); } tmp->day = day; tmp->year = year; strcpy(tmp->month,month); tmp->next = top ; top = tmp; } print_date_list(top); return 0; } 16 ? ?
  • Slide 17
  • void free_date_list(Date *top){ Date *current=top, *tmp; while (current != NULL) { tmp=current; current=current->next; free(tmp); } 17
  • Slide 18 day, current ->month, current ->year); current = current->next ; } 18">
  • void print_date_list(Date *top){ Date * current = top; while (current != NULL) { printf("%d %s %d\n", current ->day, current ->month, current ->year); current = current->next ; } 18
  • Slide 19 day = day ; tmp->year = year ; strcpy(tmp->month,month); tmp->next = top ; top = tmp; } print_date_list(top); return 0; } int main() { Date *top = NULL, *tmp = NULL ; Date date; while (scanf("%d %s %d", &date.day,date.month, &date.year) == 3){ tmp = (Date*) malloc(sizeof(Date)); if (tmp == NULL) { free_date_list(top); exit(0); } *tmp = date; tmp->next = top ; top = tmp; } print_date_list(top); free_date_list(top); return 0; }">
  • 19 int main() { Date *top = NULL, *tmp = NULL ; int day,year ; char month[4]; while (scanf("%d %s %d", &day,month, &year) != EOF){ tmp = (Date*) malloc(sizeof(Date)); if (tmp == NULL) { free_date_list(top); exit(0); } tmp->day = day ; tmp->year = year ; strcpy(tmp->month,month); tmp->next = top ; top = tmp; } print_date_list(top); return 0; } int main() { Date *top = NULL, *tmp = NULL ; Date date; while (scanf("%d %s %d", &date.day,date.month, &date.year) == 3){ tmp = (Date*) malloc(sizeof(Date)); if (tmp == NULL) { free_date_list(top); exit(0); } *tmp = date; tmp->next = top ; top = tmp; } print_date_list(top); free_date_list(top); return 0; }
  • Slide 20
  • : . events: 1 JAN 404 Last gladiator competition 6 MAY1889 Eiffel tower opens 21 NOV 1794 Honolulu harbor discovered 1 JAN 1852 First US public bath opens 2 MAR 1969 First takeoff of the Concorde 6 MAY1915 Orson Welles is born 6 MAY 1626 Manhattan purchased for 1000$ 2 MAR 1969 First landing of the Concorde 20
  • Slide 21
  • : > important_dates events enter date: 2 MAR 1969 First takeoff of the concorde First landing of the concorde enter date: 23 NOV 1999 Nothing special 21
  • Slide 22
  • 22 date events list next date events list next Historical Date date events list next event description historicalDateList
  • Slide 23
  • #define MAX_LINE_LENGTH 100 typedef struct Date_t { int day; char month[4]; int year; } Date; 23
  • Slide 24
  • typedef struct Event_t { char* description ; struct Event_t *next; } Event; 24 . description d,des,desc,dsc / : dscrptn,descript
  • Slide 25
  • typedef struct HistoricalDate_t { Date date ; Event *eventList; struct HistoricalDate_t *next ; } HistoricalDate; 25
  • Slide 26
  • ( !) int readDate(FILE* inputFile, Date* date) { if (inputFile == NULL || date == NULL) { return 0; } if (fscanf (inputFile, %d %s %d , &(date->day),date->month, &(date->year))==3) { return 1; } return 0; } 26
  • Slide 27
  • . ( , !) int readEvent(FILE* inputFile, Date* date, char* description) { if (inputFile == NULL || date == NULL || description == NULL) { return 0 ; } if (readDate(inputFile,date) ==0) { return 0; } if(fgets(description, MAX_LINE_LENGTH, inputFile)==NULL) { return 0; } return 1; } 27
  • Slide 28
  • Event* allocateEvent(char* description) { Event *newEvent; if (description == NULL) return NULL; newEvent=(Event*)malloc(sizeof(Event)); if (newEvent == NULL) return NULL; newEvent->description=(char*)malloc(strlen(description)+1); if (newEvent->description == NULL) { free (newEvent) ; return NULL; } strcpy(newEvent->description, description); newEvent->next = NULL; return newEvent; } 28
  • Slide 29
  • HistoricalDate* allocateHistoricalDate(Date date) { HistoricalDate *newHistoricalDate = (HistoricalDate*) malloc(sizeof(HistoricalDate)); if (newHistoricalDate == NULL){ return NULL; } newHistoricalDate->date = date; newHistoricalDate->eventList = NULL; newHistoricalDate->next = NULL; return newHistoricalDate; } 29
  • Slide 30
  • HistoricalDate* find(HistoricalDate* first, Date date){ if (first == NULL) { return NULL; } if ((first->date.day == date.day) && (strcmp(first->date.month,date.month) == 0) && (first->date.year == date.year)) { return first; } return find(first->next,date); } 30
  • Slide 31
  • void printEvents(Event* event) { if (event == NULL ) { return; } printf (%s\n, event->description); printEvents(event->next); } 31
  • Slide 32
  • HistoricalDate* readEvents(FILE* inputFIle) { char description[MAX_LINE_LENGTH]; Date date ; HistoricalDate *firstHistoricalDate =NULL, *currentHistoricalDate = NULL; Event *event = NULL; while (readEvent(inputFIle,&date, description)) { currentHistoricalDate = find(firstHistoricalDate,date) ; if (currentHistoricalDate == NULL) { /* in case the date doesn't exist add this date to be the first in the list */ currentHistoricalDate = allocateHistoricalDate(date); if (currentHistoricalDate == NULL) { return NULL ; } currentHistoricalDate->next = firstHistoricalDate ; firstHistoricalDate = currentHistoricalDate ; } event = allocateEvent(description); if (event == NULL) { return NULL; } event->next = currentHistoricalDate->eventList; currentHistoricalDate->eventList = event; } return firstHistoricalDate ; } 32
  • Slide 33
  • int main(int argc, char* argv[]) { FILE* inputFile = NULL; HistoricalDate* historicalDateList= NULL, *historicalDate = NULL; Date date; if (argc != 2) return 1 ; if ((inputFIle = fopen(argv[1],r))==NULL) return 2 ; historicalDateList = readEvents(inputFile); /*creating the list*/ fclose(inputFile); if (historicalDateList == NULL) return 3 ; printf(enter date:); while (readDate(stdin,&date)) { historicalDate = find(historicalDateList,date) ; if (historicalDate == NULL) printf (Nothing special\n); else printEvents(historicalDate->eventList); printf(enter date:); } return 0; } 33
  • Slide 34
  • . readEvents. . defineenum define enum 34
  • Slide 35
  • (assert) 35
  • Slide 36
  • - asse

Recommended

View more >