Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- // main method at the end of file
- int problem1(double a, double b, double c) {
- string validTriangleMessage = "The triangle is a valid right angle triangle.\n";
- string invalidTriangleMessage = "No triangle can be constructed from the given sides.\n";
- string invalidPythagoreanTripleMessage = "There exists no such a right angle triangle with the given sides.\n";
- string noNegativeSidesMessage = "Sides of a triangle must be positive\n";
- if (a < 0 || b < 0 || c < 0) {
- cout << noNegativeSidesMessage;
- return(1);
- }
- // is it really safe to compare doubles for equality in C++?
- // triangle inequality
- bool isValidTriangle = (a < c + b) && (b < a + c) && (c < a + b);
- // for the case where the sides of the triangle are given arbitrarily
- bool isValidPythagoreanTriple = (a * a + b * b == c * c) || (c * c + b * b == a * a) || (c * c + a * a == b * b);
- if (isValidTriangle)
- {
- if (isValidPythagoreanTriple) {
- cout << validTriangleMessage;
- }
- else {
- cout << invalidPythagoreanTripleMessage;
- }
- }
- else {
- cout << invalidTriangleMessage;
- }
- cout << endl;
- }
- void problem2(double a, double b, double c) {
- // variant 1 - conditional statements
- double max_if = 0;
- if (a >= b && a >= c)
- {
- max_if = a;
- }
- else if (b >= c && b >= a)
- {
- max_if = b;
- }
- else
- {
- max_if = c;
- }
- cout << "The maximum of the 3 numbers (calculated with IFs) is: " << max_if << endl;
- //variant2 - using array and loops
- double numbers[3] = { a, b, c };
- double max_loop = -numeric_limits<double>::max();
- for (int i = 0; i < size(numbers); i++)
- {
- if (numbers[i] >= max_loop) {
- max_loop = numbers[i];
- }
- }
- cout << "The maximum of the 3 numbers (calculated with loop) is: " << max_loop << endl;
- cout << endl;
- }
- void problem3(int number) {
- for (int i = 1; i <= number; ++i)
- {
- if (i == 1)
- {
- cout << setw(3) << "X";
- for (int k = 1; k <= number; k++)
- {
- cout << setw(6) << k;
- }
- cout << endl;
- for (int i = 0; i <= 6 * number; i++)
- {
- cout << "-";
- }
- cout << "---" << endl;
- }
- cout << std::setw(3) << i;
- for (int j = 1; j <= number; ++j)
- {
- cout << setw(6) << i * j;
- }
- cout << endl;//<< endl;
- }
- }
- int main() {
- //cout << "Please enter a number to run the corresponding program (1, 2 or 3) or anything else to exit." << endl;
- int programNumber;
- while (true)
- {
- cout << "Please enter a number to run the corresponding program (1, 2 or 3) or anything else to exit." << endl;
- cin >> programNumber;
- double a, b, c;
- switch (programNumber)
- {
- case 1:
- cout << "Please enter three numbers to check if they can be sides of a right angle triangle" << endl;
- cin >> a >> b >> c;
- problem1(a, b, c);
- break;
- case 2:
- cout << "Please enter three numbers. The program will return the biggest one." << endl;
- cin >> a >> b >> c;
- problem2(a, b, c);
- break;
- case 3:
- cout << "Please enter an integer. The program will create the multiplication table up to this integer." << endl;
- int n;
- cin >> n;
- problem3(n);
- break;
- default:
- cout << "You have to choose 1, 2 or 3. Goodbye." << endl;
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement