Repository navigation
Expand file tree
/
Copy pathPG1.cpp
More file actions
194 lines (155 loc) · 5.22 KB
/
Copy pathPG1.cpp
File metadata and controls
194 lines (155 loc) · 5.22 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// AUTHOR: Cole Walgren
// TITLE: Program 1
// DESCRIPTION: User enters a number they wish to square root, as well as the
// number of decimal places they wish the final answer to be set to.
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include "PG1.h"
using namespace std;
void trim(string& a) {
// takes "a" and removes any zeros at the beginning of the string
while (a.length() > 1 && a[0] == '0') a = a.substr(1);
}
string add(string a, string b) {
// takes two strings, trims them both, then arithmetically adds
// them together
trim(a);
trim(b);
while (a.length() < b.length()) a = "0" + a;
while (b.length() < a.length()) b = "0" + b;
a = "0" + a; //We put an extra zero at the beginning just in case
b = "0" + b; //we end with a carry
string ans = "";
int carry = 0;
for (int i = (int) a.length() - 1; i >= 0; i--) {
int sum = (a[i] - '0') + (b[i] - '0') + carry;
ans = (char)(sum % 10 + '0') + ans;
carry = sum / 10;
}
trim(ans);
return ans;
}
string subtract(string a, string b) {
// takes two strings, trims them both, then arithmetically
// subtracts "b" from "a"
trim(a);
trim(b);
while (a.length() < b.length()) a = "0" + a;
while (b.length() < a.length()) b = "0" + b;
for (int i = 0; i < b.length(); i++)
b[i] = (char)('9' - b[i] + '0'); //Finding 9's complement
string diff = add(add(a, b), "1").substr(1);
trim(diff);
return diff;
}
string multiply(string a, string b) {
// takes two strings, trims them both, then arithmetically
// multiplies them together
trim(a);
trim(b);
string sum = "0";
for (int i=0; i < a.length(); i++)
for (int j = 0; j < b.length(); j++) {
int prod = (a[i] - '0') * (b[j] - '0');
string addend = to_string(prod);
for (int k = 0; k < a.length() - 1 - i + b.length() - 1 - j; k++)
addend += "0";
sum = add(sum, addend);
}
return sum;
}
int compare(string a, string b){
// takes two strings, trims them both, then compares them by length
// first, then compares them by individual character second
// returns 1 if a is greater
// returns 2 if b is greater
// returns 0 if equal
trim(a);
trim(b);
if (a.length() > b.length()) return 1;
else if (a.length() < b.length()) return 2;
for (int i = 0; i < a.length() - 1; i++) {
if (a[i] > b[i]) return 1;
else if (a[i] < b[i]) return 2;
}
return 0;}
string sqrt(string n, int z){
// n = original value you wish to sqrt
// z = zeros (# of decimal places)
// takes a string "n", and using the twos compliment method,
// finds the square root of n, to "z" decimal places
z = z*2;
for(int i = 0; i < z; i++){
n = n + '0';
}
//Build fours array
int foursLength = n.length() * 2;
string * fours = new string[foursLength];
fours[0] = "1";
int foursCt = 0;
for(int i = 1; i < foursLength; i++){
string foursVal = multiply(fours[i-1],"4");
if(compare(foursVal, n) == 1){
break;
}else{
fours[i] = foursVal;
foursCt ++;
}
}
//Begins solving process, cycles through fours array backwards
// if n - (fours[i] * (4*ans+1)) works, replace ans with (2*ans+1)
// else replace ans with (2*ans)
string ans = "0";
for (int i = foursCt; i >= 0; i--){ //cycles through fours array backwards
string x = multiply("4",ans);
string y = add(x,"1");
string val = multiply(fours[i],y);
if (compare(n,val) != 2){
string sub = subtract(n,val);
string ans2 = multiply(ans,"2");
ans = add(ans2,"1");
n = sub;
}else{
ans = multiply(ans,"2");
}
}
//Memory deallocation
delete[] fours;
return ans;
}
string addZeros(string answer, int zeros){
// takes "answer", seperates into two substrings at index
// given by "zeros", then inserts a dot inbetween them,
// then finally appends the first substring onto the "." onto the
// second substring
int pos = answer.length()-zeros;
string sub1 = answer.substr(0, pos);
string sub2 = answer.substr(pos, answer.length());
string fin = sub1 + "." + sub2;
return fin;
}
int main(int argc, const char * argv[]) {
string value;
string value2;
cout << "Enter the number you wish to take the square root of: " << endl;
getline(cin, value);
int zeros = 0;
cout<< "Enter the amount of digits you would like: " << endl;
cin >> zeros;
string t = "0.";
if(value == "0"){
for(int i = 0; i < zeros; i++){
t = t + "0";
}
cout << "Square root of "<<value<<" to "<<zeros<<" digits is "<<t<<endl;
return 0;
}
string answer = sqrt(value, zeros);
string done = addZeros(answer, zeros);
cout << "Square root of "<<value<<" to "<<zeros<<" digits is "<<done<<endl;
return 0;
}