1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <iostream>
#include <string>
#include <cmath>
#include "error.h"
using namespace std;
const double pi = 3.14159; // Global constant pi accurate to 5 decimal points
// Fucntion to get positive number.
// Function must be greather than zero to count as a positive number.
double get_postivie_numb(const string& prompt= "")
{
cout << prompt;
double x;
cin >> x;
while (x <= 0)
{
cout << "Sorry, You can only enter positive numbers.\n";
cin >> x;
}
return x;
}
// Funciton to convert input to lowercase.
// Precondition: String must not contain numbers. Only letters.
string convert_to_lowercase(const string& s)
{
string result = "";
for(int i = 0; i < s.size(); i++)
{
int x = 0;
x = static_cast<int>(s[i]);
char y = x;
if (x < 97)
{
x += 32;
y = x;
}
result +=y;
}
return result;
}
// Circle Functions:
void circle_information(double& radius)
{
radius = get_postivie_numb("What is the radius of the circle?");
}
double circle_area(double& radius)
{
double area = pi * radius * radius;
return area;
}
double circle_perimeter(double& radius)
{
double perimeter = 2 * pi * radius;
return perimeter;
}
// Square Functions:
void square_information(double& height)
{
height = get_postivie_numb("What is the height of the square? \n");
}
double square_area(double& height)
{
double area = height * height;
return area;
}
double square_perimeter(double& height)
{
double perimeter = height * 4;
return perimeter;
}
// Rectangle Functions:
void rectangle_information(double& height, double& width)
{
height = get_postivie_numb("Enter the height of the rectangle.\n");
width = get_postivie_numb("Enter the width of the rectangle.\n");
}
double rectangle_area(double& height, double& width)
{
double area = height * width;
return area;
}
double rectangle_perimeter(double& height, double& width)
{
double perimeter = (2 * height) + (2 * width);
return perimeter;
}
// Triangle Functions:
void triangle_information(double& a, double& b, double& c)
{
cout << "Please enter the side lengths from least to greatest.\n";
a = get_postivie_numb("Enter side length a. \n");
b = get_postivie_numb("Enter side length b. \n");
c = get_postivie_numb("Enter side length c. \n");
}
//Precondition: Triangle must be a real triangle. NO impossible triangles.
// Example : traingle with sides 3,4,54 is not a real triangle.
double triangle_area(double& a, double& b, double& c, double s) // Heron's Formula
{
double area = sqrt((s-a)*(s-b)*(s-c)*s);
return area;
}
double triangle_perimeter(double& a, double& b, double& c)
{
double perimeter = a + b + c;
return perimeter;
}
int main()
{
string s = "";
while (s != "done" || s!= "quit")
{
cout << "Enter the name of a geometric shape. (Type \"done\" or \"quit\" to exit):";
cin >> s;
if (convert_to_lowercase(s) == "circle" || convert_to_lowercase(s) == "cir")
{
double radius = 0.0;
circle_information(radius);
cout << "The area of the circle is " << circle_area(radius) << ".\n";
cout << "The perimeter of the circle is " << circle_perimeter(radius) <<".\n\n";
}
else if (convert_to_lowercase(s) == "square" || convert_to_lowercase(s) == "sq")
{
double height = 0;
square_information(height);
cout << "The area of the square is " << square_area(height) << ". \n";
cout << "The perimeter of the square is " << square_perimeter(height) << ". \n\n";
}
else if (convert_to_lowercase(s) == "rectangle" || convert_to_lowercase(s) == "rect")
{
double height = 0;
double width = 0;
rectangle_information(height, width);
cout << "The area of the rectangle is " << rectangle_area(height, width) << ".\n";
cout << "The perimeter of the rectangle is " << rectangle_perimeter(height, width) << ".\n\n";
}
else if (convert_to_lowercase(s) == "triangle" || convert_to_lowercase(s) == "tri")
{
double a = 0;
double b = 0;
double c = 0;
triangle_information(a, b, c);
double semi = triangle_perimeter(a,b,c)/2.0; //semiperimeter of triangle
while(a > b || a > c || b > c)
{
cout << "It seems you did not type the numbers from least to greatest."
<< "Please try again.\n";
triangle_information(a, b, c);
}
double x = a + b;
if (c > x)
{
cout << "This will produce an impossible trianlge.\n\n";
}
else
{
cout << "The area of the triangle is " << triangle_area(a, b, c, semi) << ".\n";
cout << "The perimeter of the rectangle is " << triangle_perimeter(a, b, c) << ".\n\n";
}
}
else
{
cout << "Sorry. Program doesn't know that shape. Please try again.\n\n";
}
}
}
|