Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- void calculate_speed()
- {
- double distance, time, speed;
- cout << "Enter the distance in km: ";
- cin >> distance;
- cout << "Enter the time in hours: ";
- cin >> time;
- if (time != 0)
- {
- speed = distance / time;
- cout << "Speed: " << speed << " km/h" << endl;
- }
- else
- {
- cout << "The time can not be ZERO! Try again." << endl;
- }
- }
- void calculate_time()
- {
- double distance, speed, time;
- cout << "Enter the distance (km): ";
- cin >> distance;
- cout << "Enter the speed (km/h): ";
- cin >> speed;
- if (speed != 0) {
- time = distance / speed;
- cout << "Time: " << time << " h" << endl;
- }
- else
- {
- cout << "The speed cannot be 0!" << endl;
- }
- }
- void calculate_distance()
- {
- double speed, time, distance;
- cout << "Enter the speed (km/h):";
- cin >> speed;
- cout << "Enter the time (hours): ";
- cin >> time;
- distance = speed * time;
- cout << "Distance is " << distance << " km" << endl;
- }
- void solve_quadratic()
- {
- double a, b, c, discriminant, root1, root2;
- cout << "Enter a, b and c ";
- cin >> a >> b >> c;
- discriminant = b * b - 4 * a * c;
- if (discriminant > 0)
- {
- root1 = (-b + sqrt(discriminant)) / (2 * a);
- root2 = (-b - sqrt(discriminant)) / (2 * a);
- cout << "The roots of the equation: " << root1 << " and" << root2 << endl;
- }
- else if (discriminant == 0)
- {
- root1 = -b / (2 * a);
- cout << "One root: " << root1 << endl;
- }
- else
- {
- cout << "There are no roots (the discriminant is less than zero)" << endl;
- }
- }
- int main()
- {
- int choice;
- do{
- cout << "Menu:\n";
- cout << "1. Calculate the speed \n";
- cout << "2. Calculate the time \n";
- cout << "3. Calculate the distance \n";
- cout << "4. Solve the quadratic equation \n";
- cout << "5. Exit\n";
- cin >> choice;
- switch (choice)
- {
- case 1:
- calculate_speed();
- break;
- case 2:
- calculate_time();
- break;
- case 3:
- calculate_distance();
- break;
- case 4:
- solve_quadratic();
- break;
- case 5:
- cout << "Exit...\n";
- break;
- default:
- cout << "Wrong choice! Try again.\n";
- }
- }
- while (choice != 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement