Sharedcpp101 / week1 / abc-formel.cppOpen in CoCalc
Authors: Dmitrij Moreinis, Harald Schilly
Views : 3
1
//#define CATCH_CONFIG_RUNNER // This tells Catch to provide a main() - only do this in one cpp file
2
#include <iostream>
3
#include <string>
4
#include <vector>
5
#include <cmath>
6
#include <map>
7
#include <algorithm>
8
//#include "catch.hpp"
9
#include <ratio>
10
#include <tuple>
11
//using namespace std;
12
13
14
float a,b,c;
15
int indikator;
16
17
float linsolve( float b, float c )
18
{
19
return -c/b;
20
21
}
22
23
/*
24
TEST_CASE( "Wird die Lineare Gleichung korrekt gelöst?") {
25
double b=-2,c=3;
26
27
REQUIRE( linsolve(1,0) == 0 );
28
REQUIRE( linsolve(1,1) == -1 );
29
REQUIRE( linsolve(-2,3) == -c/b);
30
31
}
32
*/
33
34
35
int Typ( float a, float b , float c)
36
{
37
if ((a==0)&&(b==0)&&(c==0)) indikator=1; // Fall unendlich viele Lösungen
38
else if ((a==0)&&(b==0)&&(c!=0)) indikator=2; // Fall keine Lösung
39
else if (((a==0)&&(b!=0)&&(c==0))||((a==0)&&(b!=0)&&(c!=0))) indikator=3; // Fall Lineare Gleichung
40
else indikator=4; // Fall quadratische Gleichung
41
return indikator;
42
43
}
44
45
/*
46
TEST_CASE( "Prüfe die Fallunterscheidungen") {
47
REQUIRE( Typ(0,0,0) == 1 );
48
REQUIRE( Typ(0,0,1) == 2 );
49
REQUIRE( Typ(0,1,0) == 3 );
50
REQUIRE( Typ(0,1,1) == 3 );
51
REQUIRE( Typ(1,1,1) == 4 );
52
53
}
54
*/
55
56
std::vector<float> diskriminante (float a, float b, float c)
57
{
58
float d=std::pow(b,2)-4*a*c;
59
if (d<0) return {0,d};
60
else if (d==0) return {1,d};
61
else if (d>0) return{2,d};
62
}
63
64
/*
65
TEST_CASE( "Prüfe die Diskriminate") {
66
std::vector<double> test1 {1,std::pow(-2,2)-4*1*1};
67
REQUIRE( diskriminante(1,-2,1)== std::vector<double> {1,std::pow(-2,2)-4*1*1});
68
69
}
70
*/
71
72
int main()
73
{
74
std::cin >> a >> b >> c;
75
//std::cout << " a ist= " << b << " b ist= " << b << " c ist= " << c << "\n";
76
indikator=Typ(a,b,c);
77
//std::cout << indikator << "\n";
78
79
if (indikator==1) {} //std::cout << // " unendlich viele Lösung";
80
else if (indikator==2) {} //std::cout << // " es gibt keine Lösung";
81
else if (indikator ==3) std::cout << linsolve(b,c);
82
else if (indikator ==4)
83
{
84
std::vector<float> d= diskriminante ( a, b, c);
85
//for (auto i:d)
86
// std::cout << i << "\n";
87
// if (d[0]<0) {} ;
88
if (d[0]==1) std::cout << -b/(2*a);
89
else if (d[0]==2) std::cout << (-b+sqrt(d[1]))/(2*a) << " "<< (-b-sqrt(d[1]))/(2*a);
90
91
}
92
93
94
95
96
97
//std::cout << " Fall " << Typ(a,b,c) << "\n";
98
99
100
101
102
103
104
//int result = Catch::Session().run();
105
//return result;
106
107
}
108
109