2. •ישנןמספרפעולותשונות ()פונקציותשאפשרלבצעעלמחרוזותבשפתc.++
תוצאה פונקציה
מחזירהאתהתובמיקוםіבמחרוזתs)תו .ראשוןנמצאבמיקום0( S [ і[
מחזירהאתאורכהשלהמחרוזתs. Strlen (s(
מחזירה0אםהמחרוזותs-וtתו ,זהותאחריאם .תוהמחרוזתs
גדולהמהמחרוזתtיוחזר1ואחרת ,יוחזר1.-
Strcmp (s,t(
מעתיקהאתהמחרוזתtלתוךהמחרוזתsומחזירהערך.זה Strcpy (s,t(
מעתיקהאתnהתוויםהראשוניםשלהמחרוזתtלתוךהמחרוזתs
ומחזירהערך.זה
Strncpy (s,t,n(
•בדיקתהאםהתוובמחרוזתהואאות
for (int і = 0 ; і<max ; і++)
{
( isalpha (str[i]) ) // function from cytpe that check if the char is a letter
}
הערהיש :צורךלהגדירבתחילתהתוכניתאתהספרייה:#include <cytpe.h<
•פונקציתהפיכתאותקטנהלגדולהולהפך
2
3. void OpositeLetter (char str [ ] )
{
for ( int i=0; str[i] != NULL; і++ ) //until the NULLl character
{
if ( isalpha (str[i]) ) //function from cytpe that check if the char is a letter
}
if ( (str[i]>='a') && (str[i]<=’z’) )
}
str[i]=(str[i]-32)
continue;
{
if ((str[i]>='A') && (str[i]<=’Z’) )
{
str[i]=(str[i]+32)
continue;
}
}
}
}
•ניקוימחרוזתמתוויםשאינםאותיות
void cleaning_str (char str [n])
{
int ok_chars = 0 ;
for (int i=0; str[i] != ‘0’ ; і++)
{
if (isalpha(str[i])) //check if the character is an alphabetic
str [ ok_chars++ ] = str [ і ]
}
Str [ ok_chars++ ] = '0 '; // terminate the string
}
גלובליות פונקציות
•החלפתסדרשנימשתנים)swap(
3
4. void swap (int &a, int &b)
{
int temp;
temp = b;
a = b;
b = temp;
}
•חזקה)מתוךספרית#include <math.h<(
מבצעXבחזקתY.
pow(x,y);
•שורש)מתוךספרית#include <math.h<(
sqrt(y);
•בדיקההאםהמספרזוגיאולא
int even (int num)
}
return (num % 2 == 0 ? 1 : 0); //if Condition true return 1 else return 0
{
•הפיכתמחרוזת-לinteger)ערךמספרישעליוניתןלבצעפעולות(מתמטיות
מצריךשימושבספרית#>include <stdlib.h
int num = atoi (str) ; //ascii to integer
float num = atof (str) ; //ascii to float
מערכים
•איתחולמערךלאפס
4
5. Void init (int A [ ],const int n)
{
for (int і=0; і<n; і++)
{
str[ і ] = 0;
}
}
•קריאתנתונים-מהUserלתוךמערך
void read (int A [n], const int n )
{
for (int і=0 ; і<n ; і++)
{
cin>> A [ і ];
}
}
•הדפסתמערך
void write (int A [n], const int n )
{
for (int і=0 ; і<n ; і++)
{
cout<< A[і]<<“t “ ;
}
}
•קריאתנתונים-מהUserלתוךמערךדוממיידי()מטריצה
5
6. void read (int A [n] [n], const int n )
{
for (int і=0 ; і<n ; і++)
{
for (int j=0; j<n; j++)
{
cin>> A [ і ] [ j ];
}
}
•הדפסתמערךדוממיידי(מטריצה )
void write (int A [n] [n], const int n )
{
for (int і=0 ; і<n ; і++)
{
for (int j=0; j<n; j++)
{
cout<<”t “<< A [ і ] [ j ];
}
cout<<endl;
}
•ספירתשכניםלקורדינאטהספציפיתבמערךדוממיידי()מטריצה
6
7. Int CountNeighbours (int Matrix[N][N], const int N, int i, int j(
{
int count = 0;
int x,y;
for(int row = 0; row<3;++row(
{
)for (int column = 0; column <3; ++column
{
if (row == 1 && column == 1(
continue;
x = i+row-1;
y = j+column-1;
//Legal cell (if (x>=0 && x<N && y>=0 && y<N
];count += Matrix[x][y
{
{
return count ;
}
•פונקציתחיפושבמערךדוממיידי)לאממוין(
int if_exists (int a[n][m], int num)
}
for (int i=0; i<n; і++)
}
for (int j=0; j<m;j++)
{
if (a[i] [j] == num;
return 1;
{
{
return 0;
{
•פונקציהזוסופרתכמהמספריםעםאותוערךישבמערך)טווחמספריםמי0–9.(
הפונקציהמכניסהלמערךחדש)B(אתכמותהמספריםמכלערך.
7
8. void check_M_into_N (int A[ ],int B[ ] ,const int n ,const int m)
}
int var = 0;
for(int x = 0; x<n; x++)
{
if (A[x]<n(
{
var =A[x[;
if (var<=m(
{
B [var] ++ ;
{
{
{
{
רקורסיות
8
9. •חזקהבעזרתשימושברקורסיה
float apowerb (float a,int b)
}
if (b == 0) //stop condition
return 1;
return (a * apowerb (a,b-1)); //recursive call
{
•חיבורכלאבריהמספר:)לדג435< =11.(
int sum_f (int n(
}
if (n<10(
return (n(;
else
return (n%10) + sum_f (n/10(; //recursive call
{
•מציאתהמחלקהמשותףהמקסימליביןשנימספרים)gcd(
Int gcd (int n, int m)
{
if ( (n<0) || (m<0) )
return 1;
if (n == m)
return n;
if (n < m)
return (gcd (n, m-n));
return (gcd (m, n-m));
}
•חיבורכלהתאיםבמערך)מחזירהאתחיבורכלהתאים(
9
10. int sum_rec (int a [ ],int n(
}
if (n==1( //stop condition
return a [0[;
else
return ( sum_rec (a,n-1) + a[n-1 [ ) ; //recursive call
{
•מציאתהאיברהכיגדולבמערך)FMC = Find Max Cell(
іnt find_max_cell (int a [ ] , int n)
{
if n==1) //stop conditoin
return (a [ n-1 ]; // return a [ 0 ]
else
if (a [ n-1 ] > find_max_cell (a, n-1) //search for the biggest cell
return (a [ n-1 ] ;
else
return find_max_cell (a, n-1);
}
•מציאתזוגהתאיםהצמודיםאשרנותניםלנואתהסכוםהכיגדולעלידיחיבורם
int sum_max_couple (int a[ ],int n(
}
if (n==1) //stop conditoin
return (a [ n-1 ]); // a [ 0]
else
if ((a [ n-1 ]+ a [n-2]) > sum_max_couple (a, n-1((
return (a [ n-1 ] + a [n-2] ) ;
else
return sum_max_couple (a, n-1(;
{
•פירוקמספרלמספריםהקטניםממנועד-ל1וסכימתערכיהם
קלט :)לדג=4הפונקציה:תחזיר4+3+2+1=10
10
11. Int sum (int limit)
{
if (limit == 1)
return 1;
return (limit + sum (limit – 1); //recursive call
•הפונקציהמחשבתאתהמספרהמתקבלממכפלתכלהספרותשלnum
int son_calc (int num)
{
if (num<10) //stop condition
return (num(;
else
return (num%10) * son_calc (num/10); // recursion call
}
•הפונקציהמחשבתאתסדרתפיבונאצ"ילדגקלט :=1123581321...
עמוד:253
Int fib (int n)
{
if (n < 3 ) //stop condition
return 1;
else
return ( fib (n-2) + fib ( n-1) ); //recursive call
•הדפסתמשפטמהסוףלהתחלה)עמ256.(
Void reverse ( )
{
char ch;
cin>>ch;
if ( !cin.eof() )
reverse (); //read another character - recursively
cout<<ch;
}
היפוךמספרקלט :לדג )=235ידפיס ,532עמוד (258
11
12. Void rev_num (int n(
}
cout<<n % 10;
if ( (n / 10 ) > 0(
rev_num (n/10;(
{
•הפונקציהמקבלתמערךומחזירה1עםערךכלאחדמאיבריהמערךגדולמסכום
קודמיו)משתמשתבשניפונקציות(רקורסיביות
Int is_order_vec (int vec[ ] ,int n)
{
if (n == 1 )
return 1;
else
if (vec[ n-1] > sum_vec(vec, n-1) )
return is_order_vec (vec, n-1);
else
return 0;
}
Int sum_vec (int vev[ ] , int n)
{
if ( n == 1)
return vec[0];
else
return (sum_vec (vec,n-1) + vec[n-1]);
}
12
המערך גודל
IXEDN
13. יעילות
להל"ןהפונקציותלטיפולביעילות)לאלרשימותמקושרות(
עמוד316–318
הפונקציותהם:
Void read_input (int a [ ]);
נועדלקבלתנתוניםלתוךמערךמהקלט
Void write_output (int a[ ] );
נועדלהדפסתהמערך.הממויין
Void merge_sort (int a[ ] ,int low, int high);
מבצעמיוןמיזוגעלמערך)סיבוכיות=O (n log n.(
משתמשבפונקציות:merge.
עמוד317.
Void merge (int a, int left,int mid, int right);
ממזגשנימערכיםממויניםלאחד)נקראעלידיmerge_sort.(
Void copy_arr (int from[ ] ,int to [ ] , int ind, int length,int c_ind);
פונקציהלהעתקתערכיםמימערךאחדלשניוהיאנקראתעלידיפונקציתmerge
)318.(
13
15. רשימות
•קריאתרשימה)listמהמשתמש (עםמספרתאיםקבוע)max(
עמוד351)בכדילקרואמספריםישלהחליף-לnumberכמובןגם-בstruct(
//This function get a pointer address of a list, and read the list from the input
void read ( element *&list, const int max(
}
element *p,*last;
list=new element;
p=list;
cout<<"Enter a letter : “ ;
cin>>p->letter;
for (int cnt=0; cnt<max; cnt++)
}
p->next =new element;
last= p;
p=p->next;
cout<<"Enter a letter : “ ;
cin>> p-> letter;
{
delete (p(;
last -> next = NULL;
}
להל"ןרשימתפונקציותמתוךהספרעמודים)350–354.(
Void read (element *&list);
קוראתרשימה.מהקלט
עמוד:351
Void write (element *list);
מדפיסהאתהרשימה.המקושרת
עמוד:351
Int length (element *list);
מקבלתlistומחזירהאתהאורך.שלה
עמוד:351
15
16. Int position (element *list, int num);
מקבלתרשימהומספרומחזירהאתמיקומו
עמוד:352
Void remove (element *&list,int position );
מקבלתרשימהמיקום +ומוחקתאתהמספרבאותו.מקום
עמוד:352.
Void del (element *&list, int num);
מקבלתרשימה+מספרמבצעת ,אתהחיפושאחריהאיברומוחקת ,אותו.מהרשימה
עמוד:358
Void insert (element *&list,int new_num,int position);
מכניסהאתהאיברהחדש-לpositionהפונקציה ,שקיבלנונעזרתלביצועזאתעלידיפונקצית
addופונקציתele.
עמוד:353–354.
Element *ele (element *list,int position);
מקבלתרשימהומיקוםומחזירהpointerלאותומיקום)position.(
עמוד:353.
Void add (int new_num, element *&list,element *this_el);
מקבלתמספרומיקום ,רשמיה ,חדשכתובת /הכנסהומכניסה ,את-הnew_numלאותומיקום
עמוד:354.
Element *succ (element*list,element *this_el)
מקבלתאיברומחזירה ,ברשימהמצביעלאיברהבא)מחזירהpointer.(
עמוד:355.
Element *pred (element*list,element *this_el)
מקבלתאיברברשימהומחזירהמצביעלאיברהקודם)מחזירהpointer.(
עמוד:356.
16
21. Int empty_tree (node *root);
מחזירערך1עםהעץו ,ריק0.אחרת
עמוד:376.
Int no_of_nodes (node *root);
מונהאתמספרהצמתים.בעץ
עמוד:376.
Int no_of_leaves (node *root);
מונהאתמספרהעלים.בעץ
עמוד:376.
Int is_leaf (node *T);
מקבלצומתומחזירתשובהאםהואעלה
עמוד:377.
Int is_in_tree (node *T,int a);
מקבלמספרועץ)לאומחזיר (ממוייןתשובהאםהמספרנמצא.בעץ
עמוד:377.
Int descendant (node *T,int x, int y);
מקבלמצביעלעץTושנימספריםx-וyובודקת ,האםקיימםשניצמתיםבעץTהמכילים
אתx-וyכך ,-שyהואצאצאשל–x.
עמוד:390)10.18/(
Int min ( int a ,int b, int c);
מקבלתעץTומחזירהאתהאיברהקטןביותר.בעץ
:עמוד390)10.19(
Int max (( int a ,int b,);
מקבלתעץTומחזירהאת.גובההו
עמוד:391)10.20.(
בינארים עצים סריקת
הדפסתתכולתהעץבשלושהצורות.
21