Skip to content

Instantly share code, notes, and snippets.

@ebba0194
Created February 6, 2018 21:40
Show Gist options
  • Save ebba0194/0942d00782d9641e111e89f6bd70a9a4 to your computer and use it in GitHub Desktop.
Save ebba0194/0942d00782d9641e111e89f6bd70a9a4 to your computer and use it in GitHub Desktop.
Homework- Modular overloaded functions to calculate area and perimeter of circle, rectangle and triangle.
#include <iostream>
#include <cmath>
using namespace std;
const long double pi = 3.141592654;
void getDim (float&);
void getDim (float&, float&);
void getDim (float&, float&, float&);
float area (float);
float area (float, float);
float area (float, float, float);
float perim (float);
float perim (float, float);
float perim (float, float, float);
void display (float, float, int);
int main(){
float rad, width, length, sideA, sideB, sideC, ansArea, ansPer;
int shape;
do{
cout << "AREA/PERIMETER CALCULATOR" <<endl;
cout << "Select a shape: \nCircle (1) Rectangle (2) Triangle (3) Exit (4)\nEnter selection => ";
cin >> shape;
while (shape != 4 && shape != 3 && shape != 2 && shape != 1){
cout << "Select a shape: \nCircle (1) Rectangle (2) Triangle (3) Exit (4)\nEnter selection => ";
cin >> shape;
}
switch (shape){
case 1:
getDim (rad);
ansArea = area (rad);
ansPer = perim (rad);
display (ansArea, ansPer, shape);
break;
case 2:
getDim (width, length);
ansArea = area (width, length);
ansPer = perim (width, length);
display (ansArea, ansPer, shape);
break;
case 3:
getDim (sideA, sideB, sideC);
ansArea = area (sideA, sideB, sideC);
ansPer = perim (sideA, sideB, sideC);
display (ansArea, ansPer, shape);
break;
default: break;
}
}while (shape != 4);
cout << "\nBye...";
return 0;
}
void getDim (float& rad){
cout << "Enter circle radius: ";
cin >> rad;
while (rad < 0){
cout << "ERROR: radius may not be negative. Re-enter: ";
cin >> rad;
}
}
void getDim (float& length, float& width){
cout << "Enter rectangle length: ";
cin >> length;
while (length < 0){
cout << "ERROR: length may not be negative. Re-enter: ";
cin >> length;
}
cout << "Enter rectangle width: ";
cin >> width;
while (width < 0){
cout << "ERROR: width may not be negative. Re-enter: ";
cin >> width;
}
}
void getDim (float& sideA, float& sideB, float& sideC){
cout << "Enter first side of triangle: ";
cin >> sideA;
while (sideA < 0){
cout << "ERROR: side length may not be negative. Re-enter: ";
cin >> sideA;
}
cout << "Enter second side of triangle: ";
cin >> sideB;
while (sideB < 0){
cout << "ERROR: side length may not be negative. Re-enter: ";
cin >> sideB;
}
cout << "Enter third side of triangle: ";
cin >> sideC;
while (sideC < 0){
cout << "ERROR: side length may not be negative. Re-enter: ";
cin >> sideC;
}
}
float area (float rad){
return (pi*(rad*rad));
}
float area (float length, float width){
return (length*width);
}
float area (float sideA, float sideB, float sideC){
double s;
s = (sideA + sideB + sideC)/2;
return (sqrt(s*(s-sideA)*(s-sideB)*(s-sideC)));
}
float perim (float rad){
return (2*pi*rad);
}
float perim (float length, float width){
return ((2*length)+(2*width));
}
float perim (float sideA, float sideB, float sideC){
return (sideA + sideB + sideC);
}
void display (float ansArea, float ansPer, int shape){
if (shape == 1){
cout << "Circle ";
}
else if (shape == 2){
cout << "Rectangle ";
}
else if (shape == 3){
cout << "Triangle ";
}
cout << "Area = " << ansArea << " and perimeter " << ansPer << endl << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment