Professional Documents
Culture Documents
CPlusPart5 (WWW Yasbooks Com)
CPlusPart5 (WWW Yasbooks Com)
com
: C++
: 2
-1-
C++
www.IrPDF.com
: C++
:
:
.
:
-1 C++ . cos
-2 .
:
-1
-2 .
-3
)(main
>#include <iostream.h
-2 sqr :
)int sqr(int x
{
;return x*x
}
-3 :
)(int main
{
;int a
;cin>>a
;cout << sqr(a) << endl
;return 0
}
-2-
www.IrPDF.com
: C++
>#include <iostream.h
)int sqr(int x
{
;return x*x
}
)(int main
{
;int a
;cin>>a
;cout << sqr(a) << endl
;return 0
}
! ! ) ! (:
: ... .
sqr
. :
1
2
...
n
)( main
. :
;)int ali(int x
)(int main
{
...
}
)int ali(int x
{
...
}
)( main
. ; .
-3-
www.IrPDF.com
: C++
)void ali(int x
{
;Statement
}
:
)void ali(int x
{
;Statement
; Return
}
.
) ( sqr . ... .
.
. int .
- . :
;Cin>>a>>b>>c
;Cout<< ali(a,b,c)<<endl
- : (default value) : :
www.IrPDF.com
: C++
return . :
;x*x
return
2 x .
2 :
;2
return
...
: 2 1 10 :
:
:
>#include <iostream.h
)int sqr(int x
{
;return x*x
}
)(int main
{
)for (int i=1; i<=10; i++
{
;cout << sqr(i) << endl
}
;return 0
}
:
>#include <iostream.h
;)int sqr(int x
)(int main
{
)for (int i=1; i<=10; i++
{
;cout << sqr(i) << endl
}
;return 0
}
)int sqr(int x
{
;return x*x
}
-5-
www.IrPDF.com
: C++
: 100 1 : 3
0 1 . :
.
#include <iostream.h>
int aval(int x)
{
int w=0;
for(int i=1;i<=x;i++)
{
if (x%i==0)
w++;
}
if(w==2)
return 1;
else
return 0;
}
int main()
{
int a;
for(int i=1;i<=100; i++)
{
if(aval(i)==1)
cout<<" adad e aval = " << i<<endl;
}
return 0;
}
:
#include <iostream.h>
int aval(int x);
int main()
{
int a;
for(int i=1;i<=100; i++)
{
if(aval(i)==1)
cout<<" adad e aval = " << i<<endl;
}
return 0;
}
int aval(int x)
{
int w=0;
for(int i=1;i<=x;i++)
{
if (x%i==0)
w++;
}
if(w==2)
return 1;
else
return 0;
}
-6-
www.IrPDF.com
: C++
. :
#include <iostream.h>
int max ( int x, int y, int z)
{
int max=x;
if(max<y) max=y;
if(max<z) max=z;
return max;
}
int main()
{
int a,b,c;
cout<<"Enter three numbers: " <<endl;
cin>>a>>b>>c;
cout<<"The max number is = " <<max(a,b,c)<<endl;
return 0;
}
:
#include <iostream.h>
int max ( int x, int y, int z);
int main()
{
int a,b,c;
cout<<"Enter three numbers: " <<endl;
cin>>a>>b>>c;
cout<<"The max number is = " <<max(a,b,c)<<endl;
return 0;
}
int max ( int x, int y, int z)
{
int max=x;
if(max<y) max=y;
if(max<z) max=z;
return max;
}
-7-
www.IrPDF.com
: C++
:
.
#include <iostream.h>
void fun (int x)
{
if(x<0)
cout<<"The number is lower then zero ! " <<endl;
else
cout<<"The number is higher then zero ! (Or equal with ) " <<endl;
return ;
}
int main()
{
int a;
cout<<"Enter a number: " ;
cin>>a;
fun(a);
return 0;
}
.
.
: !
MajidOnline.com First Persian Graphic and Web design Resource : :
#include <iostream.h>
void fun ()
{
cout<<"***MajidOnline.com "<<endl
<<"First Persian Graphic and Web design Resource "
<<endl;
return ;
}
int main()
{
fun();
return 0;
}
-8-
www.IrPDF.com
: C++
:
:
pass by refrence -1
pass by value -2
:
>#include <iostream.h
) int f1( int a
{
;return a *= a
}
) void f2( int &b
{
;b*= b
}
)(int main
{
;int x = 2, z = 4
"cout << "x = " << x << " before passByValue\n
" << "Value returned by passByValue:
<< f1( x ) << endl
;<< "x = " << x << " after passByValue\n" << endl
********//
;cout << "z = " << z << " before passByReference" << endl
;) f2( z
;cout << "z = " << z << " after passByReference" << endl
;return 0
}
pass by value f1 .
.
pass by reference f2 & ) ( .
. pass by reference
.
:
x = 2 before passByValue
Value returned by passByValue: 4
x = 2 after passByValue
z = 4 before passByReference
z = 16 after passByReference
. :
-1 .
-2 .
... -3
-9-
www.IrPDF.com
: C++
& :
: 10 :
>#include <iostream.h
)void jj(int &a
{
;a*=10
}
)(int main
{
;int x
;" cout<<"Enter a number plz ! :
;cin>>x
;)jj(x
;cout<<"result is : " <<x
;return 0
}
a .
& . .
) & ( .
: static int :
Static int int & .
:
>#include <iostream.h
)(void printme
{
;static int i=1
;" "<<cout<<i++
}
)(int main
{
)for(int k=0; k<10; k++
;)(printme
;return 0
}
:
9
10
int . :
:
9
;static int i
static int )
( .
- 10 -
www.IrPDF.com
: C++
:
.
*** :
1 , 3 , 6 , 10 , 15 , 21 ,
f(n) = f(n-1) + n
,
f(1)=1
n 1 :
:
) (
) f(n ) f(n-1 . .. . )f(n ) f(n-1 . f(n-1) = f(n-2) + n-1 : ) f(n-1
) f(n-2 ... f(1)=1 .
) f(n .
:
-1
-2 . f(1)=1 .
) (
:
>#include <iostream.h
)int f ( int x
{
)if (x==1
;return 1
else
;return f(x-1)+x
}
)( int main
{
;int a
; " cout<<"enter Number of figure which u want :
;cin>>a
;cout<<"f("<<a<<")="<<f(a)<<endl
;return 0
}
*** .
: .
1 , 1 , 2 , 3 , 5 , 8 ,
>#include <iostream.h
)int f ( int x
{
))if ((x==1)||(x==2
;return 1
else
;)return f(x-1)+f(x-2
}
)( int main
{
;int a
; " cout<<"enter Number of figure which u want :
;cin>>a
;cout<<"f("<<a<<")="<<f(a)<<endl
;return 0
}
- 11 -
www.IrPDF.com
: C++
: :
;"<<a++
"<<"<<a++
>#include <iostream.h
)(int main
{
;int a=3
"<<cout<<a++<<" "<<a++
;return 0
}
.
c++ ...
.
:
>#include <iostream.h
)void vahid(int a , int b
{
;cout<<"a="<<a<<endl
;cout<<"b="<<b<<endl
; return
}
)(int main
{
;int a=3
;)vahid(a,a++
;return 0
}
.
:
-1 .
-2 n s(n) . :
S n = S n 1 + n 2
Sn = i 2
i =1
S1 = 1
-3
) ( .
- 12 -
www.IrPDF.com
: C++
:
:
www.mrh.ir
www.majidonline.com
:: 1385
- 13 -