程式員面試題精選(40):一道SPSS筆試題求解

來源:互聯網
上載者:User

/*================================================================================
題目:輸入四個點的座標,求證四個點是不是一個矩形
關鍵點:
1.相鄰兩邊斜率之積等於-1,
2.矩形邊與座標系平行的情況下,斜率無窮大不能用積判斷。
3.輸入四點可能不按順序,需要對四點排序
作者:sunnyrain
日期:2007.9.2 & 2007.9.3
運行環境:vc++ 6.0
==================================================================================*/
#include<iostream>
#include<limits>
using namespace std;

const double MAX = numeric_limits<double>::max();  //斜率最大值

class Point
{
 float x;
 float y;
public:
 Point(float _x = 0, float _y = 0):x(_x),y(_y){}
 float getX() const
 {
  return x;
 }

 float getY() const
 {
  return y;
 }

 //為點重新設定座標
 void set(float _x,float _y)
 {
  x = _x;
  y = _y;
 }

 //重載 == 成員操作符
 bool operator == (Point& p)
 {
  return (this->x == p.x && this->y == p.y);
 }

 //重載流插入操作符
 friend istream & operator >> (istream& is, Point & p)
 {
  return is>>p.x>>p.y;
 }
};

class Line  //兩點形成一條直線/線段
{
 Point start;
 Point end;
 double k;  //斜率
public:
 Line(){}
 Line(Point s,Point e):start(s),end(e)
 {
  if(start.getX() - end.getX() != 0)
   k = (start.getY() - end.getY())/(start.getX() - end.getX()) ;
  else
   k = MAX;  //兩點x座標相等則斜率無窮大
 }
 double getK()
 {
  return k;
 }
};

//尋找數組pp中是否存在點p,是返回數組序號,否返回-1
int findPoint(Point *pp,int size,Point &p)
{
 for(int i=0;i<size;i++)
 {
  if(pp[i] == p)
   return i;
 }
 return -1;
}

//主函數
int main()
{
 Point p[4];
 Point *s[4];
 int i;

 for(i=0; i<4; i++)
 {
  cout<<"Please input the coordinates of the "<<i+1<<" point in format /"x.xx y.yy/""<<endl;
  cin>>p[i];
 }
 
/* p[0].set(0,0);
 p[1].set(1,1);
 p[2].set(2,0);
 p[3].set(1,-1);*/

 float left,up,right,down;

 //擷取四點x座標最大值和最小值
 left = p[0].getX(); //left為四點x座標最小值
 right = p[0].getX(); //right為四點x座標最大值
 for(i=1;i<4;i++)
 {
  if(left>p[i].getX())
   left = p[i].getX();
  if(right<p[i].getX())
   right = p[i].getX();
 }

 //擷取四點y座標最大值和最小值
 up = p[0].getY(); //up為四點y座標最大值
 down = p[0].getY(); //四點y座標最小值
 for(i=1;i<4;i++)
 {
  if(up<p[i].getY())
   up = p[i].getY();
  if(down > p[i].getY())
   down = p[i].getY();
 }

 //判斷矩形與座標系平行情況
 Point P1(left,up),P2(right,up),P3(right,down),P4(left,down);
 if(findPoint(p,4,P1) != -1 && findPoint(p,4,P2) != -1 && findPoint(p,4,P3) != -1 && findPoint(p,4,P4) != -1)
 {
  cout<<"是矩形"<<endl;
  return 0;
 }

 //按照順時針方向對四點排序
 for(i=0;i<4;i++)
 {
  if(p[i].getX() == left)
   s[0] = &p[i];
  else if(p[i].getY() == up)
   s[1] = &p[i];
  else if(p[i].getX() == right)
   s[2] = &p[i];
  else if(p[i].getY() == down)
   s[3] = &p[i];
 }

 //排序後的四點順時針相連組成矩形邊
 Line one(*s[0],*s[1]),two(*s[1],*s[2]),three(*s[2],*s[3]),four(*s[3],*s[0]);

 cout<<"k1 = "<<one.getK()<<endl;
 cout<<"k2 = "<<two.getK()<<endl;
 cout<<"k3 = "<<three.getK()<<endl;
 cout<<"k4 = "<<four.getK()<<endl;

 //判斷相鄰邊斜率之積是否都等於0
 if(one.getK()*two.getK() == -1 || (one.getK() == 0 && two.getK() == MAX) || (one.getK() == MAX && two.getK() == 0) )
  if(two.getK()*three.getK() == -1 || (two.getK() == 0 && three.getK() == MAX) || (two.getK() == MAX && three.getK() == 0) )
   if(three.getK()*four.getK() == -1 || (three.getK() == 0 && four.getK() == MAX) || (three.getK() == MAX && four.getK() == 0) )
    if(four.getK()*one.getK() == -1 || (four.getK() == 0 && one.getK() == MAX) || (four.getK() == MAX && one.getK() == 0) )
    {
     cout<<"是矩形!"<<endl;
     return 0;
    }

 cout<<"不是矩形"<<endl;
 return 0;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.