fork download
  1. // PlayGround.cpp : Defines the entry point for the console application.
  2. //
  3. #include <iostream>
  4. #include <vector>
  5. #include <sstream>
  6. #include <limits>
  7.  
  8. using namespace std;
  9.  
  10. class Result
  11. {
  12. public:
  13. void calculate(std::vector<float> time, std::vector<float> power, std::vector<float> price, float MaxP, float MinP);
  14. Result() = default;
  15.  
  16. public:
  17. float MAX_profit = 0.0f;
  18. float Min_KWh = std::numeric_limits<float>::infinity();
  19. std::vector<int> config;
  20. };
  21.  
  22. void Result::calculate(std::vector<float> time, std::vector<float> power, std::vector<float> price, float MaxP, float MinP)
  23. {
  24. for (auto a = 0; a < 4; a++)
  25. {
  26. for (auto b = 0; b < 4; b++)
  27. {
  28. for (auto c = 0; c < 4; c++)
  29. {
  30. for (auto d = 0; d < 4; d++)
  31. {
  32. auto money = time[0] * power[a] * price[0] + time[1] * power[b] * price[1] + time[2] * power[c] * price[2] + time[3] * power[d] * price[3];
  33. auto p = time[0] * power[a] + time[1] * power[b] + time[2] * power[c] + time[3] * power[d];
  34. if ((money > MAX_profit && p <= MaxP && p >= MinP) || (abs(money - MAX_profit) < 0.0001 && p < Min_KWh))
  35. {
  36. MAX_profit = money;
  37. Min_KWh = p;
  38. config.push_back(a);
  39. config.push_back(b);
  40. config.push_back(c);
  41. config.push_back(d);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48.  
  49.  
  50.  
  51. int main()
  52. {
  53. std::vector<float> time, power, price;
  54. time.reserve(4);
  55. power.reserve(4);
  56. price.reserve(4);
  57.  
  58. float MaxP, MinP;
  59.  
  60. Result res;
  61.  
  62. time.push_back(8);
  63. time.push_back(4);
  64. time.push_back(6);
  65. time.push_back(6);
  66. power.push_back(30);
  67. power.push_back(60);
  68. power.push_back(90);
  69. power.push_back(120);
  70. price.push_back(0.1);
  71. price.push_back(0.3);
  72. price.push_back(0.2);
  73. price.push_back(0.4);
  74. MaxP = 1400;
  75. MinP = 1300;
  76.  
  77.  
  78.  
  79.  
  80. res.calculate(time, power, price, MaxP, MinP);
  81. int a;
  82. cout << "Config: " << power[res.config[0]] << " " << power[res.config[1]] << " " << power[res.config[2]] << " " << power[res.config[3]] << endl;
  83. cout << "Profit per time: " << power[res.config[0]] * time[0] * price[0] << " " << power[res.config[1]] * time[1] * price[1] << " " << power[res.config[2]] * time[2] * price[2] << " " << power[res.config[3]] * time[3] * price[3] << endl;;
  84. cout << "KWh per time: " << power[res.config[0]] * time[0] << " " << power[res.config[1]]*time[1] << " " << power[res.config[2]]*time[2] << " " << power[res.config[3]]*time[3] << endl;
  85. cout << "Overall Profit: " << res.MAX_profit << " Total KWh: " << res.Min_KWh << endl;
  86. cin >> a;
  87. return 0;
  88. }
  89.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘void Result::calculate(std::vector<float>, std::vector<float>, std::vector<float>, float, float)’:
prog.cpp:34:83: error: call of overloaded ‘abs(float)’ is ambiguous
      if ((money > MAX_profit && p <= MaxP && p >= MinP) || (abs(money - MAX_profit) < 0.0001 && p < Min_KWh))
                                                                                   ^
In file included from /usr/include/c++/6/cstdlib:75:0,
                 from /usr/include/c++/6/ext/string_conversions.h:41,
                 from /usr/include/c++/6/bits/basic_string.h:5417,
                 from /usr/include/c++/6/string:52,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:3:
/usr/include/stdlib.h:735:12: note: candidate: int abs(int)
 extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
            ^~~
In file included from /usr/include/c++/6/ext/string_conversions.h:41:0,
                 from /usr/include/c++/6/bits/basic_string.h:5417,
                 from /usr/include/c++/6/string:52,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:3:
/usr/include/c++/6/cstdlib:185:3: note: candidate: __int128 std::abs(__int128)
   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
   ^~~
/usr/include/c++/6/cstdlib:180:3: note: candidate: long long int std::abs(long long int)
   abs(long long __x) { return __builtin_llabs (__x); }
   ^~~
/usr/include/c++/6/cstdlib:172:3: note: candidate: long int std::abs(long int)
   abs(long __i) { return __builtin_labs(__i); }
   ^~~
stdout
Standard output is empty