fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <math.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. bool igualMenor(float a, float b, float epsilon){
  9. if (fabs(a-b) < epsilon) return true;
  10.  
  11. return a-b < 0;
  12. }
  13.  
  14. bool igualMaior(float a, float b, float epsilon){
  15. if (fabs(a-b) < epsilon) return true;
  16.  
  17. return a-b > 0;
  18. }
  19.  
  20. class cVarFloat
  21. {
  22. private:
  23. float fValue;
  24. float fValueSet;
  25. float fMax;
  26. float fMin;
  27. char ItemText[100];
  28. public:
  29. float GetMax()
  30. {
  31. return this->fMax;
  32. }
  33.  
  34. float GetValue()
  35. {
  36. return this->fValue;
  37. }
  38.  
  39. char* GetItemText(void)
  40. {
  41. return this->ItemText;
  42. }
  43.  
  44. void SetValue(float fValue)
  45. {
  46. this->fValue = fValue;
  47. }
  48.  
  49. /*
  50.   É nessa função que eu estou tendo o problema
  51.   */
  52. void DecValue(void)
  53. {
  54. if (igualMenor(this->fValue, this->fMin, 0.0001))
  55. this->fValue = this->fMax;
  56. else
  57. this->fValue -= this->fValueSet;
  58. }
  59.  
  60. void IncValue(void)
  61. {
  62. if (igualMaior(this->fValue, this->fMax, 0.0001))
  63. this->fValue = fMin;
  64. else
  65. this->fValue += fValueSet;
  66. }
  67.  
  68. cVarFloat(const char* ItemText, float fMin, float fMax, float fValueSet, float fInitValue)
  69. {
  70. this->fMin = fMin;
  71. this->fMax = fMax;
  72. this->fValueSet = fValueSet;
  73. this->fValue = fInitValue;
  74. strcpy(this->ItemText, ItemText);
  75. }
  76. };
  77.  
  78. int main()
  79. {
  80. // Primeiro argumento = Texto da classe
  81. // Segundo argumento = Valor mínimo
  82. // Terceiro argumento = Valor máximo
  83. // Quarto argumento = Valor na qual vai aumentar/diminuir o valor da variável
  84. // Quinto argumento = Valor inicial da variável
  85.  
  86. cVarFloat Teste("Testando um float", 0.0f, 0.5f, 0.1f, 0.5f);
  87.  
  88. cout<<fixed;
  89.  
  90. int opcao = 1;
  91.  
  92. while (opcao != 3)
  93. {
  94. cin>>opcao;
  95.  
  96. if (opcao == 2)
  97. {
  98. Teste.IncValue();
  99. cout << "Item texto: " << Teste.GetItemText() << "\nValor maximo do item: " << Teste.GetMax() << "\nValor atual: " << Teste.GetValue() << endl << endl;
  100. }
  101.  
  102. if (opcao == 1)
  103. {
  104. Teste.DecValue(); // problema nessa função
  105. cout << "Item texto: " << Teste.GetItemText() << "\nValor maximo do item: " << Teste.GetMax() << "\nValor atual: " << Teste.GetValue() << endl << endl;
  106. }
  107. }
  108. return 0;
  109. }
  110.  
Success #stdin #stdout 0s 15232KB
stdin
2
2
2
2
2
2
1
1
1
1
1
1
3
stdout
Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.000000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.100000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.200000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.300000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.400000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.500000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.400000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.300000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.200000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.100000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.000000

Item texto: Testando um float
Valor maximo do item: 0.500000
Valor atual: 0.500000