TWH13111 2023-02-24 11:40 采纳率: 0%
浏览 37

c++中未初始化对象问题,希望帮助.

在平面直角坐标系中,两点可以确定一条直线
如果有多点在一条直线上,那么这些点中任意两点确定的直线是同一条
给定平面上2x3个整点((x,y)l0<x<20y<3,xezyeZ
即横坐标是0到1(包含0和1)之间的整数、纵坐标是0到2(包含0和2)之间的整数的点这些点一共确定了11条不同的直线。
给定平面上20x21个整点{(x,y)l0x<20,0y<21,XEZyEZ
即横坐标是0到19(包含0和19)之间的整数、纵坐标是0到20(包含0和20)之间的整数的点请问这些点一共确定了多少条不同的直线。


#include<iostream>
using namespace std;
#include<vector>

class Point
{
public:
    Point()
    {

    }
    Point(int x, int y)
    {
        this->m_x = x;
        this->m_y = y;
    }
    int m_x;
    int m_y;
};

class Line
{
public:
    Line()
    {

    }
    Line(int coefficient_x, int coefficient_y, int constant)
    {
        this->m_coefficient_x = coefficient_x;
        this->m_coefficient_y = coefficient_y;
        this->m_constant = constant;
    }
    int m_coefficient_x;
    int m_coefficient_y;
    int m_constant;

    bool operator==(Line& l)
    {
        if (this->m_coefficient_x == l.m_coefficient_x && this->m_coefficient_y == l.m_coefficient_y && this->m_constant == l.m_constant)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool operator!=(Line& l)
    {
        if (this->m_coefficient_x == l.m_coefficient_x && this->m_coefficient_y == l.m_coefficient_y && this->m_constant == l.m_constant)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
};



void CreatePoint(vector<Point> &v_Point,int x,int y)
{
    for (int i = 1; i < x; ++i)
    {
        for (int j = 1; j < y; ++j)
        {
            Point p(i, j);
            v_Point.push_back(p);
        }
    }
    v_Point.resize(v_Point.size());
}

void CreateLine(vector<Line> v_Line,vector<Point> v_Point)
{
    for (vector<Point>::iterator it = v_Point.begin(); it != v_Point.end(); ++it)
    {
        for (vector<Point>::iterator it_2 = v_Point.begin(); it_2 != v_Point.end(); ++it_2)
        {
            int temp_coefficient_x = (*it).m_y - (*it_2).m_y;
            int temp_coefficient_y = (*it_2).m_x - (*it).m_x;
            int temp_constant = (*it).m_x * (*it_2).m_y - (*it_2).m_x * (*it).m_y;
            Line l(temp_coefficient_x, temp_coefficient_y, temp_constant);
            v_Line.push_back(l);
        }
    }
}

void Erase_repetition(vector<Line>& v_Line)
{
    for (vector<Line>::iterator it = v_Line.begin(); it != v_Line.end()-1; ++it)
    {
        for (vector<Line>::iterator it_2 = it+1; it_2 != v_Line.end(); ++it_2)
        {
            if ((*it) == (*it_2))
            {
                v_Line.erase(it_2);
            }
        }
    }
    v_Line.resize(v_Line.size());
}

void Output_result_Line_vector(vector<Line> &v_Line)
{
    cout << "The number of line:_____\b\b\b\b\b" << v_Line.size() << endl;
}

int main()
{
    vector<Point> v_Point;
    vector<Line> v_Line;
    int x, y;
    cout << "Enter the value of X axis:_____\b\b\b\b\b";
    cin >> x;
    --x;
    cout << "Enter the value of Y axis:_____\b\b\b\b\b";
    cin >> y;
    --y;

    CreatePoint(v_Point,x, y);
    CreateLine(v_Line, v_Point);
    Erase_repetition(v_Line);
    Output_result_Line_vector(v_Line);

    
    return 0;
}

出现以下错误:

img

不是很理解这个错误。

展开全部

  • 写回答

3条回答 默认 最新

  • 元气少女缘结神 2023-02-24 13:14
    关注

    将9-12行改成:

    Point()
        {
           m_x=0;
           m_y=0;
        }
    
    

    25~28行:

    Line()
        {
     m_coefficient_x=0;
       m_coefficient_y=0;
       m_constant=0;
        }
    
    
    评论 编辑记录
  • CSDN-Ada助手 CSDN-AI 官方账号 2023-02-24 14:36
    关注
    评论
  • 我爱OJ 2023-02-25 05:26
    关注
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        set< pair<double,double> > les;
        for(double x1=0;x1<20;++x1){
            for(double y1=0;y1<21;++y1){
                for(double x2=0;x2<20;++x2){
                    for(double y2=0;y2<21;++y2){
                        if(x1!=x2&&y1!=y2){
                            double k = (y2-y1)*1.0/(x2-x1);
                            //b=y-k*x;
                            double b = (y2*(x2-x1)-(y2-y1)*x2)*1.0/(x2-x1);    
                            les.insert(pair<double,double>(k,b));
                        }
                    }
                }
            }
        }
        cout << les.size()+20+21;
        return 0;
    }
    
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部