PDA

View Full Version : سوال: گرفتن مختصات یک مستطیل



Eng_hamed
پنج شنبه 17 آذر 1390, 13:41 عصر
این برنامه جز تمرین های کتاب دایتل است
برنامه ای بنویسید که مختصات 4 نقطه را بگیرد به صورتی که این مختصات در ربع اول باشد و x و y بزرگتر از 20 نباشد.سپس طول،عرض،محیط و مساحت رو بدست بیاره.
من این برنامه رو این جوری نوشتم ولی فقط قسمت آخرش درست نیست کسی میتونه کمک کنه!:خجالت:


#include <iostream.h>
#include <math.h>

class rectangle
{ public:
rectangle(int=0,int=0,int=0,int=0,int=0,int=0,int= 0,int=0);
void get();
void set(int,int,int,int,int,int,int,int);
int setx(int);
int sety(int);

private:
void print();
void perimeter();
void area();
void lengthwidth();
void square();
int l,w,s,p,a,b,c,d,e,f;
int fx,fy,sx,sy,tx,ty,fox,foy;
};
rectangle::rectangle(int fx,int fy,int sx,int sy,int tx,int ty,int fox,int foy)
{set(fx,fy,sx,sy,tx,ty,fox,foy);}




void rectangle::get()
{cout<<"Please enter the value for the first point:\n";
cin>>fx>>fy;
cout<<"\nPlease enter the value for the second point:\n";
cin>>sx>>sy;
cout<<"\nPlease enter the value for the third point:\n";
cin>>tx>>ty;
cout<<"\nPlease enter the value for the fourth point:\n";
cin>>fox>>foy;
cout<<"\n\n";
set(fx,fy,sx,sy,tx,ty,fox,foy);
}

void rectangle::set(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{ fx=setx(x1); fy=sety(y1);
sx=setx(x2); sy=sety(y2);
tx=setx(x3); ty=sety(y3);
fox=setx(x4); foy=sety(y4);
a=sqrt(((fx-sx)*(fx-sx))+((fy-sy)*(fy-sy)));
b=sqrt(((tx-fox)*(tx-fox))+((ty-foy)*(ty-foy)));
if(a==b)
{ c=sqrt(((fx-tx)*(fx-tx))+((fy-ty)*(fy-ty)));
d=sqrt(((sx-fox)*(sx-fox))+((sy-foy)*(sy-foy)));
if(c==d)
{ e=sqrt(((fx-fox)*(fx-fox))+((fy-foy)*(fy-foy)));
f=sqrt(((sx-tx)*(sx-tx))+((sy-ty)*(sy-ty)));
if(e==f)
{ lengthwidth();
perimeter();
area();
print();
}
else
{cout<<"The values that you have entered are invalid,Try again!\n";
get();
}
}
else
{ cout<<"The values that you have entered are invalid,Try again!\n";
get();
}
}
else
{ cout<<"The values that you have entered are invalid,Try again!\n";
get();
}


}

int rectangle::setx(int x)
{ if(x>=0)
{ if(x>20)
{ cout<<"Please enter the right value for x:\n";
cin>>x;
setx(x);
}
else
{ return x;}
}
else
{ cout<<"Please enter the right value for x:\n";
cin>>x;
setx(x);
}
}

int rectangle::sety(int y)
{ if(y>=0)
{ if(y>20)
{ cout<<"Please enter the right value for y:\n";
cin>>y;
sety(y);
}
else
{ return y;}
}
else
{ cout<<"Please enter the right value for y:\n";
cin>>y;
sety(y);
}
}

void rectangle::square()
{ if(a==b || a==e || d==e)
{ cout<<"The desired shape is Square!\n";}
else
{ cout<<"The desired shape is Rectangular!\n";}
}

void rectangle::lengthwidth()
{ if(a>c)
{ if(a<e)
{ l=a; w=c;}
}
else if(a>e)
{ if(a<c)
{l=a; w=e;}
}
else if(c>a)
{ if(c<e)
{l=c; w=a;}
}
else if(c>e)
{ if(c<a)
{l=c; w=e;}
}
else if(e>c)
{ if(e<a)
{l=e; w=c;}
}
else if(e>a)
{ if(e<c)
{l=e; w=a;}
}
}

void rectangle::perimeter()
{ p=2*(l+w);
}

void rectangle::area()
{ s=l*w;
}

void rectangle::print()
{ square();
cout<<"Length:"<<l<<"\n"<<"Width:"<<w<<"\n"<<"Perimeter:"<<p<<"\n"<<"Area:"<<s;
}

int main()
{ int r;
rectangle k;
k.get();
cin>>r;
return 0;

}