.


:




:

































 

 

 

 


3 . , */




 

/*prog14.c

, */

 

#include <stdio.h>

 

main()

{

float x, s = 0; /* s , , ; x , */

 

int flag; /* flag , scanf */

 

flag = scanf ("%f", &x);

 

/* , ; , scanf EOF, stdio.h*/

 

while (flag!= EOF)

{

s = s + x;

flag = scanf ("%f", &x);

}

 

printf ( %f\n, s);

return 0;

}

 

mydata.txt, :

 

121.45 12.78 151.11 777.76

 

prog14.c , :

 

prog14.exe < mydata.txt

 

< , prog14.exe mydata.txt. , mydata.txt.

 

:

 

(8.1) , , ;

 

(8.2) , , ;

 

(8.3) , , ;

 

(8.4) , , ;

 

(8.5) , , ;

 

(8.6) , , ;

 

(8.7) , , ;

 

(8.8) n, 1, 2,..., n ( n! - n );

 

(8.9) , ;

 

(8.10) , , ;

 

(8.11) , , ;

 

(8.12) , , ;

 

9.

 

char (character - ). ASCII-, , ( ). %.

 

/*prog15.c*/

 

#include<stdio.h>

 

main()

{

char x = A, y = 97;

printf ( %d, %c \n %d, % \n, x, x, y, y);

return 0;

}

 

, ASCII- 65, ASCII- 97 .

, int char, , . .

 

sizeof (_)

 

, .

 

/*prog16.c*/

 

#include<stdio.h>

 

main()

{

int x = 97;

char y = 97;

printf ( int %d, %d char %d, %d , x, sizeof(x), y, sizeof(y));

return 0;

}

 

printf - x, sizeof(x), y, sizeof(y) .

 

, char. ( ), . - - %s. .

 

/*prog17.c*/

 

#include<stdio.h>

 

main()

{

char str[20] = I am a student;

str[6] = 0;

printf(%s\n, str);

return 0;

}

 

, str. char, str . : str[6] = 0 str 6 0. : str , char. ; 0, 19. I am a student 11 14 , , , 15 , . 6 0, , . prog16.c, , 6 : I am a.

 

. str , .. str str[0].

 

, *.

 

/*prog18.c*/

 

#include<stdio.h>

 

main()

{

char *str = I am a student;

str[7]=S;

printf(%s \n, str);

return 0;

}

 

I am a Student. ,

 

char str[20];

 

char *str;

 

. , , .

 

prog17.c prog18.c , . = , . :

scanf(%s, str);

 

/*prog19.c*/

 

/* ( ), */

 

#include <stdio.h>

#define N 1000

 

main()

{

char str[ N ];

scanf (%s, str);

printf(%s \n, str);

return 0;

}

 

str prog19 . , . , , define, .

 

/*prog20.c*/

 

/* ( ), */

 

#include<stdio.h>

#define N 1000

 

main()

{

char c, str[ N ]; /* str , , c */

 

unsigned int i; /* i */

 

scanf (%s, str);

 

i = 0;

c = str[ i ]; /* c*/

 

while (c!= 0)

{

i++;

c = str [ i ];

}

 

/* i , ; , , i , */

 

printf(%u \n, i);

return 0;

}

 

 

, :

 

<9.1> , , ( prog20.c), while for;

 

<9.2> ( ) ;

 

<9.3> ( ), ;

 

<9.4> ( ), , , :

: aabbc,

: a a b b c;

 

<9.5> ( ) , , :

: aabbc,

:

a

a

b

b

c;

 

<9.6> ( ) , , ;

 

<9.7> ( ) , , ;

 

<9.8> , , , ;

 

 

10

 

, . , . :

 

/*prog20.c*/

 

#include<stdio.h>

main(int argc, char* argv[])

{

printf( %d \n, argc);

if (argc > 1)

printf( - %s \n, argv[ 1 ]);

else

printf ( %s \n, argv[0]);

return 0;

}

 

main , . argc argv. argc int, . argc , . (Tab), , . , , argc .

 

printf( %d \n, argc);

 

argc. :

 

prog6.c aa bb cc

 

argc 4.

 

argv char - * []:

 

har* argv[];

 

* char, argv . , , (, ), , . ([]) (*) . , argv, char* - . , argv , .. , . . argv : argv[0] - , argv [1] .., .

 

printf( - %s \n, argv[1]);

 

- argv[1].

 

, . if (). , , arg > 1. , , , else , . , .

 

printf( %s

\n, argv[0]);

 

.

 

.

 

, , , ( ). .

 

 

. ; 0, 1, 2,.. m * n, m , n . , i- j- a [ i ] [ j ]; m * n, i = 0, 1,.., m - 1, j = 0, 1,, n - 1. i j a [ i ] [ j ].

:

 

/* matrix1

1000*1000; a [ 0 ] [ 0 ], */

 

# include<stdio.h>

# define M 1000

# define N 1000

 

main()

{

int a [ M ] [ N ]; //

int i = 0, j = 0;

scanf (%d,&(a [ i ] [ j ])); // [ i ] [ j ], i = j = 0

printf ( %d\n, a [ i ] [ j ]); // [ i ] [ j ], i = j = 0

return 0;

}

 

# define; i, j (, ).

 

. matrix1.

 

, , . , /* */; , //, .

 

, , . . for.

 

/* matrix2

, ; (, )*/

 

//

 

# include<stdio.h>

# define M 1000

# define N 1000

 

main()

{

//

int a [ M ] [ N ];

int m, n, i, j;

//

scanf ( %d %d, &m, &n);

//

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

for (j = 0; j < n; j++)

scanf ( %d, &(a [ i ] [ j ]));

//

for (j = 0; i < n; i++)

for (i = 0; j < m; i++)

printf( %d , a [ i ] [ j ]);

return 0;

}

 

matrix1, , 1000*1000. , m n, , , 1000. , . , m*n.

for (i = 0; i < m; i++) i 0, i < m. , , for. i 1 ( i++, ), i < m .. i < m , , for, , .

for!!!

, for, . ; , for (i = 0; i < m; i++) . , . .

 

, , , , 2-4 .

 

. matrix2

 

, , . , , \n

 

for (j = 0; j < m; j++)

{

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

printf (%d , a [ i ] [ j ]);

printf ( \n );

}

 

( %d printf , ).

 

. .

 

, for . : . {}.

 

, , . , , . mydata.txt,

 

3 4

1 2 3 4

4 3 1 2

17 18 19 20

 

, . matrix2; matrix2.exe. ,

 

matrix2.exe < mydata.txt

 

< , matrix2.exe mydata.txt

 

. , .

 

 

 

. , - , , .

 

: m * n; .

 

matrix1, matrix2 . matrix3

 

/* matrix3

, ;

*/

 

//

# include<stdio.h>

# define M 1000

# define N 1000

 

main()

{

//

int a [ M ] [ N ];

int m, n, i, j;

 

int buf;

//

 

scanf(%d % d,&m, &n);

// ,

 

//

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

for (j = 0; j < n; j++)

scanf (%d,& (a [ i ] [ j ]));

 

//

for (j = 0; j < n; j++)

 

{ buf = a [ 0 ] [ j ];

// buf

 

// j

a [ 0 ] [ j ] = a [ m ] [ j ];

a [ m ] [ j ] = buf;

}

 

// -

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

for (j = 0; j < n; j++)

printf (%d , a [ i ] [ j ]);

return 0;

}

 

. matrix3, .

 

-. a [ i ] [ j ] a [ ] [ j ], i 0, n - 1, i 1, 1 , I .

 

 

for (j = 0; j < n; j++)

for (i = 0; = n - 1; i < ; i++, --)

{ buf = a [ i ] [ j ];

a [ i ] [ j ] = a [ ] [ j ];

a [ ] [ j ] = buf;

}

 

, , , ; , for i 0, - n - 1. i 1, 1 ( --, ).

 

 

 

. , , 1 . .

 

. , a b, , a b.

 

( ) perfnum: - - perfect number.

 

. , , 6 : 6 = 1 + 2+ 3. , :

 

 

a=2 b=10

 

perfnum.txt

 

6.

 

 

1

 

/*perfnum1.c*/

/* , */

 

#include <stdio.h>

 

main()

{

int a,b;

scanf (" a=%d b=%d", &a, &b);

printf (" a=%d b=%d\n");

return 0;

}

 

. :

 

perfnum.txt > perfnum.exe

 

2

 

/*perfnum2.c*/

/* a,b, a b*/

 

#include <stdio.h>

 

main()

{

int a,b;

int j;

 

scanf (" a=%d b=%d", &a, &b);

printf ("divisors of a: \n")

for (j = 1; j < a; j++)

if (a % j == 0)

printf (" %d ", j);

printf (" /n divisors of b: \n")

for (j = 1; j < b; j++)

if (b % j == 0)

printf ("%d ", j);

return 0;

}

 

a 1, 2,..., a-1. a j , j a, . b.

 

 

3

 

/*perfnum3.c*/

/* a,b, , a b*/

 

#include <stdio.h>

 

 

main()

{

int a, b;

int i, j, s;

 

scanf (" a=%d b=%d", &a, &b);

 

for (i = a; i <= b; i++)

{ // , a b

 

s = 0; // s

for (j = 1; j < i; j++)

if (i % j == 0) s+=j;

/* s i */

if (i == s)

printf ("%d ", i);

}

return 0;

}

 

, a b. . , , - , , -, , .. - . . , a=1, b , n. , , n, 1+2+...+n-1 : 1 n k- k-1 , k;1+2+...+n-1 = n * (n 1) / 2, , n * n .

 

, . '' - , .





:


: 2016-12-18; !; : 352 |


:

:

, - , ; , - .
==> ...

1279 - | 1314 -


© 2015-2024 lektsii.org - -

: 0.232 .