Title: ????? ?????? (assert)
1????? 3
- ?????
- ????? ?????? (assert)
??-?? ???? litalma_at_cs.technion.ac.il ???? ?''?
???? ????????
2????? - Structures
- struct ltstruct namegt
- lttype name1gt ltfield1gt
- lttype name2gt ltfield2gt
- .
- .
- lttype namengt ltfieldngt
-
- ????????? ?? ???? ?????? ?? ???? ????? ????????
????? - ??? ????? ??????? ????? ?"? ????? ???????? ??????
- ?????? ?????
- ????? ?? ??????? ????? ?????
- ????? ??? ???? ??????? - ???????? ??????? ?? ??
????? ???????
3????? - Structures
- ????? ?????
- struct ltstruct namegt ltvariable namegt
- ????? - ???? ?????? ???? ?????. ?????? ??????
?????? ?? ????? ?????. - struct Date_t
- int day
- char month4
- int year
-
- ????? ????? ???? date
- struct Date_t d
Why 4 chars ?
4????? ??? ?????
- int start_day, start_year
- int end_day, end_year
- char start_month4, end_month4
- int compare_dates(int day1, char month1, int
year1, int day2, char month2, int year2) - ...
- compare_dates(start_day, start_month, start_year,
- end_day, end_month, end_year)
5????? ?? ?????
- struct Date_t start
- struct Date_t end
- int compare_dates(struct Date_t date1,
- struct Date_t date2)
- ...
- compare_dates(start,end)
- ???? ??? ????
- ???? ???? ????
- ???? ???????? ????????? ??? ????
- ???? ????? ??????? ??? ????????
6????? - Structures
- ????? ???? ???? ????? ???? struct ????? ?"?
- ltvariable namegt.ltfield namegt
- ???? ???? ????? ?struct-, ???? ???? ???? ?????
????? - (ltpointer namegt).ltfield namegt
- ?????? ???? ?? ???????, ????? ??? ????? ??????
???? ???? ???? - ltpointer namegt -gt ltfield namegt
- ?????
- struct Date_t p
- pd
- p-gtday12 / or (p).day12 /
7????? - Structures
- ?????? ?????
- int main()
- struct Date_t date , pdate
- date.day 21
- strcpy(date.month , "NOV")
- pdate date
- pdate-gtyear 1971
- printf ("The year is d\n",date.year)
8?? ???? ??????
- ?????? ?????? ???? ????? ??? struct Date_t
- ???? ?????? ?????? ?????? struct ?? ???, ???? ???
?????? ??? ?? ???? ?????? ???? ??? ????, ????
Date - ??? ????? ???? ??????? ??????, ????? ???? ???
???????? ???????????? ?? ???? - int i
- char c
- Date date
- ?-C ?????? ?????? ?? ???? ?''? typedef
Date ??? ?? ???? ?-struct Date_t
9?? ???? ?????? ??????? typedef
- typedef ltold type namegt ltnew type namegt
- ?????? ????? ?? ???? ??? ???? ?? ?????
- ???? ?????? ??? ????? ???????? ????? ?????
- ??????
- typedef int ID
- typedef int Element
- ...
- ID i
- Element element
??? ?- int i int element
10?? ???? ????? ??????? typedef
- typedef struct Date_t Date
- ???? ?? ?????? ????? ?? ????? ?????
- typedef struct Date_t pDate
- ??
- typedef Date pDate
- ???? ???? ?? ????? ????? ?????? ?? ???? ???
- typedef struct Date_t
- int day
- char month4
- int year
- Date
- typedef Date pDate
11????? ????? ???? ????
- ???? ?????? ???? ???? ??? ?????? ???? ???
- typedef struct Date_t
- int day
- char month4
- int year
- Date
- typedef struct Person_t
- char name
- int height
- Date birth
- Person
- ???? ???? ?? ????? ??????
- Person p
- Person ppp
- p.birth.year 1994
- pp-gtbirth.year 1994
- ???? ?? pp-gtbirth-gtyear ?
12????? ?????? ????? ???? ????
- ????? ?????? ?? birth ???? ??????Date ???? ???
??????? ?????? ????? ?????? Date - typedef struct Person_t
- char name
- int height
- Date birth
- Person
- ???? ???? ?? ????? ??????
- Person p
- Person ppp
- p.birth-gtyear 1994
- pp-gtbirth-gtyear 1994
- ??? ?? ???? ?????? ?? p Person?? ????? ????
???? ???? ????? ?????? Date ??? ?????? ???? - ??? ??? ???? ????? ???? ???? !
- ???? ???? ??????
13????? ????????? ?????
- ?????? ???? ???? ?????? ???????? ?????? ?????
????? ???? - typedef struct Date_t
- int day
- char month4
- 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)-gtyear)
14????? ??????? ???? ????? ?????? ??????
- ???? ????? ?? ???? ?? ???? year ?? ?????? dt2
???? ?????? - dt2.year 1994
- pdt?year 1994
- (dt1.next) ?year 1994
- ????? ??? ????? ?????? ?? ??? ??????
- ?? ???? ????? ???? ?? ???? next ?-dt ??? ????
???? ???? ?????
pdt
15????? ?????? ?? ????? ??????? ????? ?????
- ?????
- ???? ????? ????? ???????? ???? ?? ???? ??
???????, ???????? ???? ???? ???? ?????? - ?????
- ????? ?????? ?? ???????
16????? ?????? ?? ????? ??????? ????? ?????
??? ?????? ?????? ?
- int main()
- Date top NULL, tmp NULL
- int day, year
- char month4
- while (scanf("d s d", day ,month , year) !
EOF) - tmp (Date) malloc(sizeof(Date))
- if (tmp NULL)
- free_date_list(top) exit(0)
-
- tmp-gtday day tmp-gtyear year
- strcpy(tmp-gtmonth ,month)
-
- tmp-gtnext top top tmp
-
- print_date_list(top)
- return 0
?? ??? ???? ???????? ?
17????? ?????? ?? ????? ??????? ????? ?????
- void free_date_list(Date top)
- while (top ! NULL)
- Date tmptop
- toptop-gtnext
- free(tmp)
-
-
18????? ?????? ?? ????? ??????? ????? ?????
- void print_date_list(Date top)
- Date tmp top
- while (tmp ! NULL)
- printf("d s d\n",
- tmp-gtday, tmp-gtmon, tmp-gtyear)
- tmp tmp-gtnext
-
19??????? ???? ?????
- int main()
- Date top NULL, tmp NULL
- int day,year
- char month4
- while (scanf("d s d", day ,month , year) !
EOF) - tmp (Date) malloc(sizeof(Date))
- if (tmp NULL)
- free_date_list(top) exit(0)
-
- tmp-gtday day tmp-gtyear year
- strcpy(tmp-gtmonth ,month)
-
- tmp-gtnext top top tmp
-
- print_date_list(top)
- return 0
int main() Date top NULL Date
date while (scanf("d s d", date.day
,date.month , date.year) 3) Date
tmp (Date) malloc(sizeof(Date)) if (tmp
NULL) free_date_list(top)
exit(0) tmp date
tmp-gtnext top top tmp
print_date_list(top) free_date_list(top)
return 0
20????? ????? ?????? ?????? ????????
- ????? ?????? ??????? ??????? ????????? ????
?????? ?????. - ???????
- ???? ???? ????? ??????? ?????????
- ????? ???????
- ???? ?? ????? ????? ???? ??????? ????????? ???
????? ?????? - ????? ????? ???????? events
- 1 JAN 404 Last gladiator competition
- 6 MAY 1889 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 MAY 1915 Orson Welles is born
- 6 MAY 1626 Manhattan purchased for 1000
- 2 MAR 1969 First landing of the Concorde
21????? ????? ?????? ?????? ????????
- ????? ??????? ????? ??????
- gt 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
22????? ?????
historicalDateList
event
- ????? ????? ?????? ?? ???????
- ??? ????? ????? ????? ?? ??????? ???? ?????? ??
date
events list
next
date
events list
next
23?????
- ??????
- define MAX_LINE_LENGTH 100
- ???? ?????
- typedef struct Date_t
- int day
- char month4
- int year
- Date
24?????
- ???? ?????
- typedef struct Event_t
- char description
- struct Event_t next
- Event
???? ?? ????? ?????? ?? ?????. description ????
???? ???? d,des,desc,dsc ?? ???? ??????
??????/????? ??????? dscrptn,descript
25?????
- ???? ????? ???????
- typedef struct HistoricalDate_t
- Date date
- Event eventList
- struct HistoricalDate_t next
- HistoricalDate
26???????? ??? ????? ????
- ????? ????? ????? ??? ???? (????? ?????? ???????
????? ???????? !) -
- int readDate(FILE inputFile, Date date)
- if (inputFile NULL date NULL)
- return 0
-
- if (fscanf (inputFile, d s d ,
(date-gtday),date-gtmonth, - (date-gtyear))3)
- return 1
-
- return 0
27???????? ??? ????? ????
- ????? ???? ????? ??????? ????? ??? ????. (?????
??????, ???????? ?????? ?????? ??????? ?????
???????? !) -
- int readEvent(FILE inputFile, Date date, char
buffer) - if (inputFile NULL date NULL
buffer NULL) - return 0
-
- if (readDate(inputFile,date) 0)
- return 0
-
- if(fgets(buffer, MAX_LINE_LENGTH,
inputFile)NULL) - return 0
-
- return 1
28???????? ??? ????? ???? ????? ???????
- Event allocateEvent(char description)
- Event newEvent
- if (description NULL) return NULL
- newEvent(Event)malloc(sizeof(Event))
- if (newEvent NULL) return NULL
- newEvent-gtdescription(char)malloc(strlen(desc
ription)1) - if (newEvent-gtdescription NULL)
- free (newEvent)
- return NULL
-
- strcpy(newEvent-gtdescription, description)
- newEvent-gtnext NULL
- return newEvent
29???????? ??? ????? ???? ????? ???????
- HistoricalDate allocateHistoricalDate(Date date)
- HistoricalDate newHistoricalDate
- (HistoricalDate) malloc(sizeof(HistoricalDate)
) - if (newHistoricalDate NULL)
- return NULL
-
- newHistoricalDate -gtdate date
- newHistoricalDate -gteventList NULL
- newHistoricalDate -gtnext NULL
- return newHistoricalDate
-
30???????? ??? ????? ????? ??????? ?????? ???????
- HistoricalDate find(HistoricalDate first, Date
date) - if (first NULL)
- return NULL
-
- if (first-gtdate.day date.day
- strcmp(first-gtdate.month,date.month) 0
- first-gtdate.year date.year)
- return first
-
- return find(first-gtnext,date)
-
31???????? ??? ????? ????? ??????? ?????????
- void printEvents(Event event)
- if (event NULL )
- return
-
- printf (s\n, event-gtdescription)
- printEvents(event-gtnext)
-
32???????? ??? ????? ???? ???????
- HistoricalDate readEvents(FILE inputFIle)
- char bufferMAX_LINE_LENGTH
- Date date
- HistoricalDate firstHistoricalDate NULL ,
currentHistoricalDate NULL - Event event NULL
- while (readEvent(inputFIle,date,buffer))
- currentHistoricalDate find(firstHistoricalDat
e ,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-gtnext
firstHistoricalDate - firstHistoricalDate currentHistoricalDate
-
- event allocateEvent(buffer)
- if (event NULL)
- return NULL
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(argv1,r))NULL)
- return 2
- historicalDateList readEvents(inputFile)
/creating the list/ - fclose(inputFile)
- if (historicalDateList NULL)
- return 3
- while (readDate(stdin,date))
- historicalDate find(historicalDateList
,date) - if (historicalDate NULL)
- printf (Nothing special\n)
- else
- printEvents(historicalDate-gteventList)
-
34???????? ???????
- ????? ??????? ???? ???????.
- ????? ??????? ???? ????? ????? ???? ????? ?????
???? ???????? readEvents. - ?????? ????? ?????? ??????.
- ????? ????? ??? ???????? ???? ????? ????? define
?? enum
35????? ?????? - assert
- ??? ????? ????? ?????? ?????? ???? ????? ?????
- ????? ?????? ????? ???? ?????? ??????? ?????
?????? ?????? ?????? ?????? - ?-C ????? ?''? ????? ?????? assert????? ?????
?????? ?-int - ????? ?-assert.h
- ?????? ???????? ???? ???? ??????
36????? ?????? - assert
- prog.c
- int main(int argc, char argv)
- ...
- if(argc gt 3)
- ...
- else if (argc lt 2)
- ...
-
- else
- / if we are here argc is 2 /
-
- ...
-
- ...
assert(argc 2)
37????? ?????? - assert
- gt gcc prog.c -o prog
- gt prog
- gt prog a
- gt prog a b c
- gt prog a b
- prog prog.c12 main Assertion argc 2'
failed. - Abort
- gt
38????? ?????? - assert
- ??????? ?? ??????? ??????? ????? ??????? ?????
????? ?????? ?? - ????? ??? ???????
- ????? ?????
- ?? ?????
- ???? ????
- ?? ????????
39????? ?????? - assert
- ???? ???? ?? ?????? asserts?''? ?????? ?? ???
-DNDEBUG - gt gcc DNDEBUG prog.c -o prog
- gt prog a b
- gt
- ???? ?????? ?? ???? ??? ????? ?????
(production version) - asserts ??????? ?? ????? ????? (development
version) - ?????? ????????? ?? ?????? ???? ???????? ????????
?? ???? ??? - ????? ?????? ?-asserts ???? ????? ?? ?????? ????
40????? ?????? ????? ?? ????
- ????? ?-assert ??? ????? ?????? ??????? ?? ?????
?? ?? ????? ????? - int main(int argc, char argv)
- assert(argc 2) / the program must be called
with only one parameter / -
- ...
if(argc ! 2) fprintf(stderr, the program
must be called with ) fprintf(stderr,
only one parameter\n) exit(0)
41????? ?????? ????? ?? ????
- FILE fd fopen(input.txt" , "r")
- assert(fd ! NULL)
if(fd NULL) error(Failed to open
input.txt!\n)
42????? ?????? ????? ?? ????
- ????? ???????, ??????? ?????? ??????? ????
?assert- - assert((result calculate()) gt MIN_RESULT)
- / working with result /
- ...
- ?-production version ?assert- ?? ????? ???????
????? ?? ????? !
43????? ?????? ????? ????
- result calculate()
- assert(result gt MIN_RESULT)
- / working with result /
- ...