Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <charconv>
- #include <iomanip>
- #include <iostream>
- #include <string>
- using namespace std;
- /*
- Требование 6) к лб звучит так -
- "код должен производить форматированный вывод результатов".
- Так как тип выходных данных - long double - у нас есть возможность выводить его с заданной точностью.
- Так как по условию задачи у нас нет входных данных задающих эту "точность" -
- давайте объявим ее глобальной константой.
- */
- const size_t presicion = 10;
- long double GetLongDouble() {
- long double result;
- do
- {
- string str;
- getline(cin, str);
- auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
- 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";
- }
- else
- {
- return result;
- }
- cout << "Please enter number again\n";
- } while (true);
- }
- long double 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