12. .
. .
:
- ;
- , ;
- ;
- .
C++ , . .
, . , , . , - . , , .
, , . , , .
.
, : , . , :
void print(char *str, const int i, const int j)
{
cout << str << '|' << oct << setw(4) << i << '|' << setw(4) << j
<< '|' << endl;
}
void print(float array[], const int n)
{
cout << ":" << endl;
cout.setf(ios::fixed);
cout.precision(2);
for (int i = 0; i < n; i++)
{
cout << array[i] << " ";
if ((i + 1) % 4 == 0) cout << endl;
}
cout << endl;
}
void print(Man m)
{
cout.setf(ios::fixed);
cout.precision(2);
cout << setw(40) << m.name << ' ' << m.birthday << ' '
<< m.pay << endl;
}
, . 4 ( setw ).
|
|
. setf, precision fixed, ios. , . , . setw . , . , , :
print(" ", i, j);
print(a, n);
print(m);
, , , , . , , , .
, .
. -, - , . , . , .
- . .
12.1.
, . , .
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
//
//
struct cartesian{
double x, y;
};
//
struct polar{
double r, pi;
};
// ,
//
double len(polar a, polar b)
{
cout << " " << endl;
return sqrt(pow(a.r, 2) + pow(b.r, 2) - 2*a.r*b.r*cos(a.pi - b.pi));
}
//
double len(cartesian x, cartesian y)
{
cout << " " << endl;
return sqrt(pow(y.x - x.x, 2)+pow(y.y - x.y, 2));
}
// ,
//
double len(double x1, double y1, double x2, double y2)
{
cout << " \
4- " << endl;
return sqrt(pow(x2 - x1, 2)+pow(y2 - y1, 2));
}
void main(int argc, char* argv[])
|
|
{
setlocale(LC_ALL, "Russian");
const double PI = 3.14159;
cartesian a = {3, 0},
b = {1, 1};
polar c = {1.41, PI/4},
d = {3.1, 0.95};
double x1 = 1.4, y1 = 2.5,
x2 = 2.1, y2 = 3.7;
cout << len(a, b) << endl;
cout << len(c, d) << endl;
cout << len(x1, y1, x2, y2) << endl;
}
:
2.23607
1.7246
4-
1.38924
C++ . , , , ( , - ).
. - , , . , .
: -, , , -, , -,
, . , , . main.
template <class ttype>
ttype _ (__)
{
}
ttype , .
12.2.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
template <class array_type>
array_type max(array_type *a, const int N)
{
array_type m = a[0];
for (int i = 1; i < N; i++)
if (a[i] > m)
{
m = a[i];
}
return m;
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "Russian");
double a[] = {2.5, 8.3, 6};
int b[] = {3, 5, -1, 2};
char c[] = {'A', 'b', 'Z', 'r'};
cout << max(a, sizeof(a)/sizeof(a[0])) << endl;
cout << max(b, sizeof(b)/sizeof(b[0])) << endl;
cout << max(c, sizeof(c)/sizeof(c[0])) << endl;
return 0;
}
. , , , . , .
:
1. .
2. , .
3. . class.
:
template <class type1, class type2>
, :
template <class type1, type2, type3>
|
|
4. , .. :
template <class type1, class type1, class type1>
C++ , . , . , . :
(__,...);
.
, , - . , .
, .
( ).
#include "stdafx.h"
#include <iostream>
using namespace std;
long summa(int k,...)
{
int *pik = &k;
long total = 0;
for (; k; k--)
total += *(++pik);
return total;
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "Russian");
cout << "\n summa(2, 6, 4) = " << summa(2, 6, 4);
cout << "\n summa(6, 1, 2, 3, 4, 5, 6) =" <<
summa(6, 1, 2, 3, 4, 5, 6);
return 0;
}
summa(2, 6, 4)=10
summa(6, 1, 2, 3, 4, 5, 6)=21
, pik .
#include "stdafx.h"
#include <iostream>
using namespace std;
double prod(double arg,...)
{
double result = 1.0;
double *prt = &arg;
if (*prt == 0.0)
return 0.0;
for(; *prt; prt++)
result *= *prt;
return result;
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "Russian");
cout <<"\n prod(2e0,4e0,3e0,0e0) = " << prod(2e0,4e0,3e0,0e0);
cout <<"\n prod(1.5,2.0,3.0,0.0) = " << prod(1.5,2.0,3.0,0.0);
cout <<"\n prod(1.4,3.0,0.0,16.0,84.3,0.0) = " <<
prod(1.4,3.0,0.0,16.0,84.3,0.0);
cout << "\n prod(0e0) ="<< prod(0e0);
return 0;
}
prod(2e0,4e0,3e0,0e0) = 24
prod(1.5,2.0,3.0,0.0) = 9
prod(1.4,3.0,0.0,16.0,84.3,0.0) = 4.2
prod(0e0) = 0
. , .
1. ?
2. ?
|
|
3. , ?
4. ?
5. ?
6. ?
7. ?
1. 5.1.
2. .
3. 5.2.
4. .
5. .
1. , . :
1.1. int.
1.2. int.
1.3. float.
1.4. double.
2. , . :
2.1. (, , ).
2.2. : , , .
2.3. : , ( ).
3. , . :
3.1. int.
3.2. long.
3.3. float.
3.4. double.
4. , . :
4.1. int N.
4.2. float N.
4.3. double N.
5. , . :
5.1. int.
5.2. int.
5.3. float.
5.4. double.
6. , . :
6.1. (, , ).
6.2. : , , .
6.3. : , ( ).
7. , . :
7.1. (, , ).
7.2. : , , .
7.3. : , ( ).
8. , . :
8.1. int.
8.2. long.
8.3. float.
8.4. double.
9. , . :
9.1. (, , ).
9.2. : , , .
9.3. : , .
10. , . :
10.1. (, , ).
10.2. : , , .
10.3. : , .
11. , . :
11.1. int N.
11.2. float N.
11.3. double N.
12. , . :
12.1. (, , ).
12.2. : , , .
12.3. : , ( ).
13. , . :
13.1. ( x, y).
13.2. float.
13.3. double.
14. , . :
14.1. (, , ).
14.2. : , , .
14.3. : , .
15. , . :
|
|
15.1. int N.
15.2. float N.
15.3. double N.
16. , (, 24 ). :
16.1. (, , ).
16.2. : , , .
16.3. : , .
17. , . :
17.1. int.
17.2. int.
17.3. float.
17.4. float.
17.5. double.
18. , . :
18.1. (, , ).
18.2. : , , .
18.3. : , ( ).
19. , n- x. :
19.1. : x n int.
19.2. : x n float.
19.3. : x float, n int.
20. , . :
20.1. int N.
20.2. float N.
20.3. double N.
21. , . :
21.1. (, , ).
21.2. : , , .
21.3. : , .
22. , . :
22.1. int.
22.2. int.
22.3. float.
22.4. double.
23. , ( , 1 1- ). :
23.1. (, , ).
23.2. : , , .
23.3. : , ( ).
24. , . :
24.1. int N.
24.2. float N.
24.3. double N.
25. , . :
25.1. (, , ).
25.2. : , , .
25.3. : , ( ).
5.1 . 3 .
1. .
2. .
3. .
4. .
5. .
6. .
7. .