You are on page 1of 101

:

magedxl@hotmail.com :
...

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


* " "visual c++" "Windows

.
*
.
* .
* % 100 .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

..
...
....
..
..

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

..
.. ..
..Visual C++
..

.C++
..

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


............................................................................
.......................................................................................
....................................................................................

c++
......................................................................... c++
................................................................................
.................................................... c++
......................................................................
............................................................
.......................................................................
..................................................
..........................................................
.........................................................................
.................................................................................
...............................................................................
............................................................................
...............................................................
.............................................................................
.........................................................................
..............................................................................
.......................................................................
...........................................................................
..........................................................................
...........................................
...................................................c++

Visual C++6.0
.......................................... Visual C++


..................................................................................
...................................................................

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

) ( ...........................................
.................................................................
................................
.................................................................. c++


..................................................................................
.......................................................................
......................................if statements
............................................. switch statement
...................................................................
............................................................
............................................. For
.......................................... for Loops
................................While & Do
While...........................................................
...............................................................Break
...................................................... continue
)(.............................................................. exit
................................................................ goto


..................................................................................
..................................
............................................
.............................................................

.................................................................................
...............................................................


................................................................................
)(....................................................... fopen
)(............................................ fprintf
)(.................................................... fclose
)(.......................................................putw() getw
...............................................................................

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


C++
Basic Elements of C++

C++
* C++
- A.B.C
- a.b.c
- 1.2.3
- :
<
+
>
)(
_
=>
\
/
$
#
<<

!
||
=<
%

"

><
&

][
*
>>
=!

C++
C++ C++
.
* C++
-:
- )(Identifiers
" " .
-:
-A.
-B )(.
-C.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

* -: C++
-
"_"
- "_"
- .
:
B6 .a
X_ray .b
Matrix .c
Ok_ .d
A .e
Soft_fine .f
Door12 .g
_new .h

) (invalid :
7-up .
b6.1 )(.
! salim )!(
2 .
No#1 )(#

C++
SYSTEM , system, System :
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

-
C++
C++
:
near
Static
asm
Double
long
Sizeof
do
int
While
new
auto
else
For
This
Void
Delete
Goto
if
const
Entry
char
Class
Public
Case
Continue Extern
struct
inline
float
Private
Virtual Volatile
Frinde
enum
near
Static
cdecl
Default
inline Overload Unsigned Typedef
Signed
Pascal Operator Switch Template Union
Register Protected
far
Catch
char
Const
break
Return
-


.
C++
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Numeric Constants
C++ -:
.a integer
9 0 . " "+ ""- -:
0
15
1000
321
-61
:
: 3.31 .
: 1,000 .
: J72 .
: 2 4 .
: 1999 1992 1992 .
C++


-:
40000 , 19897 .long int
45 , 80 , -16 .short int
967 , 20000 .unsigned int



unsigned int
unsigned int

.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

-b Floating-point Constants
9 0 " "+ ""- "" -:
421.5
10.6
0.0
0
01
-68.0
-:
: 1000 .

: 4,000.21 .
: 2 83.4 .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Non-numeric
) ( ) (
-:
""first
""my name is
""30+50=80
""my,no=123.04
""Islam


:
.Non-numeric
/


.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


)( .

...

x y k c,b,a.
X y

;Float x, y

;Integer k

C,b,a

;Char c,b,a
;X=2.0
;Y=10.6*x
;K=100
;A=t
;B=f
;C=4




...

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

-:
-
.
-
.
-
FALSE =0 TRUE=1

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

C++
C++ :

-:

Arithmetic Operators
C++

h Basic ** C++
..
C++ Basic
8/3
2 0.666 / .
C++ -:

+
*
/
%
-++
-

PDF created with FinePrint pdfFactory trial version www.pdffactory.com








overflow
:
) (Main
{
;int n = 33000
;n = n * 3
}

n 99000
30464
8 3 8/3
2
8.0/3.0
2.667 C++ %
Modulus Operator :
7%3

1 7/3

7 mod 3 MOD Modulus C++
% .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Unary and Binary


Operators

binary ) ( 2*3
6 ) (-1992
Unary
.

Increment and Decrement


C++ + + - - 1
1 :
;A++
;++a

1 a -:
;A=a+1

1 a -:
;--a

;a--

;A=a-1

;++a ; a=a+1
.
a++ ++a
1 a ++a a
a++
a 1 a 1

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


a . a--
:
1 z

x

;Int x,y,x
;X=y=x=0
;X=++y + ++z

;X=y++ + z++
1 y

x

;X=++y + z++

;X=y-- +--z

x=1 , z=1 , y=1


x=2 , y=2 , z=2
x z,y
1
x=3 , y=3 , z=3
x y z
1
x=5 , y=2 , z=1
:
Int x,y,z
X=y=z=0
:
Int x=y=z=0

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Precedence of
Arithmetic Operations
C++
:

++ --
* / %
+ -
=
++ ) -- (


1
2
3
4
5
6

:


) (
) (-:
X+y/z*a

-:
- : y / z
- a * (y/z) :
- : 2 x :
X+ y/ z*z


.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Relational and Logical


Operations


) (true ) (false
:
x= =y : .
matrix > 100.0 : .
C++ ) (false ) (0
) (true ). (1
:

>
<
=>
=<
==
=!
-

) And (
) Or (
) Not( unary

&&
||
!
-

: ;int a=b=3
a < 3 false 0
a<=3 true 1

a>b false 0

a!=b false 0

a= = b true 1

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

-:


)X||y (x or y
)X && y (x and y
Y
X||y
X
Y
X&&y
F
F
F
F
F
T
T
F
T
F
F
T
T
F
F
T
T
T
T
T

x
F
F
T
T


)!x (not x
x
!y
F
T
T
F

!! -:
:
F T F&&T
F F&&T
F T&&F F T&&T
. T
:
F||F F
F F
F||T T T||T ...T

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Bowties Operators
C++
] [Bit Binary
*Digit
) ( ] ) [bit
(
int char ..
:

) (not
~
) (and )(
&
) (or )(
|

>>

<<
)) (xor(
^
-

<<>>

&

|
-

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)~(
) (0 ) (1
0 1 1 0 x
) 8 (-:
x

~x

~ ) (not 0 1
0 1
)( x ~x .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

&
:
;x&y
x
y
;X&Y

0+0 0 0+1 0 1+1 1


) T T&T F F&T FF&F (

|
X y ;x|y
x

y
;x|y

0 1 1 1 1 1 0 0 . 0
) T T|F T T|T T F|T (

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

;x^y

0 0 0 1 0 1 11
0 ^ ) (XOR )| (OR
(1) true
x y true (0) false
) (OR 1|1 .1

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

>> <<
Basic
.. Pascal .
) (
.
.
:
; x>>2 ) x 23 (
:
x

;x>>2

) ( :
5 .
; 23>>2 . 5
23 .
2 .
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
;Z = - 50<<2
Z

Z<<3

)( .
1
0 .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Other Operations
the conditional operator
c++
if-then-else
:
Expression1 ? Expression2: Expression3
:
)If (b<c
;A=b
Else
A=c

: ) (b<c a=b a=c



:

A=(b<c) ? b:c
A=b
A=c

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)&( )*( the & and * Pointer


Operator
Pointer


.
&
:
;X=&y

y x
x=y y x &
y y x y
y=10 )( 120 x
120 & y *
& * .
;S=*x

x ) ( y
s y 10
10 s ) (
120 x=*&y x=y

*
& AND
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

sizeof
) (unary ) (

sizeof sizeof:
;Int n
;)N=sizeof (n

n 2 n ) (int
2 4 :
;Float x
;)Z=sizeof (x

z 4 x.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

) (, The Comma Operator


) (binary
:
Experssion1, Experssion2
:
- ) ( .
- ) (
:
;)A=(b=2,b+1

b 2 ) (
b ) (
) (b+1 3 .
:
;B=8
;)A=(b=b-4,12/b

b 8 b
) (b-4 ) (8-4 4
) (12/b ) (12/4
3 . a

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


.
:
;Int a


Dim a as integer

a .
C++
char . .
int . .
float . .
double . .
void . .
c++


:
;Int a,b,x

c++
:
;Float m,y

(C++ compiler) c++ .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com



c++
:

""\b
""\n
""\t
""\0
""\r
""\v
"\\"
""/f


8
13
9
0
13
11
92
12

:
""first line\n second line
) (
:
First line
Second line


.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Comments c++
C++
:
10 rem this is Islam

:C++
// this is Islam

) (


.
: :
this is comment//
/*an example on comment in c++ language */
main()/* start your program*/
{
int x,y,z; //this line declares type of x,y,z
}

:
/ * .
- c++
.
-
identifier . keyword

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

visual c++6.0
Run visual c++6.0

++ :
visualc++6.0
:

file

File
New ..

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

..

ok

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

..

finish

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

..

Build Execute .exe ..


. F5

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


C c++
Visual C++ ! Hello World
)( printf .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


Input/output Techniques

introduction


.

) input (read ) (print
C++


. C++

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

input\output
C++ Iostream
cout cin iostream.h
Iostream.h:
: io input/output .
: Stream ..
header file : H .
:
100 :
Print 100
C++ :
#include stdafx.h
#include iostream.h
)( main
{
;cout<<100
;return0
}

100 cout <<


100
. c

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

( )
:
:
#include stdafx.h

#include iostream.h
main ()
{
cout<<smoking is dangerous \n;;
return0;
}

endl \ n
:\ n
#include stdafx.h
#include iostream.h
main ()
{
cout<< smoking is dangerous<<endl;
return0;
}

:
smoking is dangerous

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
\n : :
#include stdafx.h
#include iostream.h
)( main
{
;cout<<matrix
;cout<<matrix \n
;cout<<matrix \n\n
;cout<<matrix \n\n\n
;cout<<matrix
;return0
}

:
matrixmatrix
matrix

matrix


:
- matrix
\n
matrix
. \n
- ) (
\n\n
\n

. \n\n\n

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


:
: 446
#include stdafx.h
#include iostream.h
main ()
{
cout<<446;
return0;
}

:
446
:
:10.5 C++
#include stdafx.h
#include iostream.h
main ()
{
cout<<10.5;
return0;
}

:
10.5

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
: C++
#include stdafx.h
#include iostream.h
main ()
{
int a=100;
cout<<a;
return0;
}

:
100
:
:
#include stdafx.h
#include iostream.h
main ()
{
float x;
x=4.25
cout<<x;
return0;
}

:
4.25

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
a x
:
#include stdafx.h
#include iostream.h
)( main
{
;int a=100
float x=4.25
;cout<<a<<x
;return0
}

) ( :
4.25

100

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)(

:
:
#include stdafx.h
#include iostream.h
)( main
{
;int a=100
;cout<<a=<<a
;return0
}

:
A=100
:
:
#include stdafx.h
#include iostream.h
)( main
{
;int=100
float x=4.25
;cout<<a=<<a<<x=<<x
;return0
}

:
A=100 x=4.25

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:

:
#include stdafx.h
#include iostream.h
main ()
{
int a=100;
float x=4.25
cout<<a=<<a<<\n<<x=<<x;
return0;
}

:
A=100
X=4.25

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Streams C++
streams
. C
:
;Cin>>a

<< >> .

.
/
.
:
;Int x
;Cin>>x
:
;Cin>>x

:
:
#include stdafx.h
#include iostream.h
)( main
{
;int=a
;cin>>a
;cout<<a
;return0
}

a
10
:
10

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:

#include stdafx.h
#include iostream.h
)( main
{
;int a
;cin>>a
;cout<<a
;cout<<a/2
;cout<<a*2
;return0
}

:
;Int a
:
20
Cin>>a
:
;Cout<<a
:
;Cout<<a/2
:
;Cout<<a*2
:
20 10 40

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
7


7 ..

#include stdafx.h
#include iostream.h
)( main
{
;int a
;cin>>a
;cout<<a*1
;cout<<a*2
;cout<<a*3
;cout<<a*4
;cout<<a*5
;cout<<a*6
;cout<<a*7
;cout<<a*8
;cout<<a*9
;cout<<a*10
;return0
}

:
7 14 21 28 35 42 49 56 63 70
:
40 25 30

40 25
30

#include stdafx.h
#include iostream.h
)( main
{
;int a,b,c
;cin>>a>>b>>c
;cout<<(a+b+c)/3
;return0
}


..
:
31

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
:
#include stdafx.h
10
#include iostream.h
main ()
{
6.3
int a;
float x;
double z;
2001.5
cin>>a>>x>>z; // input all data
cout<<endl;
cout<<\n print the following items;
cout<<endl;
cout<<a<<\n<<x<<\n<<z; // output all data
cout<<\n this is the end<<endl;
return0;
}


:
// input all data
// output all data
..
:
print the following items
10
6.3
2001.5
this is the end

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


ProgramControl,Conditional&Loop Statements

introduction
C++



:

.
C++
structured


:
GOTO
C++
.
true false C++

) ( C++
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


C++ :
- if statements
- switch statement
- ?

if statements
if statements :
;If (condition) statement1
)
( statement1 ) ( condition
true false
) ( statment2 else C++

:
;If (condition) statement1
;Else statment2
statement1 condition
C++ . if


If (condition) statement1

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
C++ x is positive
#include stdafx.h
#include iostream.h
)( main
{
;int x=5
)if (x>0
;cout<<x<< x is positive
;return0
}

x is positive )(x>0
:
x is positive
:
C++ Y :
x;x>=0
=|Y=|x
-x;x<0

10

#include stdafx.h
#include iostream.h
)( main
{
;int x
;cin>>x
;if (x>=0) cout<<x
;else cout<<-x
;return0
}

10 :
10

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
90
) (A 80 ) ( B
70 ) (C 60 )( D
50 ) ( E )(F


85

#include stdafx.h
#include iostream.h
)( main
{
;int mark
;char grade
;cin>>mark
)if (mark>=90
;'grade='a
else
)if (mark>=80
;'grade='b
else
)if (mark>=70
;'grade='c
else
)if (mark>=60
;'grade='d
else
)if (mark>=50
;'grade='e
else
;'grade='f
;cout<<grade
;return0
}

mark
grade .
85 :
B

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


:( if..else) ....
If (condition)
{
statmenet1;
}
else
{
statmenet1;
}
:
:( if..else) ( 3)
#include stdafx.h
0
#include iostream.h
main ()
{
int mark;
char grade;
cin>>mark;
if ( mark>=90){
grade='A';
{
else
if (mark>=80){
grade='B';
{
else
if (mark>=70){
grade='C';
{
else
if (mark>=60){
grade='D';
{
else
if (mark>=50){
grade='E';
}
cout<<grade;
return0;
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com




/
t1 t2

#include stdafx.h
#include iostream.h
)( main
{
;int t1,t2
;cin>>t1>>t2
)if (t1>t2
{
;cout<<t1
;cout<<(t1+t2)/2
}
else

{
;cout<<t2
;cout<<t2*2
;cout<<t2/2
}
;return0
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

switch statement
: c++ Switch

Switch (expression)
{
Case constant1: statments1; break;
Case constant2: statments2; break;
......................................................
......................................................
......................................................
Case constantn: statmentsn; break;
Default; last statements;
}



constant1



constant2



Switch

:
#include stdafx.h
#include iostream.h
void main()
{
int s1;
s1=2;
switch (s1)
{
case 2 :cout<<"y";
break;
case 3: cout<<"x";
break;
case 4: cout<<"m";
break;
default: cout<<"w";
}
}

:
y

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

?
...
:
;Variable=(condition)? Result:result2
: result1
condition ) (true result2
). (false
:
#include stdafx.h
#include iostream.h
)(void main
{
;int a,b
;a=5
;if (a>1) b=10
else
;b=20
;cout<<b
}

b 10 a>1 20
.
:
10

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Repetition and Loops


C++
:

For
for .

;For ( initial-value; condition; increment ) statement



For


For



For

For statement1

statement1 .. condition
For
:For
:

#include stdafx.h
#include iostream.h
)( main

{

;char x
)for (x=1;x<=10;x=x+1
;cout<<Islam
;cout<<endl

;return0
10
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Islam Islam Islam Islam Islam Islam Islam Islam Islam Islam
10 1 10 Islam
:
10 1
#include stdafx.h
#include iostream.h
main ()
{
int a;
for (a=1;a<=10;++a)
cout<<a<<endl;
return0;
}

:
1
2
3
4
5
6
7
8
9
10

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
15 1
#include stdafx.h
#include iostream.h
main ()
{
int a;
for (a=1;a<=15;a=a+2)
cout<<a<<endl;
return0;
}

1 2
15 1
:
1
3
5
7
9
11
13
15

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


For ( initial-value; condition; increment )
{

statement;
}
:
:
#include stdafx.h
#include iostream.h
main ()
{ int x,y,z;
y=-4;
for(x=1;x>y;x=x-2)
}
z=x;
cout<<x<<endl;
{
return0;
}


Block

:
1
-1
-3

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
#include stdafx.h
#include iostream.h
main ()

{ int a,b,c,x;
a=6;
b=1;
c=3;
for (x=11;(a*c)>(x*b);x--)
{
x=x+3;

c=c-2;
Block
cout<<x<<*;
}
return0;
}

14*
:
#include stdafx.h
#include iostream.h
main ()
{ int a,c;
a=1;
c=3;
for (a=c;c;)
{
c--;
cout<<c<<endl;
}
return0;
}


1 C

:
2
1
0

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Nested (Multiple) for


Loops
:
)For (...
)For (...
)For (...
.
.
;Statements
:
)For(...
)For (...
;Statements


outer
loop


inner
loop

) (
...

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
)For (...
)For (...
)For (...
;Statement

4
I
12

#include stdafx.h
#include iostream.h
)( main
{
;int i,j
)for (i=1;i<=3;++i
)for (j=1;j<=4;++j
;cout<<i*j
;return0
}

I
<< cout I 3
12 12 .
:
1234246836912

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:

2 20
2


30 3 3-

#include stdafx.h
#include iostream.h
)( main
{
;int i,j
)for (i=2;i<=20;i+=2
)for (j=30;j>=3;j-=3
;cout<<i<<j<<endl
;return0
}


.
1612 :
203 :

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

While & Do
while for
C++
:
;While (conditions) statements



conditions

while :
) ( ) (true
) (statements
. while
:
:
#include stdafx.h
#include iostream.h
)( main
;{ int a
;a=1

)while (a<3
;cout<<a++
;return0
}
1

:
12

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
#include stdafx.h
#include iostream.h
main ()
{ int a;
a=1;
do
{
cout<<*;
a++;
}
while (a<=4)
return0;
}


1
a
4

****

:
#include stdafx.h
#include iostream.h
main ()
{ int x,y;
x=2;
y=-2;
do
{
cout<<x<<-<<endl;
x--;
y++;
}
else
cout<<finish;
while(x!=y)
return0;
..
}

y x

y x

2--21--1

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
+
*
/


10

#include stdafx.h
#include iostream.h
)( main
;{ int a
;cout<<please enter a number
;cin>>a
)while (a>=0
{
;cout<<*<<endl
;cout<< please enter a number
;cin>>a
}

;return0

}

10 :
*
10

{} Block
) ( .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

While Nested While Loops


While for
:
:
#include stdafx.h
#include iostream.h
)( main
{
3
;int i=j=1

)while (i<=3
{
)while(j<=4
{

;cout<</n<<i*j
12
;++j
}

;++i
I 1
;j=1
}
j
;return0
j
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Break
Break

Break
:
:



i=10

#include stdafx.h
#include iostream.h
)( main
{
;int i
)for (i=1;i<100;++i
{
;cout<<i
;if (i= =10) break
}
;return0
}

10
:
12345678910

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
#include stdafx.h
#include iostream.h
main ()
{
int number;
for (number=1; number<=100;++ number)
{
if (number%2) // for odd values
break;
else if (number%4)
break;
else if (number%6)
break;
else
cout<< number<<endl;
}
return0;
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

continue
continue
:
:

#include stdafx.h
#include iostream.h
)( main
{
;int x,n
do
{
;cin>>x>>n
;if (n<1) continue
;cout<<x
;--n
}
;)while (n<1
;return0
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
6 4 2 100 1
:
#include stdafx.h
#include iostream.h
main ()
{
int number;
for (number=1;number<=100;++number)
{
if (number%2)
continue;
else if (number%4)
continue ;
else if (number%6)
continue;
else
cout<<number<<endl;
}
return0;
}

:
12
24
36
48
60
72
84
96

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)(exit
) ( end
) exit(0
) exit(1
operating
. system
:
:

#include stdafx.h
#include iostream.h
)( main
{
;char ma
;cin>>ma
;)if ( ma != A) exit(1
;cout<<\n<<ma
;return0
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

goto

goto
structured :
goto
.
:
#include stdafx.h
#include iostream.h
)( main
{
;int x
;cin>>x
;if (x<10) goto negative
;negative: cout<<value is under 10<<endl
;return0
}

10
. value is under 10
10
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


Arrays and Matrices

introduction




.... x2,x1- x100

:


...

subscripted variable
] [ subscript
index




): (:
]A[n] ...a[2], a[1], a[0
] [a
address
....
] [ c++

50 .
:

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


....
) ( one-dimensional Array
) (vector
][a1 a2 ...a3

A1
A2
:
:
:
a3
c++ :
;]Type-specifier array-name[size

double -int
char

][ a
] a[0 ] ... a[1 :
;]Int a[20
a 20 .
:
;]Char name[15
name
.
...

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
) (

#include stdafx.h
#include iostream.h
)( main
{
;]int a[20
;int I
)for (I=0;I<20;++I
;a[I]=I+1
;return0
}

a
A[0]=1 I=0
A[1]=2 I=1

a[19]=20 I=19

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
:
#include stdafx.h
#include iostream.h
main ()
{
int x[5], y[5];
int I;
for (I=0;I<5;++I)
{
x[I]=I;
y[I]=I*I;
cout<<endl<<x[I]<<y[I];
}
return0;
}

:
0
1
2
3
4

0
1
4
9
16

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Array
Initialization
][grade
}Int grade[5]={80,90,54,50,95
][name
Char name[4]=nor
][ name
.
:

#include stdafx.h
#include iostream.h
)( main
{
}int a[6]={40,60,50,70,80,90
;int I
)for(I=0;I<6;I++
;cout<<a[I]<<endl
;return0
}

:
40
60
50
70
80
90

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
5
:
87,67,81,90,55
#include stdafx.h
#include iostream.h
int m,i;
main ()
{
int a[5]={87,67,81,90,55}
int s=0;

for(i=0;i<5;i++)

s=s+m[a];
float avg=s/5;
cout<<avg<<endl;<<s<<endl;
return0;
}

:
87
735
87
735

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Addressing
Array Elements in Memory



.
;]Int x[5
x
) (

]X[0
]X[1
]X[2
]X[3
]X[4
100
101
102
103
104

] x[0 100
104 103 102 101

.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

Two-Dimensional Arrays

) (index2
:
;]Type-specifier arraey_name [index 1][index 2

:
;]Int x[2][3
int = 2
=3
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
5 3:
#include stdafx.h
#include iostream.h
)( main
{
;]int m[5][3
;int I,j
)for(I=0;I<5;I++
)for(j=0;j>3;j++
;]cin>>m[I][j
;return0
}



] x[0,0 100 :
102-101-100 105-104-103
.

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


Functions
Introduction
) ( :


c++ .
:
-
.
-
.
- .
- .
- .
c++ :
:
- .
- .
- .
- .
- .
- .
:
:
- .
)
(
.
:

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

;Type-specified function-name (formal parameters


{
function body
}

)(
.
:
#include stdafx.h
#include iostream.h
)(max1
{
;cout<<hello
}
)(void main
{
;)( max1
;)(max1
;)(max1(); max1
}

:
hello hello hello hello

main
main
)( max1 )( max1
. main

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


:
main :
#include stdafx.h
#include iostream.h
;int x,y
)(void max
{
;x=x+y
}
)(void fax
{
;)(max
;)(max
{
)(void main
{
;y=10; x=0
;)(max
;)(fax
;cout<<x<<y
}

:
:
main
fax
max
main
x y
int
main )( max
max x x+y x=0+10
x=10 max
)( fax )( fax max
max :
X=10+10 x=20 fax
)( max max :
X=20+10 x=30
main :
30 10

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
#include stdafx.h
#include iostream.h
void x1()
{
cout<<*;
}
void x2()
{
cout<<\t;
}
void yaya()
{
x1();
x2();
x1();
}
void kiki()
{
cout<<\n:
}
void main()
{
int I;
for(I=0;I<=3;I++)
{
yaya();
kiki();
}
}

:
*
*
*
*

*
*
*
*

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:


#include stdafx.h
#include iostream.h
;int x
)(check

{
)if ((x%2)==0
;cout<<even<<endl
else
;cout<<odd<<endl

}
)(main
{
;cin>>x
;)( check
9
;return0
}

..9 :
ood
9 .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:

max


1020

#include stdafx.h
#include iostream.h
;int x,y
)(max
{
)if (x>y
;cout<<x<<endl
else
;cout<<y<<endl
}
)(main
{
;cin>>x>>y
;)( max
{
;return0
}

:
20
20 10 .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com


disk Files

Introduction
Buffered filw
system C++
unbuffered UNIX-like
I/O
binary system C
ANISI-C Committee

text
binary system . ) (version C
.

stdio.h :

)(fopen
)(fclose
)(putc
)(getc
)(fseek
)(fprintf
)(fscanf
)(feof
)(ferror
)(rewind



) ( ) ( char
) ( char
) (
printf
scanf
true
true

-

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)( fopen
:
#include stdio.h
)(main

{
f
;FILE *f
;)F=fopen(C:\\matrix.txt,w
;)Fclose (f
w
;return0

}
) (
C


FILE * f
disk ) (
f.

system
matrix.txt \\
C \ w
) fclose (f
.

C ) (

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

ANSI:

""r
"" w
"" a
""rb
" "wb
""ab
""r+
""w+
""a+
""r+b
""w+b
""a+b

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)( fprintf
:
" " welcome to c++
) (1 matrix

#include stdio.h
)(main
{
;FILE *f
;)f=fopen(C:\\matrix.txt,w
;)fprintf (f,welcome to c++

;return 0
}

) (fprintf
-
c welcome to c++ .

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)( fclose
)( fopen )( fopen


.

:
#include stdio.h
)(main
{
;FILE *f
;)f=fopen(C:\\matrix.txt,w
;)fprintf (f,welcome to c++
)fclose (f

;return 0
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

)(putw() getw
c++

ANSI
)( getc )( putc
.
:
) ( 1000
: f
;)Putw (1000,f

:
x
: f
;)Putw (x,f

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

:
: f
#include stdio.h
main()
{
FILE *f;
int x;
x=getw (f);
printf (%d,x);
return 0;
}

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

PDF created with FinePrint pdfFactory trial version www.pdffactory.com

You might also like