您的位置:首页 > 其它

第五周上机实践项目1——体验常成员函数

2015-04-05 13:03 225 查看
问题及代码

/*
 * Copyright (c) 2015, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:辛彬
 * 完成日期:2015 年 4 月 5 日
 * 版 本 号:v1.0
 *
 * 问题描述:设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等。
 * 输入描述:平面坐标。
 * 程序输出:两点之间距离、到原点距离、关于坐标轴和原点的对称点。
 */
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private:
    double x;  // 横坐标
    double y;  // 纵坐标
public:
    double Distance1(CPoint p) const; //两点之间的距离(一点是当前点——想到this了吗?,另一点为p)
    double Distance0() const;          // 到原点(0,0)的距离
    CPoint SymmetricAxis(char style) const;//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
    void input();  //以x,y 形式输入坐标点
    void output(); //以(x,y) 形式输出坐标点
};
double CPoint::Distance1(CPoint p) const
{
    double dx=x-p.x,dy=y-p.y;
    return sqrt(dx*dx+dy*dy);
}
double CPoint::Distance0() const
{
    return sqrt(x*x+y*y);
}
CPoint CPoint::SymmetricAxis(char style) const
{
    CPoint p;
    if(style=='x')
    {
        p.x=x;
        p.y=-y;
    }
    else if(style=='y')
    {
        p.x=-x;
        p.y=y;
    }
    else if(style=='o')
    {
        p.x=-x;
        p.y=-y;
    }
    return p;
}
void CPoint::input()
{
    char e;
    double xx,yy;
    cout<<"请输入坐标:"<<endl;
    cin>>e>>xx>>e>>yy>>e;
    x=xx;
    y=yy;
}
void CPoint::output()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
int main()
{
    CPoint p1,p2,p;
    p1.input();
    p2.input();
    cout<<"两点之间的距离为:"<<p1.Distance1(p2)<<endl;
    cout<<"到原点之间的距离为:"<<p2.Distance0()<<endl;
    p=p2.SymmetricAxis('x');
    cout<<"与X轴对称的坐标为:";
    p.output();
    p=p2.SymmetricAxis('y');
    cout<<"与Y轴对称的坐标为:";
    p.output();
    p=p2.SymmetricAxis('o');
    cout<<"与原点对称的坐标为:";
    p.output();
    return 0;
}


运行结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: