Академический Документы
Профессиональный Документы
Культура Документы
Lab Lang C 1
Lab Lang C 1
..
2008
621.394.343 (076)
32.88 7
33
, . . ,
. .
-
:
33 /. . . . : , 2008. 27 .
210406
21040665
, .
.
.
621.394.343 (076)
32.88 7
. . , , 2008
. , 2008
1
... 4
2
PRINTF() SCANF() .....11
3
... 17
4
. 23
5
. 29
6
... 33
7
. 39
45
. 1. LCC
File New File.
(. 2). Name
, .
OK (. 3).
. 2.
. 3.
, .. exe, Compiler Compile.
Ctrl+F5.
.
:
< > <_>;
,
5
int arg;
arg.
1.1.
int
( 32 )
short
( 16 )
char
8
float
32
double
64
,
.
, .
_.
:
arg
cnt
bottom_x
Arg
don_t
&arg
$cnt
bottom-x
2Arg
dont
arg Arg , ..
.
,
.
: , , . ,
int a, b;
a=4;
b=8;
, ,
:
6
int
c =
=
c =
c =
c;
a+b;
a-b;
a*b;
a/b;
//
//
//
//
,
,
c = 10+5;
c = 8*4;
float d;
d = 7/2;
15 32
, d
3, 3,5. , 7 ++
,
. 0,5 .
:
d = 7.0/2;
d = (float )7/2;
() d.
: 7
float, 2. ,
:
int a,b;
a = 7; b = 2;
d = a/b;
d 3,
d = (float )a/b;
3,5. , d
,
.
,
7
. ,
.
:
double n=2, SCALE = 1.2;
double arg = 25.0 + 60.0*n/SCALE;
,
, , .
.
( )
double arg = (25.0 + 60.0)*n/SCALE;
.
, ..
1 .
i++;
++i;
i--;
--i;
//
//
//
//
:
int i=10,j=10;
int a = i++;
int b = ++j;
// a = 10; i = 11;
// b = 11; j = 11;
,
,
. ,
.
a = 10, b = 11.
1. .
2.
( ).
3. .
8
1.
2.
1.
2.
1.
2.
1.
2.
1. (a + b) 3
2. (a + b) 2
1. a1b1 + a 2 b2 + ... + a10 b10
2. f ( x) = kx + b , x = 1,2,...,5
1. (a b) 2
2.
1.
2.
1. 10% 456
2. a1b1 + a 2 b2 + ... + a10 b10
10
1.
2. f ( x) = x 2 + b , x = 1,2,...,5
1. ,
, .
2. .
3. .
4. .
1. .
2. c float c=7/2 ?
9
3.
4.
5.
6.
.
?
i i=i+1?
?
2
PRINTF() SCANF()
:
printf() scanf().
, .
.
, :
#define, #elif, #else, #endif, #if, #ifdef, #ifndef, #include, #undef.
#define , ,
, .
:
#define <> <>
, ; .
#define.
1. #define.
#include <stdio.h>
#define TWO 2
10
:
X 2.
X 4.
X 9.
#undef ,
#define. , -
FOUR. :
#undef FOUR
. ,
#define
FOUR , ..
. FOUR
#undef,
#define FOUR.
,
#if, #ifdef, #ifndef, #elif, #else #endif.
.
#if defined(GRAPH)
#include <graphics.h> //
#elif defined(TEXT)
#include <conio.h>
//
#else
#include <io.h>
// -
#endif
11
.
GRAPH #define,
#include.
GRAPH , TEXT,
/. , -
, /.
#if defined #ifdef #ifndef
:
#ifdef GRAPH
#include <graphics.h>
//
#ifdef TEXT
#include <conio.h> //
#else
#include <io.h>
// -
#endif
,
,
SIZE.
#include
. ,
#include <stdio.h>
stdio.h
. ,
.
/ printf() scanf()
printf()
.
stdio.h :
12
*format ,
. , ,
. ,
.
printf()
:
printf( .);
:
.
,
%.
num
printf():
int num;
num = 5;
printf(%d, num);
num int. .
printf() .
, . %d.
,
, printf().
, %d 5, ..
5.
, %d
, int.
.
:
%
%d
%f ( )
%s ( )
%u
%% -
printf()
. :
13
int num_i;
float num_f;
num_i = 5;
num_f = 10.5;
printf(num_i = %d, num_f = %f, num_i, num_f);
:
num_i = 5, num_f = 10.5
printf()
, \n, \t . ,
, , printf()
:
printf(num_i = %d,\n num_f = %f, num_i, num_f);
.
scanf()
stdio.h, :
int scanf( const char *format [,argument]... );
, printf(), *format
printf(). ,
. scanf() 2.
2. scanf().
#include <stdio.h>
int main()
{
int age;
float weight;
printf( : );
scanf(%d, &age);
printf( : );
scanf(%f, &weigth);
printf( = %d, = %f, age, weigth);
return 0;
}
14
scanf() printf()
& ,
.
scanf() .
, .
scanf(),
:
scanf( %d,
%d , &n, &m);
scanf() , ,
, , .
scanf() .
, ,
- ,
0.
1.
.
2. printf() scanf()
.
3. .
a + b
1
#define
2
3
(aM + bN ) / MN
#define
2
x , x = 1,2,...,5
M 1, M 2,..., M 5
#define
kx + b , x = 1,2,...,5
M 1 + 2 M 2 + 3M 3 + 4 M 4 + 5M 5
15
printf() scanf()
( a b) 2
8
#define
3
x , x = 2,1,...,2
M N
10
#define
2
( x + y ) , x, y = 1,2,...,5
(M + N ) 2 / 2
,
,
,
,
1. , ,
.
2. .
3. .
4. .
1. printf()
.
2. scanf()
3. #define LENGTH
10
4. , .
5.
?
6. printf()
.
16
: if
switch.
if switch
.
,
. ,
, arg
.
if (),
.
if :
if ()
<>
,
, . ,
,
. . 2
if.
if(a < b)
if(a > b)
if(a == b)
if(a <= b)
if(a >= b)
if(a != b)
2.
, a
b
.
, a
b
.
, a
b
.
, a
b
.
, a
b
.
, a
b
.
17
if(a)
, a
, .
if.
.
3. .
#include <stdio.h>
int main()
{
float x;
printf( : );
scanf(%f,&x);
if(x < 0)
printf( %f .\n, x);
if(x >= 0)
printf( %f .\n, x);
return 0;
}
,
,
if ()
<1>
else
<2>
. ,
1, 2.
, -
, ,
..
if ()
{
< >
}
else
{
< >
}
, else
if,
:
18
if(1) <1>
else if(2) <2>
else <3>
x<0.
if .
:
&& -
|| -
!
. , exp1, exp2 exp3,
, . 3.
3.
if(exp1 > exp2 && exp2 < exp3 )
,
exp1
exp2 exp2
exp3.
if(exp1 <= exp2 || exp1 >= exp3 )
,
exp1
exp2
exp2
exp3.
if(exp1 && exp2 && !exp3)
, exp1
exp2
exp3.
if(!exp1 || !exp2 && exp3)
, exp1
exp2
exp3.
,
, .
, ..
. ,
.
if ,
.
.
if else if else.
switch .
:
19
switch()
{
case 1:
<>
case 2:
<>
default:
<>
}
, case.
, ,
default. switch . ,
1 ,
case.
2,
.
break
switch.
4
switch.
4. switch.
#include <stdio.h>
int main()
{
int x;
printf( : );
scanf(%d,&x);
switch(x)
{
case 1 : printf( 1\n);break;
case 2 : printf( 2\n); break;
default : printf( \n);
}
char ch;
printf( : );
scanf(%,&h);
switch(ch)
{
case a : printf( \n); break;
case b : printf( b\n); break;
default : printf( \n);
}
return 0;
}
20
switch. ,
. ,
case, .. x<0
.
1. if
.
2. switch
.
3. .
1
2
3
4
if
switch
-2 2
0 5
-6 -2
-5 -1
21
a f
A F
0 9
0 9
0 5
,
z.
0 5
,
-1
a f
,
Z
A F
,
9
10
1 / a a 0
x
0,
4,8,9 30
a, s, d, j e
1. , ,
.
2. .
3. .
4. .
1. if var.
2. switch?
3. ,
[10; 20).
4.
switch.
5. if?
6. .
7.
if?
22
:
while, for do while.
. ,
N
..
.
while
,
, .
:
while(<>)
{
< >
}
S = i S < N :
i =0
int N=20, i = 0;
long S = 0L;
while(S < N)
{
S=S+i;
i++;
}
while i < N.
i=0, N=20,
, i
1. , 20 i=20,
. ,
:
int N=20, i = 0;
long S = 0L;
while((S=S+i++) < N);
23
,
,
, , .
S=21.
, , scanf():
int num;
while(scanf(%d,&mun) == 1)
{
printf( %d\n,num);
}
,
, .
, while
.
break. , ,
0.
int num;
while(scanf(%d,&mun) == 1)
{
if(num == 0) break;
printf( %d\n,num);
}
break, ..
, printf()
while. ,
:
int num;
while(scanf(%d,&mun) == 1 && num != 0)
{
printf( %d\n,num);
}
, ,
if.
for
for while ,
for
. ,
. for :
24
for(<
>)
{
< >
}
>;<>;<
ASCII .
char ch;
for(ch = a; ch <= z; ch++)
printf( ASCII %c - %d.\n,ch,ch);
ch,
a. , ch
97 a.
. z 122,
[97; 122]. , ch
, ,
printf(). ,
:
for(char ch = 97; ch <= 122; ch++)
printf( ASCII %c - %d.\n,ch,ch);
, ch for.
-
.
,
,
.
.
int line_cnt = 1;
double debet;
for(debet = 100.0; debet < 150.0; debet = debet*1.1,
line_cnt++)
printf(%d. %.2f.\n,line_cnt, debet);
.
int exit = 1;
for(int num = 0;num < 100 && !exit; num += 1)
{
scanf(%d,&mov);
if(mov == 0) exit = 0;
25
for :
int i=0;
for(;i < 100;) i++;
int i=0;
for(;;;) {i++; if(i > 100) break;}
break for, ..
.
do while
, ,
, ,
.
. ,
,
, .
do while, .
.
const int secret_code = 13;
int code_ent;
do
{
printf( : );
scanf(%d,&code_ent);
}
while(code_ent != secret_code);
,
, , ..
.
, while
.
, while,
break.
26
,
. , ,
.
, , , ..
, .
N M
S = i * j :
i =0 j =0
long S = 0L;
int M = 10, N = 5;
for(int i = 0; i <= N;i++)
{
for(int j = 0;j <= M;j++)
S += i*j;
}
while.
1. while for
.
2. do while
.
3. .
2
3
while for
50
1/ i 2
i =1
for
f ( x) = kx + b ,
x = 1,2,...,100
while
50 30
i + j
1
1
i=
j=
for
S =
do while
,
0
, q
10 ,
i =1
i S < 50 5 ,
while
27
S =
i =1
, 0
,
0
50 10
1 /(i + j ) c
i =1 j =1
while
f ( x) = x + b ,
x = 10,9,...,10
for
2
10
10
1 / i 3 , i 0
i = 10
for
20
10
1 /(i + j )
i = 10 j = 0
i + j 0 c
10
for
f ( x) = 1 / x , x 0
x = 10,9,...,10
for
5
10
1. , ,
.
2. .
3. .
4. .
1.
2.
3.
4.
5.
6.
7.
while do while?
?
?
?
while?
?
.
28
:
.
. ,
.
,
. ,
:
< > < >[ ];
,
int array_int[100];
double array_d[25];
// 100
//
// 25
//
,
[].
, .
.
f ( x) = kx + b :
double k=0.5, b = 10.0;
double f[100];
for(int x=0;i < 100;i++)
{
f[i] = k*x+b;
printf(%.2f ,f[i]);
}
29
, ,
int powers[4] = {1, 2, 4, 6};
powers[0] 1, powers[1] 2,
.. ,
, . ,
:
int N=100;
float array_f[N];
//,
:
#include <stdio.h>
#define N 100
int main()
{
float array_f[N];
return 0;
}
,
. ,
.
#define SIZE
4
int data[SIZE]={512, 1024};
for(int i = 0;i < SIZE;i++)
printf(%d, \n,data[i]);
:
512, 1024, 0, 0
,
. ,
,
. , ,
++:
int data[] = {2, 16, 32, 64, 128, 256};
6 .
: ,
30
?
, ,
.
,
. ,
0, 1, ..
, ,
.
:
int array2D[100][20];
// 10020
, .. array2D[0][0]
, array2D[0][1] ,
..
:
long array2D[3][2] = {{1, 2}, {3, 4}, {5, 6}};
1.
.
2.
.
3. .
1
2
f ( x) = kx + b , x = 1,2,...,100
f ( x, y ) = x + y , 0 x 20 ,
0 y 10
5x4
f ( x) = a sin( x / 100) ,
x = 1,2,...,100
31
20
f ( x) = a cos( x / 50) ,
x = 1,2,...,100
10
f ( x) = x 2 + b , x = 1,2,...,10
20
f ( x) = 1 / x + b , x = 1,2,...,50
10
10
f ( x, y ) = 1 /( x + y ) , 0 x 30 ,
1 y 20
6x3
f ( x, y ) = ( x + y ) 2 , 0 x 5 ,
0 y 3
6x5
f ( x, y ) = 1 /(( x y ) 2 + 1) ,
0 x 5 , 0 y 10
3x3
f ( x, y ) = x y , 0 x 20 ,
0 y 10
44
1. , ,
.
2. .
3. .
4. .
1.
2.
3.
4.
5.
6.
?
1, 2 3.
?
?
?
?
32
6
: .
.
( char).
:
char str_1[100] = {,,,,,,\0};
char str_2[100] = ;
char str_3[] = ;
printf(%s\n%s\n%s\n,str_1,str_2,str_3);
. ,
.
, .
: ++ ?
, str_2 100 , str_3
100, .
,
.. \0 ++
.
.
char str1[10] = {,,,,,,\0};
char str2[10] = {,,,, \0,, };
char str3[10] = {,, \0,,,, };
printf(%s\n%s\n%s\n,str1,str2,str3);
:
33
\0
. , ( )
, \0
.
string.h
:
int strlen(char* str);
char* str ( ).
strlen().
5. strlen().
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = !;
int length = strlen(str);
printf( = %d.\n,length);
return 0;
}
11. ,
, ,
\0.
. ,
char str1[] = ;
char str2[] = ;
str1 = str2;
.
.
string.h
:
char* strcpy(char* dest, char* src);
34
src dest
dest. 6 strcpy().
6. strcpy().
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = !;
char dest[100];
strcpy(dest,src);
printf(%s\n,dest);
return 0;
}
.
,
. :
int strcmp(char* str1, char* str2);
string.h. ,
str1 str2 .
.
char str1[] = ;
char str2[] = ;
if(strcmp(str1,str2) == 0) printf( %s
%s\n,str1,str2);
else printf( %s %s\n,str1,str2);
,
.
scanf(),
:
char str[100];
scanf(%s,str);
, str
.
scanf() gets() stdio.h,
:
gest(str);
35
,
Enter, .. \n.
\n \0
.
printf()
puts() stdio.h,
.
.
#define DEF
char str[] = ;
puts(str);
puts(DEF);
puts(&str[4]);
:
sprintf() stdio.h.
printf() ,
, :
int age;
char name[100], str[100];
printf( : );
scanf(%s,name);
printf( : );
scanf(%d,&age);
sprintf(str, %s. %d ,name,age);
puts(str);
str
.
, sprintf()
,
..
1.
.
36
2
3
4
5
6
7
8
9
10
1-
2-
Hello World
The laboratory, work
6 :
The laboratory work 6
sprintf()
*)
*)
*)
.
*)
hello
*)
strcmp()
* ,
1. , ,
.
2. .
3. .
4. .
1. ?
2. strcpy()
?
3. .
37
4.
5.
6.
7.
?
strcmp()?
?
strlen()?
:
.
.
. ,
.
. ,
,
. :
<> < > ([ ]) { < > }
.
.
- .
, .
.
7. .
double square(double x)
{
x = x*x;
return x;
38
}
int main()
{
double arg = 5;
double sq1=square(arg);
double sq2=square(3);
return 0;
}
square,
double,
return.
,
return. ,
. ,
int square(int x)
{
x = x*x;
return x;
printf(%d,x);
}
printf() , ..
return square. return
, - .
void, .. , return
.
,
. ,
.
main(),
.
,
( void).
:
void show(int x,int y,int z) {}
,
.
.
:
void show(int x, y, z) {}
//
39
(50 ),
.
, 100 ,
.
: ,
.
,
.
.
8. .
#include <stdio.h>
double abs(double arg);
float abs(float arg);
int abs(int arg);
int main()
{
double a_d = -5.6;
float a_f = -3.2;
int a_i;
a_d = abs(a_d);
a_f = abs(a_f);
a_i = abs(-8);
return 0;
}
double abs(double arg)
{
if(arg < 0) arg = arg*(-1);
return arg;
}
float abs(float arg)
{
return (arg < 0) ? arg : arg;
}
int abs(int arg)
{
return (arg < 0) ? arg : arg;
}
abs
.
main()
abs.
.
.
40
,
, .. .
.
void some_func(int a = 1, int b = 2, int c = 3)
{
printf(a = %d, b = %d, c = %d\n,a,b,c);
}
,
some_func() :
int main(void)
{
show_func();
show_func(10);
show_func(10,20);
show_func(10,20,30);
return 0;
}
, :
a
a
a
a
= 1, b = 2, c = 3
= 10, b = 2, c = 3
= 10, b = 20, c = 3
= 10, b = 20, c = 30
,
.
, a, b c
.
:
.
:
void
void
void
void
my_func(int
my_func(int
my_func(int
my_func(int
a, int b = 1, int
a, int b, int c =
a=1, int b, int c
a, int b = 1, int
c = 1);//
1);
//
= 1); //
c);
//
.
.
.
.
41
9. .
#include <stdio.h>
void up_and_down(int );
int main(void)
{
up_and_down(1);
return 0;
}
void up_and_down(int n)
{
printf( %d\n,n);
if(n < 4) up_and_down(n+1);
printf( %d\n,n);
}
:
1
2
3
4
4
3
2
1
. main() up_and_down()
1. n 1
printf() .
n < 4, up_and_down() 1
n+1. .
, 4.
if printf(),
4.
, .
up_and_down() n=3,
printf(), 6
3. ,
, .. up_and_down()
main(),
.
42
1.
.
1-
2-
,
,
,
,
7
8
9
10
1. , ,
.
2. .
3. .
4. .
1. ,
.
43
2. , :
int abs(int x);
float abs(float x);
long abs(long x);
float a = abs(-6);?
3. .
4. .
5. ?
6. ,
.
1. , . ++.: . . 3- .
.: , 2003.
2. , . ++ .: . . 4- .- . [ .]:
, 2001.
3. , . /++ .: . .
.: BHV--, 2001.
4. , .
++. .: . . .: -, 2005.
5. , . ++.: .
2- . .: . , 2002.
44
. .
09. 09. 2008. 6084/16.
. . . 1,65.
60 .
,
432027, , . , 32.
, 432027, , . , 32.
45