C++ , . main. .
:
1.
# include < >
2. :
Void main()
{ }
main
-
-
-
-
3. .
, , . , , , , .. #. . :
1) #include
#include . :
#include " "
#include < >
, . , , . , . #include , , .
2) #define
#define , , . , , . , , , .
#define
#define
. . , . .
:
#define WIDTH 80
#define LENGTH (WIDTH+10)
WIDTH 80, LENGTH (80+10) .
3) #undef
#undef #define.
|
|
#undef
#define . #undef , #define.
:
#undef WIDTH
#undef MAX
3.2. / C++
3.2.1. / C++
/ C++ C++, iostream.h.
<< >>.
istream C++ - cin, , . :
Int a;
Float b;
cin >> a >> b;
a b , cin.
(, , ), cin :
Enter
Enter 5.78 Enter
Enter. , , , , .
ostream - cout, , .
:
cout << a << b;
a b , . .
endl:
cout << a <<" "<< b << endl;
a b , , , : 34 5.78.
cin getline(Str,Count), Str (Count−1):
char str1[128];
Cin.getline(str1,9);
cout << str1 << endl;
abcdefghj, str1 8 , abcdefgh.
3.2.2. /
scanf() - . (). , , , .
scanf() stdio.h:
|
|
scanf('' , );
.
: , . %.
! scanf(), , . , &().
:
scanf("%d",&x);
scanf("%s", &str);
scanf("%d%*c%d",&i,&j);
scanf() ( %s), !! .. " !" scanf().
. . .
, . * % ( ) , .
, . : scanf("%5s",str);
printf() . , , . . % .
, % ( ). : %.n< >. n - , < > - .
, x=10.3563 float 3- , :
printf(" x = %.3f",x);
, l h.
. , .
, :
printf("%5d",20);
:
, 20 . , 0.
:
printf("%05d",20);
:
(. 1)
if
if :
If () 1;
[else 2;]
, . , , . else .
( , 0), , if; , else. , if, else, .
. (,)
2+2<=1 (<=0 <=0 +>=-2)
#include <windows.h>
#include <stdio.h>
Int main(void)
{
|
|
Float x,y;
printf(" a: ");
scanf("%f%f", &x,&y);
if(x*x+y*y<=1 || x<=0 && y<=0 && x+y>=-2) printf("");
else printf(" ");
Sleep(20000);
}
!
(==) (=), ,
if(a=-l)b=0;
, , / . b .
. , 0<<1. , 0<, (true false, int) 1. : if(0<x && <1)
if. if if, if else., if : else if. :
If (x)
if (y) printf(1);
else printf(2);
if else?
: else if ( ), else. else if(y). , else if(x), , :
if (x) {
if (y) printf(1);
}
else printf(2);
if-else-if. if-else-if. :
If()
;
Else if()
;
Else if()
;
.
.
.
[else
;]
. , , , . , , else. else , , , , , else. else , .
#include <stdio.h>
/* 4 */
Int main(void)
{
int magic = 123; /* */
Int guess;
printf( : );
scanf(%d, &guess);
if(guess == magic) {
printf();
printf(%d ,magic);
}
else if(guess > magic) {
printf(. );
else printf(. );
Return 0;
}
switch
switch . :
Switch ()
{ []
:
[ case -1]: [ -1]
[ case -2]: [ -2]
:
:
[ default: [ ]]
}
, switch , , , . .
|
|
. switch , case -.
, . .
switch . , case, , , default.
, . switch .
, switch , case, .
switch :
- ;
- , case;
- , , case;
- , , default, switch .
switch: default switch. case default switch , switch. , , , - switch. , case, . break.
:
int i=2;
Switch (i)
{
case 1: i += 2;
case 2: i *= 3;
case 0: i /= 2;
case 4: i -= 5;
default:;
}
switch , case 2. , i , 6, , case 0, case 4, i 3, -2. , default, .
, if, switch.
char ZNAC;
int x,y,z;
switch (ZNAC)
{
case '+': x = y + z; break;
case '-': x = y - z; break;
case '*': x = y * z; break;
case '/': x = u / z; break;
default:;
}
break switch, , switch.
, switch switch, case .
:
:
switch (a)
{
case 1: b=c; break;
case 2:
switch (d)
{ case 0: f=s; break;
case 1: f=9; break;
case 2: f-=9; break;
}
case 3: b-=c; break;
:
}
for
for - . :
for ( 1; 2; 3) [< >];
1 , . 2 - , , . 3 , .
for:
1. 1.
2. 2.
3. 2 (), , 3 2, 2 (), , for.
, . , , .
|
|
:
Int main()
{ int i,b;
for (i=1; i<10; i++) b=i*i; return 0; }
1 9.
for , .
:
Int main()
{ int top, bot;
char string[100], temp;
for (top=0, bot=100; top < bot; top++, bot--)
{ temp=string[top];
string[bot]=temp;
}
}
, , top bot. , 1 3 , , .
for . , break.
:
For (;;)
{...
Break;
...
}
, for . .
:
for (i=0; t[i]<10; i++);
i t, 10.
while
while :
while () < >;
, , . while :
1. .
2. , while . , while.
3. 1.
for ( 1; 2; 3) [< >];
while :
-1;
While (-2)
{ < >
-3;}
for, while . while , .
for while , .
do while
do while , . :
do < > while ();
do while:
1. ( ).
2. .
3. , do while . , 1.
, , break.
while do while .
:
Int i,j,k;
...
i=0; j=0; k=0;
do { i++;
J--;
while (a[k] < i) k++;
}
while (i<30 && j<-30);
.
, :
- , , , ( );
- , , ;
- ;
- , , , , .
, .
do while , (, ).
for ( ).
while , , .
break
. switch. , . break , , .
break, , , .
break :
for(t=0; t<100; t++) {
count = 1;
for(;;) {
printf(%d , count);
count++;
if(count == 10) break;
}
}
continue
continue - break. continue , . , :
Do
{scanf(%d, &x);
if(x<0) continue;
printf(%d , x);
}
while(x!=100);
while do/while continue . for , , , .
continue, break, .
return
return , , , , . main . :
return [];
, , . , . , .
- return, . . , void.
, return , .
:
Int sum (int a, int b)
{ renurn (a+b); }
sum a b int, int, , . return .
:
void prov (int a, double b)
{ double c;
if (a<3) return; else if (b>10) return;
else { c=a+b;
if ((2*c-b)==11) return;
}
}
return .
goto
goto , .
:
Goto -;
...
-: ;
goto , -. , goto, , .. - . - - .
. goto, . , , .
exit;
:
Exit( );
- int. . .
exit if, , . exit stdlib.h