Geometry Calculator in C++Homework Sample

You need to write a program to solve various simple geometry calculations. The user should be either able to solve coordinate problems, or euclidean geometry calculations. For coordinates, you should be able to calculate the midpoint, the slope or the distance between 2 points. For euclidean calculations, you should be able to calculate the area and the perimeter for a rectangle, circle and triangle. The methods should check for invalid values (negative lengths for example), and not allow the calculation to proceed. For more C++ assignment help contact us for a quote.

Solution:

#include <iostream>
#include “std_lib_facilities.h”
#include “math.h”

const double PI=3.14159;

double area_rectangle(int side_1,int side_2){
return side_1*side_2;
}

double area_triangle(int base, int height){
return base*height/2;
}

double area_circle(int radius){
return PI*radius*radius;
}

double perimeter_rectangle(int side_1,int side_2){
return 2*side_1+2*side_2;
}

double perimeter_triangle(int side_1,int side_2,int side_3){
return side_1+side_2+side_3;
}

double perimeter_circle(int radius){
return (3/4)*PI*radius;
}

double slope(int x1,int y1, int x2, int y2){
return (y2-y1)/(x2-x1);
}

double midpoint_x(int x1,int x2){
return (x1+x2)/2;
}

double midpoint_y(int y1, int y2){
return (y1+y2)/2;
}

double distance(int x1, int y1, int x2, int y2){
return pow(pow((double)(x2-x1),2)+pow((double)(y2-y1),2),0.5);
}

int main()
{
std::string option=”N”;

while(option!=”C” && option!=”E”){
cout<<“Select C for Coordinate or E for Euclidean geometry: “;
cin>>option;
if(option!=”C” && option!=”E”)
cout<<“Wrong input, please try again! \n\n”;
}
cout<<“You’ve selected: “<<option<<“\n”;

if(option==”C”){
std::string coordinate_option=”N”;

while(coordinate_option!=”M” && coordinate_option!=”D” && coordinate_option!=”S”){
cout<<“Select M for midpoint, D for distance or S for slope: “;
cin>>coordinate_option;
if(coordinate_option!=”M” && coordinate_option!=”D” && coordinate_option!=”S”)
cout<<“Wrong input, please try again! \n\n”;
}
cout<<“You’ve selected: “<<coordinate_option<<“\n”;
int point_1_x=0;
int point_1_y=0;
int point_2_x=0;
int point_2_y=0;

cout<<“Please enter the X coordinate for the first point: “;
cin>>point_1_x;
cout<<“\n\nPlease enter the y coordinate for the first point: “;
cin>>point_1_y;

cout<<“Please enter the X coordinate for the second point: “;
cin>>point_2_x;
cout<<“\n\nPlease enter the y coordinate for the second point: “;
cin>>point_2_y;

if(coordinate_option==”S”){
cout<<“Slope: “<<slope(point_1_x,point_1_y,point_2_x,point_2_y)<<“\n”;
}
if(coordinate_option==”M”){
cout<<“Midpoint: (“<<midpoint_x(point_1_x,point_2_x)<<“,”<<midpoint_y(point_1_y,point_2_y)<<“)\n”;
}
if(coordinate_option==”D”){
cout<<“Distance: “<<distance(point_1_x,point_1_y,point_2_x,point_2_y)<<“\n”;
}

}else{
std::string euclidean_option=”N”;

while(euclidean_option!=”R” && euclidean_option!=”C” && euclidean_option!=”T”){
cout<<“Select R for rectangle, C for circle or T for triangle: “;
cin>>euclidean_option;
if(euclidean_option!=”R” && euclidean_option!=”T” && euclidean_option!=”C”)
cout<<“Wrong input, please try again! \n\n”;
}

cout<<“\nYou’ve selected: “<<euclidean_option<<“\n”;
std::string euclidian_secondary_option=”N”;

while(euclidian_secondary_option!=”A” && euclidian_secondary_option!=”P”){
cout<<“Select A for area or P for perimeter: “;
cin>>euclidian_secondary_option;
if(euclidian_secondary_option!=”A” && euclidian_secondary_option!=”P”)
cout<<“Wrong input, please try again! \n\n”;
}

if(euclidian_secondary_option==”A”){
if(euclidean_option==”R”){
int side_1=-1;
int side_2=-1;
while(side_1<=0 && side_2<=0){
cout<<“Enter the value of one side of the rectangle: “;
cin>>side_1;
cout<<“\nEnter the value of the other side of the rectangle: “;
cin>>side_2;
if(side_1<=0 && side_2<=0)
cout<<“\nValues must be greater than zero! Please, try again! \n”;
}
cout<<“Area of rectangle: “<<area_rectangle(side_1,side_2);
}
if(euclidean_option==”T”){
int base=-1;
int height=-1;
while(base<=0 && height<=0){
cout<<“Enter the value of one base of the triangle: “;
cin>>base;
cout<<“\nEnter the value of the height of the triangle: “;
cin>>height;
if(base<=0 && height<=0)
cout<<“\nValues must be greater than zero! Please, try again! \n”;
}
cout<<“Area of triangle: “<<area_triangle(base,height);
}
if(euclidean_option==”C”){
int radius=-1;
while(radius<=0){
cout<<“Enter the value of the radius of the circle: “;
cin>>radius;
if(radius<=0)
cout<<“\nRadius must be greater than zero! Please, try again! \n”;
}
cout<<“Area of circle: “<<area_circle(radius);
}
}else{
if(euclidean_option==”R”){
int side_1=-1;
int side_2=-1;
while(side_1<=0 && side_2<=0){
cout<<“Enter the value of one side of the rectangle: “;
cin>>side_1;
cout<<“\nEnter the value of the other side of the rectangle: “;
cin>>side_2;
if(side_1<=0 && side_2<=0)
cout<<“\nValues must be greater than zero! Please, try again! \n”;
}
cout<<“Perimeter of rectangle: “<<perimeter_rectangle(side_1,side_2);
}
if(euclidean_option==”T”){
int side_1=-1;
int side_2=-1;
int side_3=-1;
while(side_1<=0 && side_2<=0 && side_3<=0){
cout<<“Enter the value of one side of the rectangle: “;
cin>>side_1;
cout<<“\nEnter the value of the second side of the rectangle: “;
cin>>side_2;
cout<<“\nEnter the value of the third side of the rectangle: “;
cin>>side_3;
if(side_1<=0 && side_2<=0 && side_3<=0)
cout<<“\nValues must be greater than zero! Please, try again! \n”;
}
cout<<“Perimeter of triangle: “<<perimeter_triangle(side_1,side_2,side_3);
}
if(euclidean_option==”C”){
int radius=-1;
while(radius<=0){
cout<<“Enter the value of the radius of the circle: “;
cin>>radius;
if(radius<=0)
cout<<“\nRadius must be greater than zero! Please, try again! \n”;
}
cout<<“Perimeter of circle: “<<perimeter_circle(radius);
}
}
}

return 0;
}