# Untitled

unknown
plain_text
a month ago
6.2 kB
7
Indexable
Never
```/* ***************************************************************
* Program Title: Area Program csc102 H001
* Program Name: w10178836-P02.cpp
* Name: Sarah Mirza
* StuID: w10178836
* Date: 18 March 2024
*
* Objectives/Description: To calculate the area and perimeter of the given figures.
* Input: shape names and their measurements.
* Output: area and perimeter
* ****************************************************************
*/

/* *************************************************
* Function Names: square, rectangle, circle, triangle and trapezoid (all individual functions).
* Function Description: it askes for the required measurements and then calculate the area and parameter of the specific shape.
* Function Parameters: width, height, lenght(1,2,3,4), and radius.
* Paramter type, name and use. the parameters are all double&
* What is returned, if anything. Nothing is returned since they are void functions.
* *************************************************
*/

#include <iostream>
#include <cmath>
#include <limits>
#include <string>

using namespace std;

const double pi = 3.14159265358979323846;

void getValidInput(double& input, const string& prompt) {
cout << prompt;
while (!(cin >> input)) {
cout << "Invalid input. Please enter a numerical value." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << prompt;
}
}

class Square {
private:
double side;

public:
Square() : side(0.0) {}

void setSide(double s) {
side = s;
}

double getArea() const {
return side * side;
}

double getPerimeter() const {
return 4 * side;
}
};

class Rectangle {
private:
double length;
double width;

public:
Rectangle() : length(0.0), width(0.0) {}

void setDimensions(double l, double w) {
length = l;
width = w;
}

double getArea() const {
return length * width;
}

double getPerimeter() const {
return 2 * (length + width);
}
};

class Triangle {
private:
double side1;
double side2;
double height;

public:
Triangle() : side1(0.0), side2(0.0), height(0.0) {}

void setDimensions(double s1, double s2, double h) {
side1 = s1;
side2 = s2;
height = h;
}

double getArea() const {
return 0.5 * side1 * height;
}

double getPerimeter() const {
return side1 + side2 + height;
}
};

class Circle {
private:

public:

}

double getArea() const {
}

double getPerimeter() const {
return 2 * pi * radius;
}
};

class Trapezoid {
private:
double base1;
double base2;
double side1;
double side2;
double height;

public:
Trapezoid() : base1(0.0), base2(0.0), side1(0.0), side2(0.0), height(0.0) {}

void setDimensions(double b1, double b2, double s1, double s2, double h) {
base1 = b1;
base2 = b2;
side1 = s1;
side2 = s2;
height = h;
}

double getArea() const {
return 0.5 * (base1 + base2) * height;
}

double getPerimeter() const {
return base1 + base2 + side1 + side2;
}
};

int main() {
string usershape;

while (true) {
cout << "Choose a shape to calculate its area and perimeter, or type 'quit' to exit. Shapes available: square, rectangle, triangle, circle, trapezoid: ";
cin >> usershape;

if (usershape == "quit") {
cout << "Program ended.";
break;
}

if (usershape == "square") {
Square square;
double side;
getValidInput(side, "Enter side length: ");
square.setSide(side);
cout << "Area: " << square.getArea() << endl;
cout << "Perimeter: " << square.getPerimeter() << endl;
}

else if (usershape == "rectangle") {
Rectangle rectangle;
double length, width;
getValidInput(length, "Enter length: ");
getValidInput(width, "Enter width: ");
rectangle.setDimensions(length, width);
cout << "Area: " << rectangle.getArea() << endl;
cout << "Perimeter: " << rectangle.getPerimeter() << endl;
}

else if (usershape == "triangle") {
Triangle triangle;
double side1, side2, height;
getValidInput(side1, "Enter side 1 length: ");
getValidInput(side2, "Enter side 2 length: ");
getValidInput(height, "Enter height length: ");
triangle.setDimensions(side1, side2, height);
cout << "Area: " << triangle.getArea() << endl;
cout << "Perimeter: " << triangle.getPerimeter() << endl;
}

else if (usershape == "circle") {
Circle circle;
cout << "Area: " << circle.getArea() << endl;
cout << "Perimeter: " << circle.getPerimeter() << endl;
}

else if (usershape == "trapezoid") {
Trapezoid trapezoid;
double base1, base2, side1, side2, height;
getValidInput(base1, "Enter length of base 1: ");
getValidInput(base2, "Enter length of base 2: ");
getValidInput(side1, "Enter length of side 1: ");
getValidInput(side2, "Enter length of side 2: ");
getValidInput(height, "Enter height: ");
trapezoid.setDimensions(base1, base2, side1, side2, height);
cout << "Area: " << trapezoid.getArea() << endl;
cout << "Perimeter: " << trapezoid.getPerimeter() << endl;
}

else {
cout << "Shape not recognized." << endl;
}
}

return 0;
}
```