Name: Anonymous 2008-03-28 18:48
Sup, /prog/, this is my code.
If you can make it smaller and more efficient, whilst doing the same tasks, I will award you with some Internet points.
#include <iostream>
#include <string>
#include <math.h>
class Shape {
protected:
double height, width;
public:
virtual void set_values() = 0;
virtual double area() = 0;
virtual double perimeter() = 0;
void printarea()
{ std::cout << "\nThe area is " << this->area() << " squared.\n"; }
virtual void printperim() = 0;
};
class Triangle: public Shape {
public:
double area()
{
return height * width / 2; }
double perimeter() {
double z = ((width * width) - ((height / 2) * (height / 2)));
z = sqrt(z);
return z + z + height;
}
void printperim() {
std::cout << "\nThe perimeter is " << this->perimeter() << ".\n";
}
void set_values() {
std::cout << "Please enter the height: ";
std::cin >> height;
std::cout << "And the width: ";
std::cin >> width;
}
};
class Rectangle: public Shape {
public:
double area()
{
return height * width; }
double perimeter() {
return (height * 2) + (width * 2);
}
void printperim() {
std::cout << "The perimeter is " << this->perimeter() << ".\n";
}
void set_values() {
std::cout << "Please enter the height: ";
std::cin >> height;
std::cout << "And the width: ";
std::cin >> width;
}
};
class Circle: public Shape {
public:
double area()
{
return height * height * 3.14159265; }
double perimeter() {
return (height * 2) * 3.14159265; }
void printperim() {
std::cout << "The circumfrence is " << this->perimeter() << ".\n";
}
void set_values() {
std::cout << "Please enter the radius: ";
std::cin >> height; }
};
int main() {
std::string Input;
while(true) {
std::cout << "Please choose a shape (Type it in, then press enter)\n Rectangle\n Triangle\n Circle\n Exit (To close program)\n";
std::getline(std::cin, Input);
if(Input == "Rectangle" || Input == "rectangle" || Input == "RECTANGLE") {
Shape* rect = new Rectangle;
std::cout << "\nDo you want to find the\n Area\n Perimeter\n";
std::getline(std::cin, Input);
if(Input == "Area" || Input == "AREA" || Input == "area") {
rect->set_values();
rect->printarea();
delete rect;
std::cin.ignore();
}
else if(Input== "Perimeter" || Input == "PERIMETER" || Input == "perimeter") {
rect->set_values();
rect->printperim();
delete rect;
std::cin.ignore();
}
else { std::cout << "\n\nWrong Input Received."; delete rect; }
}
else if(Input == "Triangle" || Input == "triangle" || Input == "TRIANGLE") {
Shape* tri = new Triangle;
std::cout << "\nDo you want to find\n Area\n Perimeter\n";
std::getline(std::cin, Input);
if(Input == "Area" || Input == "area" || Input == "AREA") {
tri->set_values();
tri->printarea();
delete tri;
std::cin.ignore();
}
else if(Input == "Perimeter" || Input == "Perimeter" || Input == "PERIMETER") {
tri->set_values();
tri->printperim();
delete tri;
std::cin.ignore();
}
else { std::cout << "\n\nWrong Input Received."; delete tri; }
}
else if(Input == "Circle" || Input == "circle" || Input == "CIRCLE") {
Shape* circ = new Circle;
std::cout << "\nDo you want to find the\n Area\n Circumfrence\n";
std::getline(std::cin, Input);
if(Input == "Area" || Input == "area" || Input == "AREA") {
circ->set_values();
circ->printarea();
delete circ;
std::cin.ignore();
}
else if(Input == "Circumfrence" || Input == "circumfrence" || Input == "CIRCUMFRENCE") {
circ->set_values();
circ->printperim();
delete circ;
std::cin.ignore();
}
else { std::cout << "\n\nWrong Input Received."; delete circ; }
}
else if(Input == "Exit" || Input == "exit" || Input == "EXIT") {
exit(0);
}
else std::cout << "\nWrong input received. \nPlease make sure you spelt the word correctly and used capitals.";
std::cout << "\n\n\n Press <Enter> to continue.";
std::cin.get();
system("CLS");
}
return 0;
}
If you can make it smaller and more efficient, whilst doing the same tasks, I will award you with some Internet points.
#include <iostream>
#include <string>
#include <math.h>
class Shape {
protected:
double height, width;
public:
virtual void set_values() = 0;
virtual double area() = 0;
virtual double perimeter() = 0;
void printarea()
{ std::cout << "\nThe area is " << this->area() << " squared.\n"; }
virtual void printperim() = 0;
};
class Triangle: public Shape {
public:
double area()
{
return height * width / 2; }
double perimeter() {
double z = ((width * width) - ((height / 2) * (height / 2)));
z = sqrt(z);
return z + z + height;
}
void printperim() {
std::cout << "\nThe perimeter is " << this->perimeter() << ".\n";
}
void set_values() {
std::cout << "Please enter the height: ";
std::cin >> height;
std::cout << "And the width: ";
std::cin >> width;
}
};
class Rectangle: public Shape {
public:
double area()
{
return height * width; }
double perimeter() {
return (height * 2) + (width * 2);
}
void printperim() {
std::cout << "The perimeter is " << this->perimeter() << ".\n";
}
void set_values() {
std::cout << "Please enter the height: ";
std::cin >> height;
std::cout << "And the width: ";
std::cin >> width;
}
};
class Circle: public Shape {
public:
double area()
{
return height * height * 3.14159265; }
double perimeter() {
return (height * 2) * 3.14159265; }
void printperim() {
std::cout << "The circumfrence is " << this->perimeter() << ".\n";
}
void set_values() {
std::cout << "Please enter the radius: ";
std::cin >> height; }
};
int main() {
std::string Input;
while(true) {
std::cout << "Please choose a shape (Type it in, then press enter)\n Rectangle\n Triangle\n Circle\n Exit (To close program)\n";
std::getline(std::cin, Input);
if(Input == "Rectangle" || Input == "rectangle" || Input == "RECTANGLE") {
Shape* rect = new Rectangle;
std::cout << "\nDo you want to find the\n Area\n Perimeter\n";
std::getline(std::cin, Input);
if(Input == "Area" || Input == "AREA" || Input == "area") {
rect->set_values();
rect->printarea();
delete rect;
std::cin.ignore();
}
else if(Input== "Perimeter" || Input == "PERIMETER" || Input == "perimeter") {
rect->set_values();
rect->printperim();
delete rect;
std::cin.ignore();
}
else { std::cout << "\n\nWrong Input Received."; delete rect; }
}
else if(Input == "Triangle" || Input == "triangle" || Input == "TRIANGLE") {
Shape* tri = new Triangle;
std::cout << "\nDo you want to find\n Area\n Perimeter\n";
std::getline(std::cin, Input);
if(Input == "Area" || Input == "area" || Input == "AREA") {
tri->set_values();
tri->printarea();
delete tri;
std::cin.ignore();
}
else if(Input == "Perimeter" || Input == "Perimeter" || Input == "PERIMETER") {
tri->set_values();
tri->printperim();
delete tri;
std::cin.ignore();
}
else { std::cout << "\n\nWrong Input Received."; delete tri; }
}
else if(Input == "Circle" || Input == "circle" || Input == "CIRCLE") {
Shape* circ = new Circle;
std::cout << "\nDo you want to find the\n Area\n Circumfrence\n";
std::getline(std::cin, Input);
if(Input == "Area" || Input == "area" || Input == "AREA") {
circ->set_values();
circ->printarea();
delete circ;
std::cin.ignore();
}
else if(Input == "Circumfrence" || Input == "circumfrence" || Input == "CIRCUMFRENCE") {
circ->set_values();
circ->printperim();
delete circ;
std::cin.ignore();
}
else { std::cout << "\n\nWrong Input Received."; delete circ; }
}
else if(Input == "Exit" || Input == "exit" || Input == "EXIT") {
exit(0);
}
else std::cout << "\nWrong input received. \nPlease make sure you spelt the word correctly and used capitals.";
std::cout << "\n\n\n Press <Enter> to continue.";
std::cin.get();
system("CLS");
}
return 0;
}