Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 12 27.12.2010.

Similar presentations


Presentation on theme: "1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 12 27.12.2010."— Presentation transcript:

1 1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 12 27.12.2010

2 מטרת התרגול קבצים עצים בינאריים - המשך 2 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

3 טעינת קובץ לרשימה מקושרת נתון קובץ בשם customer.txt כך שכל שורה מכילה פרטים של לקוח באופן הבא : – תעודת זהות - 9 תווים – שם פרטי – עד 26 תווים – שם משפחה - עד 26 תווים – כל הפרטים מופרדים ע " י רווח – לדוגמא : 040255065 Rami Golan 340245079 Alex Carmel כתבו תוכנית אשר טוענת את הקובץ לרשימה מקושרת 3 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

4 טעינת קובץ לרשימה מקושרת הגדירו מבנה אשר מייצג לקוח הגדירו איבר של רשימת מקושרת אשר מכיל לקוח 4 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

5 טעינת קובץ לרשימה מקושרת 5 #define IDSIZE 10 #define NSIZE 26 typedef struct { char Id[IDSIZE]; char forename[NSIZE]; char surname[NSIZE]; } Customer; typedef struct _node { Customer customer; struct _node * next; } Node; Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

6 טעינת קובץ לרשימה מקושרת 6 int main() } Node * head = NULL; Customer customer; FILE* customersFile; // Open file for read customersFile = fopen(“customers.txt”, “r”); if ( customersFile == NULL) return 0; // Load file to list while (EOF != fscanf(customersFile, “%s%s%s”, customer.Id, customer.forename, customer.surname)) { if (!AddNode(&head, &customer)) { fclose ( customersFile ); FreeList (head ); return 0; } fclose ( customersFile ); FreeList (head ); return 1; } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

7 העתקת קובץ נתון קובץ customer.txt בפורמט אשר הוגדר מקודם. יש לכתוב תוכנית אשר יוצרת עותק של הקובץ ללא הלקוח שתעודת הזהות שלו היא : 040255065 7 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

8 העתקת קובץ 8 int main() } Customer customer; FILE* customersFile; FILE* newCustomersFile; // Open file for read customersFile = fopen(“customers.txt”, “r”); newCustomersFile = fopen(“newCustomers.txt”, “r”); if (customersFile == NULL || new customersFile == NULL) return 0; // Load file to list while (EOF != fscanf(customersFile, “%s%s%s”, customer.Id, customer.forename, customer.surname)) { if (strcmp(customer.Id, “040255065”) != 0) { fprintf(newCustomersFile, “%s %s %s\n”, customer.Id, customer.forename, customer.surname) } fclose (customersFile); fclose (newCustomersFile); return 1; } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

9 שאלת מבחן: מה מבצעת הפונקציה הבאה? (פתרון בעמוד הבא) 9 struct node{ int key; node *next; }node; node* what( int num, node* head){ node* temp; if(!head) return NULL; temp = what( num, head -> next); if(temp) return temp; if(head -> key == num) return head; return NULL; } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

10 פתרון הפונקציה מחזירה את הכתובת של ההופעה האחרונה של num ברשימה, אחרת NULL. 10 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

11 שאלת מבחן 11 נתון עץ בינארי שקדקודיו מהטיפוס הבא : struct node{ int value; node *left; node *right; }node; כתוב פונקציה countNodes אשר מקבלת שורש של עץ בינארי ורמה בעץ וסופרת כמה קדקודים יש מרמה זו ומטה. ( השורש נמצא ברמה 0) Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

12 פתרון 12 int countNodesFrom(node *root, int level) { int count; if (!root) return 0; count = level <= 0 ? 1 : 0; count += countNodeFrom( root->left, level – 1) count += countNodeFrom( root->right, level – 1); return count; } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel


Download ppt "1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 12 27.12.2010."

Similar presentations


Ads by Google