fork(13) download
  1. #include <string>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. // Interface for SafeArray class
  8. //--------------------------------
  9. class SafeArray
  10. {
  11. public:
  12. SafeArray();
  13. SafeArray(const SafeArray & copy);
  14. ~SafeArray();
  15.  
  16. bool GetElement(const int i, float & Value) const;
  17. bool SetElement(const int i, float Value);
  18. bool ReadArray(const string Filename);
  19. bool WriteArray(const string Filename) const;
  20. void Print() const;
  21.  
  22. private:
  23. static const int SIZE = 20;
  24. float Array[SIZE];
  25. };
  26.  
  27. // Implementation for SafeArray class
  28. //-----------------------------------
  29. SafeArray::SafeArray()
  30. {
  31. cout << "construct\n";
  32. for (int i = 0; i < SIZE; i++)
  33. Array[i] = 0;
  34. }
  35.  
  36. SafeArray::SafeArray(const SafeArray & copy)
  37. {
  38. cout << "copy\n";
  39. for (int i = 0; i < SIZE; i++)
  40. Array[i] = copy.Array[i];
  41. }
  42.  
  43. SafeArray::~SafeArray()
  44. {
  45. cout << "destruct\n";
  46. }
  47.  
  48. bool SafeArray::GetElement(const int i, float & Value) const
  49. {
  50. bool Success = false;
  51. if ((i >= 0) && (i < SIZE))
  52. {
  53. Success = true;
  54. Value = Array[i];
  55. }
  56. return Success;
  57. }
  58.  
  59. bool SafeArray::SetElement(const int i, float Value)
  60. {
  61. bool Success = false;
  62. if ((i >= 0) && (i < SIZE))
  63. {
  64. Success = true;
  65. Array[i] = Value;
  66. }
  67. return Success;
  68. }
  69.  
  70. bool SafeArray::ReadArray(const string Filename)
  71. {
  72. ifstream infile;
  73. infile.open(Filename.c_str());
  74. if (infile.fail())
  75. return false;
  76.  
  77. for (int i = 0; i < SIZE; i++)
  78. infile >> Array[i];
  79. infile.close();
  80. return true;
  81. }
  82.  
  83. bool SafeArray::WriteArray(const string Filename) const
  84. {
  85. ofstream outfile;
  86. outfile.open(Filename.c_str());
  87. if (outfile.fail())
  88. return false;
  89.  
  90. for (int i = 0; i < SIZE; i++)
  91. outfile << Array[i] << endl;
  92. outfile.close();
  93. return true;
  94. }
  95.  
  96. void SafeArray::Print() const
  97. {
  98. for (int i = 0; i < SIZE; i++)
  99. cout << "Array[" << i << "] = " << Array[i] << endl;
  100. }
  101.  
  102. // Program to demonstrate SafeArray class
  103. //---------------------------------------
  104. int main()
  105. {
  106. SafeArray data1;
  107. SafeArray data2;
  108.  
  109. //data1.Print();
  110.  
  111. for (int i = -5; i < 25; i++)
  112. {
  113. if(data1.SetElement(i, i * 0.1)){
  114. cout << "set " << i << " " << i * 0.1 << endl;
  115. }
  116. }
  117. float val;
  118. for (int i = -5; i < 25; i++)
  119. {
  120. if(data1.GetElement(i, val)){
  121. cout << "get " << i << " " << i * 0.1 << endl;
  122. }
  123. }
  124. return 0;
  125.  
  126.  
  127. }
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
construct
construct
set 0 0
set 1 0.1
set 2 0.2
set 3 0.3
set 4 0.4
set 5 0.5
set 6 0.6
set 7 0.7
set 8 0.8
set 9 0.9
set 10 1
set 11 1.1
set 12 1.2
set 13 1.3
set 14 1.4
set 15 1.5
set 16 1.6
set 17 1.7
set 18 1.8
set 19 1.9
get 0 0
get 1 0.1
get 2 0.2
get 3 0.3
get 4 0.4
get 5 0.5
get 6 0.6
get 7 0.7
get 8 0.8
get 9 0.9
get 10 1
get 11 1.1
get 12 1.2
get 13 1.3
get 14 1.4
get 15 1.5
get 16 1.6
get 17 1.7
get 18 1.8
get 19 1.9
destruct
destruct