Vous êtes sur la page 1sur 47

,

-3 ,

: . //


.
.
www.emust.edu.mn
C++
.

,
.
.

1. //hello.cpp
2. #include <iostream.h>
3.
4. #define greeting = ?;
5.
6. void main()
7. {
8.
9. cout << greeting;
10.
11. }
C++
Compile Run
.

. .
.
.
2- iostream.h .

. greeting
.
C++

void main()

.
C++- ( {} ) .
, ,
.
.
.
.

cout << greeting;

cout iostream.h .
C++
Compile Run .
, .
,
.
Run .
(*.OBJ )
, .

( *.EXE ) .
.
C++


.
( ) . ..

.
. 2
.
, .
. 2
.

. C++
(char, int, float .) (
) . , ,
. .
: 2 ,
.

RELAT
.

//relat.cpp
#include <iostream.h>

void main()
{
int numb;

cout << : ;
cin >> numb;

cout << numb<10 << (numb<10) << endl;


cout << numb>10 << (numb>10) << endl;
cout << numb==10 << (numb==10) << endl;
}

cin cout
. ..
.
10 3
. 20
.

: 20
numb<10 0
numb>10 1
numb=10 0

numb 10 . numb 10 2
, numb 10- 3 .
C++ 1-, 0
. , 2 Boolean
C++ .
.

C++- :


>
<
==
!=
>=
<=

.
2 Harry, Jane
.
Jane=44
Harry=12
(jane=harry)
(harry<=12)
(jane>harry)
(Jane>=44)
(harry!=12)
(7<harry)
(0)
(44)

2 (=) . (=) .
C++
.
.
C++ 0- 0-
.
. 2
.
.


.
.
. C++ for, while, do 3
.

FOR

C++- ,
. for
BASIC
.
. .
.

0- 14 FORDEMO
.

//Fordemo.cpp
#include <iostream.h>

void main()
{
int j;
for (j=0; j<=15; ++j)
cout << j*j << ;
}

0 1 4 9 16 25 36 49 64 81 100 121 144 169 196


? For for 3
. , ,
. ( )
.
j .
. cout << j*j <<
. j-
. j 0, 1, 2, 3

.
For ( ; ) .
.

.

For .


. j = 0 .

.
.

.
.


.
j- 1- . ++j
. j-
. . --j
j- .

j=j+1 j=j-1

.
. += -=
. .
j+=3
j-=5

j- 3- ( 5- ) .
.

j=j+3
j=j-5

?

15 . j-
0 j 14 .
. j = 15 .
. 0- ,
,
.


. ( { } )
.
.
Cubelist
. 1- 10
.

//cubelist.cpp
#include <iostream.h>
#include <iomanip.h>

void main()
{
int numb;
for (numb=1; numb<=10; numb++)
{
cout << setw(4) << numb;
int cube=numb*numb*numb;
cout << setw(6) << cube << endl;
}
}

.

//cubelist.cpp
#include <iostream.h> 1 1
#include <iomanip.h> 2 8
3 27
void main() 4 64
{ 5 125
6 216
int numb;
7 343
for (numb=1; numb<=10; numb++)
8 512
{ 9 729
cout << setw(4) << numb; 10 1000
int cube=numb*numb*numb;
cout << setw(6) << cube << endl;
}
}


.
. int cube
.
int cube=numb*numb*numb;
.

cube=10;
cube
.
.
.


..
. For 1 , cubelist
.
.
.
.
for (numb=1; numb<=10; numb++) {
cout << setw(4) << numb;
int cube=numb*numb*numb;
cout << setw(6) << cube << endl; }
,
, .

C++ -

C++ -
. F8
( ).
(
).
. .. F8 .
.


. Debug Watch Add
Watch .
.
numb . Watch ,
.
.
.
Watch
.
.

FOR

,
. . Factor
(

). : 5- 5*4*3*2*1=120 .

//factor.cpp

void main()
{
unsigned int numb;
unsigned long fact=1;

cout << : ;
cin >> numb;

for (int j=numb; j>0; j--)


fact*=j;

cout << << fact;


}

numb, 1
(numb > 0), 1- .
unsigned long
. :

: 10
3628800

FOR

. j
.

for (int j=numb; j>0; j--)

C++- .
.

.

For
. ( , ) .
. .

For (j=0, alpha=100; j<50; j++, beta--)

j alpha
, beta . j alpha,
beta .

WHILE

for .
?
EndOn0 0
.
.

//EndOn0.cpp
#include <iostream.h>

void main()
{
int n=99;
while (n!=0) cin>>n;
}
.
.
1
27
33
144
9
0

While for .

.
. 0
n!=0 .
:

.
cin >> n .

While

While4 while
. Cubelist Cubelist
4 .
4 4
9999- .
4 .
.

//while4.cpp
#include <iostream.h>
#include <iomanip.h>

void main()
{
int pow=1;
int numb=1;
while (pow<9999)
{
cout << setw(2) << numb;
cout << setw(5) << pow << endl;
++numb;
pow=numb*numb*numb*numb;
}
}

Pow Numb- 4 numb-
, pow . :

1 1
2 16
3 81
4 256
5 625
6 1296
7 2401
8 4096
9 6561
10000
.

.
.

1 1 2 3 5 8 13 21 34 55 ..


.

. ,

.

//fibo.cpp
#include <iostream.h>
.
void main()
{ 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
const unsigned long limit=4294967295; 1597 2584 4181 6765 10946 17711 28657
unsigned long next=0; 46368 75025 121393 196418 317811 514229
unsigned long last=0; 832040 1346269 2178309 3524578 5702887
while (next<limit/2) 9227465 14930352 24157817 39088169
{ 63245986 102334155 165580141 267914296
cout << last << ; 433494437 701408733 1134903170
long sum=next+last; 1836311903 2971295073
next=last;
last=sum;
}
}

unsigned long
. while
.
Limit .
.
(next<limit/2)

next- limit/2- .

(next<(limit/2))

.
? .
.
.

DO

while .
.

. do
.
Divdo
, .

//divdo.cpp
#include <iostream.h>
void main()
{
long dividend, divisor;
char ch;
do
{
cout << : ; cin >> dividend;
cout << : ; cin >> divisor;
cout << << dividend/divisor;
cout << , << dividend%divisor;
cout << \n ? (Y/N): ;
cin >> ch;
}
while (ch!=n); }

do . Do
, {} .
while . while while

( ; ) .

n- .
.
: 11
: 3
3, 2
? (Y/N) y
: 222
: 17
13, 1
? (Y/N) n

?

. For

.
do
while .

.

1. ,
2. , 2- , -
3. U.CS101
4. he2must.blogspot.com

Vous aimerez peut-être aussi

  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 13
    Lecture 13
    Document46 pages
    Lecture 13
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lec1-1 Corporate Activity Shine
    Lec1-1 Corporate Activity Shine
    Document61 pages
    Lec1-1 Corporate Activity Shine
    Amarsaikhan Tuvshinbayar
    0% (1)
  • Lecture 9
    Lecture 9
    Document46 pages
    Lecture 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 11
    Lecture 11
    Document24 pages
    Lecture 11
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 15
    Lecture 15
    Document40 pages
    Lecture 15
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 12
    Lecture 12
    Document34 pages
    Lecture 12
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 4 2018
    Lecture 4 2018
    Document47 pages
    Lecture 4 2018
    Amarsaikhan Tuvshinbayar
    100% (4)
  • Lecture - 10
    Lecture - 10
    Document35 pages
    Lecture - 10
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 5 2018
    Lecture 5 2018
    Document54 pages
    Lecture 5 2018
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 3 2018
    Lecture 3 2018
    Document42 pages
    Lecture 3 2018
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 6 2018
    Lecture 6 2018
    Document50 pages
    Lecture 6 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 8 2018
    Lecture 8 2018
    Document54 pages
    Lecture 8 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 7 20108
    Lecture 7 20108
    Document47 pages
    Lecture 7 20108
    Amarsaikhan Tuvshinbayar
    75% (4)
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • IT101 Lecture 4
    IT101 Lecture 4
    Document72 pages
    IT101 Lecture 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 2 2018
    Lecture 2 2018
    Document33 pages
    Lecture 2 2018
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 2
    Lecture 2
    Document80 pages
    Lecture 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 7
    Programming Lec 7
    Document57 pages
    Programming Lec 7
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 1 2018
    Lecture 1 2018
    Document39 pages
    Lecture 1 2018
    Amarsaikhan Tuvshinbayar
    100% (6)
  • Koosen 2018 Algorithm Programming 3
    Koosen 2018 Algorithm Programming 3
    Document46 pages
    Koosen 2018 Algorithm Programming 3
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 8
    Programming Lec 8
    Document32 pages
    Programming Lec 8
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • PL - 3
    PL - 3
    Document47 pages
    PL - 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 1
    Lecture 1
    Document54 pages
    Lecture 1
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Programming Lec 9
    Programming Lec 9
    Document39 pages
    Programming Lec 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 4
    Programming Lec 4
    Document39 pages
    Programming Lec 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm 2
    Koosen 2018 Algorithm 2
    Document48 pages
    Koosen 2018 Algorithm 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation