• 0

[C++] Any Suggestions?


Question

If you have any suggestions or hints plz elaborate so I can fix.

PURPOSE: Use and ADT to hold values of a triangle and include basic functions

//NEED TO FINISH COMMENTS AND ADD SOME FIXES HERE AND THERE THATS IT

#include <iostream>
#include <cmath> // Used to implement sqrt()
using namespace std;

void menu_a(); // Menu for system-defined Triangle
void menu_b(); // Menu for user-designed Triangle

/*
This class defines the Triangle and holds the 
*/
class Triangle{
private:
	float side_a;
	float side_b;
	float side_c;
	bool equil;
	bool right;
	bool isos;

public:
	float perimeter;
	float area;
	float s;
	int blah;

	Triangle();
	Triangle(float x, float y, float z);

	void getSides();
	void setSides(float x, float y, float z);
	void check();
	void getArea();
	void getPerimeter();
};

Triangle::Triangle()
{
	blah = 0;
	side_a = 9;
	side_b = 9;
	side_c = 9;
}

Triangle::Triangle(float x, float y, float z)
{
	blah = 0;
	side_a = x;
	side_b = y;
	side_c = z;

	getSides();
}

void Triangle::check()
{
	//TEST for Right Triangle
	if ((side_c)*(side_c) == (((side_b)*(side_b))+(side_a)*(side_a)))
  right = true;
	if ((side_b)*(side_b) == (((side_c)*(side_c))+(side_a)*(side_a)))
  right = true;
	if ((side_a)*(side_a) == (((side_b)*(side_b))+(side_c)*(side_c)))
  right = true;

	//TEST for Equilateral
	if (side_a == side_b && side_a == side_c)
  equil = true;

	//TEST for Isosceles
	if (side_c == side_b)
  isos = true;
	if (side_c == side_a)
  isos = true;
	if (side_a == side_b)
  isos = true;

	cout << "------------------------------------------" << endl;
	cout << "Right triangle: ";
	if (right == true)
  cout << "TRUE";
	cout << endl;
	cout << "Equilateral triangle:  ";
	if (equil == true)
  cout << "TRUE";
	cout << endl;
	cout << "Isosceles  Triangle: ";
	if (isos == true)
  cout << "TRUE";
	cout << endl;

}

/* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */
void Triangle::getArea()
{
	s = (side_a + side_b + side_c)/2;

	area = sqrt(s * (s - side_a) * (s - side_b) * (s - side_b));
	cout << "------------------------------------------" << endl;
    cout << "Area of the Triangle is " << area << endl;
}


/* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */
void Triangle::getPerimeter()
{
	perimeter = (side_a + side_b + side_c);
	cout << "------------------------------------------" << endl;
	cout << "Perimeter of the Triangle is " << perimeter << endl;
}

/* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */
void Triangle::getSides()
{
	cout << "------------------------------------------" << endl;
	cout << "Side a = " << side_a << endl;
	cout << "Side b = " << side_b << endl;
	cout << "Side c = " << side_c << endl;
}

/* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */
void Triangle::setSides(float x, float y, float z)
{
	side_a = x;
	side_b = y;
	side_c = z;

	getSides();
}

int main()
{
	int question_1 = 0;

	cout << "         Welcome to Triangulate    " << endl;
	cout << "------------------------------------------" << endl;
	cout << "Note: Triangulate will set default values" << endl;
	cout << "to the triangle to a isoceles triangle with" << endl;
	cout << "the lengths of the sides all measuring 9" << endl;
	cout << "------------------------------------------" << endl;
    do
	{  
  cout << "Would you like to use this triangle or define your own?" << endl;
  cout << "	(Please input 1 or 2 and Press ENTER}" << endl;
  cout << "1. Use the Predefined Triangle" << endl;
  cout << "2. Define your own Triangle" << endl;
  cin >> question_1;

	}while(question_1 < 1 || question_1 > 2);

	if (question_1 == 1)
  menu_a();
	else if (question_1 == 2)
  menu_b();
	else
  cout << "You caused an error.  Please restart the program." << endl;

	return 0;
}

/* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */
void menu_a()
{

	Triangle a;
	int question_2 = 0;
	do
	{
  cout << "------------------------------------------" << endl;
  cout << "1. Get triangle measurements" << endl;
  cout << "2. Get Area of the triangle" << endl;
  cout << "3. Get the Perimeter" << endl;
  cout << "4. Check for recognizable triangle shape" << endl;
  cout << "5. Make your own triangle" << endl;
  cout << "6. Exit Triangulate" << endl;
  cin >> question_2;

  if (question_2 == 1)
 	 a.getSides();
  else if (question_2 == 2)
 	 a.getArea();
  else if (question_2 == 3)
 	 a.getPerimeter();
  else if (question_2 == 4)
 	 a.check();	
  else if (question_2 == 5)
 	 menu_b();
	}while(question_2 != 6);
}

/* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */
void menu_b()
{
	Triangle b;
	int question_3 = 0;
	float side_x, side_y, side_z;
	cout << "------------------------------------------" << endl;
	cout << "Please input your triangle sides lengths" << endl;
	cout << "Enter side a: ";
	cin >> side_x;
	cout << "Enter side b: ";
	cin >> side_y;
	cout << "Enter side c: ";
	cin >> side_z;

	b.setSides(side_x, side_y, side_z);

	do
	{
  cout << "------------------------------------------" << endl;
  cout << "1. Get triangle measurements" << endl;
  cout << "2. Get Area of the triangle" << endl;
  cout << "3. Get the Perimeter" << endl;
  cout << "4. Check for recognizable triangle shape" << endl;
  cout << "5. Make your own triangle" << endl;
  cout << "6. Exit Triangulate" << endl;
  cin >> question_3;

  if (question_3 == 1)
 	 b.getSides();
  else if (question_3 == 2)
 	 b.getArea();
  else if (question_3 == 3)
 	 b.getPerimeter();
  else if (question_3 == 4)
 	 b.check();
  else if (question_3 == 5)
  {
 	 cout << "Please input your triangle sides lengths" << endl;
 	 cout << "Enter side a: ";
 	 cin >> side_x;
 	 cout << "Enter side b: ";
 	 cin >> side_y;
 	 cout << "Enter side c: ";
 	 cin >> side_z;

 	 b.setSides(side_x, side_y, side_z);
            
  }
	}while(question_3 != 6);
}

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0
correct me if i'm wrong, but aren't ADT's supposed to be pure virtual? i would think it would be something like ADT Shape, with a derived triangle class (or square, etc.). About.com

are you thinking about Dynamic Memory Allocatin using pointers?

Link to comment
Share on other sites

  • 0

found it

This lesson covers abstract data types and pure virtual functions.

ADTs and virtual functions are discussed on that about.com page

ADT's and virtual functions arent the same

Link to comment
Share on other sites

  • 0
the point is pure virtual funcs keep someone from instantiating the base class. either way, i'm thinking he wants to instantiate the triangle class, which would mean it's not an ADT.

Yeah, you're right... sorry. My brain is kinda mush right now. I was thinking of other languages.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.