Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. //Nicholas Jacks
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class bigInt{
  9. private:
  10. vector<int> digits;
  11. public:
  12. bigInt(){ //default constructor vector set to a size of 1; digits = {0};
  13. digits.push_back(0);
  14. }
  15. explicit bigInt(string s){ //converts string type numbers to int type numbers and places them digits 0-9 in their own position within a vector of digits; ex: "123" = {1, 2, 3}
  16. for(int i=0; i<=s.length()-1; i++){
  17. digits.push_back(int(s[i]-'0'));
  18. }
  19. }
  20. explicit bigInt(int n) { //constructor for int to bigInt;
  21. int count=0;
  22. while (n != 0) {
  23. count++;
  24. digits.push_back(n % 10);
  25. n /= 10;
  26. }
  27. int i = 0; //reflector 005 --> 500;
  28. int j = count - 1;
  29. while (j >= count/2){
  30. int temp = digits[j];
  31. digits[j] = digits[i];
  32. digits[i] = temp;
  33. j--;
  34. i++;
  35. }
  36. }
  37. static void swap(bigInt & a, bigInt & b){ //swaps bigInt types;
  38. bigInt temp = a;
  39. a = b;
  40. b = temp;
  41. }
  42.  
  43. static bigInt subtraction(bigInt & op1, bigInt & op2){
  44. bigInt C;
  45. int i = 1;
  46. bool negative = false;
  47. if(op1.digits.size()<op2.digits.size()){
  48. swap(op1,op2);
  49. negative = true;
  50. }
  51. for(int y = i; y<op1.digits.size(); y++){
  52. C.digits.push_back(0);
  53. }
  54. int j = op2.digits.size()-1;
  55. int pos = C.digits.size()-1;
  56. for(int k = op1.digits.size()-1; k>=0; k--){
  57. if(op1.digits[k]>=op2.digits[j] && j>=0){
  58. C.digits[pos] = op1.digits[k]-op2.digits[j];
  59. }
  60. else if(op1.digits[k]<op2.digits[j] && j>=0){
  61. int temp = k-1;
  62. bool found = false;
  63. while(!found){
  64. if(op1.digits[temp]>0){
  65. op1.digits[temp]-=1;
  66. op1.digits[k]+=10;
  67. C.digits[pos] = op1.digits[k]-op2.digits[j];
  68. found = true;
  69. }
  70. else{
  71. op1.digits[temp]+=9;
  72. found = false;
  73. temp--;
  74. }
  75. }
  76. }
  77. else{
  78. C.digits[pos] = op1.digits[k];
  79. }
  80. j--;
  81. pos--;
  82. }
  83. while(C.digits[0]==0){ //avoids 0 in first place; ex: {1, 2}*{1, 2}={0, 1, 4, 4} ---> {1, 4, 4}
  84. bigInt fix;
  85. fix.digits[0] = C.digits[1];
  86. for (int f = 2; f <= C.digits.size() - 1; f++) {
  87. fix.digits.push_back(C.digits[f]);
  88. }
  89. C = fix;
  90. }
  91. return(C);
  92. }
  93.  
  94. static bigInt addition(bigInt & op1, bigInt & op2){
  95. bigInt C;
  96. if(op2.digits.size()>op1.digits.size()){ //op1 size must be >= than op1 size to properly work; op1*op2 <--- good op2*op1 <--- fail
  97. swap(op1,op2);
  98. }
  99. for(int i = 1; i<op1.digits.size()+op2.digits.size()-1; i++){
  100. C.digits.push_back(0);
  101. }
  102. int pos = C.digits.size()-1;
  103. int carry = 0;
  104. int j = op2.digits.size()-1;
  105. int res = 0;
  106. for(int i=op1.digits.size()-1; i>=0; i--){
  107. if(j<0){
  108. res = carry+op1.digits[i];
  109. }
  110. else{
  111. res = carry+op1.digits[i]+op2.digits[j];
  112. }
  113. C.digits[pos] = res%10;
  114. carry = res/10;
  115. j--;
  116. pos--;
  117. if(i==0&&carry>0){
  118. C.digits[pos] = carry;
  119. }
  120.  
  121. }
  122. while(C.digits[0]==0){ //avoids 0 in first place; ex: {1, 2}*{1, 2}={0, 1, 4, 4} ---> {1, 4, 4}
  123. bigInt fix;
  124. fix.digits[0] = C.digits[1];
  125. for (int i = 2; i <= C.digits.size() - 1; i++) {
  126. fix.digits.push_back(C.digits[i]);
  127. }
  128. C = fix;
  129. }
  130. return(C);
  131. }
  132. static bigInt multiply(bigInt & op1, bigInt & op2){ //multiply's two bigInts;
  133. bigInt C;
  134. if(op2.digits.size()>op1.digits.size()){ //op1 size must be >= than op1 size to properly work; op1*op2 <--- good op2*op1 <--- fail
  135. swap(op1,op2);
  136. }
  137. for(int k=1; k<op1.digits.size()+op2.digits.size(); k++){ //sets the result vector to max amount of possible digits due to multiplication with 0 in each place; op1
  138. C.digits.push_back(0); //{1 , 2, 3}*{3, 2, 1} = {0, 0, 0, 0, 0, 0}
  139. }
  140. for(int i=op2.digits.size()-1; i>=0; i--){ //main for() loop running the iteration of multiplication for op2;
  141. int carry = 0;
  142. int carry2 = 0;
  143. for(int j=op1.digits.size()-1;j>=0; j--){ //nested for() loop running the iteration of multiplication for op1;
  144. int pos = i+j+1;
  145. int res = op1.digits[j]*op2.digits[i]+carry;
  146. int res2 = C.digits[pos] + res%10 + carry2;
  147. carry=res/10;
  148. carry2=res2/10;
  149. C.digits[pos] = res2%10;
  150. if(j==0&&(res>=10||res2>=10)){
  151. C.digits[pos-1] = res/10+carry2;
  152. }
  153. }
  154. }
  155. if(C.digits[0] == 0){ //avoids 0 in first place; ex: {1, 2}*{1, 2}={0, 1, 4, 4} ---> {1, 4, 4}
  156. bigInt fix;
  157. fix.digits[0] = C.digits[1];
  158. for(int i = 2; i<=C.digits.size()-1; i++){
  159. fix.digits.push_back(C.digits[i]);
  160. }
  161. C = fix;
  162. }
  163. return(C); //return bigInt product;
  164. }
  165. void print(){ //prints a bigInt number;
  166. int x = digits.size()-1;
  167. for(int i = 0; i<=x; i++){
  168. cout << digits[i];
  169. }
  170. }
  171. static bigInt factorial(int n){
  172. bigInt result(1);
  173. for(int i=2; i<=n; i++){
  174. bigInt num2(i);
  175. result = bigInt::multiply(result,num2);
  176. }
  177. return(result);
  178. }
  179. };
  180.  
  181. int main(){
  182. string s1;
  183. string s2;
  184. bigInt op1;
  185. bigInt op2;
  186. int n=1;
  187. char operand;
  188. cout << "Welcome to the Big Int calculator!\n" << endl;
  189. cout << "Inset an operand. You may pick (+ ! * -)\n";
  190. cin >> operand;
  191. if(operand=='!'){
  192. cout << "Insert a reasonable sized integer" << endl;
  193. cin >> n;
  194. }
  195. else {
  196. cout << "Insert op1\n";
  197. cin >> s1;
  198. cout << "Inset op2\n";
  199. cin >> s2;
  200. bigInt temp(s1);
  201. bigInt temp2(s2);
  202. op1 = temp;
  203. op2 = temp2;
  204. }
  205. bigInt result;
  206. if(operand == '*'){
  207. result = bigInt::multiply(op1,op2);
  208. cout << "Answer = ";
  209. result.print();
  210. }
  211. else if(operand == '!'){
  212. cout << "Answer = ";
  213. result = bigInt::factorial(n);
  214. result.print();
  215. }
  216. else if(operand == '+'){
  217. cout << "Answer = ";
  218. result = bigInt::addition(op1,op2);
  219. result.print();
  220. }
  221. else if(operand == '-'){
  222. cout << "Answer = ";
  223. result = bigInt::subtraction(op1,op2);
  224. result.print();
  225. }
  226. else{
  227. cout << "error with selection";
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement