View difference between Paste ID: 7SU1GmhB and R0rjNU7M
SHOW: | | - or go back to the newest paste.
1
------------------------------------------------------------------------------------------
2
# PLIK1 - MathModule.h
3
4
#pragma once
5
6
class MathModule
7
{
8
public:
9-
    double divide(double x, double y);
9+
	double divide(double a, double b);
10-
    double power(double x, double y);
10+
	double power(double a, double b);
11-
    double abs(double x);
11+
	double abs(double a);
12-
    bool isDivisible(double x, double y);
12+
	bool isDivisible(double a, double b);
13
	double graphic_function(double a);
14
};
15
16
------------------------------------------------------------------------------------------
17
# PLIK 2 - MathModule.cpp
18
19
#include "MathModule.h"
20
#include <cmath>
21-
double MathModule::divide(double x, double y)
21+
22
double MathModule::divide(double a, double b)
23-
    return x / y;
23+
24
	return a / b;
25
}
26-
double MathModule::power(double x, double y)
26+
27
double MathModule::power(double a, double b)
28-
    return std::pow(x, y);
28+
29
	return pow(a, b);
30
}
31-
double MathModule::abs(double x)
31+
32
double MathModule::abs(double a)
33-
    return std::abs(x);
33+
34
	return std::abs(a);
35
}
36-
bool MathModule::isDivisible(double x, double y)
36+
37
bool MathModule::isDivisible(double a, double b)
38-
    return std::fmod(x, y) == 0;
38+
39
	return fmod(a, b) == 0;
40
}
41
42
double MathModule::graphic_function(double a)
43
{
44
	if (a < -5) {
45-
#include <string>
45+
		return 0;
46
	}
47
	else if (a <= -1 && a >= -2) {
48
		return abs(a) - 1;
49
	}
50
	else if (a > -1 && a <= 0) {
51
		return a + 1;
52-
    if (!condition)
52+
	}
53-
    {
53+
	else if (a > 0 && a < 2) {
54-
        std::cout << errorMessage << '\n';
54+
		return 1;
55-
        ++nTestErrors;
55+
	}
56-
    }
56+
	else if (a >= 2 && a <= 5) {
57-
    return condition;
57+
		return 2;
58
	}
59
	else {
60
		return 0;
61
	}
62-
    MathModule mathModule;
62+
63
64-
    // 1. Tests.
64+
65-
    checkError(mathModule.divide(4, 2) == 2, "Wrong division result.");
65+
66-
    checkError(mathModule.power(2, 4) == 16, "Wrong power result.");
66+
67-
    checkError(mathModule.abs(-4) == 4, "Wrong abs result.");
67+
68-
    checkError(mathModule.isDivisible(4, 2), "Wrong isDivisible result.");
68+
69-
    if (nTestErrors > 0) return 0;
69+
70
#include <array>
71-
    // 2. Lab3 task
71+
72-
    // ...
72+
73
74-
    return 0;
74+
75
{
76
	if (!condition)
77
	{
78
		std::cout << errorMessage << '\n';
79
		++nTestErrors;
80
	}
81
	return condition;
82
}
83
84
int main()
85
{
86
	double a, b;
87
	int choice;
88
	std::array<std::string, 5> functions = { "1. Divide", "2. Power", "3. Abs", "4. isDivisible", "5. Graphic function" };
89
90
	MathModule mathModule;
91
92
	checkError(mathModule.divide(4, 2) == 2, "Wrong division result.");
93
	checkError(mathModule.power(2, 4) == 16, "Wrong power result.");
94
	checkError(mathModule.abs(-4) == 4, "Wrong abs result.");
95
	checkError(mathModule.isDivisible(4, 2), "Wrong isDivisible result.");
96
	if (nTestErrors > 0) return 0;
97
98
	std::cout << "Choose the number of the function:" << "\n" << std::endl;
99
100
	for (int i = 0; i < functions.size(); i++)
101
		{
102
				std::cout << functions[i] << std::endl;
103
		}
104
105
	std::cout << "\n" << "Choice: ", std::cin >> choice;
106
107
	switch (choice) 
108
		{
109
	case 1:
110
		std::cout << "Dividend: ", std::cin >> a;
111
		std::cout << "Divider: ", std::cin >> b;
112
		std::cout << "Result: " << mathModule.divide(a, b);
113
		break;
114
	case 2:
115
		std::cout << "Base: ", std::cin >> a;
116
		std::cout << "Power: ", std::cin >> b;
117
		std::cout << "Result: " << mathModule.power(a, b);
118
		break;
119
	case 3:
120
		std::cout << "Number: ", std::cin >> a;
121
		std::cout << "Result: " << mathModule.abs(a);
122
		break;
123
	case 4:
124
		std::cout << "Dividend: ", std::cin >> a;
125
		std::cout << "Divider: ", std::cin >> b;
126
		std::cout << "Result: " << std::boolalpha << mathModule.isDivisible(a, b);
127
		break;
128
	case 5:
129
		std::cout << "X = ", std::cin >> a;
130
		std::cout << "Result: Y = " << mathModule.graphic_function(a);
131
		break;
132
		}
133
134
	return 0;
135
}