Professional Documents
Culture Documents
ملخص برمجة 1
ملخص برمجة 1
ملخص برمجة 1
الوحدة األولى
AbdulRahman M.Baraka
2012
مفهوم البرمجة الكينونية
الكينون:
وحدة برمجية لها استقاللها.
المزايا:
خصوصية المعلومات واخفائها (.)Information Privacy and Hiding
الوحدة الثانية
AbdulRahman M.Baraka
2012
االدخال واالخراج
.) لالخراج على الشاشةCout( تستخدم
.) لالدخال من لوحة المفاتيحCin( واالمر
.يفصل بين أي مدخلين بفراغ او بسطر جديد
<iostream.h> كال االمرين موجودين في مكتبة
cin>>num1;
cout<<“Number 1 is :”<<num1;
cin.getline(str,length,terminating character);
cin.getline(myname,10,”X”);
\n new line
\a alert
\” “
\’ \’
Structures
مجموعة من الحقول خاصة بكائن معين (تختلف/تتفق) في
نوع البيانات.
تستخدم لوصف خصائص كائن معين.
struct structure name
{
;Variables
/* accessing element 0 */
x[0] = ‘T’;
char y[5];
int i;
for (i=0; i<5; i++)
{
cout<<y[i]<<“\n”;
}
Array-Example (p3)
# include <iostream.h>
int main(int argc, char* argv[])
{
int y[5];
int i;
cout<<"Please enter five numbers:";
for (i=0; i<5; i++)
{
cout<<"\nnumber("<<i<<"):";
cin>>y[i];
}
class class_name
{
private:
variable declarations;//class
function declarations;//members
public:
variable declarations;//class
function declarations;//members
} Object_Name;
Access Modifier
خاص (:)Private
ال يسمح بالتعامل معه إال اعضاء نفس الصنف (.)Class
:الصيغة العامة
Object declaration:
class_name obj1;
class_name obj2,obj3;
class class_name
{……}obj1,obj2,obj3;
Member Function
.تشبه الدوال العادية ولكن يجب تحديد الصنف مرتبطة به
void Class_name :: member_function_name()
{}
Example:
class time
{
private:
int hour; void time::setsec(int h)
int min; { … };
int sec;
public:
int gethour();
int getmin();
int getsec();
void sethour(int h);
void setmin(int m);
void setsec(int s);
;}
Constructor Function
دالة منتمية لصنف تحمل نفس اسمه.
ال يجوز أن ترجع أي قيمة.
تستدعى تلقائيا عند انشاء كائن من صنفها.
)( person :: person
{
;age=20; height=170; weight=70
}
اذا كان هناك معامالت لها فانه ال يجوز انشاء كائن من الصنف
إال بتمرير قيمة لهذه المعامالت.
;) person p(180,73
Example of Constructor
Example:
class time
{
private:
int hour;
int min;
int sec; void time::time(int h)
public: { … };
int gethour();
int getmin();
int getsec();
Void time(int h, int min, int sec);
void sethour(int h);
void setmin(int m);
void setsec(int s);
;}
Destructor Function
دالة منتمية لصنف تحمل نفس اسمه.
اذا تم االستدعاء بمعامالت جديدة يتم اعتمادها واال فانه يتم
استخدام القيم االولية.
ترتيب قيم اسناد المعامالت مهم جدا.
person :: person (int age, float tall=166, float
)weight=89
{
}
Const Functions and Const Objects
ال يمكن ان يجري عبرها أي تعديل على متغير.
الوحدة الثالثة
AbdulRahman M.Baraka
2012
المصفوفات واألصناف
:مصفوفة أعداد
int x[10];
1 55 6 8 0 15 26 74 10 65
0 1 2 3 4 ….
:ايجاد أكبر عدد في مصفوفة
int max=x[0];
for (int i=0 ; i<10 ; i++)
{
if (max<x[i])
max=x[i]
}
cout<<max
Max-Array.cpp
المصفوفات واألصناف
:مصفوفة داخل صنف
class Student
{
private:
string name;
int no;
double Avg;
float mark[3]; void Student::Readmark()
public: {
float sum;
string getName(); for(int y=0;y<3;y++)
void ChangeName(string x); {
double getAvg(); cout<<"enter mark("<<y+1<<"):";
void Readmark(); cin>>mark[y];
}; sum += mark[y];
}
Avg=sum/3;
}
Student2.cpp
المصفوفات واألصناف
:مصفوفة كائنات
Student x[3]; Student1 Student2 Student3
getName()
ChangeName()
void Student::Readmark()
{ readmark()
.
float sum;
for(int y=0;y<3;y++) .
{
cout<<endl<<"enter mark("<<y+1<<"):";
cin>>mark[y];
sum += mark[y];
}
Avg=sum/3; Student3.cpp
}
الفرز االنتقائي
Sorting an Array of Integers
Example:
we are 70
given an 60
array of six 50
integers 40
30
that we
20
want to sort
10
from 0
smallest to [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
largest
الفرز االنتقائي
The Selection Sort Algorithm
Start by
finding the 70
smallest 60
entry. 50
40
30
20
10
0
[1] [0][2] [1] [3] [2][4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Swap the
smallest 70
entry with 60
the first 50
entry. 40
30
20
10
0
[1] [0][2] [1] [3] [2][4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Swap the
smallest 70
entry with 60
the first 50
entry. 40
30
20
10
0
[1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side Unsorted side
70
Part of the 60
array is now 50
sorted. 40
30
20
10
0
[1] [0] [2][1] [3][2] [4][3] [5][4] [6][5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side Unsorted side
70
Find the 60
smallest 50
element in 40
the 30
unsorted 20
side. 10
0
[1] [0] [2][1] [3][2] [4][3] [5][4] [6][5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side Unsorted side
70
Swap with 60
the front of 50
the 40
unsorted 30
side. 20
10
0
[1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side Unsorted side
70
We have 60
increased 50
the size of 40
the sorted 30
side by one 20
element. 10
0
[1] [0][2] [1] [3] [2][4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side Unsorted side
70
The 60
Smallest
process 50 from
continues... 40 unsorted
30
20
10
0
[1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side Unsorted side
70
The 60
process 50
continues... 40
30
20
10
0
[1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
Sorted side
is bigger Sorted side Unsorted side
70
The 60
process 50
continues... 40
30
20
10
0
[1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
The process
Sorted side Unsorted side
keeps adding 70
one more 60
number to the 50
sorted side. 40
30
The sorted
20
side has the 10
smallest 0
numbers, [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
We can stop
Sorted side Unsorted side
when the 70
unsorted side 60
number, since 40
30
that number
20
must be the
10
largest 0
number. [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
الفرز االنتقائي
The Selection Sort Algorithm
The array is
now sorted. 70
We repeatedly
60
50
selected the
40
smallest 30
element, and 20
moved this 10
element to the 0
front of the [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]
المصفوفات واألصناف
:)تمرير مصفوفة كائنات الى صنف (الفرز االنتقائي
void ssort(Student* st,int n)
{ n j
for(int i=0; i<n-1 ; i++) 0 1 2
{ st Student1 Student2 Student3
double min=st[i].getAvg();
int pos=i; Name: Ali Name: Omar Name: Mona
for (int j=i+1 ; j<n ; j++) Avg: 66 Avg: 99 Avg: 53
{
if (st[j].getAvg()<min)
{ i=0
min=st[j].getAvg(); min=66
pos=j; Pos=0
} j=1
Student a= st[pos];
st[pos]= st[i]; Step-1
st[i]=a;
} }} ssort.cpp
)المصفوفات واألصناف (تابع
:تمرير مصفوفة كائنات الى صنف
void ssort(Student* st,int n)
{ n j
for(int i=0; i<n-1 ; i++) 0 1 2
{ st Student1 Student2 Student3
double min=st[i].getAvg();
int pos=i; Name: Ali Name: Omar Name: Mona
for (int j=i+1 ; j<n ; j++) Avg: 66 Avg: 99 Avg: 53
{
if (st[j].getAvg()<min)
{ i=0
min=st[j].getAvg(); min=53
pos=j; Pos=2
} j=2
Student a= st[pos];
st[pos]= st[i]; Step-2
st[i]=a;
} }} ssort.cpp
)المصفوفات واألصناف (تابع
:تمرير مصفوفة كائنات الى صنف
void ssort(Student* st,int n)
{ n j
for(int i=0; i<n-1 ; i++) 0 1 2
{ st Student1 Student2 Student3
double min=st[i].getAvg();
int pos=i; Name: Ali Name: Omar Name: Mona
for (int j=i+1 ; j<n ; j++) Avg: 66 Avg: 99 Avg: 53
{
if (st[j].getAvg()<min)
{ Name: Mona i=0
min=st[j].getAvg(); a Avg: 53 min=53
pos=j; Pos=2
} j=2
Student a= st[pos];
st[pos]= st[i]; Step-3
st[i]=a;
} }} ssort.cpp
)المصفوفات واألصناف (تابع
:تمرير مصفوفة كائنات الى صنف
void ssort(Student* st,int n)
{ n j
for(int i=0; i<n-1 ; i++) 0 1 2
{ st Student1 Student2 Student3
double min=st[i].getAvg();
int pos=i; Name: Mona Name: Omar Name: Ali
for (int j=i+1 ; j<n ; j++) Avg: 53 Avg: 99 Avg: 66
{
if (st[j].getAvg()<min)
{ Name: Mona i=0
min=st[j].getAvg(); a Avg: 53 min=53
pos=j; Pos=2
} j=2
Student a= st[pos];
st[pos]= st[i]; Step-4
st[i]=a;
} }} ssort.cpp
البحث الثنائي
يجب أن تكون المصفوفة مرتبة تصاعديا.
نبحث عن الرقم .11
Step 1: high
mid
low
البحث الثنائي
يجب أن تكون المصفوفة مرتبة تصاعديا.
نبحث عن الرقم .11
Step 2: high
mid
low
البحث الثنائي
يجب أن تكون المصفوفة مرتبة تصاعديا.
نبحث عن الرقم .11
Step 2: high
mid
low
البحث الثنائي
يجب أن تكون المصفوفة مرتبة تصاعديا.
نبحث عن الرقم .11
Step 2: high
mid
low
المصفوفات واألصناف
:)تمرير مصفوفة كائنات الى صنف (البحث الثنائي
int bsearch(Student* st, int n , string sn)
{
int lo=0;
int hi=n-1;
while (lo<=hi) االسم المراد البحث عنه
{
int mid = (lo+hi)/2; حجم المصفوفة
string r=st[mid].getName();
if(r==sn)
مصفوفة االسماء
return mid;
else
if( sn>r) )(الحظ أنه في الحروف حرف االلف أصغر من الباء
lo=mid+1;
else
hi=mid-1;
}
return -1; // not found bsearch.cpp
}
المصفوفات واألصناف
اضافة عنصر الى مصفوفة مرتبة تصاعديا:
اضافة الرقم :8
AddRemoveArray.cpp
المؤشرات
Memory
Attribute Location Value
16
17
18
19
20
21
مالحظة :األشكال هي للتوضيح فقط وليست كما يحدث بالذاكرة تماما 22
المؤشرات
int x=10;
Memory
Attribute Location Value
x 16 10
17
18
19
20
21
22
المؤشرات
int x=10;
String name=“Ali”;
Memory
Attribute Location Value
x 16 10
name 25 A
26 l
27 i
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
Memory
Attribute Location Value
x 16 10
name 25 A
26 l
27 i
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
Attribute Location Value
x 16 10
name 25 A
26 l
27 i
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
int *p=&x; Variables Location Value
x 16 10
name 25 A
26 l
27 i
*p 28 16
29 Address
30
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
int *p=&x; Variables Location Value
cout<<p; => 16 x 16 10
name 25 A
26 l
27 i
*p 28 16
29
30
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
int *p=&x; Variables Location Value
cout<<p; => 16 x 16 10
cout<<*p; => 10 name 25 A
26 l
27 i
*p 28 16
29
30
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
int *p=&x; Variables Location Value
cout<<p; => 16 x 16 10
cout<<*p; => 10 name 25 A
26 l
string *s=&name; 27 i
*p 28 16
*s 29 25
30
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
int *p=&x; Variables Location Value
cout<<p; => 16 x 16 10
cout<<*p; => 10 name 25 A
26 l
string *s=&name; 27 i
*p 28 16
cout<<s; =>25
*s 29 25
30
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
Memory
int *p=&x; Variables Location Value
cout<<p; => 16 x 16 10
cout<<*p; => 10 name 25 A
26 l
string *s=&name; 27 i
*p 28 16
cout<<s; =>25
*s 29 25
cout<<*s; =>”Ali” 30
المؤشرات
int x=10;
String name=“Ali”;
cout<<x; => 10
cout<<&x; => 16
int *p=&x;
cout<<p; => 16 Memory
cout<<*p; => 10
Variables Location Value
string *s=&name; x 16 10
cout<<s; =>25 name 25 A
cout<<*s; =>”Ali” 26 l
cout<<&s?? *p
27
28
i
16
*s 29 25
30
المؤشرات
class Student
{
private:
string name;
int no;
public:
string getName();
void changeNo(); Memory
};
Variables Location Value
s 215 Student:
Student s; getName()
ChangeNo()
المؤشرات
class Student
{
private:
string name;
int no;
public:
string getName();
void changeNo(); Memory
};
Variables Location Value
s 215 Student:
Student s; 216 name
Student *p=&s; 217 no
218 …..
*p 300 215
المؤشرات
class Student
{
private:
string name;
int no;
public:
string getName();
void changeNo(); Memory
};
Variables Location Value
s 215 Student:
Student s; 216 name
Student *p=&s; 217 no
s.getName(); 218 …..
*p 300 215
المؤشرات
class Student
{
private:
string name;
int no;
public:
string getName();
void changeNo(); Memory
};
Variables Location Value
s 215 Student:
Student s; 216 name
Student *p=&s; 217 no
s.getName(); 218 …..
(*p).getName(); *p 300 215
المؤشرات
class Student
{
private:
string name;
int no;
public:
string getName();
void changeNo(); Memory
};
Variables Location Value
s 215 Student:
Student s; 216 name
Student *p=&s; 217 no
s.getName(); 218 …..
(*p).getName(); *p 300 215
P->getName();
حجز الذاكرة
أنواع حجز الذاكرة:
-1الستاتيكية:
يقوم مترجم اللغة بحجز الذاكرة لجميع المتغيرات عند ترجمة البرنامج.
تبقى الذاكرة محجوزة طوال فترة التنفيذ حتى وان لم تستخدم.
مثالint x; :
-2الديناميكية:
يتم الحجز وقت تنفيذ البرنامج.
يتم حجز الذاكرة التي نحتاج لها فقط وقت التنفيذ.
استخدام للذاكرة بشكل أفضل.
يتم استخدام الذاكرة بالدالة ( )newوتحرر باستخدام (.)delete
الحجز الديناميكي للذاكرة
int *p;
P= new int;
Memory
Variables Location Value
*p 1214
1215
1216
1217
1218
1219
1220
الحجز الديناميكي للذاكرة
int *p;
P= new int;
*p=10;
Memory
Variables Location Value
*p 1214 10
1215
1216
1217
1218
1219
1220
الحجز الديناميكي للذاكرة
int *p;
P= new int;
*p=10;
*p=*p+5;
Memory
Variables Location Value
*p 1214 15
1215
1216
1217
1218
1219
1220
الحجز الديناميكي للذاكرة
int *p;
P= new int;
*p=10;
*p=*p+5;
Memory
int *x=new int[3];
Variables Location Value
*p 1214 15
*x 1215
1216
1217
1218
1219
1220
الحجز الديناميكي للذاكرة
int *p;
P= new int;
*p=10;
*p=*p+5;
Memory
int *x=new int[3];
Variables Location Value
for(int i=0;i<3;i++) *p 1214 15
x[i]=i+2; *x 1215 2
1216 3
1217 4
1218
1219
1220
الحجز الديناميكي للذاكرة
int *p;
P= new int;
*p=10;
*p=*p+5;
Memory
int *x=new int[3];
Variables Location Value
for(int i=0;i<3;i++) *p 1214 15
x[i]=i+2; 1215
1216
delete x; 1217
1218
1219
1220
القوائم المتصلة
NULL
Head
8
}
6 8 10 12
head
البحث في قائمة متصلة
node* linklist::Search(int x)
{
node* p=head;
while (p)
if (p->data==x)
return p;
else
p=p->next;
return NULL; 6 8 10 12
}
إضافة عقدة جديدة داخل قائمة متصلة
void linklist::addNode2(int n)
{
node* newNode= new node;
node* pre= NULL;
node* current=head;
while (current!=NULL && n < (*current)->data)
{
pre=current;
current=current->next; newNode n
}
newNode->data=n;
newNode->next=current;
pre->next=newNode;
} 6 8 10 12
Head Pre Current
إضافة عقدة جديدة في نهاية قائمة متصلة
void linklist::addNode3(int n)
{
node* newNode= new node;
newNode->data=n;
newNode->next=NULL; newNode
if (head==NULL)
head=newNode; n
else
{
node* _node= head;
while (_node->next!=NULL)
_node=_node->next;
6 8 10 12
_node->next=newNode;
} Head End
}
حذف عقدة من داخل قائمة متصلة
int linklist::DeleteNode(int n)
{
node* pre= NULL;
node* current=head;
while (current !=NULL && n != (*current)->data)
{
pre=current;
current=current->next;
}
if (current=NULL)
return 0;
if ((pre=NULL)
head =head->next;
else
6
pre->next=current->next; 8 10 12
delete current;
return 1; Head Pre Current
}
نهاية
الوحدة
)1( برمجة
الوحدة الرابعة
الدوال الصديقة والعمليات
Friend Functions & Operators
AbdulRahman M.Baraka
2012
تمرير العوامل للدوال
Parameters Passing
(Function)) الى الدوالParameters( يتم تمرير العوامل بالقيمة
.وأي تغيير على القيم داخل الدالة ال يغير القيم الحقيقية للعوامل
Variables Reference Value
a 1214 10
b 1215 20
1216
1217
1218
int main() { 1219
int a=10; 1220
int b=20;
a:10
cout << “a: " << a << endl; b:20
cout << “b: " << b <<endl<<endl;
} Passing.cpp
تمرير العوامل للدوال
Parameters Passing
(Function)) الى الدوالParameters( يتم تمرير العوامل بالقيمة
.وأي تغيير على القيم داخل الدالة ال يغير القيم الحقيقية للعوامل
Variables Reference Value
void swap1 (int x, int y) a 1214 10
{
int z; b 1215 20
20
z=x; x 1216 10
x=y; 10
y 1217 20
y=z;}
z 1218 10
int main() { 1219
int a=10; 1220
int b=20;
swap1(a,b); X:10
cout << “a: " << a << endl; Y:20
cout << “b: " << b <<endl<<endl;
} Passing.cpp
تمرير العوامل للدوال
Parameters Passing
.يمكن تمرير العوامل بالقيمة مع تغيير القيم الحقيقية للعوامل
اذا كانت الدالة ( )Addهي دالة صديقة فإنها تستدعى كالتالي ().)Add(C
Friends.cpp
الدوال واألصناف الصديقة
Friend Functions & Classes
الدوال الصديقة يحق لها معالجة األصناف الخاصة بالصنف
(.)Private
اذا كانت الدالة ( )Addهي دالة صديقة فإنها تستدعى كالتالي ().)Add(C
Friends.cpp
Function Overloading
تحميل الدوال.
ال يمكن تعريف أكثر من دالة لها نفس المعامالت ونفس القيمة
المرجعة.
Overloading.cpp
نهاية
الوحدة
)1( برمجة
الوحدة الخامسة
الوراثة
Inheritance
AbdulRahman M.Baraka
2012
الوراثة
.هي عملية اشتقاق صنف من صنف آخر
GraduatedStudent
<<private>> GradYear:int
<<public>> int getYear ().
<<public>> void changeYear().
Inheritance1a.cpp
<<public>> void print().
الوراثة :البناء والهدام
;GraduatedStudent x
GraduatedStudent
<<public GraduatedStudent().
<<public>> ~ GraduatedStudent().
Inheritance3.cpp
مستوى الوصول ()Access Modifier
خاص (:)Private
ال يسمح بالتعامل معه إال اعضاء نفس الصنف (.)Class
Type of Inheritance
private - - -
protected private protected protected
public private protected public
Inheritance4.cpp
الوراثة المتعددة
ـ
Inheritance5.cpp
البناء والهدام: الوراثة المتعددة
Constructor
3 1 2
Class C : public A, public B.
1 3 2
Destructor
Template األصناف والدوال القالبية
.هي المقدرة على تأجيل تحديد نوع البيانات المستخدمة
:الصيغة العامة
int main()
{
cout<<Max(10,50)<<endl;
system ("pause");
return 0;
} template.cpp
تعدد األوجه ()Polymorphism
هي المقدرة على تأجيل تحديد هوية الدالة المراد استعمالها من وقت
الترجمة ( )Compile Timeالى وقت التنفيذ (.)Run Time
Inheritance4.cpp
نهاية
الوحدة
برمجة ()1
الوحدة السادسة
التعامل مع الملفات
AbdulRahman M.Baraka
2012
هيكلية البيانات
. . . . . 14/2/1998 محمد غزة
1/5/2000 رفح علي ملف
محمد حقل
11011101 بايت
بايت
1
تنظيم الملفات
التنظيم التتابعي:
يتم تخزين السجالت بشكل تسلسلي.
أول سجل يحمل أصغر رقم وهكذا..
للوصول الى السجل رقم ( )nيجب المرور على العدد ( )n-1من
السجالت.
template.cpp
تنظيم الملفات
التنظيم العشوائي:
يتم تخزين السجالت بشكل عشوائي.
يتم الوصول الى السجل المراد بشكل مباشر.
while(cin>>sno>>sname)
{ StudentFile<<sno<<","<<sname<<endl; }
StudentFile.close();} seqFiles1.cpp
Files and Stream
مالحظة هامة:
عند فتح الملف باستخدام .ios::out
فإنه يتم انشاء الملف من جديد اذا لم يكن موجودا.
أما اذا كان موجودا فإنه يقوم بحذفه جميع البيانات الموجودة بداخله.
ويقوم بإدخال البيانات الجديدة من بداية الملف.
while(StudentFile>>sno>>sname)
{
cerr<<"Student no:"<<sno<<endl<<"Student name:"<<sname<<endl;
count++;
}
cerr<<"Number of Students: "<<count<<end;
StudentFile.close();
seqFiles3.cpp
نهاية
الوحدة