Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 מבוא למחשב שפת C תרגילי חזרה. 2 תרגיל 1 : מחשבון קלט : תרגיל חשבוני המורכב ממספרים שלמים עם פעולות חשבוניות ביניהם. התרגיל מסתיים ב =. אפשריים רווחים.

Similar presentations


Presentation on theme: "1 מבוא למחשב שפת C תרגילי חזרה. 2 תרגיל 1 : מחשבון קלט : תרגיל חשבוני המורכב ממספרים שלמים עם פעולות חשבוניות ביניהם. התרגיל מסתיים ב =. אפשריים רווחים."— Presentation transcript:

1 1 מבוא למחשב שפת C תרגילי חזרה

2 2 תרגיל 1 : מחשבון קלט : תרגיל חשבוני המורכב ממספרים שלמים עם פעולות חשבוניות ביניהם. התרגיל מסתיים ב =. אפשריים רווחים בין המספרים לפעולות מספר יכול להיות שלילי, או חיובי. מספר חיובי עשוי להופיע עם + לפניו דוגמאות קלט : 123*34 = 3*+4 * 8= הנחה : הקלט תקין

3 3 תרגיל 1 : מחשבון ( המשך ) הקלט נעשה בשני שלבים : – קריאה של רצף תווים המייצגים מספר לתוך מחרוזת. הפונקציה get_num_from_input – הפיכת המחרוזת למספר שלם. הפונקציה getint התוכנית מנוהלת ע " י לולאה שקוראת בכל פעם מספר ופעולה. הלולאה נעצרת כאשר מופיע ‘ = ‘

4 4 הפונקציה get_num_from_input // reads characters representing an int from input // writes to num_string a string representing a number // returns the next char in input which is not a space char get_num_from_input(char *num_string) { char c; //reads a char at a time int i=1; scanf("%c",&c); while(is_space(c)) //skip possible spaces scanf("%c",&c); num_string[0]=c; // may also be - or + scanf("%c",&c); while (is_digit(c)) { num_string[i++]=c; scanf("%c",&c); } num_string[i]='\0'; while(is_space(c)) scanf("%c",&c); return c; }

5 5 הפונקציה getint // converts a string into an integer void getint(int *pn, char *num_string) { char c; //reads a char at a time int sign=1; //the sign of the number if(!is_digit(*num_string)) { sign = (num_string[0]=='-') ? -1 : 1; num_string++; } for (*pn = 0; *num_string; num_string++) *pn = 10* *pn + (*num_string-'0'); *pn *= sign; }

6 6 הפונקציות is_digit ו is_space // returns true if c is a space character int is_space(char c) { return c==' ' || c=='\n' || c=='\t'; } // returns true if c is a digit character int is_digit(char c) { return c>='0' && c<='9'; }

7 7 הפונקציה main // program: calculator // Reads a sequence of operations with integers // and prints the result // the operation must end at '=', spaces are allowed #include char get_num_from_input(char *); void getint(int *, char *); int main() { int num, result=0; char op,next_op; char snumber[11]; printf ("Enter a computation with integers ending with =:\n"); next_op=get_num_from_input(snumber); getint(&result,snumber);

8 8 הפונקציה main do { op=next_op; next_op=get_num_from_input(snumber); getint(&num,snumber); switch (op) { case '+': result+=num; break; case '*': result*=num; break; case '-': result-=num; break; case '/': result/=num; break; case '%': result%=num; break; default:; } } while(next_op!='='); printf("%d\n",result); return 0; }

9 9 תרגיל 2 : מחשבון של מספרים ממשיים קלט : תרגיל חשבוני המורכב ממספרים ממשיים עם פעולות חשבוניות ביניהם. התרגיל מסתיים ב =. אפשריים רווחים בין המספרים לפעולות מספר יכול להיות שלילי, או חיובי. מספר חיובי עשוי להופיע עם + לפניו מספר יכול להופיע עם או בלי נקודה עשרונית דוגמאות קלט : 12.34*56 = 3.14*+4.00 * -8= הנחה : הקלט תקין

10 10 תרגיל 2 : מחשבון של מספרים ממשיים ( המשך ) הקלט נעשה בשני שלבים : – קריאה של רצף תווים המייצגים מספר לתוך מחרוזת. הפונקציה get_num_from_input – הפיכת המחרוזת למספר ממשי. הפונקציה getdouble התוכנית מנוהלת ע " י לולאה שקוראת בכל פעם מספר ופעולה. הלולאה נעצרת כאשר מופיע ‘ = ‘

11 11 הפונקציה get_num_from_input // get_num_from_input: reads characters representing a floating // point number from input // writes to num_string a string representing a number // returns the next char in input which is not a space char get_num_from_input(char *num_string) { char c; //reads a char at a time int i=1; scanf("%c",&c); while(is_space(c)) //skip possible spaces scanf("%c",&c); num_string[0]=c; // may also be - or + scanf("%c",&c); while (is_digit(c)) { num_string[i++]=c; scanf("%c",&c); }

12 12 הפונקציה get_num_from_input ( המשך ) if (c=='.') // decimal point { num_string[i++]=c; scanf("%c",&c); while (is_digit(c)) { num_string[i++]=c; scanf("%c",&c); } num_string[i]='\0'; while(is_space(c)) scanf("%c",&c); return c; }

13 13 הפונקציה getdouble // converts a string into a floating point void getdouble(double *pn, char *num_string) { char c; //reads a char at a time int sign=1; //the sign of the number double decimal_part; double decimal_fraction=0.1; if(!is_digit(*num_string)) // starts with + or - { sign = (num_string[0]=='-') ? -1 : 1; num_string++; } for (*pn = 0.0; is_digit(*num_string); num_string++) *pn = 10.0* *pn + (*num_string-'0');

14 14 הפונקציה getdouble ( המשך ) if(*num_string=='.') { ++num_string; for (decimal_part=0.0; *num_string ; num_string++) { decimal_part += decimal_fraction*(*num_string-'0'); decimal_fraction/=10; } *pn+=decimal_part; } *pn *= sign; }

15 15 הפונקציה main // program: calculator // Reads a sequence of operations with real numbers // and prints the result // the operation must end at '=', spaces are allowed #include char get_num_from_input(char *); void getdouble(double *, char *); int main() { double num, result=0; char op, next_op; char snumber[11]; printf("Enter a computation with real numbers ending with =:\n"); next_op = get_num_from_input(snumber); getdouble(&result,snumber);

16 16 הפונקציה main do { op=next_op; next_op=get_num_from_input(snumber); getdouble(&num,snumber); switch (op) { case '+': result+=num; break; case '*': result*=num; break; case '-': result-=num; break; case '/': result/=num; break; default:; } }while(next_op!='='); printf("%lf\n",result); return 0; }

17 17 תרגיל 3 : פירוק מספר שלם למכפלת ראשוניים קלט : מספר שלם חיובי פלט : פרוק של המספר למכפלת מספרים ראשוניים דוגמה : 1350 = 2 * 3^3 * 5^2

18 18 הפונקציה decompose // decompose: decomposes n into a product of primes. // and prints the decomposition. n assumed positive // 1. find deg: highest power that 2 divides n //2. print 2^deg, and divide n by 2^deg //3. redo 1 and 2 for all odd integers, from 3 up to // square root of n void decompose(int n) { int i; // runs over divisors n int deg; // used as a degree of a prime if(n==1 || n==2 || n==3){ printf("%d = %d\n", n,n); return; } printf ("%d =",n); if (!(n%2))//n is even { deg=degree(2,n);//the highest power that 2 divides n printf (deg>1?" 2^%d ":" 2 ",deg); n /= pow_int(2,deg);// divide n by 2^deg if (n>1)// if n is a power of 2, stop here printf("*"); }

19 19 הפונקציה decompose ( המשך ) for (i=3; n>1 && i*i <= n; i+=2)//run over odd integers { if (!(n%i)) { deg=degree(i,n); printf (deg>1?" %d^%d ":" %d ",i,deg); n /= pow_int(i,deg); if (n>1) printf("*"); } if (n>1) // n is divisible by a prime > sqrt(n) printf (" %d",n); }

20 20 הפונקציה degree // degree: returns the highest power that d divides n // Algorithm. start from exp = 0. keep deviding n by d, //until d no longer divides n. increment exp by 1 //each time n is divided. int degree(int d, int n) { int exp=0; //power of d while (!(n%d)) { ++exp; n/=d; } return exp; }

21 21 הפונקציה pow_int // pow_int: returns a to the bth power //algorithm: a^b = a*a*...*a (b times). int pow_int(int a, int b) { int product=1; for (; b>0; --b) product*=a; return product; }

22 22 הפונקציה main /* module: primedec.c writes positive integers as a product of prime numbers program is teminated when typing 1 (or a smaller integer). Input: a sequence of positive integers, ended by 1. Output: For each number its decomposition to product of primes */ #include void decompose(int);int main() { int number; // this number will be decomposed do { printf("\nType a positive integer (1 or less to quit): "); scanf("%d", &number); if (number>0) { decompose(number); } } while (number>1); return 0; }

23 23 תרגיל 4 : פירוק מספר שלם למכפלת ראשוניים בעזרת רקורסיה קלט : מספר שלם חיובי פלט : פרוק של המספר למכפלת מספרים ראשוניים דוגמה : 1350 = 2 * 3 * 3 * 5 * 5

24 24 הפונקציה decompose void decompose(int n, int min_div) { int div=min_div; if(n==1) return; while(1) { if(n%div==0) { printf("%d",div); n/=div; if(n>1) printf(" * "); decompose(n,div); return; } div++; }

25 25 הפונקציה main int main() { int number; // this number will be decomposed do { printf("\nType a positive integer (1 or less to quit): "); scanf("%d", &number); if (number > 1) { printf("%d = ", number); decompose(number,2); } }while (number > 1); return 0; }


Download ppt "1 מבוא למחשב שפת C תרגילי חזרה. 2 תרגיל 1 : מחשבון קלט : תרגיל חשבוני המורכב ממספרים שלמים עם פעולות חשבוניות ביניהם. התרגיל מסתיים ב =. אפשריים רווחים."

Similar presentations


Ads by Google