-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (161 loc) · 6.46 KB
/
main.cpp
File metadata and controls
162 lines (161 loc) · 6.46 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
156
157
158
159
160
161
162
#include <cstdio>
using namespace std;
int long_division() {
printf("\n Author: Saif Salah El-deen Yahya\n"
"\n ID: 1901529\n"
"\n Section: 7\n"
"\n Department: General Electrical Engineering\n"
"\n CSE131 Project - Long Division for Polynomials Function\n");
printf("\n ************************ READ CAREFULLY ************************ \n\n"
" ---> Polynomial Equations are Only Allowed To Be Entered in The Following Format <---\n"
"Enter Coefficient of Used Variable (Usually 'x') Followed Directly By the Power of That Variable Inside"
" Parentheses\nFor Example:\n ---> 5x^(2) - 7x^(1) + 3x^(Zero) Written as: 5(2) -7(1) 3(0)\n"
" ---> x^(2) - x^(Zero) Written as: 1(2) -1(0)\n"
"IMPORTANT NOTES:\n * First Example has 3 Terms While Second Example has 2 Terms and so on ... .\n"
" * Spaces Between terms MUST Be Used. Example: 5(2)-7(1)3(Zero) Won't Be Recognised.\n"
" * Order of Terms Doesn't Matter. Example: -7(1) 3(Zero) 5(2) is Allowed.\n");
int degree,number_of_terms = 0,q;
do {
printf("\nDegree of Dividend (Numerator): ");
scanf(" %d",°ree);
if (degree < 0)
printf("\n ---> error <--- \n"
"\n ---> invalid degree, try again <--- \n");
} while (degree < 0);
q = degree;
do {
printf("\nNumber of terms of Dividend (Numerator): ");
scanf(" %d",&number_of_terms);
if (number_of_terms < 0)
printf("\n ---> error <--- \n"
"\n ---> invalid number of terms, try again <--- \n");
} while (number_of_terms < 0);
if (number_of_terms > (degree+1)) {
printf("\n ---> error <--- \n"
"\n ---> number of terms is too large <--- \n");
return -4;
}
double dividend_coefficients[degree+1];
for (int i = 0 ; i < degree+1 ; i++)
dividend_coefficients[i] = 0;
printf("\nDividend (Numerator) Polynomial: ");
for (int i = 0 ; i < number_of_terms ; i++) {
double x=0;int y=-1;
scanf("%lf(%d)",&x,&y);
if (y > degree || y < 0) {
printf("\n ---> error <--- \n"
"\n ---> 'maximum degree exceeded' OR 'index error' OR 'not enough number of terms' <--- \n");
return -2;
}
if (x == 0) {
printf("\n ---> error <--- \n"
"\n ---> coefficient cannot be zero <--- \n");
return -5;
}
if (dividend_coefficients[y] != 0) {
printf("\n ---> error <--- \n"
"\n ---> double coefficients of x^(%d) exists <--- \n",y);
return -3;
}
dividend_coefficients[y] = x;
}
do {
printf("\nDegree of Divisor (Denominator): ");
scanf(" %d",°ree);
if (degree < 0)
printf("\n ---> error <--- \n"
"\n ---> invalid degree, try again <--- \n");
} while (degree < 0);
if (degree > q) {
printf("\n ---> degree of numerator less than that of denominator <--- \n"
"\n ---> Dividend (numerator) / Divisor (denominator) = Remainder <--- \n");
return -1;
}
do {
printf("\nNumber of terms of Divisor (Denominator): ");
scanf(" %d",&number_of_terms);
if (number_of_terms < 0)
printf("\n ---> error <--- \n"
"\n ---> invalid number of terms, try again <--- \n");
} while (number_of_terms < 0);
if (number_of_terms > (degree+1)) {
printf("\n ---> error <--- \n"
"\n ---> number of terms is too large <--- \n");
return -4;
}
double divisor_coefficients[degree+1];
for (int i = 0 ; i < degree+1 ; i++)
divisor_coefficients[i] = 0;
printf("\nDivisor (Denominator) Polynomial: ");
for (int i = 0 ; i < number_of_terms ; i++) {
double x=0;int y=-1;
scanf("%lf(%d)",&x,&y);
if (y > degree || y < 0) {
printf("\n ---> error <--- \n"
"\n ---> 'maximum degree exceeded' OR 'index error' OR 'not enough number of terms' <--- \n");
return -2;
}
if (x == 0) {
printf("\n ---> error <--- \n"
"\n ---> coefficient cannot be zero <--- \n");
return -5;
}
if (divisor_coefficients[y] != 0) {
printf("\n ---> error <--- \n"
"\n ---> double coefficients of x^(%d) exists <--- \n",y);
return -3;
}
divisor_coefficients[y] = x;
}
double results[q+1];
for (int i = 0 ; i < q+1 ; i++)
results[i] = 0;
for (int i = q,j = degree ; i >= j ; i--) {
if (dividend_coefficients[i] == 0)
continue;
double helper[q+1];
for (int k = 0 ; k < q+1 ; k++) {
helper[k] = 0;
}
results[i-j] = dividend_coefficients[i] / divisor_coefficients[j];
for (int p = 0 ; p < degree+1 ; p++) {
if (divisor_coefficients[j-p] == 0)
continue;
helper[i-p] = results[i-j] * divisor_coefficients[j-p];
}
for (int y = 0 ; y < q+1 ; y++) {
dividend_coefficients[i-y] -= helper[i-y];
}
}
printf("\n\nLong Division Result: ");
for (int i = 0 ; i < q+1 ; i++) {
if (results[q-i] == 0)
continue;
printf("%0.1lf(%d) ",results[q-i],q-i);
}
if (dividend_coefficients[0] == 0)
printf("\n");
for (int i = 0 ; i < q+1 ; i++) {
if (dividend_coefficients[i] != 0) {
printf("( ");
for (int s = 0 ; s < q+1 ; s++) {
if (dividend_coefficients[q-s] == 0)
continue;
printf("%0.1lf(%d) ",dividend_coefficients[q-s],q-s);
}
printf("/ ( ");
for (int s = 0 ; s < degree+1 ; s++) {
if (divisor_coefficients[degree-s] == 0)
continue;
printf("%0.1lf(%d) ",divisor_coefficients[degree-s],degree-s);
}
printf(") )\n");
break;
}
}
return 0;
}
int main() {
long_division();
return 0;
}