C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
| #define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include<ctime>
const double Pi = 3.14159265;
using namespace std;
class Object {
protected: string name;
protected: int number;
public: static int count;
public: Object(const string&n, int num) {
name = n;
number = num;
++count;
}
Object() {
name = "";
number = 0;
count = 0;
}
Object(const Object&A) {
name = A.name;
number = A.number;
++count;
}
public:
void info(ostream&S)const {
S << "Номер объекта: " << number << "Имя объекта: " << name;
}
friend ostream &operator<<(ostream&S, const Object&A) {
A.info(S);
return S;
}
};
int Object::count = 0;
class line : public Object {
protected:
double a, b, c;//коэфиценты прямой
public:
line(double a1, double b1, double c1, int t, string s = "Прямая") :Object(s, t) {
a = a1; b = b1; c = c1;
}
line() {
a = 0, b = 0; c = 0;
}
line(const line&A) {
a = A.a; b = A.b; c = A.c;
number = A.number, name = A.name;
}
void Norma() {//нормальное уравнение прямой
a = a / sqrt(a*a + b*b); b = b / sqrt(a*a + b*b); c = c / sqrt(a*a + b*b);
}
double Length(double x, double y) {//расстояние от точки до прямой
return (abs(a*x + b*y + c)) / sqrt(a*a + b*b);
}
double Pointx(const line&f) {//точка пересечения двух прямых
return ((c*f.b - f.c*b) / (a*f.b - f.a*b));
}
double Pointy(const line&f) {
return (a*f.c - f.a*c) / (a*f.b - f.a*b);
}
line(double a1, double b1, double c1) {
a = a1; b = b1; c = c1;
}
line& operator=(const line&A) {
a = A.a; b = A.b; c = A.c;
number = A.number, name = A.name;
return *this;
}
friend istream&operator >> (istream&S, line&A) {
S >> A.a >> A.b >> A.c;
return S;
}
friend ostream&operator<<(ostream&S, const line&A) {
S << A.a << "x" << "+"<< A.b << "y" <<"+"<< A.c << "=0" << endl;
return S;
}
void info(ostream&S)const {
S << "Номер объекта: " << number << " " << "Имя объекта: " << name << " " << "\n";
}
};
class VLine : public line {
double a, b, c;//Длина стороны и высоты
public:
VLine() { // конструктор по умолчанию
a = 0; c = 0;
}
VLine(double a1, double c1) {
a = a1; c = c1;
}
VLine(const VLine&d) {
a = d.a; c = d.c;
}
VLine& operator=(const VLine&d) {
a = d.a; c = d.c;;
return *this;
}//указатель на самого себя
void Norma() {//нормальное уравнение прямой
a = a / sqrt(a*a); b = 0; c = c / sqrt(a*a);
}
double Length(double x, double y) {//расстояние от точки до прямой
return (abs(a*x + c)) / sqrt(a*a);
}
double Pointx(const VLine&f) {//точка пересечения двух прямых
return ((c*f.b - f.c*b) / (a*f.b - f.a*b));
}
double Pointy(const VLine&f) {
return 0;
}
friend istream& operator>>(istream &s, VLine &d) {
s >> d.a >> d.c;
return s;
}
friend ostream& operator<<(ostream &s, const VLine&d) {
s << d.a << "x" << d.c << "=0" << endl;
return s;
}
};
int main() {
setlocale(0, "");
string s = "";
srand(time(NULL));
vector <line> A(5);
int i = 1;
generate(A.begin(), A.end(), []() {return line(rand() % 10 + 1, rand() % 10 + 1, rand() % 10 + 1, rand() % 100+1); });
vector <line>::iterator it;
cout << "До сортировки: \n";
for (it = A.begin(); it != A.end(); it++) {
it->info(cout);
cout << *it;
cout << "Расстояние: " << it->Length(0, 0) << '\n';
}
sort(A.begin(), A.end(), [](line A, line B) {return A.Length(0,0) < B.Length(0,0);
});
cout << '\n';
cout << "После сортировки: \n";
for (it = A.begin(); it != A.end(); it++) {
it->info(cout);
cout << *it;
cout << "Расстояние от (0,0) до прямой: " << it->Length(0,0) << '\n';
}
cout << "\n Уравнения прямых нормального вида: \n";
for (it = A.begin(); it != A.end(); it++) {
it->info(cout);
it->Norma();
cout << *it;
}
system("pause");
return 0;
} |
|
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
| #define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include<ctime>
const double Pi = 3.14159265;
using namespace std;
class Object {
protected: string name;
protected: int number;
public: static int count;
public: Object(const string&n, int num) {
name = n;
number = num;
++count;
}
Object() {
name = "";
number = 0;
count = 0;
}
Object(const Object&A) {
name = A.name;
number = A.number;
++count;
}
public:
void info(ostream&S)const {
S << "Номер объекта: " << number << "Имя объекта: " << name;
}
friend ostream &operator<<(ostream&S, const Object&A) {
A.info(S);
return S;
}
};
int Object::count = 0;
class line : public Object {
protected:
double a, b, c;//коэфиценты прямой
public:
line(double a1, double b1, double c1, int t, string s = "Прямая") :Object(s, t) {
a = a1; b = b1; c = c1;
}
line() {
a = 0, b = 0; c = 0;
}
line(const line&A) {
a = A.a; b = A.b; c = A.c;
number = A.number, name = A.name;
}
void Norma() {//нормальное уравнение прямой
a = a / sqrt(a*a + b*b); b = b / sqrt(a*a + b*b); c = c / sqrt(a*a + b*b);
}
double Length(double x, double y) {//расстояние от точки до прямой
return (abs(a*x + b*y + c)) / sqrt(a*a + b*b);
}
double Pointx(const line&f) {//точка пересечения двух прямых
return ((c*f.b - f.c*b) / (a*f.b - f.a*b));
}
double Pointy(const line&f) {
return (a*f.c - f.a*c) / (a*f.b - f.a*b);
}
line(double a1, double b1, double c1) {
a = a1; b = b1; c = c1;
}
line& operator=(const line&A) {
a = A.a; b = A.b; c = A.c;
number = A.number, name = A.name;
return *this;
}
friend istream&operator >> (istream&S, line&A) {
S >> A.a >> A.b >> A.c;
return S;
}
friend ostream&operator<<(ostream&S, const line&A) {
S << A.a << "x" << "+" << A.b << "y" << "+" << A.c << "=0" << endl;
return S;
}
void info(ostream&S)const {
S << "Номер объекта: " << number << " " << "Имя объекта: " << name << " " << "\n";
}
};
class VLine : public line {
double a, b, c;//Длина стороны и высоты
public:
VLine() { // конструктор по умолчанию
a = 0; c = 0;
}
VLine(double a1, double c1) {
a = a1; c = c1;
}
VLine(const VLine&d) {
a = d.a; c = d.c;
}
VLine& operator=(const VLine&d) {
a = d.a; c = d.c;;
return *this;
}//указатель на самого себя
void Norma() {//нормальное уравнение прямой
a = a / sqrt(a*a); b = 0; c = c / sqrt(a*a);
}
double Length(double x, double y) {//расстояние от точки до прямой
return (abs(a*x + c)) / sqrt(a*a);
}
double Pointx(const VLine&f) {//точка пересечения двух прямых
return ((c*f.b - f.c*b) / (a*f.b - f.a*b));
}
double Pointy(const VLine&f) {
return 0;
}
friend istream& operator>>(istream &s, VLine &d) {
s >> d.a >> d.c;
return s;
}
friend ostream& operator<<(ostream &s, const VLine&d) {
s << d.a << "x" << d.c << "=0" << endl;
return s;
}
};
int main() {
setlocale(0, "");
string s = "";
srand(time(NULL));
vector <line> A(5);
int i = 1;
generate(A.begin(), A.end(), []() {return line(rand() % 10 + 1, rand() % 10 + 1, rand() % 10 + 1, rand() % 100 + 1); });
vector <line>::iterator it;
cout << "До сортировки: \n";
for (it = A.begin(); it != A.end(); it++) {
it->info(cout);
cout << *it;
cout << "Расстояние: " << it->Length(0, 0) << '\n';
}
transform(A.begin(), A.end(), A.begin(), [](line A, line B) {return A.Length(0, 0) < B.Length(0, 0);});
cout << '\n';
cout << "После сортировки: \n";
for (it = A.begin(); it != A.end(); it++) {
it->info(cout);
cout << *it;
cout << "Расстояние от (0,0) до прямой: " << it->Length(0, 0) << '\n';
}
cout << "\n Уравнения прямых нормального вида: \n";
for (it = A.begin(); it != A.end(); it++) {
it->info(cout);
it->Norma();
cout << *it;
}
system("pause");
return 0;
} |
|
В первой программе используется метод sort, а нужно чтобы работало через transform. Всё уже перепробовал, выводит ошибку С2064 (Результатом вычисления фрагмента не является функция, принимающая 1 аргументов)
0
|