Académique Documents
Professionnel Documents
Culture Documents
-4
: . //
.
.
www.emust.edu.mn
.
, ?
. , ;
, , ;
, ..
.
. C++- .
Ifelse . ( else -
.) 2 switch
.
. .
IF
IF . Ifdemo
.
//ifdemo.cpp
#include <iostream.h>
void main()
{
int x;
cout << : ;
cin >> x;
if (x>100) cout << 100- \n
}
If .
If While .
If , While -
.
.
: 2000
100-
100- .
IF
() .
//if2.cpp
#include <iostream.h>
void main() :
{
int x; : 12345
cout << : ; 12345 100-
cin >> x;
if (x>100)
{
cout << x << ;
cout << 100- \n
}
}
. .. , ,
, .
prime- .
.( 1
.)
//prime.cpp
#include <iostream.h>
#include <process.h>
void main()
{
unsigned long n, j;
cout << : ;
cin >> n;
for (j=2; j<=n/2; j++)
if (n%j==0)
{
cout << << j
<< - . << endl;
exit(0);
}
cout << \n; }
N , 2- N/2 .
j . j N
. 0-
.
. :
: 13
: 22229
: 22231
11- .
(
.)
for {} . If
.
Exit ()
. exit
.
.
. 0
. ( (*.bat)
errorlevel .)
IF ELSE
if
.
. ,
ifelse .
if () { } else { }
.
ifelse.cpp .
//ifelse.cpp
#include <iostream.h>
void main()
{
int x;
cout << : ;
cin >> x;
if (x>100)
cout << 100- \n;
else
cout << 100- \n;
}
,
.
GETCHE ()
while ifelse .
getche . Chcount
.
//chcount.cpp
#include <iostream.h>
#include <conio.h>
void main()
{
int chcount=0;
int wdcount=1;
char ch=a;
while (ch!=\r) {
ch=getche();
if (ch= )
wdcount++;
else
chcount++; }
cout << \n= << wdcount << endl
<< = << chcount << endl; }
CIN .
ENTER
. .. , ENTER
.
. getche() .
. ,
conio.h . getche
ch . ( getch()
getche()
.)
space ( ) wdcount
, chcount
. ..
. .
:
This is an example
=4
=15
ENTER getche() \r
.
//chcnt2.cpp
#include <iostream.h>
#include <conio.h>
void main() {
int chcount=0;
int wdcount=1;
char ch;
while ((ch=getche())!=\r)
{
if (ch= )
wdcount++;
else
chcount++;
}
cout << \n= << wdcount << endl
<< = << chcount << endl; }
. getche() ch while
.
. .. getche() a
ch=getche() a .
\r- .
.
x=y=z=0
Z 0 . 0 Y 0 ,
X 0 .
(ch =
getche() ) .
ch .
chcnt2 while
. .
ifelse
adifelse .
//adifelse.cpp
#include <iostream.h>
#include <conio.h>
void main()
{
char dir=a;
int x=10, y=10;
cout << Enter \n
while (dir!=\r)
{
cout << : << x <<, << y;
cout << \n (n, s, e, w):;
dir=getche();
if (dir==n)
y--;
else
if (dir==s)
y++;
else
if (dir==e)
x++;
else
if (dir==w)
x--;
}
}
. 10, 10
. ENTER .
,
. ,
. .
: 10,10
(n, s, e, w): n
: 10,9
(n, s, e, w): e
: 11,9
(n, s, e, w):
.
.
.
.
.
.
ifelse .
.
//badelse.cpp
#include <iostream.h>
void main()
{
int a, b, c;
cout << a, b, c :;
cin >> a >> b >> c;
if (a==b)
if (b==c)
cout << \n;
else
cout << a, b ;
}
a, b, c- . .
else a, b .
. else- if-
if- .
Ifelse- ,
. if else- if- else-
. .
if (a==b)
if (b==c)
cout << a, b, c ;
else
cout << b, c ;
else
cout << a, b ;
else
.
if (a==b)
{
if (b==c)
cout << a, b, c ;
}
else
cout << a, b ;
SWITCH
,
switch .
.
.
//platters.cpp
#include <iostream.h>
void main() {
int speed;
cout << \n33, 45, 78- : ;
cin >> speed;
switch(speed)
{
case 33:
cout << \n;
break;
case 45:
cout << \n;
break;
case 78:
cout << \n;
break; } }
Switch- . Case
. Switch
case- break
( ) .
:
case break .
( !) .
,
case .
break .
DEFAULT
//adswitch.cpp
#include <iostream.h>
#include <conio.h>
void main()
{
char dir=a;
int x=10, y=10;
while (dir!=\r)
{
cout << << x <<, << y;
cout << \n (n, s, e, w):;
dir=getche();
switch(dir)
{
case n : y--; break;
case s : y++; break;
case e : x++; break;
case w : x--; break;
case \r : cout \n; break;
default : cout \n;
break;
}
}
}
Adswitch switch Default
. case-
default
. Try again .
break .
Switch Ifelse
switch , ifelse ?
.
If (SteamPressure*Factor>56)
.....
else if (VoltageIn+VoltageOut<23000)
.....
else if (day==Thursday)
.....
ifelse .
switch-
. (a<3) case (a<3)
ifelse .
case (a+32) switch .
switch
.
Case-
3
, .
.
If (alpha<beta)
Min=alpha;
Else
Min=beta;
C++- .
(+, - ..) 2 .
Min=(Alpha<Beta) ? alpha:beta
? : .
, ( : )
.
.
. min .
.
Absvalue=(n<0) ? n:n;
8 ( * ) condi.cpp .
//condi.cpp
#include <iostream.h>
void main()
{
for (int j=0; j<80; j++)
{
char ch=(j%8) ? :* ;
cout << ch;
}
}
j- 8 , 8- 0
() , 0- () .
8- ,
. .
.
.
1. ,
2. , 2- , , ,
, ,
3. U.CS101
4. he2must.blogspot.com