Advertisement
Pweebs

Untitled

Nov 14th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. // Jevin Olano
  2. // jolano
  3. // CSE 101
  4. // November 9, 2019
  5. // Arithmetic.c
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "List.h"
  10. #include "BigInteger.h"
  11.  
  12. // helper
  13. void readBigInteger(FILE* in, char *dest, int size) {
  14. dest[0] = fgetc(in);
  15. int i = 1;
  16. if (dest[0] == '-') {
  17. size++;
  18. }
  19. for (; i < size; i++) {
  20. dest[i] = fgetc(in);
  21. }
  22. dest[size] = '\0';
  23. printf("%s\n", dest);
  24. }
  25.  
  26. int main(int argc, char* argv[]) {
  27.  
  28. // Declarations -------------------------------------
  29.  
  30. FILE *in, *out; // handlers for input/output files
  31. char line[256];
  32. char stringA[256];
  33. char stringB[256];
  34. int size = 0;
  35.  
  36. // Preliminary --------------------------------------
  37.  
  38. // check command line for correct number of arguments
  39. if (argc != 3) {
  40. printf("Usage: %s <input> <output>\n", argv[0]);
  41. exit(1);
  42. }
  43.  
  44. // open input file for reading
  45. in = fopen(argv[1], "r");
  46. if (in == NULL) {
  47. printf("Unable to read from file %s\n", argv[1]);
  48. exit(1);
  49. }
  50.  
  51. // open output file for writing
  52. out = fopen(argv[2], "w");
  53. if (out == NULL) {
  54. printf("Unable to write to file %s\n", argv[2]);
  55. exit(1);
  56. }
  57.  
  58. // Arithmetic Operations ----------------------------
  59.  
  60. fgets(line, sizeof(line), in); // 1st and 2nd line of input
  61. sscanf(line, "%d", &size);
  62. printf("Size of string A: %d\n", size);
  63. readBigInteger(in, stringA, size);
  64.  
  65. fgets(line, sizeof(line), in); // 3rd and 4th line of input
  66. fgets(line, sizeof(line), in);
  67. sscanf(line, "%d", &size);
  68. printf("Size of string B: %d\n", size);
  69. readBigInteger(in, stringB, size);
  70.  
  71. BigInteger A = stringToBigInteger(stringA);
  72. printBigInteger(stdout, A);
  73. printf("Successfully assigned BigInteger A\n");
  74. BigInteger B = stringToBigInteger(stringB);
  75. printBigInteger(stdout, B);
  76. printf("Successfully assigned BigInteger B\n");
  77.  
  78. // print A
  79. //BigInteger answer1 = A;
  80. printf("assigned answer1\n");
  81. printBigInteger(out, A);
  82. printf("freed answer1\n");
  83. fprintf(out, "\n");
  84.  
  85. // print B
  86. //BigInteger answer2 = B;
  87. printf("assigned answer2\n");
  88. printBigInteger(out, B);
  89. printf("freed answer2\n");
  90. fprintf(out, "\n");
  91.  
  92. // print A + B
  93. BigInteger answer3 = sum(A, B);
  94. printf("assigned answer3\n");
  95. printBigInteger(out, answer3);
  96. //free(&answer3);
  97. printf("freed answer3 (commented out)\n");
  98. fprintf(out, "\n");
  99.  
  100. // print A - B
  101. BigInteger answer4 = diff(A, B);
  102. printf("assigned answer4\n");
  103. printBigInteger(out, answer4);
  104. //free(&answer4);
  105. printf("freed answer4 (commented out)\n");
  106. fprintf(out, "\n");
  107.  
  108. // print A - A
  109. BigInteger answer5 = diff(A, A);
  110. printf("assigned answer5\n");
  111. printBigInteger(out, answer5);
  112. freeBigInteger(&answer5);
  113. printf("freed answer5\n");
  114. fprintf(out, "\n");
  115.  
  116. // print 3A - 2B
  117. BigInteger three = stringToBigInteger("3");
  118. printf("assigned three\n");
  119. BigInteger two = stringToBigInteger("2");
  120. printf("assigned two\n");
  121. BigInteger prodA = prod(A, three);
  122. printf("assigned prodA\n");
  123. BigInteger prodB = prod(B, two);
  124. printf("assigned prodB\n");
  125. BigInteger answer6 = diff(prodA, prodB);
  126. printf("assigned answer6\n");
  127. printBigInteger(out, answer6);
  128. freeBigInteger(&three);
  129. printf("freed three\n");
  130. freeBigInteger(&two);
  131. printf("freed two\n");
  132. freeBigInteger(&prodA);
  133. printf("freed prodA\n");
  134. freeBigInteger(&prodB);
  135. printf("freed prodB\n");
  136. freeBigInteger(&answer6);
  137. printf("freed answer6\n");
  138. fprintf(out, "\n");
  139.  
  140. // print AB
  141. BigInteger answer7 = prod(A, B);
  142. printf("assigned answer7\n");
  143. printBigInteger(out, answer7);
  144. //free(&answer7);
  145. printf("freed answer7 (commented out)\n");
  146. fprintf(out, "\n");
  147.  
  148. // print A^2
  149. BigInteger answer8 = prod(A, A);
  150. printf("assigned answer8\n");
  151. printBigInteger(out, answer8);
  152. printf("freed answer8\n");
  153. fprintf(out, "\n");
  154.  
  155. // print B^2
  156. BigInteger answer9 = prod(B, B);
  157. printf("assigned answer9\n");
  158. printBigInteger(out, answer9);
  159. printf("freed answer9\n");
  160. fprintf(out, "\n");
  161.  
  162. // print 9A^4 + 16B^5
  163. BigInteger fourthPower1 = prod(answer8, answer8);
  164. printf("assigned fourthPower1\n");
  165. BigInteger fourthPower2 = prod(answer9, answer9);
  166. printf("assigned fourthPower2\n");
  167. BigInteger fifthPower = prod(answer9, B);
  168. printf("assigned fifthPower\n");
  169. BigInteger nine = stringToBigInteger("9");
  170. printf("assigned nine\n");
  171. BigInteger sixteen = stringToBigInteger("16");
  172. printf("assigned sixteen\n");
  173. BigInteger partA = prod(fourthPower1, nine);
  174. printf("assigned partA\n");
  175. BigInteger partB = prod(fifthPower, sixteen);
  176. printf("assigned partB\n");
  177. BigInteger answer10 = sum(partA, partB);
  178. printf("assigned answer10\n");
  179. printBigInteger(out, answer10);
  180. freeBigInteger(&answer8);
  181. printf("freed answer8\n");
  182. freeBigInteger(&answer9);
  183. printf("freed answer9\n");
  184. freeBigInteger(&fourthPower1);
  185. printf("freed fourthPower1\n");
  186. freeBigInteger(&fourthPower2);
  187. printf("freed fourthPower2\n");
  188. freeBigInteger(&fifthPower);
  189. printf("freed fifthPower\n");
  190. freeBigInteger(&nine);
  191. printf("freed nine\n");
  192. freeBigInteger(&sixteen);
  193. printf("freed sixteen\n");
  194. freeBigInteger(&partA);
  195. printf("freed partA\n");
  196. freeBigInteger(&partB);
  197. printf("freed partB\n");
  198. freeBigInteger(&answer10);
  199. printf("freed answer10\n");
  200. fprintf(out, "\n");
  201.  
  202. return(EXIT_SUCCESS);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement