haunted_mind

float num final final

Dec 4th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #pragma hdrstop
  2. #pragma argsused
  3.  
  4. #ifdef _WIN32
  5. #include <tchar.h>
  6. #else
  7. typedef char _TCHAR;
  8. #define _tmain main
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <iostream>
  13. #include <conio.h>
  14. #include <math.h>
  15.  
  16. using namespace std;
  17.  
  18. int _tmain(int argc, _TCHAR* argv[])
  19. {
  20. float b;
  21. cin >>b;
  22. cout << "b : "<<b<< endl;
  23. char str[51]={'\0'};
  24. float c = 1.4013e-45;
  25. if (b<c) {
  26. str[0]='0';
  27. cout <<"str : "<< str;
  28. getch();
  29. return 0;
  30. }
  31. if (b==10) {
  32. str[0]='1';
  33. str[1]='0';
  34. cout <<"str : "<< str;
  35.  
  36. getch();
  37. return 0;
  38. }
  39. if (b==1) {
  40. str[0]='1';
  41. cout <<"str : "<< str;
  42.  
  43. getch();
  44. return 0;
  45. }
  46.  
  47. //cout << "c : "<<c<< endl;
  48. //cout << "str : "<<str<< endl;
  49.  
  50. float f = b;
  51. int i=0;
  52.  
  53. if ((f>10)||(f<1)) do
  54. {
  55. i++;
  56. f= b>1?f/10:f*10;
  57. //cout << "f "<<i<<" : "<<f<< endl;
  58.  
  59. }while ((f>=10)||(f<1)) ;
  60. f += 5.1e-6;
  61. if(f>=10){ i--; f/=10; }
  62. //cout << "i "<<i<< endl;
  63. str[0]=char((int)f%10+'0');
  64. cout << "str[0] " <<str[0]<< endl;
  65. str[1]='.';
  66. for (int j= 2; j < 7; j++) {
  67. f*=10;
  68. //str[j]=char((int)f%10+'0');
  69. //cout << "str["<<j<<"] : "<<str[j]<< endl;
  70. }
  71. int x=(int) round(f);
  72. cout << "x : "<<x<< endl;
  73. for (int j = 6; j>1; j--) {
  74.  
  75. str[j]=char(x%10+'0'); cout << "str["<<j<<"] : "<<str[j]<< endl;
  76. x/=10;
  77. //cout << "x : "<<x<< endl;
  78. }
  79. /*int j;
  80. for (j= 6; j >1; j--)
  81. {
  82. if (str[j]=='0') {
  83. str[j]='\0';
  84. }
  85. else break;
  86. }
  87. if (j==1)
  88. {
  89. str[1]='\0';
  90. j--;
  91. }
  92. if (i>=6)
  93. {
  94. str[j+1]='e';
  95. if (b>1) {
  96. str[j+2]='+';
  97. }
  98. else str[j+2]='-';
  99. str[j+3]=char(i/10+'0');
  100. str[j+4]=char(i%10+'0');
  101. }
  102. */
  103. int pofe = 0;
  104. int point = 0;
  105. if (i<6) {
  106. if (b>1) {
  107. for (int h = 2; h <=i+1; h++) {
  108. str[h-1]=str[h]; cout << "str[" << h-1 << "] : " << str[h-1] << endl;
  109. }
  110. str[i+1]='.'; point = i+1;
  111. pofe=6;
  112. }
  113. else {
  114.  
  115. str[1]=str[0];
  116. for (int h = 6; h >0; h--)
  117. {
  118. str[h+i]=str[h];
  119. }
  120. str[0]='0';
  121. str[1]='.'; point = 1;
  122. for (int h = 2; h < 1+i; h++)
  123. {
  124. str[h]='0';
  125. }
  126. pofe=6+i;
  127.  
  128. }
  129. }
  130. else {
  131. pofe=6; point = 1;
  132. /* str[7]='e';
  133. if (b>1) {
  134. str[8]='+';
  135. }
  136. else str[j+2]='-';
  137. str[9]=char(i/10+'0');
  138. str[10]=char(i%10+'0'); */
  139.  
  140.  
  141. }
  142. int j;
  143. for (j= pofe; j >point; j--)
  144. {
  145. if (str[j]=='0') {
  146. str[j]='\0';
  147. }
  148. else break;
  149. }
  150. if (j==point)
  151. {
  152. str[point]='\0';
  153. j--;
  154. }
  155. if (i>=6)
  156. {
  157. str[j+1]='e';
  158. if (b>1) {
  159. str[j+2]='+';
  160. }
  161. else str[j+2]='-';
  162. str[j+3]=char(i/10+'0');
  163. str[j+4]=char(i%10+'0');
  164. }
  165.  
  166.  
  167.  
  168. cout << "str " <<str<< endl;
  169. //cout << "f : "<<f<< endl;
  170. getch();
  171. getch();
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment