fork download
  1. /* author: Leonardone @ NEETSDKASU */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. #define BIGNUM_SIZE (60)
  8. #define BIGNUM_FORMAT "%+055.25lf"
  9.  
  10. typedef char BigNum[BIGNUM_SIZE];
  11.  
  12. void initBigNumInt(BigNum num, int value);
  13. void initBigNumDbl(BigNum num, double value);
  14. void addBigNum(BigNum res, const BigNum num1, const BigNum num2);
  15. void subBigNum(BigNum res, const BigNum num1, const BigNum num2);
  16. int beginOfBigNum(const BigNum num);
  17. void printBigNum(const BigNum num);
  18. void mulBigNumInt(BigNum res, const BigNum num, int m);
  19.  
  20. int main(void) {
  21. BigNum n1, n2, n3, n4, n5, n6;
  22.  
  23. initBigNumInt(n1, 12345);
  24. initBigNumDbl(n2, 4.0 * atan(1.0));
  25. initBigNumInt(n3, 0);
  26. initBigNumInt(n4, 0);
  27. initBigNumInt(n5, 0);
  28. initBigNumInt(n6, 0);
  29.  
  30. printf(BIGNUM_FORMAT "\n", atof(n1));
  31. printf(BIGNUM_FORMAT "\n", atof(n2));
  32. printf(BIGNUM_FORMAT "\n", atof(n3));
  33.  
  34. addBigNum(n3, n1, n2);
  35. subBigNum(n4, n1, n2);
  36. addBigNum(n5, n4, n2);
  37. mulBigNumInt(n6, n2, 300);
  38.  
  39. printf(BIGNUM_FORMAT "\n", atof(n3));
  40. printf(BIGNUM_FORMAT "\n", atof(n4));
  41. printf(BIGNUM_FORMAT "\n", atof(n5));
  42. printf(BIGNUM_FORMAT "\n", atof(n6));
  43.  
  44. puts("---------");
  45.  
  46. puts(n1);
  47. puts(n2);
  48. puts(n3);
  49. puts(n4);
  50. puts(n5);
  51. puts(n6);
  52.  
  53. puts("---------");
  54.  
  55. printBigNum(n1); putchar('\n');
  56. printBigNum(n2); putchar('\n');
  57. printBigNum(n3); putchar('\n');
  58. printBigNum(n4); putchar('\n');
  59. printBigNum(n5); putchar('\n');
  60. printBigNum(n6); putchar('\n');
  61.  
  62. return 0;
  63. }
  64.  
  65. int beginOfBigNum(const BigNum num) {
  66. int i;
  67. int len = strlen(num);
  68. for (i = 1; i < len; i++) {
  69. switch (num[i]) {
  70. case '.':
  71. return i - 1;
  72. case '0':
  73. break;
  74. default:
  75. return i;
  76. }
  77. }
  78. return 0;
  79. }
  80.  
  81. void printBigNum(const BigNum num) {
  82. int ps = beginOfBigNum(num);
  83. if (num[0] == '-') {
  84. putchar('-');
  85. }
  86. printf("%s", num + ps);
  87. }
  88.  
  89. void initBigNumInt(BigNum num, int value) {
  90. if (num != NULL) {
  91. sprintf(num, BIGNUM_FORMAT, (double)value);
  92. }
  93. }
  94.  
  95. void initBigNumDbl(BigNum num, double value) {
  96. if (num != NULL) {
  97. sprintf(num, BIGNUM_FORMAT, value);
  98. }
  99. }
  100.  
  101. void addBigNum(BigNum res, const BigNum num1, const BigNum num2) {
  102. int len, d1, d2, d3, f;
  103. if (res == NULL || num1 == NULL || num2 == NULL) {
  104. return;
  105. }
  106. len = strlen(num1) - 1;
  107. f = 0;
  108. while (len > 0) {
  109. d1 = (int)num1[len] - '0';
  110. d2 = (int)num2[len] - '0';
  111. d3 = d1 + d2 + f;
  112. f = d3 / 10;
  113. res[len] = (char)((d3 % 10) + '0');
  114. len--;
  115. if (num1[len] == '.') {
  116. len--;
  117. }
  118. }
  119. }
  120. void subBigNum(BigNum res, const BigNum num1, const BigNum num2) {
  121. int len, d1, d2, d3, f;
  122. if (res == NULL || num1 == NULL || num2 == NULL) {
  123. return;
  124. }
  125. len = strlen(num1) - 1;
  126. f = 0;
  127. while (len > 0) {
  128. d1 = (int)num1[len] - '0';
  129. d2 = (int)num2[len] - '0';
  130. d3 = d1 - d2 - f;
  131. if (d3 < 0) {
  132. d3 += 10;
  133. f = 1;
  134. } else {
  135. f = 0;
  136. }
  137. res[len] = (char)(d3 + '0');
  138. len--;
  139. if (num1[len] == '.') {
  140. len--;
  141. }
  142. }
  143. }
  144.  
  145. void mulBigNumInt(BigNum res, const BigNum num, int m) {
  146. int len, f, d1, d2;
  147. if (res == NULL || num == NULL || m < 0) {
  148. return;
  149. }
  150. len = strlen(num) - 1;
  151. f = 0;
  152. while (len > 0) {
  153. d1 = (int)num[len] - '0';
  154. d2 = d1 * m + f;
  155. f = d2 / 10;
  156. d2 = d2 % 10;
  157. res[len] = (char)(d2 + '0');
  158. len--;
  159. if (num[len] == '.') {
  160. len--;
  161. }
  162. }
  163. }
  164.  
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
+0000000000000000000000012345.0000000000000000000000000
+0000000000000000000000000003.1415926535897931159979635
+0000000000000000000000000000.0000000000000000000000000
+0000000000000000000000012348.1415926535901235183700919
+0000000000000000000000012341.8584073464098764816299081
+0000000000000000000000012345.0000000000000000000000000
+0000000000000000000000000942.4777960769379205885343254
---------
+0000000000000000000000012345.0000000000000000000000000
+0000000000000000000000000003.1415926535897931159979635
+0000000000000000000000012348.1415926535897931159979635
+0000000000000000000000012341.8584073464102068840020365
+0000000000000000000000012345.0000000000000000000000000
+0000000000000000000000000942.4777960769379347993890500
---------
12345.0000000000000000000000000
3.1415926535897931159979635
12348.1415926535897931159979635
12341.8584073464102068840020365
12345.0000000000000000000000000
942.4777960769379347993890500