-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
155 lines (134 loc) · 4.39 KB
/
Copy pathmain.c
File metadata and controls
155 lines (134 loc) · 4.39 KB
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
/*!
* \file
* \version 1.1 Теперь ввод реализуется через функцию getNewData
*
* \brief Файл с примером работы с функцией solveSquare()
*
* Программа показывает пример работы с функцией solveSquare() а также включает возможность
* юнит-тестирования этой функции
*
* Сначала проводится юнит-тест (если он включен параметром TEST_SOLVESQUARE), далее программа считывает из стандартного ввода 3 числа - коэффициенты a, b, c - и
* вычисляет корни соответсвующего квадратного уравнения
*/
#include <stdio.h>
#include "stdlib.h" //for function free()
#include "solveSquare.h"
#include "input.h"
#define bool short //!< Введено для удобства, т.к. в C нет типа bool.
#define true 1
#define false 0
/*!
* \brief Включает/выключает режим юнит-тестирования функции solveSquare()
*
* При значении 0 тест выключен.
* При значении 1 - включен1
*/
#define TEST_SOLVESQUARE 1
#if (TEST_SOLVESQUARE)
int testSolveSquare();
#endif
/*!
* \brief Содержит реализацию примера работы.
*/
int main()
{
double x1 = 0, x2 = 0;
int nRoots = 0;
#if(TEST_SOLVESQUARE)
if(testSolveSquare() == 1) //if test is unsuccessful
return 1; //main returns 1;
#endif
double *coeffs = getNewData("Enter a, b, c:", 3);
nRoots = solveSquare(coeffs[0], coeffs[1], coeffs[2], &x1, &x2);
switch(nRoots)
{
case 0:
printf("No roots:(\n");
break;
case 1:
printf("One root: x1 = x2 = %lg;\n", x1);
break;
case 2:
printf("Two roots: x1 = %lg; x2 = %lg;\n", x1, x2);
break;
case INF_ROOTS:
printf("Inf. number of roots;\n");
break;
default:
printf("Error! nRoots = %d\n", nRoots);
break;
}
free(coeffs);
coeffs = NULL;
return 0;
}
#if (TEST_SOLVESQUARE)
/*!
* \brief Функция для тестирования solveSquare()
*
* В случае возникновения ошибки выводит сообщение с пояснениями в стандартный вывод и возвращает 1.
* \returns 0 Если тест прошёл успешно
* \returns 1 Если возникла ошибка.
*/
int testSolveSquare()
{
const double PRECISION = 0.001; //!< sets precision of checking for the test
double a = 0, b = 0, c = 0;
double x1 = 0, x2 = 0;
int nRoots = 0;
printf("\nTest of solveSquare is started.\n");
const int INTERVAL = 100;
//test's body
for(a = -INTERVAL; a < INTERVAL; ++a)
for(b = -INTERVAL; b < INTERVAL; ++b)
for(c = -INTERVAL; c < INTERVAL; ++c)
{
bool errorOccured = false;
nRoots = solveSquare(a, b,c, &x1, &x2);
if(nRoots != 1 && nRoots != 2 && nRoots !=0 && nRoots != INF_ROOTS)
{
printf("\nERROR: nRoots is incorrect! \n");
errorOccured=true;
}
if(nRoots == 0)
{
double D = b * b - 4 * a * c;
if( !doublesAreEqual(a, 0.0, PRECISION) && D >= 0 )
{
printf("\nERROR: nRoots == 0, but D >= 0 \n");
errorOccured = true;
}
if( doublesAreEqual(a, 0.0, PRECISION) && doublesAreEqual(b, 0.0, PRECISION) && doublesAreEqual(c, 0.0, PRECISION) )
{
printf("\nERROR: nRoots==0, but number of roots is infinitive.\n");
errorOccured = true;
}
}
if( (nRoots == 1 || nRoots == 2 ) && !doublesAreEqual(a * x1 * x1 + b * x1 + c, 0.0, PRECISION) )
{
printf("\nERROR: x1 is not a root! \n");
errorOccured = true;
}
if( (nRoots == 2) && !doublesAreEqual(a * x2 * x2 + b * x2 + c, 0.0, PRECISION) )
{
printf("\nERROR: x2 is not a root! \n");
errorOccured = true;
}
if( nRoots == INF_ROOTS &&
!(doublesAreEqual(a, 0.0, PRECISION) && doublesAreEqual(b, 0.0, PRECISION) && doublesAreEqual(c, 0.0, PRECISION)) )
{
printf("\nERROR: nRoots==INF_ROOTS, but nubmer of roots is not infinitive \n");
errorOccured = true;
}
if(errorOccured)
{
printf("solveSquare works incorrectly!\n");
printf("Input values:\n a: %lg; \n b: %lg; \n c: %lg; \n", a, b, c);
printf("Output values:\n nRoots: %d; \n x1: %lg; \n x2: %lg; \n", nRoots, x1, x2);
return 1;
}
}
printf("Tested successfully! \n\n");
return 0;
}
#endif