Началась четвертая неделя обучения. Структуры и классы.
Задание:
Дана структура LectureTitle:
| C++ | 1
2
3
4
5
| struct LectureTitle {
string specialization;
string course;
string week;
}; |
|
Допишите конструктор и структуры Specialization, Course, Week так, чтобы объект LectureTitle можно было создать с помощью кода
| C++ | 1
2
3
4
5
| LectureTitle title(
Specialization("C++"),
Course("White belt"),
Week("4th")
); |
|
но нельзя было с помощью следующих фрагментов кода:
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| LectureTitle title("C++", "White belt", "4th");
LectureTitle title = {"C++", "White belt", "4th"};
LectureTitle title = {{"C++"}, {"White belt"}, {"4th"}};
LectureTitle title(
Course("White belt"),
Specialization("C++"),
Week("4th")
);
LectureTitle title(
Specialization("C++"),
Week("4th"),
Course("White belt")
); |
|
Моё решение:
| 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
| #include <iostream>
#include <vector>
using namespace std;
struct Specialization {
string value;
explicit Specialization(string new_val) {
value = new_val;
}
};
struct Course {
string value;
explicit Course(string new_val) {
value = new_val;
}
};
struct Week{
string value;
explicit Week(string new_val) {
value = new_val;
}
};
struct LectureTitle {
string specialization;
string course;
string week;
LectureTitle(Specialization new_s, Course new_c, Week new_w) {
specialization = new_s.value;
course = new_c.value;
week = new_w.value;
}
}; |
|

И ещё небольшое заданьице:
Реализуйте рассказанный на лекции класс Function, позволяющий создавать, вычислять и инвертировать функцию, состоящую из следующих элементарных операций:- прибавить вещественное число x;
- вычесть вещественное число x.
При этом необходимо объявить константными все методы, которые по сути такими являются.
Пример:
Код
| 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
| struct Image {
double quality;
double freshness;
double rating;
};
struct Params {
double a;
double b;
double c;
};
Function MakeWeightFunction(const Params& params,
const Image& image) {
Function function;
function.AddPart('-', image.freshness * params.a + params.b);
function.AddPart('+', image.rating * params.c);
return function;
}
double ComputeImageWeight(const Params& params, const Image& image) {
Function function = MakeWeightFunction(params, image);
return function.Apply(image.quality);
}
double ComputeQualityByWeight(const Params& params,
const Image& image,
double weight) {
Function function = MakeWeightFunction(params, image);
function.Invert();
return function.Apply(weight);
}
int main() {
Image image = {10, 2, 6};
Params params = {4, 2, 6};
cout << ComputeImageWeight(params, image) << endl;
cout << ComputeQualityByWeight(params, image, 46) << endl;
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
| #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*struct Image {
double quality;
double freshness;
double rating;
};
struct Params {
double a;
double b;
double c;
};*/
class FunctionPart {
public:
FunctionPart(char new_operation, double new_value) {
value = new_value;
operation = new_operation;
}
double Apply(const double& sourse_value) const{
if (operation == '+') {
return sourse_value + value;
}
else {
return sourse_value - value;
}
}
void Invert() {
if (operation == '+') {
operation = '-';
}
else {
operation = '+';
}
}
private:
char operation;
double value;
};
class Function {
public:
void AddPart(char operation, double value) {
parts.push_back({ operation, value });
}
double Apply(double value) const {
for (const FunctionPart& part : parts) {
value = part.Apply(value);
}
return value;
}
void Invert() {
for (FunctionPart& part : parts) {
part.Invert();
}
reverse(begin(parts), end(parts));
}
private:
vector<FunctionPart> parts;
}; |
|
и добавление к предыдущему заданию:
Добавьте в класс Function из задачи «Обратимая функция» обработку умножения ('*') и деления ('/'). Гарантируется отсутствие элементарных операций умножения и деления на 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
| struct Image {
double quality;
double freshness;
double rating;
};
struct Params {
double a;
double b;
double c;
};
Function MakeWeightFunction(const Params& params,
const Image& image) {
Function function;
function.AddPart('*', params.a);
function.AddPart('-', image.freshness * params.b);
function.AddPart('+', image.rating * params.c);
return function;
}
double ComputeImageWeight(const Params& params, const Image& image) {
Function function = MakeWeightFunction(params, image);
return function.Apply(image.quality);
}
double ComputeQualityByWeight(const Params& params,
const Image& image,
double weight) {
Function function = MakeWeightFunction(params, image);
function.Invert();
return function.Apply(weight);
}
int main() {
Image image = {10, 2, 6};
Params params = {4, 2, 6};
cout << ComputeImageWeight(params, image) << endl;
cout << ComputeQualityByWeight(params, image, 52) << endl;
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
| #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class FunctionPart {
public:
FunctionPart(char new_operation, double new_value) {
value = new_value;
operation = new_operation;
}
double Apply(const double& sourse_value) const {
if (operation == '+') {
return sourse_value + value;
}
else if (operation == '-') {
return sourse_value - value;
}
else if (operation == '*') {
return sourse_value * value;
}
else {
return sourse_value / value;
}
}
void Invert() {
if (operation == '+') {
operation = '-';
}
else if(operation == '-') {
operation = '+';
}
else if (operation == '*') {
operation = '/';
}
else {
operation = '*';
}
}
private:
char operation;
double value;
};
class Function {
public:
void AddPart(char operation, double value) {
parts.push_back({ operation, value });
}
double Apply(double value) const {
for (const FunctionPart& part : parts) {
value = part.Apply(value);
}
return value;
}
void Invert() {
for (FunctionPart& part : parts) {
part.Invert();
}
reverse(begin(parts), end(parts));
}
private:
vector<FunctionPart> parts;
}; |
|
Далее работа с потоками...
|