Advertisement
Guest User

dumb

a guest
Dec 7th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class BigInt {
  8. public:
  9. BigInt(); //Initializes the BigInt to zero
  10. BigInt(int x);//Initializes the BigInt to have the same value as x
  11. explicit BigInt(string x);//Initializes the BigInt to have the value of the given string
  12.  
  13. friend ostream& operator<<(ostream& out, const BigInt& right);
  14. friend BigInt operator+(const BigInt& left, const BigInt& right);
  15. friend BigInt operator-(const BigInt & left, const BigInt & right);
  16. friend string addition_helper(string one, string two);
  17. friend string subtraction_helper(string one, string two);
  18. friend bool operator<(const BigInt& left, const BigInt& right);
  19. private:
  20. string data;
  21. bool isNegative;
  22. };
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. #include <iostream>
  41. #include <string>
  42. using namespace std;
  43. #include "BigInt.h"
  44.  
  45. int main() {
  46. cout << "This works here" << endl;
  47. BigInt a(-1000);
  48. cout << "This works here" << endl;
  49.  
  50. BigInt b("+999");
  51. cout << "This works here" << endl;
  52.  
  53. cout << b << "+" << a << endl << "= " << b + a << endl;
  54. a = BigInt("1000");
  55. b = BigInt("999");
  56. cout << a << "-" << b << endl << "= " << a - b << endl;
  57. a = BigInt("99999999999999999999999999999999999999999999999999999999999999999999999999999999");
  58. b = BigInt("1");
  59. cout << " " << a << "+" << b << endl << "= " << a + b << endl;
  60. cout << "Expecting 1, got: " << (BigInt("-30") < BigInt("-1")) << endl;
  61. cout << "Expecting 0, got: " << (BigInt("30") < BigInt("-1")) << endl;
  62. cout << "Expecting 1, got: " << (BigInt("30") < BigInt("300")) << endl;
  63. cout << "Expecting 0, got: " << (BigInt("-30") < BigInt("-300")) << endl;
  64. return 0;
  65. }
  66.  
  67. BigInt::BigInt() {
  68. isNegative = false;
  69. data = "0";//format for a string
  70. }
  71.  
  72. BigInt::BigInt(int x) {
  73. if (x < 0) {
  74. isNegative = true;
  75. }
  76. else {
  77. isNegative = false;
  78. }
  79. data = to_string(x);
  80. }
  81.  
  82. BigInt::BigInt(string x) {
  83. string y;//for necessary input for what we need from x
  84. int i = 0;
  85. while (iswspace(x[i])) {
  86. i++;
  87. }
  88. if (x[i] == '-') {
  89. isNegative = true;
  90. y.insert(y.begin(), x[i]);
  91. }
  92. else if (x[i] == '+') {
  93. isNegative = false;
  94. }
  95. else if (isdigit(x[i])) {
  96. isNegative = false;
  97. y.insert(y.begin(), x[i]);
  98. }
  99. else {
  100. cout << "tried to construct an invalid BigInt from string:" << endl;
  101. cout << x << endl;
  102. cout << "exiting";
  103. exit(1);
  104. }
  105. i++;
  106. while (!iswspace(x[i])){
  107. if (isdigit(x[i])) {
  108. y.insert(y.end(), x[i]);
  109. }
  110. else {
  111. cout << "tried to construct an invalid BigInt from string:" << endl;
  112. cout << x << endl;
  113. cout << "exiting";
  114. exit(1);
  115. }
  116. i++;
  117. }
  118. data = y;
  119. }
  120.  
  121. ostream& operator<<(ostream& out, const BigInt& right) {
  122. return out << right.data;
  123. }
  124.  
  125. bool operator<(const BigInt& left, const BigInt& right) {
  126. int i = 0;
  127. //if (left.data.length() > right.data.length()) { //comparing sizes
  128. // swap(left, right);
  129. //}
  130. // comparing signs
  131. if (left.isNegative == true && right.isNegative == false) {//if the left is negative and the right is positive
  132. return true;
  133. }
  134. else if (left.isNegative == false && right.isNegative == true) { //if the left is negative and the right is positive
  135. return false;
  136. }
  137. else if (left.isNegative == true && right.isNegative == true) {// if both are negative
  138. //comparing sizes of each
  139. if (left.data.size() > right.data.size()) {
  140. return true;
  141. }
  142. else if (left.data.size() < right.data.size()) {
  143. return false;
  144. }
  145. while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
  146. if (left.data[i] != right.data[i]) { //if they are differences spotted
  147. if (left.data[i] > right.data[i]) {
  148. return true;
  149. }
  150. else if (left.data[i] < right.data[i]) {
  151. return false;
  152. }
  153. if (left.data[i] == right.data[i]) {
  154. return false;
  155. }
  156. }
  157.  
  158. }
  159. }
  160. else if (!left.isNegative && !right.isNegative) {
  161. //check for different lengths
  162. if (left.data.size() < right.data.size()) {
  163. return true;
  164. }
  165. if (left.data.size() > right.data.size()) {
  166. return false;
  167. }
  168. while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
  169. if (left.data[i] != right.data[i]) { //if they are differences spotted
  170. if (left.data[i] < right.data[i]) {
  171. return true;
  172. }
  173. if (left.data[i] > right.data[i]) {
  174. return false;
  175. }
  176. else if (left.data[i] == right.data[i]) {
  177. return false;
  178. }
  179. }
  180.  
  181. }
  182. }
  183. }
  184.  
  185. string addition_helper(string one, string two) {
  186. int carry = 0; //carry each calculation
  187. /*if (one.length() > two.length()) {
  188. swap(one, two);
  189. }*/
  190. string data = "";//to put the final sum in after adding the first two together
  191. int total1 = one.size() - 1;
  192. int total2 = two.size() - 1;
  193. while (total1 > 0 && total2 > 0) {
  194. int sum = ((one[total1] - '0') + (two[total2] - '0')) + carry;// Calculate the sum of digits by adding the previous carry.
  195. //take the number and subtract it by the character zero. DO it to the other number
  196. //add each other
  197. data.push_back((sum % 10) + '0');//push the result onto the string
  198. carry = sum / 10; // change carry for the next numbers
  199. total1--;//moving left towards the next numbers
  200. total2--;//moving left towards the next numbers
  201. }
  202. while (total1 >= 0) {
  203. int sum = (one[total1] - '0') + carry; // Add digits to the carry and store the carry
  204. data.push_back((sum / 10) + '0');
  205. carry = sum / 10;//update carry
  206. total1--;
  207. }
  208. while (total2 >= 0) { // Add digits to the carry and store the carry
  209. int sum = (two[total2] - '0') + carry;
  210. data.push_back((sum / 10) + '0');
  211. carry = sum / 10;//update carry
  212. total2--;
  213. }
  214. if (carry != 0) { //If after finishing adding two numbers, if there is a leftover carry, take it into the final sum.
  215. data.push_back(carry % 10 + '0'); // store onto stack
  216. return data;
  217.  
  218. }
  219. }
  220.  
  221.  
  222. string subtraction_helper(string one, string two) {
  223. int borrow = 0; //carry each calculation
  224. if (one.length() > two.length()) {
  225. swap(one, two);
  226. }
  227. string data = "";//to put the FINAL sum in after adding the first two together
  228. int total1 = one.size() - 1;
  229. int total2 = two.size() - 1;
  230. while (total1 > 0 && total2 > 0) {
  231. int difference = ((one[total1] - '0') - (two[total2] - '0'));//subtract '0' from each integer in the string
  232. //and add carry which is 0 unless otherwise
  233. while (total1 >= 0) {
  234. int difference = ((one[total1] - '0') - borrow);
  235. if (difference < 0){
  236. difference = difference + 10;
  237. borrow = 1;
  238. }
  239. else {
  240. borrow = 0;
  241. data.push_back(difference + '0');
  242. }
  243. }
  244. while (total2 >= 0) { // Add digits to the carry and store the carry
  245. int difference = ((two[total2] - '0') - borrow);
  246. if (difference < 0){
  247. difference = difference + 10;
  248. borrow = 1;
  249. }
  250. else {
  251. borrow = 0;
  252. data.push_back(difference + '0');
  253. }
  254. }
  255. if (difference < 0) { // If subtraction is less then zero
  256. difference = difference + 10;// add 10 for borrowing
  257. borrow = 1;//carry = 1 because borrowed
  258. }
  259. else {
  260. borrow = 0;//no borrowing needed
  261. data.push_back(difference + '0');
  262. }
  263. }
  264. return data;
  265.  
  266. }
  267.  
  268. BigInt operator+(const BigInt& left, const BigInt& right) {
  269. string result = addition_helper(left.data, right.data);
  270. return BigInt(result);
  271. }
  272.  
  273. BigInt operator-(const BigInt& left, const BigInt& right) {
  274. string result = subtraction_helper(left.data, right.data);
  275. return BigInt(result);
  276. }
  277.  
  278. //
  279. //bool operator<(const BigInt& left, const BigInt& right){
  280. // int i = 0;
  281. // //if (left.data.length() > right.data.length()) { //comparing sizes
  282. // // swap(left, right);
  283. // //}
  284. // // comparing signs
  285. // if (left.isNegative == true && right.isNegative == false) {//if the left is negative and the right is positive
  286. // return true;
  287. // }
  288. // else if (left.isNegative == false && right.isNegative == true) { //if the left is negative and the right is positive
  289. // return false;
  290. // }
  291. // else if (left.isNegative == true && right.isNegative == true) {// if both are negative
  292. // //comparing sizes of each
  293. // if (left.data.size() > right.data.size()) {
  294. // return true;
  295. // }
  296. // else if (left.data.size() < right.data.size()) {
  297. // return false;
  298. // }
  299. // while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
  300. // if (left.data[i] != right.data[i]) { //if they are differences spotted
  301. // if (left.data[i] > right.data[i]) {
  302. // return true;
  303. // }
  304. // else if (left.data[i] < right.data[i]) {
  305. // return false;
  306. // }
  307. // if (left.data[i] == right.data[i]) {
  308. // return false;
  309. // }
  310. // }
  311. //
  312. // }
  313. // }
  314. // else if (!left.isNegative && !right.isNegative) {
  315. // //check for different lengths
  316. // if (left.data.size() < right.data.size()) {
  317. // return true;
  318. // }
  319. // if (left.data.size() > right.data.size()) {
  320. // return false;
  321. // }
  322. // while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
  323. // if (left.data[i] != right.data[i]) { //if they are differences spotted
  324. // if (left.data[i] < right.data[i]) {
  325. // return true;
  326. // }
  327. // if (left.data[i] > right.data[i]) {
  328. // return false;
  329. // }
  330. // else if (left.data[i] == right.data[i]) {
  331. // return false;
  332. // }
  333. // }
  334. //
  335. // }
  336. // }
  337. //}
  338.  
  339. //BigInt operator+(const BigInt& left, const BigInt& right) {
  340. // string result = addition_helper(left.data, right.data);
  341. // return BigInt(result);
  342. //}
  343. //
  344. //BigInt operator-(const BigInt& left, const BigInt& right) {
  345. // string result = subtraction_helper(left.data, right.data);
  346. // return BigInt(result);
  347. //}
  348.  
  349.  
  350. //to do addition BigInt
  351. //reverse the two strings
  352. //comapre the size of each string
  353. //start from the back
  354. //take the two numbers
  355. //subtract the number from character zero
  356. //do it with the second string
  357. //add the two numbers and mod(%) by 10 if you need to carry over a 1
  358. //repeat this adn return the answer in BigInt
  359.  
  360.  
  361.  
  362.  
  363. //to do subtraction operator
  364. //comapre the size of each string
  365. //reverse the two strings
  366. //start from the back
  367. //take the two numbers
  368. //subtract the number from character zero
  369. //do it with the second string
  370. //if str1 < str2 , add 10 to str 2 element and subtract one from second element in the reverse string for str1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement