Advertisement
Leeen

dop lab for Bezus №1

Oct 20th, 2018
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. // Лабораторная работа для Гайделя №1 (допзадание. Рассчет веса на других планетах)
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. // Продолжение работы программы
  7. bool Prod()
  8. {
  9.     cout << "Continue Y/N" << endl;
  10.     char yn;
  11.     cin >> yn;
  12.     cin.clear();
  13.     if (yn == 'Y' && cin.get() == '\n') return true;
  14.     else if (yn == 'N' && cin.get() == '\n') return false;
  15.     else
  16.     {
  17.         while (cin.get() != '\n');
  18.         cout << "Error. Try again" << endl;
  19.         Prod();
  20.     }
  21. }
  22.  
  23. // Ввод первой планеты
  24. char inputFirstPlanet()
  25. {
  26.     cout << "Earth(E)" << endl << "Venus(V)" << endl << "Mars(M)" << endl;
  27.     char firstPlanet;
  28.     cin >> firstPlanet;
  29.     cin.clear();
  30.     if (firstPlanet == 'E' || firstPlanet == 'V' || firstPlanet == 'M') return firstPlanet;
  31.     else
  32.     {
  33.         while (cin.get() != '\n');
  34.         cout << "Error. Try again" << endl;
  35.         return inputFirstPlanet();
  36.     }
  37. }
  38.  
  39. // Ввод второй планеты
  40. char inputSecondPlanet()
  41. {
  42.    
  43.     cout << "Earth(E)" << endl << "Venus(V)" << endl << "Mars(M)" << endl;
  44.     char secondPlanet;
  45.     cin >> secondPlanet;
  46.     cin.clear();
  47.     if (secondPlanet == 'E' || secondPlanet == 'V' || secondPlanet == 'M') return secondPlanet;
  48.     else
  49.     {
  50.         while (cin.get() != '\n');
  51.         cout << "Error. Try again" << endl;
  52.         return inputSecondPlanet();
  53.     }
  54. }
  55.  
  56. // Получение отношения масс планет
  57. double getCoefficient(char firstPlanet, char secondPlanet)
  58. {
  59.     if (firstPlanet == 'E' && secondPlanet == 'V') return 0.82;
  60.     if (firstPlanet == 'E' && secondPlanet == 'M') return 1.1;
  61.     if (firstPlanet == 'V' && secondPlanet == 'E') return 1.22;
  62.     if (firstPlanet == 'V' && secondPlanet == 'M') return 1.34;
  63.     if (firstPlanet == 'M' && secondPlanet == 'E') return 0.9;
  64.     if (firstPlanet == 'M' && secondPlanet == 'V') return 0.75;
  65.     if (firstPlanet == secondPlanet) {
  66.         cout << "Error. First planet = second planet" << endl;
  67.         return Prod();
  68.     }
  69. }
  70.  
  71. // Получение веса на второй планете
  72. double getSecondWeight(double coefficient, double firstWeight) {
  73.     return coefficient * firstWeight;
  74. }
  75.  
  76.  
  77. // Получение веса на первой планете
  78. double inputFirstWeight() {
  79.     double firstWeight;
  80.     cin >> firstWeight;
  81.     cin.clear();
  82.     if (firstWeight > 0 && cin.get() == '\n') return firstWeight;
  83.     else {
  84.         while (cin.get() != '\n');
  85.         cout << "Error. Try again" << endl;
  86.         inputFirstWeight();
  87.     }
  88. }
  89.  
  90. int main()
  91. {
  92.     char firstPlanet;
  93.     char secondPlanet;
  94.     double coefficient;
  95.     double firstWeight;
  96.     double secondWeight;
  97.  
  98.     do {
  99.  
  100.         cout << "select a first planet" << endl;
  101.         firstPlanet = inputFirstPlanet();
  102.        
  103.         cout << "first weight = ";
  104.         firstWeight = inputFirstWeight();
  105.  
  106.         cout << "select a second planet" << endl;
  107.         secondPlanet = inputSecondPlanet();
  108.  
  109.         coefficient = getCoefficient(firstPlanet, secondPlanet);
  110.  
  111.         secondWeight = getSecondWeight(coefficient, firstWeight);
  112.        
  113.         cout << "second weight = " << secondWeight << endl;
  114.  
  115.     } while (Prod());
  116.     system("pause");
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement