C++没有使用循环,但输出语句一直输出,且顺序错乱

Yeusheau 2024-11-15 21:39:54

主函数

#include<iostream>
#include"midtermExam.h"
using namespace std;


int main()
{
    Vector3 v1(1.0,2.0,2.0);
    Vector3 v2(3.0,4.0,0.0);
    cout<<"向量v1:"<<"("<<v1.getX()<<","<<v1.getY()<<","<<v1.getZ()<<")"<<endl;
    cout<<"向量v2:"<<"("<<v2.getX()<<","<<v2.getY()<<","<<v2.getZ()<<")"<<endl;
    cout<<"向量v1的模:"<<v1.module()<<endl;
    cout<<"向量v2的模:"<<v2.module()<<endl;
    cout<<"向量v1与v2的夹角:"<<v1.angle(v2)<<"度"<<endl;
    v1.unitzation();
    cout<<"向量v1单位化:"<<"("<<v1.getX()<<","<<v1.getY()<<","<<v1.getZ()<<")"<<endl;
    cout<<endl;

    v1++;
    cout<<"运算符++重载 "<<"v1++:"<<"("<<v1.getX()<<","<<v1.getY()<<","<<v1.getZ()<<")"<<endl;
    v2--;
    cout<<"运算符--重载 "<<"v2--:"<<"("<<v2.getX()<<","<<v2.getY()<<","<<v2.getZ()<<")"<<endl;
    Vector3 v3 = v1 + v2;
    cout<<"运算符+重载 "<<"v1+v2:"<<"("<<v3.getX()<<","<<v3.getY()<<","<<v3.getZ()<<")"<<endl;
    Vector3 v4 = v1 - v2;
    cout<<"运算符-重载 "<<"v1-v2:"<<"("<<v4.getX()<<","<<v4.getY()<<","<<v4.getZ()<<")"<<endl;
    cout<<"运算符*重载(点乘) "<<"v1*v2:"<<v1 * v2<<endl;
    cout<<endl;

    Vector3 point(1.5,2.0,1.0);
    Vector3 vo(2.0,3.0,4.0);
    Vector3 t(1.0,1.0,2.0);
    Line l(vo,t);
    cout<<"一点p:"<<"("<<point.getX()<<","<<point.getY()<<","<<point.getZ()<<")"<<endl;
    cout<<"直线l.vo"<<"("<<l.getVO().getX()<<","<<l.getVO().getY()
        <<","<<l.getVO().getZ()<<")"<<endl;
    cout<<"直线l.t"<<"("<<l.getT().getX()<<","<<l.getT().getY()
        <<","<<l.getT().getZ()<<")"<<endl;
    cout<<"点p到直线l的距离:"<<l.pointToLineDistance(point)<<endl;

    Vector3 projPoint = l.projectionPoint(point);
    cout<<"点p在直线l上的投影点:"<<"("<<projPoint.getX()<<","<<projPoint.getY()
        <<","<<projPoint.getZ()<<")"<<endl;
    cout<<endl;

    Vector3 vo1(1.0,1.5,2.0);
    Vector3 t1(1.0,2.0,3.0);
    Vector3 vo2(1.0,1.5,3.0);
    Vector3 t2(2.0,1.0,1.0);
    Line l1(vo1,t1);
    Line l2(vo2,t2);
    Plane plane(l1,l2);
    Vector3 nV = l1.getT().crossProduct(l2.getT());//法向量
    cout<<"构成平面的直线:"<<endl;
    cout<<"直线l1.vo1"<<"("<<l1.getVO().getX()<<","<<l1.getVO().getY()
        <<","<<l1.getVO().getZ()<<")"<<endl;
    cout<<"直线l1.t1"<<"("<<l1.getT().getX()<<","<<l1.getT().getY()
        <<","<<l1.getT().getZ()<<")"<<endl;
    cout<<"直线l2.vo2"<<"("<<l2.getVO().getX()<<","<<l2.getVO().getY()
        <<","<<l2.getVO().getZ()<<")"<<endl;
    cout<<"直线l2.t2"<<"("<<l2.getT().getX()<<","<<l2.getT().getY()
        <<","<<l2.getT().getZ()<<")"<<endl;
    cout<<"平面plane的法向量:"<<"("<<nV.getX()<<","<<nV.getY()<<","<<nV.getZ()<<")"<<endl;
    cout<<"平面plane:";
    plane.planarEquation();
    cout<<"点p到平面plane的距离:"<<plane.pointToPlaneDistance(point)<<endl;
    return 0;
}

头文件

#pragma once
#define _USE_MATH_DEFINES 
#include<iostream>
#include<cmath>
using namespace std;

class Vector3
{
private:
    double x, y, z;
public:
    Vector3():x(0),y(0),z(0){}
    Vector3(double x,double y,double z):x(x),y(y),z(z){}

    void setX(double x){
        this->x = x;
    }
    void setY(double y){
        this->y = y;
    }
    void setZ(double z){
        this->z = z;
    }
    double getX(){
        return x;
    }
    double getY(){
        return y;
    }
    double getZ(){
        return z;
    }

    double module(){//求模长
        return sqrt(x * x + y * y + z * z);
    }
    double angle(Vector3 otherVector){//求夹角
        return (acos((x * otherVector.x + y * otherVector.y + z * otherVector.z) 
        / (sqrt(x * x + y * y + z * z) * otherVector.module()))) 
        * 180 / M_PI ;
    }
    void unitzation(){//单位化
        x /= sqrt(x * x + y * y + z * z);
        y /= sqrt(x * x + y * y + z * z);
        z /= sqrt(x * x + y * y + z * z);
    }

    Vector3 crossProduct(Vector3 t){//向量叉乘
        return Vector3(y * t.z - z * t.y,
                       z * t.x - x * t.z,
                       x * t.y - y * t.x);
        }
    //运算符重载
    Vector3 operator+(Vector3& otherVector){
        Vector3 temp;
        temp.setX(x + otherVector.getX());
        temp.setY(y + otherVector.getY());
        temp.setZ(z + otherVector.getZ());
        return temp;
    }
    Vector3 operator-(Vector3& otherVector){
        Vector3 temp;
        temp.setX(x - otherVector.getX());
        temp.setY(y - otherVector.getY());
        temp.setZ(z - otherVector.getZ());
        return temp;
    }
    double operator*(Vector3& otherVector){
        return x * otherVector.getX() + y * otherVector.getY() + z * otherVector.getZ();
    }
    Vector3 operator++(int){
        x++;
        y++;
        z++;
        return *this;
    }
    Vector3 operator--(int){
        x--;
        y--;
        z--;
        return *this;
    }

    virtual ~Vector3(){

    }
};

class Line : public Vector3
{
    private:
        Vector3 vo;
        Vector3 t;
    public:
        Line():vo(Vector3()),t(Vector3()){};
        Line(Vector3 vo,Vector3 t):vo(vo),t(t){};

        Vector3 getT(){
            return t;
        }
        Vector3 getVO(){
            return vo;
        }
        Vector3 getLinePoint(double s){
            Vector3 temp(s * t.getX(),s * t.getY(),s * t.getZ());
            return vo + temp;
        }

        double pointToLineDistance(Vector3 p){//点到直线距离
            Vector3 temp = p - vo;
            return temp.crossProduct(t).module() / t.module(); 
        }

        Vector3 projectionPoint(Vector3 p){//投影点
            Vector3 temp1 = p - vo;
            t.unitzation();
            double temp2 = temp1 * t;
            Vector3 temp3 = Vector3(temp2 * t.getX(),temp2 * t.getY(),temp2 * t.getZ());
            return vo + temp3;
        }
        
        virtual ~Line(){

        }

};

class Plane : public Line
{
    private:
        Line l1;
        Line l2;
    public:
        Plane():l1(Line()),l2(Line()){};
        Plane(Line l1,Line l2):l1(l1),l2(l2){};

        void planarEquation(){//求平面方程
            Vector3 nV = l1.getT().crossProduct(l2.getT());//法向量
            double d = -l1.getVO().getX() * nV.getX() - l1.getVO().getY() 
                        * nV.getY() - l1.getVO().getZ() * nV.getZ();
            cout<<nV.getX()<<"x"<<"+"<<nV.getY()<<"y"<<"+"<<nV.getZ()<<"z"<<"="<<d<<endl;
        }

        double pointToPlaneDistance(Vector3 p){//点到平面距离
            Vector3 nV = l1.getT().crossProduct(l2.getT());//法向量
            double d = -l1.getVO().getX() * nV.getX() - l1.getVO().getY() 
                        * nV.getY() - l1.getVO().getZ() * nV.getZ();
            double temp1 = fabs(nV.getX() * p.getX() + nV.getY() 
                                * p.getY() + nV.getZ() * p.getZ() - d);
            double temp2 = nV.module();

            return temp1 / temp2;
        }

        virtual ~Plane(){
            
        }
};

输出结果

 输出结果中投影点一直输出,求助

...全文
232 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2024-11-18
  • 打赏
  • 举报
回复

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧