תכנות תרגול 12 שבוע : 22.1.06. מבנים מטרת המבנים היא לאפשר...

Preview:

Citation preview

1212 תכנות תרגולתכנות תרגול

::שבועשבוע

22.1.0622.1.06

מבניםמבנים

מטרת המבנים היא לאפשר למתכנת להגדיר מטרת המבנים היא לאפשר למתכנת להגדיר •טיפוסי משתנים חדשים אשר מתאימים טיפוסי משתנים חדשים אשר מתאימים

ספציפית לבעיה שאותה התוכנית פותרת. ספציפית לבעיה שאותה התוכנית פותרת.

היא לאפשר למתכנת היא לאפשר למתכנת typedeftypedefמטרת המילה מטרת המילה •לתת שמות חדשים לטיפוסי משתנים קיימים. לתת שמות חדשים לטיפוסי משתנים קיימים.

מבניםמבנים

נניח שאנו כותבים תוכנית שמטרתה היא לחשב נניח שאנו כותבים תוכנית שמטרתה היא לחשב • a+bia+biערכים באמצעות מספרים מרוכבים ערכים באמצעות מספרים מרוכבים

הכי נוח לנו הוא אם כמו שהיה לנו טיפוס למספר הכי נוח לנו הוא אם כמו שהיה לנו טיפוס למספר •( אז יהיה לנו גם טיפוס למספר ( אז יהיה לנו גם טיפוס למספר doubledoubleממשי )ממשי )

מרוכב. השימוש במבנים מאפשר זאת. מרוכב. השימוש במבנים מאפשר זאת.

Syntax Syntax מבניםמבניםהגדרת מבנה:הגדרת מבנה:•

struct complex {struct complex {double real;double real;

double image;double image;

};}; הגדרת משתנים מסוג זההגדרת משתנים מסוג זה•

struct complex c;struct complex c;

struct complex* pc;struct complex* pc;

של המבנהmembersה

Syntax Syntax מבניםמבניםstruct complex {struct complex {

double real;double real;

double image;double image;

};};int main()int main()

{{

struct complex c;struct complex c;

struct complex* pc;struct complex* pc;

c.real = 5;c.real = 5;

c.image = 7;c.image = 7;

}}

real

image

5

7

c

Syntax Syntax מבניםמבניםstruct complex {struct complex {

double real;double real;

double image;double image;

};};int main()int main()

{{

struct complex c;struct complex c;

struct complex* pc;struct complex* pc;

c.real = 5;c.real = 5;

c.image = 7;c.image = 7;

pc = &c;pc = &c;

pc->real = 3;pc->real = 3;

pc->image = 4;pc->image = 4;

}}

real

image

5

7

c 6024

pc6024

3

4

Syntax Syntax מבניםמבניםstruct complex {struct complex {

double real;double real;

double image;double image;

};};

סיכום :סיכום :

.. נשתמש בנקודה נשתמש בנקודהcc של של membersmembersלגישה ל לגישה ל

נשתמש ב >- נשתמש ב >-pcpc של של membersmembersלגישה ל לגישה ל

#include <stdio.h<struct complex{

double real;double im;

};

void PrintComplex )struct complex num(;void ScanComplex)struct complex *p_num(;

int main)({

struct complex x;ScanComplex)&x(;PrintComplex)x(;return 0;

}

void PrintComplex )struct complex num({

printf)“%lf + %lf i\n”, num.real, num.im(;}

void ScanComplex)struct complex *p_num({

scanf)“%lf%lf”, &)p_num-<real(, &)p_num-<im((;}

תרגילתרגיל

כתבו פונקציה המקבלת שני מספרים מרוכבים, כתבו פונקציה המקבלת שני מספרים מרוכבים, •מחברת ביניהם ומחזירה את הסכום.מחברת ביניהם ומחזירה את הסכום.

כתבו פונקציה המחשבת את הערך המוחלט של כתבו פונקציה המחשבת את הערך המוחלט של •..מספר מרוכב ומחזירה את הערך הזהמספר מרוכב ומחזירה את הערך הזה

double AbsComplex(struct complex a)double AbsComplex(struct complex a)

{{

return sqrt(a.real * a.realreturn sqrt(a.real * a.real + a.im * a.im); + a.im * a.im);

}}

struct complex Add(struct complex a, struct struct complex Add(struct complex a, struct complex b)complex b)

{{

struct complex s;struct complex s;

s.real = a.real + b .real;s.real = a.real + b .real;

s.im = a.im + b.im;s.im = a.im + b.im;

return s;return s;

}} שימוש בפונקציותשימוש בפונקציות

x = Add(x,y);x = Add(x,y);

ab = AbsComplex(x);ab = AbsComplex(x);

!העברה לפי ערך

typedeftypedef מבנים ושימוש ב-מבנים ושימוש ב-struct complex {struct complex {

double real;double real;

double image;double image;

};};

int main()int main()

{{

struct complex struct complex c={5,7};c={5,7};

struct complex* pc;struct complex* pc;

}}

struct complex {struct complex {

double real;double real;

double image;double image;

};};

typedef struct complex complextypedef struct complex complex_t;_t;

int main()int main()

{{

complex_t c={5,7};complex_t c={5,7};

complex_t* pc;complex_t* pc;

}}

typedef struct complex complextypedef struct complex complex_t;_t;

טיפוס קיים שם חדש לטיפוס קיים

double AbsComplex(double AbsComplex(complex_tcomplex_t a) a)

{{

return sqrt(a.real * a.real + a.im * a.im);return sqrt(a.real * a.real + a.im * a.im);

}}

complex_t Add(complex_t Add(complex_tcomplex_t a, a, complex_tcomplex_t b) b)

{{

complex_tcomplex_t s; s;

s.real = a.real + b .real;s.real = a.real + b .real;

s.im = a.im + b.im;s.im = a.im + b.im;

return s;return s;

}}

double AbsComplex(double AbsComplex(complex_t* complex_t* pa)pa)

{{

return sqrt(pa->real*pa->real + pa->im*pa-return sqrt(pa->real*pa->real + pa->im*pa->im);>im);

}}

complex_t Add(complex_t Add(complex_t*complex_t* pa, pa, complex_t*complex_t* pb) pb)

{{

complex_tcomplex_t s; s;

s.real = pa->real + pb->real;s.real = pa->real + pb->real;

s.im = pa->im + pb->im;s.im = pa->im + pb->im;

return s;return s;

}}

מערכים ומבניםמערכים ומבנים

מאחר ומבנים מגדירים סוג חדש של משתנים מאחר ומבנים מגדירים סוג חדש של משתנים •הרי הרי

שכמו לסוגים רגילים נרצה ליצור מערכים עבור שכמו לסוגים רגילים נרצה ליצור מערכים עבור סוגים סוגים

realאלואלו 5

7image

2

1

7

2

1

8

complex_t arr[SIZE]={{5,7},{2,1},{7,2},{1,8}};

arr

מערכים ומבניםמערכים ומבנים

פונקציה להדפסת מערךפונקציה להדפסת מערך•

void PrintComplexArr(complex_t arr[],int n)

{

int i;

for (i=0;i<n;i++)

{

PrintComplex(arr[i]);

}

}

תרגילתרגיל

כתוב פונקציה המקבלת מערך של מספרים כתוב פונקציה המקבלת מערך של מספרים מרוכבים מרוכבים

ומחזירה את האינדקס של האיבר בעל הערך ומחזירה את האינדקס של האיבר בעל הערך המוחלט המוחלט

..המקסימאליהמקסימאלי

פתרוןפתרוןint MaxAbs(complex_t arrint MaxAbs(complex_t arr[][],int n),int n){{

intint i,maxIn;i,maxIn;doubledouble maxAbs=maxAbs=-1-1;;for (i=0;i<n;i++)for (i=0;i<n;i++)

if (if ( AbsComplex(arr[i])AbsComplex(arr[i]) > > maxAbsmaxAbs )){{

maxAbs = AbsComplex(arr[i]);maxAbs = AbsComplex(arr[i]);maxIn = i;maxIn = i;

}}return maxIn;return maxIn;

}}

רשימות מקושרותרשימות מקושרות

ישנו מבנה נתונים אשר מאפשרישנו מבנה נתונים אשר מאפשר•לנו לבצע את הוספת האיברים בצורהלנו לבצע את הוספת האיברים בצורה

נוחה יותר. מבנה זה נקרא רשימה מקושרת. נוחה יותר. מבנה זה נקרא רשימה מקושרת.

איבר

מידע

מצביע לאיבר

מצביע לראש

הרשימה

איבר

מידע

מצביע לאיבר

איבר

מידע

מצביע לאיבר

איבר

מידע

מצביע לאיבר

איבר

מידע

מצביע לאיבר

5 7 1 8

struct list {struct list { int data;int data; struct list *next;struct list *next;};};

typedef strcut list typedef strcut list Item;Item;

מידע

מצביע לאיבר

:פעולות על הרשימה

יצירה והריסה של רשימה•ספירת מספר האיברים ברשימה•חיבור שתי רשימות•מחיקה/הוספה של איבר לרשימה•הדפסת רשימה•

CCרשימות מקושרות ב-רשימות מקושרות ב-

נחזיק מצביע אשר יהיה מצביע לראש נחזיק מצביע אשר יהיה מצביע לראש ..--mainmainהרשימה וישב בהרשימה וישב ב

תהיה לנו פונקצית הוספה אשר תוסיף תהיה לנו פונקצית הוספה אשר תוסיףאיברים לרשימה בסוף הרשימה. איברים לרשימה בסוף הרשימה.

הפונקציה תדע לקבל מצביע לראש הפונקציה תדע לקבל מצביע לראשהרשימה וערך להוספה ותוסיף את האיבר הרשימה וערך להוספה ותוסיף את האיבר

בסוף הרשימה.בסוף הרשימה.

איבר

מידע

מצביע לאיבר

3

איבר

מידע

מצביע לאיבר

7

איבר

מידע

מצביע לאיבר

5

CCרשימות מקושרות ב-רשימות מקושרות ב-int main()int main()

{{

Item *L_head = NULL;Item *L_head = NULL;

L_head = Add(L_head,5);L_head = Add(L_head,5);

L_head = Add(L_head,3);L_head = Add(L_head,3);

L_head = Add(L_head,7);L_head = Add(L_head,7);

}}מצביע לראש

הרשימה

Item* Add(Item* L_head, int value)Item* Add(Item* L_head, int value){{ Item* L_tail = L_head;Item* L_tail = L_head; Item* I_new = malloc(sizeof (Item));Item* I_new = malloc(sizeof (Item)); I_new->value = value;I_new->value = value; I_new->next = NULL;I_new->next = NULL;

if (L_tail == NULL) if (L_tail == NULL) return I_new;return I_new;

while (L_tail->next != NULL)while (L_tail->next != NULL) L_tail=L_tail->next;L_tail=L_tail->next; L_tail->next = I_new;L_tail->next = I_new; return L_head;return L_head;} }

איבר

מידע

מצביע לאיבר

3

איבר

מידע

מצביע לאיבר

7

איבר

מידע

מצביע לאיבר

5

CCרשימות מקושרות ב-רשימות מקושרות ב-int main()int main()

{{

Item *L_head = NULL;Item *L_head = NULL;

… …

L_head = Delete(L_head,3);L_head = Delete(L_head,3);

}}

מצביע לראש

הרשימה

Item *Delete(Item * head,int value)Item *Delete(Item * head,int value){{

Item* curr = head;Item* curr = head;Item* prev = head;Item* prev = head;if (curr->value == value)if (curr->value == value){{

curr = curr->next;curr = curr->next;free(head);free(head);return curr;return curr;

}}

האיבר למחיקההוא הראשון

while (curr != NULL)while (curr != NULL){{

if (curr->value == value)if (curr->value == value){{

prev->next = curr->next;prev->next = curr->next;free(curr);free(curr);return head;return head;

}}prev = curr;prev = curr;curr = curr->next;curr = curr->next;

}}return head;return head;

}}

חיפוש ומחיקתאיבר

void PrintList)Item *head({

if )head == NULL({

printf)"\n"(;return;

}else{

printf)"%d ",head-<data(;PrintList)head-<next(;return;

}}

הדפסת רשימה

כתוב פונקציה המקבלת מצביע לרשימה כתוב פונקציה המקבלת מצביע לרשימה •וסופרת כמה איברים יש ברשימהוסופרת כמה איברים יש ברשימה

int Count(Item * L){

int counter=0;while (L != NULL){

counter++;L=L->next;

}return counter;

}int CountR(Item * L){

if (L == NULL)return 0;

return 1 + CountR(L->next);}

כתוב פונקציה המקבלת מצביעים לשתי כתוב פונקציה המקבלת מצביעים לשתי •רשימות מקושרות ומחברת אותן. רשימות מקושרות ומחברת אותן.

איבר

מידע

מצביע לאיבר

3

איבר

מידע

מצביע לאיבר

7

איבר

מידע

מצביע לאיבר

5

1רשימה איבר

מידע

מצביע לאיבר

3

איבר

מידע

מצביע לאיבר

5

2רשימה

איבר

מידע

מצביע לאיבר

3

איבר

מידע

מצביע לאיבר

7

איבר

מידע

מצביע לאיבר

5

איבר

מידע

מצביע לאיבר

3

איבר

מידע

מצביע לאיבר

5

Item* Meld(Item* L1,Item* L2){

Item* L1_head = L1;if (L1 == NULL)

return L2;if (L2 == NULL)

return L1;while (L1->next != NULL) L1=L1->next;L1->next = L2;return L1_head;

}

Recommended