Advertisement
hotsbefastt

LabTask1

Nov 23rd, 2024
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. void calculate_speed()
  5. {
  6.     double distance, time, speed;
  7.     cout << "Enter the distance in km: ";
  8.     cin >> distance;
  9.     cout << "Enter the time in hours: ";
  10.     cin >> time;
  11.     if (time != 0)
  12.     {
  13.         speed = distance / time;
  14.         cout << "Speed: " << speed << " km/h" << endl;
  15.     }
  16.     else
  17.     {
  18.         cout << "The time can not be ZERO! Try again." << endl;
  19.     }
  20. }
  21.  
  22. void calculate_time()
  23. {
  24.     double distance, speed, time;
  25.     cout << "Enter the distance (km): ";
  26.     cin >> distance;
  27.     cout << "Enter the speed (km/h): ";
  28.     cin >> speed;
  29.     if (speed != 0) {
  30.         time = distance / speed;
  31.         cout << "Time: " << time << " h" << endl;
  32.     }
  33.     else
  34.     {
  35.         cout << "The speed cannot be 0!" << endl;
  36.     }
  37. }
  38.  
  39. void calculate_distance()
  40. {
  41.     double speed, time, distance;
  42.     cout << "Enter the speed (km/h):";
  43.     cin >> speed;
  44.     cout << "Enter the time (hours): ";
  45.     cin >> time;
  46.     distance = speed * time;
  47.     cout << "Distance is " << distance << " km" << endl;
  48. }
  49.  
  50. void solve_quadratic()
  51. {
  52.     double a, b, c, discriminant, root1, root2;
  53.     cout << "Enter a, b and c ";
  54.     cin >> a >> b >> c;
  55.     discriminant = b * b - 4 * a * c;
  56.     if (discriminant > 0)
  57.     {
  58.         root1 = (-b + sqrt(discriminant)) / (2 * a);
  59.         root2 = (-b - sqrt(discriminant)) / (2 * a);
  60.         cout << "The roots of the equation: " << root1 << " and" << root2 << endl;
  61.     }
  62.     else if (discriminant == 0)
  63. {
  64.         root1 = -b / (2 * a);
  65.         cout << "One root: " << root1 << endl;
  66.     }
  67.     else
  68.     {
  69.         cout << "There are no roots (the discriminant is less than zero)" << endl;
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.     int choice;
  76.     do{
  77.     cout << "Menu:\n";
  78.         cout << "1. Calculate the speed \n";
  79.         cout << "2. Calculate the time \n";
  80.         cout << "3. Calculate the distance \n";
  81.         cout << "4. Solve the quadratic equation \n";
  82.         cout << "5. Exit\n";
  83.         cin >> choice;
  84.         switch (choice)
  85.         {
  86.             case 1:
  87.                 calculate_speed();
  88.                 break;
  89.             case 2:
  90.                 calculate_time();
  91.                 break;
  92.             case 3:
  93.                 calculate_distance();
  94.                 break;
  95.             case 4:
  96.                 solve_quadratic();
  97.                 break;
  98.             case 5:
  99.                 cout << "Exit...\n";
  100.                 break;
  101.             default:
  102.                 cout << "Wrong choice! Try again.\n";
  103.         }
  104.     }
  105.     while (choice != 5);
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement