fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdint.h>
  5. #include <float.h>
  6.  
  7. //#pragma warning(disable : 4996)
  8.  
  9. #define L 1024
  10.  
  11. static const char Digit[] = "0123456789";
  12.  
  13. int GetDigit(int64_t N){
  14. int i = 0;
  15. while (N != 0){
  16. i++;
  17. N /= 10;
  18. }
  19.  
  20. return i;
  21. }
  22.  
  23. int MakeUpper(char Out[L], double N){
  24. int64_t U = (int64_t)floor(N);
  25. int D = GetDigit(U);
  26. int V = 0;
  27.  
  28. if (U == 0){
  29. Out[0] = Digit[0];
  30. Out[1] = '\0';
  31. return 0;
  32. }
  33.  
  34. Out[D] = '\0';
  35. D--;
  36.  
  37. while (U != 0){
  38. V = U % 10;
  39. Out[D] = Digit[V];
  40. D--;
  41. U /= 10;
  42. }
  43.  
  44. return 0;
  45.  
  46. }
  47.  
  48. int MakeBottom(char Out[L], double N){
  49. double U = floor(N);
  50. double Diff = 1;
  51. double P = 1;
  52. int i = 0;
  53. int j = 0;
  54. char Z[L] = "\0";
  55. char B[L] = "\0";
  56. while ((Diff) > DBL_EPSILON){
  57. P *= 10;
  58. Diff = (N-U)*P - floor((N-U)*P);
  59. }
  60. for (i = 10; i < P; i *= 10){
  61. if ((N - U) <= DBL_EPSILON) break;
  62. if (floor((N-U)*i) > 0) break;
  63. Z[j] = Digit[0];
  64. j++;
  65. }
  66. Z[j] = '\0';
  67.  
  68. MakeUpper(B, (N-U)*P);
  69. strcat(Out, Z);
  70. strcat(Out, B);
  71. return 0;
  72. }
  73.  
  74. int MakeHoge(char Out[L], double N){
  75. char U[L] = "\0";
  76. char D[L] = "\0";
  77.  
  78. MakeUpper(U, N);
  79. MakeBottom(D, N);
  80. Out[0] = '\0';
  81.  
  82. if (N < 0) strcat(Out, "-");
  83. strcat(Out, U);
  84. strcat(Out, ".");
  85. strcat(Out, D);
  86.  
  87. return 0;
  88. }
  89.  
  90. int OutPut(char B[L]){
  91. int i = 0;
  92. for (i = 0; i < L; i++){
  93. if (B[i] == '\0') break;
  94. putchar(B[i]);
  95. }
  96. putchar('\n');
  97. return 0;
  98. }
  99.  
  100. int main(){
  101.  
  102. char Out[L];
  103. MakeHoge(Out, 123456789.0123456789);
  104. OutPut(Out);
  105. MakeHoge(Out,0.031415926);
  106. OutPut(Out);
  107. MakeHoge(Out,0.31415926);
  108. OutPut(Out);
  109. MakeHoge(Out,3.1415926);
  110. OutPut(Out);
  111. MakeHoge(Out,31.415926);
  112. OutPut(Out);
  113. MakeHoge(Out,314.15926);
  114. OutPut(Out);
  115. MakeHoge(Out,0.00031415926);
  116. OutPut(Out);
  117. MakeHoge(Out,0.0);
  118. OutPut(Out);
  119. return 0;
  120. }
Success #stdin #stdout 0s 2296KB
stdin
Standard input is empty
stdout
123456789.0
0.0
0.31415926000000000
3.1415926000000000768
31.415925999999998912
314.159260000000017504
0.00031415926000000000
0.0