1 / 8

מבוא למדעי המחשב

מבוא למדעי המחשב. תרגול 2 שעת קבלה: יום שני 11:00-12:00 דוא"ל: keren@eli-wigs.com http://www.pitt.edu/~stephenp/misc/turboC.exe. פעולות חשבון. פעולות החשבון הבסיסיות בהן שפת c תומכת הן: חיבור + חיסור -

gram
Download Presentation

מבוא למדעי המחשב

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. מבוא למדעי המחשב תרגול 2 שעת קבלה: יום שני 11:00-12:00 דוא"ל: keren@eli-wigs.com http://www.pitt.edu/~stephenp/misc/turboC.exe

  2. פעולות חשבון פעולות החשבון הבסיסיות בהן שפת c תומכת הן: חיבור + חיסור - כפל * חילוק / מודול % (שארית החלוקה) פעולות נוספות מוגדרות בספריה: math.h ועל מנת להשתמש בהן יש להוסיף את הפקודה: #include <math.h> למשל: הפקודה pow(x,y) המוגדרת בספרייה זו, מבצעת x בחזקת y.

  3. הערות • מטרת ההערות בתכנית היא לתת הסברים על שלבי התכנית על מנת לעזור למתכנת ולקוראי התוכנית להתמצא. • פורמט: /* הערה */ • המהדר מתעלם מההערות. מחיקת מסך הפלט: • באמצעות הפקודה: clrscr(); • פקודה זו מוגדרת בספרייה conio.h , ולכן על מנת להשתמש בה יש לכלול ספרייה זו: #include <conio.h>

  4. קליטת ערך מהמשתמש - scanf • scanfהיא פונקצית קלט שהוגדרה מראש בספריה stdio.h. • דוגמה: נקלוט ערך למשתנה grade מטיפוס שלם: scanf (“%d”,&grade); • פורמט: scanf(“<מציין טיפוס 2><מציין טיפוס 1>…”,&<שם משתנה 1>,&<שם משתנה 2>,…); • רצוי לכתוב הוראת הדפסה לפני הוראת קלט, על מנת לידע את המשתמש מה עליו לעשות. מציין טיפוס שם המשתנה

  5. scanf - דוגמה כתוב תכנית שקולטת שני משתנים מטיפוס ממשי ומדפיסה את סכומם. #include <stdio.h> void main() { float num1,num2; printf (“\n please enter 2 real numbers”); scanf (“%f%f”,&num1,&num2); printf (“\n %f+%f=%f”,num1,num2,num1+num2); }

  6. תרגיל כתוב תכנית שקולטת מהמשתמש שני מספרים שלמים למשתנים a ו b ומחליפה את ערכיהם. #include <stdio.h> void main() { int a,b,temp; printf (“\n please enter 2 numbers”); scanf (“%d%d”,&a,&b); temp=a; a=b; b=temp; printf (“\n a=%d \n b=%d”,a,b); }

  7. תרגיל כתוב תכנית שקולטת שלושה ציונים (ממשיים) ומחשבת את הממוצע שלהם. #include <stdio.h> void main() { float grade1,grade2,grade3,ave; printf (“\n please, enter 3 numbers \n”); scanf (“%f%f%f”,&grade1,&grade2,&grade3); ave=(grade1+grade2+grade3)/3; printf (“ the average is: \t %f”,ave); }

  8. תרגיל כתוב תכנית שקולטת שני מספרים ומתייחסת אליהם כמידות של מלבן. הפלט יהיה ההיקף, השטח ואורך האלכסון של המלבן. #include <stdio.h> #include <math.h> void main() { double length,width,area,perimeter,diag; printf (“\n enter length of rectangle: “); scanf (“%lf”,&length); printf (“\n enter width of rectangle: “); scanf (“%lf”,&width); area=length*width; perimeter=2*(length+width); diag=sqrt(length*length+width*width); printf (“\n the area of rectangle: %0.2lf \n the perimeter: %0.2lf “,area,perimeter); printf (“\n the diagonal is: %0.2lf”,diag); }

More Related