Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <charconv>
- #include <iomanip>
- #include <iostream>
- #include <limits>
- #include <string>
- using namespace std;
- /*
- Требование 6) к лб звучит так -
- "код должен производить форматированный вывод результатов".
- Так как тип выходных данных - long double - у нас есть возможность выводить его с заданной точностью.
- Так как по условию задачи у нас нет входных данных задающих эту "точность" -
- давайте объявим ее глобальной константой.
- */
- const size_t presicion = 10;
- //покаа рабтает не всегда корректно, нужно донастроить
- long double GetLongDouble() {
- string str;
- long double result;
- bool is_converted = false;
- do
- {
- getline(cin, str);
- auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
- if (ec == std::errc())
- {
- return result;
- }
- else if (ec == std::errc::invalid_argument)
- {
- std::cout << "This is not a number.\n";
- }
- else if (ec == std::errc::result_out_of_range)
- {
- std::cout << "This number is larger than an long double.\n";
- }
- std::cin.clear(); // reset the fail flags
- std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); // ignore the bad input until line end
- cout << "Please enter value again\n";
- } while (true);
- /*
- do
- {
- try
- {
- getline(cin, long_double);
- //https://en.cppreference.com/w/cpp/string/basic_string/stof
- result = stold(long_double);
- std::cin.clear(); // reset the fail flags
- std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); // ignore the bad input until line
- // end
- return result;
- } catch (invalid_argument const &ex)
- {
- cout << "Invalid argument.\n";
- } catch (out_of_range const &ex)
- {
- cout << "Argument value is out of range\n";
- }
- cout << "Please enter value again\n";
- std::cin.clear(); // reset the fail flags
- std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); // ignore the bad input until line end
- } while (true);
- */
- }
- long double GetMass() {
- double mass = GetLongDouble();
- //немного выпендрежный вариант с рекурсией. Возможно переполнение стека
- if (mass > 0)
- {
- return mass;
- }
- cout << "The mass of the point should be greater than 0.\n"
- << "Enter the mass of the point again\n";
- return GetMass();
- /*еще один вариант
- double mass = GetLongDouble();
- while (mass <= 0)
- {
- cout << "The mass of the point should be greater than 0.\n"
- << "Enter the mass of the point again\n";
- mass = GetLongDouble();
- }
- return mass;
- */
- }
- void CalculateCenterOfMass() {
- cout << "Enter coordinate of the point x1\n";
- long double x1 = GetLongDouble();
- cout << "Enter mass of the point x1\n";
- long double mass1 = GetMass();
- cout << "Enter coordinate of the point x2\n";
- long double x2 = GetLongDouble();
- cout << "Enter mass of the point x2\n";
- long double mass2 = GetMass();
- cout << "The coordinate of the center of mass is "
- << setprecision(presicion)
- << (x1 * mass1 + x2 * mass2) / (mass1 + mass2) << '\n';
- }
- bool DoYouWantCalculateCenterOfMassAgain() {
- cout << "Enter \"yes\" if you want to calculate the center of mass again\n";
- string replay;
- getline(cin, replay);
- if (replay == "yes") {
- return true;
- }
- return false;
- }
- int main() {
- do {
- CalculateCenterOfMass();
- } while (DoYouWantCalculateCenterOfMassAgain());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement