fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. std::string printSizeApproximately(uint64_t n) {
  6. /* 0 */ /* 1 */ /* 2 */ /* 3 */ /* 4 */
  7. static std::string Suffix[] = { std::string("Bytes"), std::string("KiB"), std::string("MiB"), std::string("GiB"), std::string("TiB") };
  8. uint64_t t = n;
  9. uint64_t s = 1;
  10. uint64_t r;
  11. int i = 0;
  12. for (;;) {
  13. r = t;
  14. t >>= 10;
  15.  
  16. /* out of loop : 2 conditions */
  17. if (t == 0)
  18. break;
  19. if (i == 4)
  20. break;
  21.  
  22. s <<= 10;
  23. i++;
  24. }
  25. std::stringstream ss;
  26. if (i == 0) {
  27. ss << n << " " << Suffix[i];
  28. } else {
  29. if (r >= 1 && r < (int)(1024 / 100.0)) { /* 1.52KiB */
  30. ss << (int)(n * 100.0 / s) / 100.0 << ' ' << Suffix[i];
  31. } else if (r >= (int)(1024 / 100.0) && r < (int)(1024 / 10.0)) { /* 15.2KiB */
  32. ss << (int)(n * 10.0 / s) / 10.0 << ' ' << Suffix[i];
  33. } else { /* r >= (int)(1024 / 10.0 152KiB */
  34. ss << (int)((double)n / s) << ' ' << Suffix[i];
  35. }
  36. }
  37. std::string output_s = ss.str();
  38. return output_s;
  39. }
  40.  
  41. int main() {
  42. uint64_t n;
  43. n = 555; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  44. n = 1123; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* KiB */
  45. n = 18234; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  46. n = 252344; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  47. n = 3252344; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* MiB */
  48. n = 73241344; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  49. n = 697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  50. n = 9697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* GiB */
  51. n = 39697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  52. n = 439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  53. n = 6439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* TiB */
  54. n = 36439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  55. n = 236439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  56. n = 8236439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  57. n = 38236439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
  58. return 0;
  59. }
  60. /* end */
  61.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
n = 555 : 555 Bytes
n = 1123 : 1.09 KiB
n = 18234 : 17.8 KiB
n = 252344 : 246 KiB
n = 3252344 : 3.1 MiB
n = 73241344 : 69.8 MiB
n = 697351323 : 665 MiB
n = 9697351323 : 9.03 GiB
n = 39697351323 : 36.9 GiB
n = 439697351323 : 409 GiB
n = 6439697351323 : 5.85 TiB
n = 36439697351323 : 33.1 TiB
n = 236439697351323 : 215 TiB
n = 8236439697351323 : 7490 TiB
n = 38236439697351323 : 34775 TiB