Advertisement
Derga

Untitled

Sep 16th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <charconv>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <limits>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. /*
  10. Требование 6) к лб звучит так -
  11. "код должен производить форматированный вывод результатов".
  12.  
  13. Так как тип выходных данных - long double - у нас есть возможность выводить его с заданной точностью.
  14. Так как по условию задачи у нас нет входных данных задающих эту "точность" -
  15. давайте объявим ее глобальной константой.
  16. */
  17. const size_t presicion = 10;
  18.  
  19. //покаа рабтает не всегда корректно, нужно донастроить
  20. long double GetLongDouble() {
  21. string str;
  22. long double result;
  23. bool is_converted = false;
  24.  
  25. do
  26. {
  27. getline(cin, str);
  28. auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
  29.  
  30. if (ec == std::errc())
  31. {
  32. return result;
  33. }
  34. else if (ec == std::errc::invalid_argument)
  35. {
  36. std::cout << "This is not a number.\n";
  37. }
  38. else if (ec == std::errc::result_out_of_range)
  39. {
  40. std::cout << "This number is larger than an long double.\n";
  41. }
  42.  
  43. std::cin.clear(); // reset the fail flags
  44. std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); // ignore the bad input until line end
  45. cout << "Please enter value again\n";
  46. } while (true);
  47.  
  48. /*
  49. do
  50. {
  51. try
  52. {
  53. getline(cin, long_double);
  54.  
  55. //https://en.cppreference.com/w/cpp/string/basic_string/stof
  56. result = stold(long_double);
  57. std::cin.clear(); // reset the fail flags
  58. std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); // ignore the bad input until line
  59. // end
  60. return result;
  61.  
  62. } catch (invalid_argument const &ex)
  63. {
  64. cout << "Invalid argument.\n";
  65. } catch (out_of_range const &ex)
  66. {
  67. cout << "Argument value is out of range\n";
  68. }
  69. cout << "Please enter value again\n";
  70. std::cin.clear(); // reset the fail flags
  71. std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); // ignore the bad input until line end
  72. } while (true);
  73. */
  74. }
  75.  
  76. long double GetMass() {
  77. double mass = GetLongDouble();
  78.  
  79. //немного выпендрежный вариант с рекурсией. Возможно переполнение стека
  80. if (mass > 0)
  81. {
  82. return mass;
  83. }
  84.  
  85. cout << "The mass of the point should be greater than 0.\n"
  86. << "Enter the mass of the point again\n";
  87.  
  88. return GetMass();
  89.  
  90. /*еще один вариант
  91. double mass = GetLongDouble();
  92.  
  93. while (mass <= 0)
  94. {
  95. cout << "The mass of the point should be greater than 0.\n"
  96. << "Enter the mass of the point again\n";
  97.  
  98. mass = GetLongDouble();
  99. }
  100.  
  101. return mass;
  102. */
  103. }
  104.  
  105. void CalculateCenterOfMass() {
  106. cout << "Enter coordinate of the point x1\n";
  107. long double x1 = GetLongDouble();
  108. cout << "Enter mass of the point x1\n";
  109. long double mass1 = GetMass();
  110. cout << "Enter coordinate of the point x2\n";
  111. long double x2 = GetLongDouble();
  112. cout << "Enter mass of the point x2\n";
  113. long double mass2 = GetMass();
  114.  
  115. cout << "The coordinate of the center of mass is "
  116. << setprecision(presicion)
  117. << (x1 * mass1 + x2 * mass2) / (mass1 + mass2) << '\n';
  118. }
  119.  
  120. bool DoYouWantCalculateCenterOfMassAgain() {
  121. cout << "Enter \"yes\" if you want to calculate the center of mass again\n";
  122. string replay;
  123. getline(cin, replay);
  124. if (replay == "yes") {
  125. return true;
  126. }
  127. return false;
  128. }
  129.  
  130. int main() {
  131. do {
  132. CalculateCenterOfMass();
  133. } while (DoYouWantCalculateCenterOfMassAgain());
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement