8.
, .
:
- ;
- , ;
- ;
- .
, , . . , . , , . . , .
, , , . , .
, . , , .. , , .
. , , . , , , .
(. . ). , , : , , , , .
(, , ) , . () :
[ ] ([ __ ]);
, , , :
[ ] ([ __ ])
{
}
, , .
.
|
|
1. , extern static:
- extern ( );
- static , .
2. , ( ). , void.
3. , . . , , ( ), ( ).
, ( ). , , :
([ __ ]);
, . , , ( ).
inline, . inline . , ( , ). inline . inline- . , inline- .
, , , . . , , . , , , , .
, . , static.
.
. return , . ; , . .
|
|
, . , , . void, , , .
, :
#include "stdafx.h"
#include <iostream>
using namespace std;
int sum(int , int b); //
void main()
{
int a = 2, b = 3, c, d;
c = sum(a, b); //
cin >> d;
cout << sum(c, d) << endl; //
a = 3*sum(c, d) - b; //
cout << a << endl;
return;
}
int sum(int a, int b) //
{
return (a + b);
}
, , .
:
#include "stdafx.h"
#include <iostream>
using namespace std;
int max(int x, int y) //
{
if (x > y)
return x;
else
return y;
}
void main()
{
int a = 2, b = 3, c, d;
c = max(a, b); //
cout << c << endl;
cin >> d;
cout << max(c, d) << endl; //
return;
}
.
, , , , , .
. , , , .
, ; , . .
: .
, . , , , .
, . .
#include "stdafx.h"
#include <iostream>
using namespace std;
void f(int a, int* b, int& c)
{
a++;
(*b)++;
c++;
}
void main()
{
int a = 1, b = 1, c = 1;
cout << "a b c" << endl;
cout << a << ' ' << b << ' ' << c << endl;
f(a, &b, c);
cout << a << ' ' << b << ' ' << c << endl;
return;
}
:
a b c
1 1 1
1 2 2
(a) . . (b) , , . (c) .
|
|
, . , . , , .
, , , .
, const:
int f(const char *);
, , , .
, (, , , , , ), .
, . . .
, :
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
float ax(float a, float x, float eps = 0.001f)
{
float xn = 1, y, y0;
unsigned int n = 1, nf = 1;
y = 1;
do
{
nf *= n;
xn *= x*log(a);
y0 = y;
y += xn/nf;
n++;
}
while (abs(y - y0) > eps);
return y;
}
void main()
{
setlocale(LC_ALL, "Russian");
const float eps = 0.1f;
float a, x, y;
cout << " a, x:" << endl;
cin >> a >> x;
//
y = ax(a, x, eps);
cout <<\
" 0.1 ( ): "\
<< setw(8) << setprecision(5) << y <<endl;
// esp
//
y = ax(a, x);
cout <<\
" 0.001 ( ): "\
<< setw(8) << setprecision(5) << y <<endl;
//
y = pow(a, x);
cout <<\
" : "\
<< setw(8) << setprecision(5) << y <<endl;
}
:
a, x:
2.5
0.1 ( ): 15.591
0.001 ( ): 15.625
: 15.625
, . , .. . , .
|
|
, , .
#include "stdafx.h"
#include <iostream>
using namespace std;
// ( ),
//
//
void max_vect(int n, int *x, int *y, int *z)
{
for (int i=0; i<n; i++)
z[i] = (x[i]>y[i])?x[i]:y[i];
}
void main()
{
setlocale(LC_ALL, "Russian");
//
const int N=7;
int a[N]={1,4,3,-1,5,6,1};
int b[N]={7,6,-2,4,3,2,4};
int c[N];
// ,
//
max_vect(N, a, b, c);
//
for (int i=0; i < N; i++)
cout << "\t" << c[i];
}
:
7 6 3 4 5 6 4
, , , . , .
.
#include "stdafx.h"
#include <iostream>
using namespace std;
int sum(int *x, const int n, const int m)
{
int s = 0;
for (int i=0; i < n; i++)
for (int j=0; j < m; j++)
// :
//
//
// x[i*m + j] x[i][j]
//
s += x[i*m + j];
return s;
}
void main()
{
setlocale(LC_ALL, "Russian");
int a[2][2]={{1,2},{3,4}};
// a -
// &a[0][0]
cout << sum(&a[0][0], 2, 2);
}
. : , . .
, .
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
int sum(int **x, const int n, const int m)
{
int s = 0;
for (int i=0; i < n; i++)
for (int j=0; j < m; j++)
s += x[i][j];
return s;
}
void main()
{
setlocale(LC_ALL, "Russian");
const int N=4, M=3;
// N
//( , -*)
int **a = new int* [N];
for(int i=0; i < N; i++)
// M
//
a[i] = new int[M];
//
for(int i=0; i < N; i++)
{
for(int j=0; j < M; j++)
{
// (%) 0 9
a[i][j] = rand()%10;
// i
// j -
cout << a[i][j] << "\t";
}
cout << endl;
}
cout << sum(a, N, M);
//
for(int i=0; i < N; i++)
//
delete [] (a[i]);
//
delete [] a;
}
8.1.
8.1
1. ?
2. ?
3. ?
4. ?
5. ?
6. ?
7. inline-?
8. , ?
|
|
9. ?
10. ?
11. ?
12. ?
13. ?
14. ?
1. 5.1.
2. .
5.2.
3. .
4. 5.3.
5. .
6. .