Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <assert.h>
  6. #define MAX_SIZE 500
  7.  
  8.  
  9.  
  10. typedef char* verylong;
  11.  
  12. verylong input_long(void);
  13. verylong add_verylong(verylong vl1, verylong vl2);
  14. verylong add(verylong ln, verylong sn);
  15. verylong multi(verylong vl1, verylong vl2);
  16. int c2n(char ch);
  17. char n2c(int n);
  18.  
  19.  
  20.  
  21. void main() {
  22. verylong a, b, c;
  23.  
  24. do {
  25. printf("enter the first long integer: ");
  26. a = input_long();
  27. } while (!a);
  28. do {
  29. printf("enter the second long integer: ");
  30. b = input_long();
  31. } while (!b);
  32.  
  33. c = add_verylong(a, b);
  34. printf("%s + %s = %s\n", a, b, c);
  35. c = multiply_verylong(a, b);
  36. printf("%s * %s = %s\n", a, b, c);
  37. free(c);
  38. free(a);
  39. free(b);
  40. //system("pause");
  41. }
  42.  
  43.  
  44.  
  45.  
  46. verylong input_long(void) {
  47. /*
  48. * This function is used to input a 'verylong' number from the user:
  49. */
  50.  
  51. // Setting up a place in the RAM for the number:
  52. verylong res = (verylong)malloc(sizeof(char) * MAX_SIZE);
  53. assert(res);
  54. char dig;
  55. int i = 0;
  56.  
  57. // Getting the number from the user:
  58. gets(res);
  59.  
  60. // Iterating through 'res' until we reach NULL:
  61. for (i = 0; *(res + i) != '\0'; i++) {
  62. dig = *(res + i);
  63. if ((dig > '9') || (dig < '0')) {
  64. printf("wrong input");
  65. return NULL;
  66. }
  67. }
  68.  
  69. // Finally, returning the place in the memory the correct number is located in, after resizing it and asserting it:
  70. res = (verylong)realloc(res, sizeof(char) * (strlen(res) + 1));
  71. assert(res);
  72. return res;
  73. }
  74.  
  75.  
  76.  
  77.  
  78. verylong add_verylong(verylong vl1, verylong vl2) {
  79. /*
  80. * Since the logic 'add' function only handles the option in which vl1.size >= vl2.size, we use this function to invoke 'add_vl' properly:
  81. */
  82. if (strlen(vl1) > strlen(vl2)) add_vl(vl1, vl2);
  83. else add(vl2, vl1);
  84. }
  85.  
  86.  
  87.  
  88. verylong multiply_verylong(verylong vl1, verylong vl2) {
  89. /*
  90. * Since the logic 'multi' function only handles the option in which vl1.size >= vl2.size, we use this function to invoke 'add_vl' properly:
  91. */
  92. if (strlen(vl1) > strlen(vl2)) add_vl(vl1, vl2);
  93. else add(vl2, vl1);
  94. }
  95.  
  96.  
  97.  
  98. verylong add(verylong ln, verylong sn) {
  99. /*
  100. * This function calculates the sum between numbers when ln is longer than sn physically.
  101. */
  102. int i, j, longerlen, shorterlen, len1 = strlen(ln), len2 = strlen(sn), pow = 1, carry = 0, sum;
  103.  
  104. // Allocating memory space for the result: (the size will be the longer number's size +2 for nullchar and carry)
  105. verylong res = (verylong)calloc(sizeof(char), (len1 + 2)), res_opt = (verylong)calloc(sizeof(char), (len1 + 1));
  106.  
  107. // Adding each number exactly like decimal addition:
  108. for (i = len1, j = len2; i > 0; i--, j--) {
  109. if (j > 0) {
  110. sum = c2n(*(ln + -1 + i)) + c2n(*(sn + j - 1)) + carry;
  111. *(res + i) = n2c((sum % 10));
  112. carry = sum / 10;
  113. }
  114. else {
  115. sum = c2n(*(ln + i - 1)) + carry;
  116. *(res + i) = n2c((sum % 10));
  117. carry = sum / 10;
  118. }
  119. }
  120. *res = '0' + carry * 1;
  121. *(res + len1 + 1) = '\0';
  122. if (*res == '0') {
  123. strcpy(res_opt, res + 1);
  124. return res_opt;
  125. }
  126.  
  127.  
  128. return res;
  129. }
  130.  
  131.  
  132.  
  133.  
  134. verylong multi(verylong ln, verylong sn) {
  135. /*
  136. * This function calculates the multiplication between numbers when ln is longer than sn physically.
  137. */
  138. int i, pow = 1, temp, j, len2;
  139. // Allocating memory for the reuslt: (the size will be the size of vl1) (this will be changed in future runs)
  140. verylong res = (verylong)calloc(sizeof(char), (strlen(ln) + 1));
  141. assert(res);
  142.  
  143. *res = '0';
  144. *(res + 1) = '\0';
  145. len2 = strlen(sn);
  146.  
  147. for (i = len2; i >= 0; i--) {
  148. // Calculating the temporary vl2 as a number one place at a time:
  149. temp = c2n(*(ln + i - 1)) * pow;
  150.  
  151. // Multiplying vl1 by vl2 one place at a time:
  152. for (j = 0; j < temp; j++) {
  153. res = (add_verylong(res, sn));
  154. }
  155.  
  156. // Going up the places:
  157. pow = pow * 10;
  158. }
  159.  
  160. // Finally, returning the multiplication result:
  161. return res;
  162. }
  163.  
  164.  
  165.  
  166.  
  167. /*
  168. * Those are optional helping functions that help us calculate and convert from int to char and vice versa:
  169. */
  170. int c2n(char ch) {
  171. /*
  172. * This function converts a charnum into a real int:
  173. * (does not have input checking)
  174. */
  175.  
  176. return (ch - '0');
  177. }
  178.  
  179.  
  180.  
  181.  
  182. char n2c(int n) {
  183. /*
  184. * This function converts a charnum into a real int:
  185. * (does not have input checking)
  186. */
  187.  
  188. return (n + '0');
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement