a_6_1.cpp
来自「C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体」· C++ 代码 · 共 39 行
CPP
39 行
#include "stdafx.h"
#include <iostream>
#include <string>
#include<math.h>
using namespace std;
class Point {
public:
void setPointCoordinate(); // 分别输入两个点的坐标
double distance(); // 计算两点间的距离
private:
double X1,Y1,X2,Y2;
};
void Point::setPointCoordinate()
{ cout<<"请输入第1个点的坐标!"<<endl<<"x1=";
cin>>X1;
cout<<"y1=";
cin>>Y1;
cout<<endl<<"请输入第2个点的坐标!"<<endl<<"x2=";
cin>>X2;
cout<<"y2=";
cin>>Y2;
}
double Point::distance()
{
double distance;
distance=sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
return distance;
}
void main()
{
Point a; // 创建一个类对象
a.setPointCoordinate();
cout<<"这两个点之间的距离是:"<<a.distance();
cout<<endl;
cin.get();cin.get(); //等待结束,以便调测程序,可以删除
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?