1 / 17

תכנות בשפת C תרגול 15 תרגול חזרה 2 תרגילים ממבחנים 12.06.2011

תכנות בשפת C תרגול 15 תרגול חזרה 2 תרגילים ממבחנים 12.06.2011. fscanf - format specification Fields. A format specification has the following form: % [ * ] [ width ] type

Télécharger la présentation

תכנות בשפת C תרגול 15 תרגול חזרה 2 תרגילים ממבחנים 12.06.2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. תכנות בשפת C תרגול 15 תרגול חזרה 2 תרגילים ממבחנים 12.06.2011

  2. fscanf - format specification Fields • A format specification has the following form: %[*] [width] type • If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set. • For example: fscanf( ptr, "%20[^#] %*c %9d", name, &id); • the function fscanf reads 20 characters, or till letter (‘#') , or till newline from the input stream and stores them in field name, then it reads the next 9 characters and converts them into integer id, then it reads one symbol which is not stored.

  3. תרגיל 1 - רשימות הפונקציה הבאה מחפשת את האמצע של הרשימה ושם חוצה את הרשימה לשתי רשימות. הפונקציהמחזירה את המצביע לחצי השני של הרשימה. השלם את הקטעים החסרים. Node* middle(Node* source){ Node *fast, *slow, *source2; if (source==NULL || ?? 1 ??) return NULL; slow = source; fast = ?? 2 ?? ; while (?? 3 ??) { fast = fast->next; if ( ?? 4 ?? ) { slow = slow->next; fast = fast->next; } } source2 = slow->next; slow->next = ?? 5 ?? ; return ?? 6 ?? ; } typedef struct node{ int value; struct node* next; }Node;

  4. פתרון תרגיל 1 רשימות 1: !source->next 2: source->next 3: fast->next 4: fast->next 5: NULL 6: source2

  5. תרגיל 2 רקורסיה שאלה מס'4: כתוב פונקציה רקורסיביתvoid X(int lines) שמדפיסה את האות X באמצעות כוכביות ב-lines שורות (נניח ש-lines תמיד אי-זוגית). לדוגמא עבור X(9) יודפס:  * * * * * * * * * * * * * * * * * הכוכבית הראשונה חייבת להופיע בתחילת השורה הראשונה. אין להגדיר פונקציה נוספת.

  6. פתרון תרגיל 2 פתרון: הלוגיקה היא להדפיס את השורה הראשונה של X, אחר כך לפתור את תת-הבעיה עבור X ללא השורה הראשונה והאחרונה ואז להדפיס את השורה האחרונה (שהיא זהה לראשונה). ניתן להדגים את סדר הפעולות הכרונולוגי בכל קריאה רקורסיבת באמצעות התרשים הבא:

  7. פתרון תרגיל 2 void X(int lines){ int static blank; int I; for (i=0; i<blank; i++) putchar(' '); putchar('*');   if(lines==1){ puts(""); return; }   for (i=0; i<lines-2; i++) putchar(' '); puts("*");   blank++; X(lines-2); blank--; for (i=0; i<blank; i++) putchar(' '); putchar('*'); for (i=0; i<lines-2; i++) putchar(' '); puts("*"); }

  8. תרגיל 3 רקורסיה • נתונה התכנית הבאה: int what1(char *str){ if(*str == '\0')return 0; if(*str >= '0' && *str <='9') return what1(str+1)*10 + *str - '0'; return what1(str+1); } void what2(char **arr, int num){ if(!num) return; printf("%d\n",what1(*arr)); what2(arr+1, num-1); } void main(){ char *a[ ]={"e=mc2", "1=one, 2=two, 3=three", "one, two, three", "(4+5)*5-7=38"}; what2(a, 4); } מה הפלט של התכנית הנ"ל ? הסבר במשפט אחד מה יעודה של הפונקציה what1 ? הסבר במשפט אחד מה יעודה של הפונקציה what2 ?

  9. פתרון תרגיל 3 א. מה הפלט של התכנית הנ"ל ? 2 321 0 837554 ב. הסבר במשפט אחד מה יעודה של הפונקציה what1 ? הפונקציה מחזירה מספר מורכב מספרות המחרוזת בסדר הפוך. ג. הסבר במשפט אחד מה יעודה של הפונקציה what2 ? הפונקציה הופכת את המחרוזות של המערך הדו-מימדי למספרים מורכבים מספרות המחרוזות בסדר הפוך ומדפיסה אותם.

  10. תרגיל 4 עצים נתונה ההגדרה: typedef struct treeNodetreeNode; struct treeNode{ int value; treeNode *left, *right; }; כתוב פונקציה treeNode* freeMin(treeNode* root) שמקבלת שורש של עץ בינארי ממוין (לפי הכלל "שמאל קטן- ימין גדול"). הפונקציה משחררת את הקודקוד המכיל את הערך הקטן ביותר בעץ ומעדכנת את העץ בהתאם. אין להעתיק ערך (value) ממבנה למבנה.

  11. תרגיל 4 עצים • לדוגמא:

  12. פתרון תרגיל 4 treeNode* freeMin(treeNode* root){ treeNode *temp; if(!root) return NULL; if(! root->left){ temp = root ->right; free(root); return temp; } root->left = freeMin(root ->left); return root; }

  13. תרגיל 5 מחרוזות כתוב פונקציה void blank(char *sentence)שמקבלת מחרוזת שהיא משפט ומצמצמת רווחים במשפט. • אין להשאיר רוח לפני המילה הראשונה • אין להשאיר רוח בסוף המשפט • אסור שיהיו שני רווחים ברצף במשפט • אין ליצור או להשתמש במערך או במחרוזת עזר. דוגמה: הפונקציה תהפוך את המחרוזת " this is an example of sentence. ” ל- "this is an example of sentence.”

  14. פתרון תרגיל 5 void blank(char *sentence){ int i=0, j=0, first=0; while(sentence[i]){ if(sentence[i]!=' '){ first=1; sentence[j++]=sentence[i++]; } else if(first){ sentence[j++]=sentence[i++]; first=0; } else i++; } if(sentence[j]==' ') j--; sentence[j]='\0'; }

  15. תרגיל 6 רשימות הפונקציה void reduce(item *head) שמקבלת עוגן של רשימה משורשרת של מבנים מטיפוס item ומצמצמת את הרשימה כך שמכל רצף של אותו מספר נשאר רק מבנה אחד עם המספר עצמו ומספר המופעים (occur) של המספר ברצף (שאר המבנים באותו רצף משתחררים).  typedef struct item{ int number; //המספר int occur; // מספר המופעים struct item *next; }item;

  16. תרגיל 6 רשימות השלימו בדפי התשובות את הקטעים החסרים. void reduce(item *head){ item* temp; int count = ?? 1 ??; if ?? 2 ?? ; while(?? 3 ??) if(?? 4 ?? ){ ?? 5 ??; ?? 6 ??; ?? 7 ?? ; count++; } else{ head->occur = count; ?? 8 ?? ; ?? 9 ?? ; } ?? 10 ?? ; }

  17. פתרון תרגיל 6 void reduce(item *head){ item* temp; int count = 1; if(!head) return;  while(head->next) if(head->number == head->next->number){ temp = head->next; head->next = temp->next; free(temp); count++; } else{ head->occur = count; head = head ->next; count = 1; } head->occur = count; }

More Related