Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- bool isNumeric(string strChoose, bool isDouble)
- {
- for (int i = 0; i < strChoose.length(); i++) {
- if (!isdigit(strChoose[i]) && isDouble && strChoose[i] == '.')
- {
- if (strChoose[i + 1] < '0' || strChoose[i + 1] > '9') { return false; }
- }
- else if (!isdigit(strChoose[i])) {
- return false;
- }
- }
- return true;
- }
- void calculateTriangle()
- {
- string strInput;
- double sideA, sideB, sideC, halfP;
- cout << "Calculate perimeter and area of TRIANGLE" << endl;
- cout << "" << endl;
- do
- {
- cout << "Enter value of side A(base) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideA = stod(strInput) : sideA = 0;
- } while (sideA <= 0);
- cout << "" << endl;
- do
- {
- cout << "Enter value of side B(left hit) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideB = stod(strInput) : sideB = 0;
- } while (sideB <= 0);
- cout << "" << endl;
- do
- {
- cout << "Enter value of side C(right hit) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideC = stod(strInput) : sideC = 0;
- } while (sideC <= 0);
- cout << "" << endl;
- halfP = (sideA + sideB + sideC) / 2;
- cout << "Perimeter is: " << sideA + sideB + sideC << " cm" << endl;
- cout << "Area is: " << sqrt(halfP * (halfP - sideA) * (halfP - sideB) * (halfP - sideC)) << " cm2" << endl;
- }
- void calculateTrapezoid()
- {
- string strInput;
- double sideA, sideB, sideC, sideD, heigth;
- cout << "Calculate perimeter and area of TRAPEZOID" << endl;
- cout << "" << endl;
- do
- {
- cout << "Enter value of side A(big base) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideA = stod(strInput) : sideA = 0;
- } while (sideA <= 0);
- cout << "" << endl;
- do
- {
- cout << "Enter value of side B(small base) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideB = stod(strInput) : sideB = 0;
- } while (sideB <= 0);
- cout << "" << endl;
- do
- {
- cout << "Enter value of side C(left hit) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideC = stod(strInput) : sideC = 0;
- } while (sideC <= 0);
- cout << "" << endl;
- do
- {
- cout << "Enter value of side D(right hit) in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? sideD = stod(strInput) : sideD = 0;
- } while (sideD <= 0);
- cout << "" << endl;
- do
- {
- cout << "Enter value of heigth in cm: ";
- cin >> strInput;
- isNumeric(strInput, true) ? heigth = stod(strInput) : heigth = 0;
- } while (heigth <= 0);
- cout << "" << endl;
- cout << "" << endl;
- cout << "Perimeter is: " << sideA + sideB + sideC + sideD << " cm" << endl;
- cout << "Area is: " << ((sideA + sideB) * heigth) / 2 << " cm2" << endl;
- }
- int main()
- {
- string strChoose;
- unsigned int choose;
- do
- {
- cout << "Enter value of figure\n[1]Triangle\n[2]Trapezoid\n: ";
- cin >> strChoose;
- cout << "" << endl;
- isNumeric(strChoose, false) ? choose = stoul(strChoose) : choose = 0;
- if (choose == 1) {
- calculateTriangle();
- }
- else if (choose == 2) {
- calculateTrapezoid();
- }
- if (choose >= 1 && choose <= 2) {
- cout << "" << endl;
- do
- {
- cout << "New calculation? [y/Y - Yes or n/N - No]: ";
- cin >> strChoose;
- if (strChoose == "Y" || strChoose == "y" || strChoose == "N" || strChoose == "n") {
- break;
- }
- else {
- strChoose == "countine";
- continue;
- }
- } while (true);
- if (strChoose == "Y" || strChoose == "y") {
- system("CLS");
- choose = 0;
- }
- else {
- exit(0);
- }
- }
- } while (choose < 1 || choose > 2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement