alansam

TireSlot

Dec 10th, 2020 (edited)
715
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <array>
  6. #include <vector>
  7. #include <cstddef>
  8. #include <cmath>      //  TODO: C++ needs cmath not math.h
  9. #include <memory>
  10.  
  11. //using namespace std;  //  TODO: don't do this, it's a bad habit!
  12.  
  13. char constexpr SS = '/';  //  TODO: it is a constant, make it so
  14.  
  15. class TireSlot {  //  TODO: keep class definition neat
  16. public:
  17.   std::string Size;
  18.   std::array<float, 3> SizeDblArr {};
  19.   float TireWidth;
  20.   float TireaSpect;
  21.   float TireRim;
  22.   char  Src;
  23.   int   Amt;
  24.   int   AmtS;
  25. };
  26.  
  27. float constexpr WAr[/*29*/] { //  TODO: it is a constant, make it so
  28.   //  TODO: braces around elements are not needed
  29.   //  TODO: make it cleare these are floats
  30.   125.0f, 135.0f, 145.0f, 155.0f, 165.0f, 175.0f, 185.0f,
  31.   195.0f, 205.0f, 215.0f, 225.0f, 235.0f, 245.0f, 255.0f,
  32.   265.0f, 275.0f, 285.0f, 295.0f, 305.0f, 315.0f, 325.0f,
  33.   335.0f, 345.0f, 355.0f, 365.0f, 375.0f, 385.0f, 395.0f,
  34.   405.0f,
  35. };
  36. size_t constexpr WAR_l(sizeof(WAr) / sizeof(*WAr)); //  TODO: let the compiler do it
  37.  
  38. float constexpr RAr[/*38*/] { //  TODO: it is a constant, make it so
  39.   10.0f, 10.5f, 11.0f, 11.5f, 12.0f, 12.5f, 13.0f,
  40.   13.5f, 14.0f, 14.5f, 15.0f, 15.5f, 16.0f, 16.5f,
  41.   17.0f, 17.5f, 18.0f, 18.5f, 19.0f, 19.5f, 20.0f,
  42.   20.5f, 21.0f, 21.5f, 22.0f, 22.5f, 23.0f, 23.5f,
  43.   24.0f, 24.5f, 25.0f, 25.5f, 26.0f, 26.5f, 27.0f,
  44.   27.5f, 28.0f, 28.5f,
  45. };
  46. size_t constexpr RAR_l(sizeof(RAr) / sizeof(*RAr)); //  TODO: let the compiler do it
  47.  
  48. float constexpr SAr[/*15*/] { //  TODO: it is a constant, make it so
  49.   25.0f, 30.0f, 35.0f, 40.0f, 45.0f,
  50.   50.0f, 55.0f, 60.0f, 65.0f, 70.0f,
  51.   75.0f, 80.0f, 85.0f, 90.0f, 95.0f,
  52. };
  53. size_t constexpr SAR_l(sizeof(SAr) / sizeof(*SAr)); //  TODO: let the compiler do it
  54.  
  55. #define AS_DYNAMIC_
  56.  
  57. #ifdef AS_DYNAMIC_
  58. //  MARK: Structure DB_container
  59. struct DB_container {
  60.   std::array<std::array<std::array<TireSlot, /*29*/WAR_l>, /*15*/SAR_l>, /*38*/RAR_l> DB;
  61.  
  62.   DB_container() {
  63.     std::cout << "DB_container constructor" << std::endl;
  64.   }
  65.   ~DB_container() {
  66.     std::cout << "DB_container destructor" << std::endl;
  67.   }
  68. #endif
  69. };
  70. //  MARK: DB Globally allocated
  71. auto DBC = std::make_shared<DB_container>();
  72.  
  73. /*
  74.  *  MARK: main()
  75.  */
  76. int main() {
  77.   //  TODO: dispense with magic numbers
  78. #ifdef AS_DYNAMIC_
  79.   if (!DBC) {
  80.     //  TODO: go BOOM!
  81.     throw std::runtime_error("Datastore unavailable.");
  82.   }
  83. #else
  84.   std::array<std::array<std::array<TireSlot, /*29*/WAR_l>, /*15*/SAR_l>, /*38*/RAR_l> DB;
  85. #endif
  86.  
  87.   //DB[29][15][38]; //  TODO: not needed
  88.  
  89.   for (size_t ac = 0; ac < /*29*/WAR_l; ac++) { //  TODO: array index can be up to SIZE_MAX which is > short
  90.     for (size_t bc = 0; bc < /*15*/SAR_l; bc++) {
  91.       for (size_t cc = 0; cc < /*38*/RAR_l; cc++) {
  92. #ifndef AS_DYNAMIC_
  93.         DB[ac][bc][cc].TireRim    = RAr[cc];
  94.         DB[ac][bc][cc].TireaSpect = SAr[bc];
  95.         DB[ac][bc][cc].TireWidth  = WAr[ac];
  96. #else
  97.         DBC->DB[ac][bc][cc].TireRim    = RAr[cc];
  98.         DBC->DB[ac][bc][cc].TireaSpect = SAr[bc];
  99.         DBC->DB[ac][bc][cc].TireWidth  = WAr[ac];
  100. #endif
  101.         std::string tStr;
  102.         float AC = WAr[ac];
  103.         float BC = SAr[bc];
  104.         float CC = RAr[cc];
  105.         std::string As = std::to_string(AC);
  106.         std::string Bs = std::to_string(BC);
  107.         std::string Cs = std::to_string(CC);
  108.         std::string Ss = "/";
  109.         As += Ss;
  110.         Bs += Ss;
  111.         tStr = As + Bs + Cs;
  112. #ifndef AS_DYNAMIC_
  113.         DB[ac][bc][cc].Size = tStr;
  114.         DB[ac][bc][cc].Amt  = 0;
  115.         DB[ac][bc][cc].AmtS = 0;
  116. #else
  117.         DBC->DB[ac][bc][cc].Size = tStr;
  118.         DBC->DB[ac][bc][cc].Amt  = 0;
  119.         DBC->DB[ac][bc][cc].AmtS = 0;
  120. #endif
  121.       }
  122.     }
  123.   }
  124.  
  125.   size_t count = 0;
  126.   for (size_t zx = 0; zx < /*29*/WAR_l; zx++) { //  TODO: array index can be up to SIZE_MAX which is > int
  127.     for (size_t zy = 0; zy < /*15*/SAR_l; zy++) {
  128.       for (size_t zz = 0; zz < /*38*/RAR_l; zz++) {
  129.         //  TODO: pretty print
  130.         std::cout << "Entry:" << std::setfill(' ') << std::setw(10) << ++count << '\n'
  131.                   << "["  << std::setfill(' ') << std::setw(3) << zx
  132.                   << "][" << std::setfill(' ') << std::setw(3) << zy
  133.                   << "][" << std::setfill(' ') << std::setw(3) << zz
  134.                   << "]\n";
  135. #ifndef AS_DYNAMIC_
  136.         std::cout << std::fixed << std::setw(5) << std::setprecision(1) << std::setfill('0')
  137.                   << DB[zx][zy][zz].TireWidth
  138.                   << SS
  139.                   << std::fixed << std::setw(5) << std::setprecision(1) << std::setfill('0')
  140.                   << DB[zx][zy][zz].TireaSpect
  141.                   << SS
  142.                   << std::fixed << std::setw(5) << std::setprecision(1) << std::setfill('0')
  143.                   << DB[zx][zy][zz].TireRim
  144.                   << " ["
  145.                   << DB[zx][zy][zz].Amt
  146.                   << SS
  147.                   << DB[zx][zy][zz].AmtS
  148.                   << "]"
  149.                   << '\n'
  150.                   << std::endl; //  TODO: keep std::endl to a minimum, it calls a buffer flush.
  151. #else
  152.         std::cout << std::fixed << std::setw(5) << std::setprecision(1) << std::setfill('0')
  153.                   << DBC->DB[zx][zy][zz].TireWidth
  154.                   << SS
  155.                   << std::fixed << std::setw(5) << std::setprecision(1) << std::setfill('0')
  156.                   << DBC->DB[zx][zy][zz].TireaSpect
  157.                   << SS
  158.                   << std::fixed << std::setw(5) << std::setprecision(1) << std::setfill('0')
  159.                   << DBC->DB[zx][zy][zz].TireRim
  160.                   << " ["
  161.                   << DBC->DB[zx][zy][zz].Amt
  162.                   << SS
  163.                   << DBC->DB[zx][zy][zz].AmtS
  164.                   << "]"
  165.                   << '\n'
  166.                   << std::endl; //  TODO: keep std::endl to a minimum, it calls a buffer flush.
  167. #endif
  168.       };
  169.     };
  170.   };
  171.  
  172.   return 0;
  173. };
  174.  
RAW Paste Data