计算机天才从不认输 2022-03-18 16:39 采纳率: 40%
浏览 53
已结题

我输入的坐标对了,但不是长方形它也会输出长和宽来,怎么改才能使当它不是长方形的时候就不输出长和宽,是长方形或正方形的时候才输出?最好可以贴出改之后的代码看看,不要回答没有用的东西


#include <iostream>
#include <math.h>
using namespace std;

class Rectangle
{
 public:
  Rectangle(float *x,float *y)
  {
   setCoordinate(x,y); 
  }
  
  void setCoordinate(float *x,float *y)  //设置坐标 
  {
   for(int i=0;i<4;i++)
   {
    if((x[i]<=0.0)||(x[i]>20.0)||(y[i]<=0.0)||(y[i]>20.0)) 
    {
    cout<<"有错误,请重新输入\n"; 
    break; 
    }
    else
    X[i]=x[i];
    Y[i]=y[i];
   }
   
if(x[3]-x[2]==x[1]-x[0]&&y[3]-y[2]==y[1]-y[0])
{
if((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0])+(x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0])==
(x[3]-x[1])*(x[3]-x[1])+(y[3]-y[1])*(y[3]-y[1]))
   cout<<"该四边形是长方形\n";
}
else
cout<<"该四边形不是长方形\n";
   }
  //获得长
  float getLength()                    
  {
   float length,temp1,temp2;
   temp1=pow((Y[1]-Y[0])*(Y[1]-Y[0])+(X[1]-X[0])*(X[1]-X[0]),0.5);
   temp2=pow((Y[2]-Y[1])*(Y[2]-Y[1])+(X[2]-X[1])*(X[2]-X[1]),0.5);
   if(temp1>temp2) 
       length=temp1;
   else
       length=temp2;
   return length;
  }
  //获得宽
  float getWidth()        
  {
    
   float width,temp1,temp2;
   temp1=pow((Y[1]-Y[0])*(Y[1]-Y[0])+(X[1]-X[0])*(X[1]-X[0]),0.5);
   temp2=pow((Y[2]-Y[1])*(Y[2]-Y[1])+(X[2]-X[1])*(X[2]-X[1]),0.5);
   if(temp1>temp2) 
       width=temp2;
   else
       width=temp1;
   return width;
  }
  //计算周长
  float getPerimeter()          
  {
   return (2*getLength()+2*getWidth());
  } 
  //计算面积 
  float getArea()           
  {
   return (getLength()*getWidth());
  } 
  //判断是否是正方形 
  void guessSquare()        
  {
   if((getWidth()==0)||(getLength()==0))
   cout<<"无法判断\n"; 
   
   else if(getWidth()==getLength())
   {
   cout<<"该图是正方形\n";
   }
   else  
   {
   cout<<"该图不是正方形\n";
   }  
  }
  
 private:
  float X[4],Y[4],*d; 
};

int main()
{
 int i;
 float x[4],y[4];
 float *p=x,*q=y;  //指针指向数组
 cout<<"从长方形左上角开始顺时针输入坐标(坐标在第一象限且小于等于20\n";
 for (i=0;i<4;i++)              
 {
 cin>>x[i];//输入数组
 cin>>y[i];
 }
  Rectangle nb(p,q);//给类成员
 cout<<"长为:"<<nb.getLength()<<endl;
 cout<<"宽为:"<<nb.getWidth()<<endl;
 nb.guessSquare();
 return 0; 
}

  • 写回答

2条回答 默认 最新

  • 关注

    运行结果:
    是长方形的结果:

    img

    不是长方形的结果:

    img

    代码:

    
    #include <iostream>
    #include <math.h>
    using namespace std;
    class Rectangle
    {
    public:
        Rectangle(double* x, double* y)
        {
            width = 0;
            length = 0;
            setCoordinate(x, y);
        }
    
        void setCoordinate(double* x, double* y)  //设置坐标 
        {
            for (int i = 0; i < 4; i++)
            {
                if ((x[i] <= 0.0) || (x[i] > 20.0) || (y[i] <= 0.0) || (y[i] > 20.0))
                {
                    cout << "有错误,请重新输入\n";
                    break;
                }
                else
                    X[i] = x[i];
                Y[i] = y[i];
            }
    
            //左顶点x[0],y[0],右顶点x[1],y[1] , 0 1 2节点构成直角三角形
            double length1 = (x[1] - x[0]) * (x[1] - x[0]) + (y[1]-y[0]) * (y[1]-y[0]);
            double width1 = (x[1] - x[2]) * (x[1] - x[2]) + (y[1] - y[2]) * (y[1] - y[2]);
            double duijiaoxian = (x[2] - x[0]) * (x[2] - x[0]) + (y[2] - y[0]) * (y[2] - y[0]);
            //0 2 3节点构成直角三角形
            double width2 = (x[0] - x[3]) * (x[0] - x[3]) + (y[0] - y[3]) * (y[0] - y[3]);
            double length2 = (x[2] - x[3]) * (x[2] - x[3]) + (y[2] - y[3]) * (y[2] - y[3]);
    
            if (length1 == length2 && width1 == width2 && length1 + width1 == duijiaoxian)
            {
                isRectangle = 1;
                if (length1 > width1)
                {
                    length = sqrt(length1);
                    width = sqrt(width1);
                }
                else
                {
                    length = sqrt(width1);
                    width = sqrt(length1);
                }
                cout << "该四边形是长方形\n";
            }
            else
            {
                isRectangle = 0;
                cout << "该四边形不是长方形\n";
            }
                
            
    
        }
        //获得长
        double getLength()
        {
            return length;
        }
        //获得宽
        double getWidth()
        {
            return width;
        }
        //计算周长
        double getPerimeter()
        {
            return (2 * getLength() + 2 * getWidth());
        }
        //计算面积 
        double getArea()
        {
            return (getLength() * getWidth());
        }
        //判断是否是正方形 
        void guessSquare()
        {
            if ( width == 0 || length == 0)
                cout << "无法判断\n";
    
            else if ( length == width)
            {
                cout << "该图是正方形\n";
            }
            else
            {
                cout << "该图不是正方形\n";
            }
        }
    
    private:
        double X[4], Y[4], width,length;
        int isRectangle; //是否是长方形标记
    };
    int main()
    {
        int i;
        double x[4], y[4];
        double* p = x, * q = y;  //指针指向数组
        cout << "从长方形左上角开始顺时针输入坐标(坐标在第一象限且小于等于20\n";
        for (i = 0; i < 4; i++)
        {
            cin >> x[i];//输入数组
            cin >> y[i];
        }
        Rectangle nb(p, q);//给类成员
        cout << "长为:" << nb.getLength() << endl;
        cout << "宽为:" << nb.getWidth() << endl;
        nb.guessSquare();
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月18日
  • 已采纳回答 3月18日
  • 创建了问题 3月18日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)