-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial_long_division.cpp
More file actions
116 lines (116 loc) · 3.85 KB
/
Copy pathpolynomial_long_division.cpp
File metadata and controls
116 lines (116 loc) · 3.85 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
#include <deque>
#include <map>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
bool is_integer(string str) {
if (str.empty()) return false;
while (str.front() == ' ') {
str = str.substr(1);
}
size_t start = 0;
if (str[0] == '-' || str[0] == '+') {
if (str.size() == 1) return false;
start = 1;
}
for (size_t i = start; i < str.size(); ++i) {
if (!std::isdigit(str[i])) {
return false;
}
}
return true;
}
struct polynomial {
int degree = 0;
map <int, int> coefficients;
};
polynomial dividend, divisor, quotient, temp;
void take_inputs() {
string temp;
while (true) {
cout << "Enter the dividend polynomial degree:\n";
getline(cin, temp);
if (is_integer(temp) && stoi(temp) >= 0) {
dividend.degree = stoi(temp);
break;
}
else { cout << "Invalid input! Attempt aborted.\n\n"; }
}
for (int i = dividend.degree; i >= 0; --i) {
while (true) {
cout << "Enter the coefficient for term x^" << i << " or 0 if term is not present in the dividend polynomial:\n";
getline(cin, temp);
if (is_integer(temp)) {
dividend.coefficients[i] = stoi(temp);
break;
}
else { cout << "Invalid input! Attempt aborted.\n\n"; }
}
}
while (true) {
cout << "Enter the divisor polynomial degree:\n";
getline(cin, temp);
if (is_integer(temp) && stoi(temp) >= 0) {
divisor.degree = stoi(temp);
break;
}
else { cout << "Invalid input! Attempt aborted.\n\n"; }
}
for (int i = divisor.degree; i >= 0; --i) {
while (true) {
cout << "Enter the coefficient for term x^" << i << " or 0 if term is not present in the divisor polynomial:\n";
getline(cin, temp);
if (is_integer(temp)) {
divisor.coefficients[i] = stoi(temp);
break;
}
else { cout << "Invalid input! Attempt aborted.\n\n"; }
}
}
}
void output_given_polynomial(polynomial a) {
cout << "f(x) = ";
if (a.coefficients.empty()) { cout << 0; return; }
for (int i = a.degree; i >= 0; --i) {
cout << a.coefficients[i];
if (i != 0) {
cout << "x^" << i << " + ";
}
}
cout << '\n';
}
int main() {
//Input:
take_inputs();
cout << "\nGiven Dividend Polynomial:\n";
output_given_polynomial(dividend);
cout << "\nGiven Divisor Polynomial:\n";
output_given_polynomial(divisor);
quotient.degree = dividend.degree - divisor.degree;
while (dividend.degree >= divisor.degree) {
int scale = dividend.coefficients[dividend.degree]/divisor.coefficients[divisor.degree];
if (scale*divisor.coefficients[divisor.degree] != dividend.coefficients[dividend.degree]) {
cout << "\nNot divisible over integers.\n";
return 0;
}
quotient.coefficients[dividend.degree - divisor.degree] = scale;
for (int i = 0; i <= divisor.degree; ++i) {
dividend.coefficients[dividend.degree - i] -= (divisor.coefficients[divisor.degree - i]*scale);
cout << dividend.coefficients[dividend.degree - i] << '\n';
}
while (!dividend.coefficients.empty() && dividend.coefficients[dividend.degree] == 0) {
dividend.coefficients.erase(dividend.degree);
dividend.degree--;
}
}
cout << "Long Division Answer:\nQuotient:\n";
output_given_polynomial(quotient);
cout << "\nRemainder:\n";
output_given_polynomial(dividend);
cout << '\n';
while (true) {}
return 0;
}