Hey everyone. I'm writing a program which allows the user to enter information about 2 shapes, either a square, triangle, or rectangle, then 'adds' the shaps together. The resulting shape is a polygon. I understand how this is done, just not sure about the code. I need to overload the + operator to add the shapes. Also I know that I need to make a comparison between sides begin combined, and subtract the smaller side from the area. Also I understand that I need to renumber the sides. I'm just not sure how to do this. Code is posted below -
-
#include <iostream>
-
using namespace std;
-
class Polygon
-
{
-
protected:
-
int length[20],num_sides;
-
public:
-
void set_values (int sl[], int n);
-
Polygon();
-
~Polygon(){}
-
void dimensions(int length[],int a);
-
void perimeter(int length[],int a);
-
void area (int length[],int a);
-
Polygon operator+(Polygon &);
-
};
-
Polygon::Polygon()
-
{
-
int i;
-
for (i=0;i<20;++i)
-
{
-
length[i]=0;
-
}
-
num_sides=0;
-
}
-
void Polygon::set_values(int sl[],int n)
-
{
-
int i=0,j=1, length[20];
-
for(i=0;i<n;++i)
-
{
-
length[i]=sl[i];
-
}
-
perimeter(length,n);
-
area(length,n);
-
dimensions(length,n);
-
}
-
//Polygon Polygon::operator+(Polygon &value)
-
//{
-
// return(0);
-
//}
-
void Polygon::dimensions(int length[], int a)
-
{
-
int i;
-
if (a==1)
-
{
-
cout<<"Dimensions: "<<length[0]<<"x"<<length[0]<<endl;
-
}
-
else if (a==2)
-
{
-
cout<<"Dimensions: "<<length[0]<<"x"<<length[1]<<endl;
-
}
-
else
-
cout<<"Dimensions: "<<length[0]<<"x"<<length[1]<<"x"<<length[2]<<endl;
-
}
-
void Polygon::perimeter(int length[],int a)
-
{
-
int perim=0,i=0;
-
if (a==2)
-
{
-
perim=(2*length[0]+2*length[1]);
-
}
-
else if (a==1)
-
{
-
perim=4*length[0];
-
}
-
else if (a==3)
-
{
-
perim=length[0]+length[1]+length[2];
-
}
-
cout<<"Perimeter: "<<perim<<endl;
-
}
-
void Polygon::area(int length[],int a)
-
{
-
int area=0;
-
if (a==1)
-
{
-
area=length[0]*length[0];
-
}
-
else if (a==2)
-
{
-
area=length[0]*length[1];
-
}
-
else if (a==3)
-
{
-
area=(length[0]*length[1])/2;
-
}
-
cout<<"Area: "<<area<<endl;
-
}
-
class Rectangle: public Polygon
-
{
-
public:
-
Rectangle(){}
-
~Rectangle(){}
-
void set_values();
-
};
-
void Rectangle::set_values()
-
{
-
int i, rs[4]={0};
-
cout<<"Enter the length of your side: "<<endl;
-
cin>>rs[0];
-
rs[2]=rs[0];
-
cout<<"Enter the width of your side: "<<endl;
-
cin>>rs[1];
-
rs[3]=rs[1];
-
Polygon::set_values(rs,2);
-
}
-
class Triangle: public Polygon
-
{
-
public:
-
Triangle(){}
-
~Triangle(){}
-
void set_values ();
-
};
-
void Triangle::set_values()
-
{
-
int ls[3]={0};
-
cout<<"Enter the length of side 1 of your triangle: "<<endl;
-
cin>>ls[0];
-
cout<<"Enter the length of side 2 of your triangle: "<<endl;
-
cin>>ls[1];
-
cout<<"Enter the length of side 3 of your triangle: "<<endl;
-
cin>>ls[2];
-
Polygon::set_values(ls, 3);
-
}
-
class Square: public Polygon
-
{
-
public:
-
Square (){}
-
~Square(){}
-
void set_values ();
-
};
-
void Square::set_values()
-
{
-
int i=0, ss[1]={0};
-
cout<<"Enter the length of the side of your square: "<<endl;
-
cin>>ss[0];
-
Polygon::set_values(ss,1);
-
}
-
void main ()
-
{
-
int ans=0,sel=0,sel1=0,n=0;
-
Polygon x,y,z;
-
Rectangle rect;
-
Triangle trgl;
-
Square sqr;
-
cout<<"What would you like to do?"<<endl;
-
do
-
{
-
cout<<"Enter 1 to enter and display dimensions of a shape."<<endl;
-
cout<<"Enter 2 to add two shapes."<<endl;
-
cin>>sel;
-
if (sel==1)
-
{
-
do
-
{
-
cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle"<<endl;
-
cin>>ans;
-
if(ans==1)
-
{
-
cout<<"You have chosen a rectangle!"<<endl;
-
rect.set_values();
-
}
-
else if (ans==2)
-
{
-
cout<<"You have chosen a square!"<<endl;
-
sqr.set_values();
-
}
-
else if (ans==3)
-
{
-
cout<<"You have chosen a triangle!"<<endl;
-
trgl.set_values();
-
}
-
else
-
cout<<"Make a selection from the menu!"<<endl<<endl;
-
}
-
while (ans<1||ans>3);
-
}
-
else if (sel==2)
-
{
-
while (n<2)
-
{
-
cout<<"Select your shape: "<<endl;
-
cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle."<<endl;
-
cin>>sel1;
-
if (sel==1)
-
{
-
cout<<"Enter your dimensions for the rectangle"<<endl;
-
rect.set_values();
-
++n;
-
}
-
else if (sel==2)
-
{
-
cout<<"Enter your dimensions for the square"<<endl;
-
sqr.set_values();
-
++n;
-
}
-
else if (sel==3)
-
{
-
cout<<"Enter your dimensions for the triangle"<<endl;
-
trgl.set_values();
-
++n;
-
}
-
else
-
cout<<"Make a selection from the menu."<<endl;
-
}
-
}
-
else
-
cout<<"Make a selection from the menu."<<endl;
-
}
-
while(sel<1||sel>2);
-
}
-
-
Thanks for looking,
J
8 2700
Also I know that I need to make a comparison between sides begin combined, and subtract the smaller side from the area.
I do not understand what this means.
I do not understand what this means.
Heh...yeah that doesn't make sense. Say I am adding a triangle and a rectangle. When those shapes are added together, I will have a shape which is now neither a triangle or rectangle...it will be a polygon. The length of the side that is on the triangle, where the rectangle is added, will change. That is where the comparison comes in. The length of the side (on the triangle) will have to have the length of the rectangle side subtracted from it.
Say you have a triangle with each side being 3 inches, and you have a rectangle with a side that is 2 inches. When you combine the two sides, you have to subtract the 2 inches off, because you now have some odd shape, but 1 inch is left on the original triangle side.
Ugh...does that make sense, it's hard to explain without a diagram.
J
How do you decide which side to make common between the two polygons?
Of, course, if you use only squares and triangles with equal sides, it doesn't matter.
How do you decide which side to make common between the two polygons?
I can pick any side....the instructor said it doesn't matter
J
It doesn't matter?!?
This seems to be a poorly defined problem. No wonder you are having difficulties with it.
In order to design an algorithm to do something, the designer needs to know what is required.
I suggest you come up with what are sometimes called "use cases". Invent some concrete examples of "adding" polygons together and write down the inputs and outputs. This exercise should show what your options are, and you can pin down exactly what you want to do.
Some questions you might want to think about.
Do you want to keep the area of the summed polygons equal to the summed area of the originals?
How about the perimiter?
If I remember the original code, then it can only handle squares, rectangles and triangles. How many vertices does the sum of a triangle and a square have? How will you code deal with more than 4?
Ok so I've worked on the problem some more, and unfortunately I can't edit the other post to remove that code. I have added 6 overloaded operator functions however I am receiving an error. The error has to do with the return value in those newly added functions. I receive a syntax error ';' on the return statement line, however if I remove the semicolon I receive another error. -
-
#include <iostream>
-
using namespace std;
-
class Polygon
-
{
-
protected:
-
int length[20],num_sides;
-
public:
-
void set_values (int sl[], int n);
-
Polygon();
-
~Polygon(){}
-
void dimensions(int length[],int a);
-
void perimeter(int length[],int a);
-
void area (int length[],int a);
-
-
};
-
Polygon::Polygon()
-
{
-
int i;
-
for (i=0;i<20;++i)
-
{
-
length[i]=0;
-
}
-
num_sides=0;
-
}
-
void Polygon::set_values(int sl[],int n)
-
{
-
int i=0,j=1, length[20];
-
for(i=0;i<n;++i)
-
{
-
length[i]=sl[i];
-
}
-
perimeter(length,n);
-
area(length,n);
-
dimensions(length,n);
-
}
-
-
void Polygon::dimensions(int length[], int a)
-
{
-
int i;
-
if (a==1)
-
{
-
cout<<"Dimensions: "<<length[0]<<"x"<<length[0]<<endl;
-
}
-
else if (a==2)
-
{
-
cout<<"Dimensions: "<<length[0]<<"x"<<length[1]<<endl;
-
}
-
else
-
cout<<"Dimensions: "<<length[0]<<"x"<<length[1]<<"x"<<length[2]<<endl;
-
}
-
void Polygon::perimeter(int length[],int a)
-
{
-
int perim=0,i=0;
-
if (a==2)
-
{
-
perim=(2*length[0]+2*length[1]);
-
}
-
else if (a==1)
-
{
-
perim=4*length[0];
-
}
-
else if (a==3)
-
{
-
perim=length[0]+length[1]+length[2];
-
}
-
cout<<"Perimeter: "<<perim<<endl;
-
}
-
void Polygon::area(int length[],int a)
-
{
-
int area=0;
-
if (a==1)
-
{
-
area=length[0]*length[0];
-
}
-
else if (a==2)
-
{
-
area=length[0]*length[1];
-
}
-
else if (a==3)
-
{
-
area=(length[0]*length[1])/2;
-
}
-
cout<<"Area: "<<area<<endl;
-
}
-
class Rectangle: public Polygon
-
{
-
public:
-
Rectangle(){}
-
~Rectangle(){}
-
Polygon Rectangle::operator+(Rectangle A);
-
void set_values();
-
};
-
void Rectangle::set_values()
-
{
-
int i, rs[4]={0};
-
cout<<"Enter the length of your side: "<<endl;
-
cin>>rs[0];
-
rs[2]=rs[0];
-
cout<<"Enter the width of your side: "<<endl;
-
cin>>rs[1];
-
rs[3]=rs[1];
-
Polygon::set_values(rs,2);
-
}
-
Polygon Rectangle::operator +(Rectangle A)
-
{
-
Polygon temp;
-
return (Polygon);
-
}
-
class Triangle: public Polygon
-
{
-
public:
-
Triangle(){}
-
~Triangle(){}
-
Polygon Triangle::operator+(Triangle A);
-
Polygon Triangle::operator +(Rectangle A);
-
void set_values ();
-
};
-
void Triangle::set_values()
-
{
-
int ls[3]={0};
-
cout<<"Enter the length of side 1 of your triangle: "<<endl;
-
cin>>ls[0];
-
cout<<"Enter the length of side 2 of your triangle: "<<endl;
-
cin>>ls[1];
-
cout<<"Enter the length of side 3 of your triangle: "<<endl;
-
cin>>ls[2];
-
Polygon::set_values(ls, 3);
-
}
-
Polygon Triangle::operator+(Triangle A)
-
{
-
Polygon temp;
-
return(temp);
-
}
-
Polygon Triangle::operator +(Rectangle A)
-
{
-
Polygon temp;
-
return(Polygon);
-
}
-
class Square: public Polygon
-
{
-
public:
-
Square (){}
-
~Square(){}
-
Polygon Square::operator+(Square A);
-
Polygon Square::operator +(Rectangle A);
-
Polygon Square::operator +(Triangle A);
-
void set_values ();
-
};
-
void Square::set_values()
-
{
-
int i=0, ss[1]={0};
-
cout<<"Enter the length of the side of your square: "<<endl;
-
cin>>ss[0];
-
Polygon::set_values(ss,1);
-
}
-
Polygon Square::operator +(Square A)
-
{
-
Polygon temp;
-
return (temp);
-
}
-
Polygon Square::operator +(Rectangle A)
-
{
-
Polygon temp;
-
if(*this->side1>A.side1)
-
{
-
*this->side1=*this->side1-A.side1;
-
temp.side1=*this->side1;
-
}
-
return (temp);
-
}
-
Polygon Square::operator +(Triangle A)
-
{
-
return (Polygon);
-
}
-
void main ()
-
{
-
int ans=0,sel=0,sel1=0,sel2=0,n=0;
-
Polygon x,y,z;
-
Rectangle rect;
-
Triangle trgl;
-
Square sqr;
-
cout<<"What would you like to do?"<<endl;
-
do
-
{
-
cout<<"Enter 1 to enter and display dimensions of a shape."<<endl;
-
cout<<"Enter 2 to add two shapes."<<endl;
-
cin>>sel;
-
if (sel==1)
-
{
-
do
-
{
-
cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle"<<endl;
-
cin>>ans;
-
if(ans==1)
-
{
-
cout<<"You have chosen a rectangle!"<<endl;
-
rect.set_values();
-
}
-
else if (ans==2)
-
{
-
cout<<"You have chosen a square!"<<endl;
-
sqr.set_values();
-
}
-
else if (ans==3)
-
{
-
cout<<"You have chosen a triangle!"<<endl;
-
trgl.set_values();
-
}
-
else
-
cout<<"Make a selection from the menu!"<<endl<<endl;
-
}
-
while (ans<1||ans>3);
-
}
-
else if (sel==2)
-
{
-
cout<<"Select your shape: "<<endl;
-
cout<<"Enter 1 for rectangle, 2 for square, 3 for triangle."<<endl;
-
cin>>sel1;
-
if (sel1==1)
-
{
-
cout<<"Enter your dimensions for the rectangle"<<endl;
-
rect.set_values();
-
++n;
-
}
-
else if (sel1==2)
-
{
-
cout<<"Enter your dimensions for the square"<<endl;
-
sqr.set_values();
-
++n;
-
}
-
else if (sel1==3)
-
{
-
cout<<"Enter your dimensions for the triangle"<<endl;
-
trgl.set_values();
-
++n;
-
}
-
else
-
{
-
cout<<"Make a selection from the menu."<<endl;
-
}
-
cout<<"Select your second shape."<<endl;
-
cin>>sel2;
-
if (sel2==1)
-
{
-
cout<<"Enter your dimensions for the rectangle"<<endl;
-
rect.set_values();
-
++n;
-
}
-
else if (sel2==2)
-
{
-
cout<<"Enter your dimensions for the square"<<endl;
-
sqr.set_values();
-
++n;
-
}
-
else if (sel2==3)
-
{
-
cout<<"Enter your dimensions for the triangle"<<endl;
-
trgl.set_values();
-
++n;
-
}
-
else
-
cout<<"Make a selection from the menu."<<endl;
-
}
-
while(sel<1||sel>2);
-
if (sel1==1&&sel2==1)
-
{
-
x=trgl+trgl;
-
}
-
else if (sel1==1&&sel2==2||sel1==2&&sel2==1)
-
{
-
x=sqr+rect;
-
}
-
else if (sel1==1&&sel2==3||sel1==3&&sel2==1)
-
{
-
x=trgl+rect;
-
}
-
else if (sel1==2&&sel2==3||sel1==3&&sel2==2)
-
{
-
x=sqr+trgl;
-
}
-
else if (sel1==2&&sel2==2)
-
{
-
x=sqr+sqr;
-
}
-
else if (sel1==1&&sel2==1)
-
{
-
x=rect+rect;
-
}
-
}
-
}
-
-
Thanks for looking,
J
Just a stab, but it looks like in some cases you are returning the data type and not a variable of the data type...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David |
last post by:
i am using the imagepolygon function to create a shape on an image i have,
however i want to put more than just one polygon onto this image, i have no
idea how to do this. Do i have to call the...
|
by: David |
last post by:
I have an array that contains numbers in string elements , i want to convert
this to integers, i have made a for loop that uses the number of elements in
the array and then adds 0, thereby...
|
by: smjmitchell |
last post by:
Hi All,
I am developing a application in VB 6.0. I need to plot a 3D mesh of
quadrilateral and triangular polygons. I have this working OK in
wireframe. The question I have is what is the...
|
by: Sri |
last post by:
How do you add an n-bit number in C?
Regards,
Sri
|
by: weberwhennner |
last post by:
Hi All,
Since two days Ive been trying to draw lines and polygons interactively (using mouse clicks), but all ive found is some libraries that draw shapes using predefined cooredinates.
Ive...
|
by: 418928 |
last post by:
Hi everybody,
I would like to do things like zooming a svg image and (if possible)
adding labels to identify some polygons. Is there any tool (if
possible, free) that can help me with this? Of...
|
by: Number 11950 - GPEMC! Replace number with 11950 |
last post by:
Are polygonal buttons or perhaps polygons with tooltips and and click events
available in VS8 or is it too early yet?
Thanks in advance...
--
Timothy Casey GPEMC! >11950 is the...
|
by: santiago |
last post by:
I guess one cannot do this:
arraytot = arraytot + arraydet;
So, what's the trick to adding arrays like this?
Thanks.
|
by: nttamdn |
last post by:
Anyone help me please.
I want anytime mousemove to one polygon, it's opacity becomes 0.3, and mouseout polygon, i'ts opacity return to 0 again. I means mousemove to show the polygon and hiding it...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |