-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (114 loc) · 3.05 KB
/
main.cpp
File metadata and controls
141 lines (114 loc) · 3.05 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
/*******************
* Luke Gauldin *
* 12/16/14 *
* version 2.6 *
*******************/
#include <iostream>
#include <list>
#include <iomanip>
#include <cmath>
using namespace std;
//Receives the Equation
list<int> getEquation(){
int terms = 0;
int coefficient = 0;
list<int> equation;
cout << "How many terms are there (include terms with a coefficient of zero): ";
cin >> terms;
for(int x = 1; x <= terms; x++){ // terms; terms>0, terms--
cout << "\n What is the coefficient of this term: ";
cin >> coefficient;
equation.push_back(coefficient);
}
return equation;
}
//Factoring function
list<int> factor(int num){
list<int> factors;
int intNum=(int) num;
if(intNum < 0){
intNum *= -1;
}
for(int x = -intNum; x <= intNum; x++){
if(x != 0){
if (intNum % x == 0){
factors.push_back(x);
}
}
}
return factors;
}
//Calculates rational roots
list<float> getRoots(list<int> factorOne, list<int> factorTwo){
list<float> roots;
for(float x : factorOne){
for(float y : factorTwo){
roots.push_back(y / x);
}
}
roots.sort();
roots.unique();
return roots;
}
//Tests zeros
list<float> zeroTest(list<float> rationalRoots, list<int> equation){
float total = 0;
int term = 0;
float base = 0;
list<float> zeros;
for(float x : rationalRoots){
total = 0;
term = equation.size() - 1;
for(int y : equation){
if(x < 0 && term % 2 == 0){
base = x * -1;
}
else{
base = x;
}
if(term != 0){
total += y * powf(base, term);
}
else{
total += y;
}
term--;
/**********************************************************************
*Debugging line uncomment when testing *
*Displays the root being tested, the coefficient, and the total amount*
**********************************************************************/
//cout << base << " " << y << " " << total << "\n";
}
if(total > -8e-006 && total < 8e-006){
zeros.push_back(x);
}
}
zeros.sort();
return zeros;
}
int main()
{
list<int> equation;
list<int> factorTwo;
list<float> rationalRoots;
list<int> factorOne;
list<float> zeros;
equation = getEquation();
factorOne = factor(equation.front());
factorTwo = factor(equation.back());
rationalRoots = getRoots(factorOne, factorTwo);
zeros = zeroTest(rationalRoots, equation);
cout << "\nThe rational roots are: ";
for(float x : rationalRoots){
cout << x << " ";
}
cout << "\n" << rationalRoots.size();
cout << "\nThe rational zeros are: ";
for(float x : zeros){
cout << x << " ";
}
cout << "\n\nPress any key to exit...";
cin.ignore();
cin.get();
return 0;
}