问题描述
假设有一个点类point,具有两个实数坐标。希望主程序使用这个类完成下述功能:
(l)主程序为类point定义10个点的对象数组(也可以动态申请空间)。
(2)要求调用一个函数Set()从键盘输入10个对象的属性。
(3)要求调用一个函数Display()显示10个对象的值。
(4)要求调用一个函数Lenth(),计算将这些点连成一条折线时,这条折线的长度。
输入形式
参考的输入(每个点要求提示“Input x,y:”):
Input x,y:0 0
Input x,y:3 5
Input x,y:2 4
Input x,y:9 8
Input x,y:2 3
Input x,y:8 9
Input x,y:7 6
Input x,y:2 9
Input x,y:5 8
Input x,y:6 6
输出形式
参考的输出:
(0,0)
(3,5)
(2,4)
(9,8)
(2,3)
(8,9)
(7,6)
(2,9)
(5,8)
(6,6)
Length:46.7866
代码如下
#include <iostream>
#include <cmath>
using namespace std;
class point{
public:
int a[10],b[10];
void Set();
void Display();
void Lenth();
};
void point::Set() {
for (int i = 0;i < 10; ++i) {
cout << "Input x,y:";
cin >> a[i] >> b[i];
}
}
void point::Display() {
for (int j = 0; j < 10; ++j) {
cout <<"(" << a[j] << "," << b[j] << ")" << endl;
}
}
void point::Lenth() {
double lenth=0;
for (int i = 1; i < 10; ++i) {
lenth += pow(pow(a[i]-a[i-1],2)+pow(b[i]-b[i-1],2),0.5);
}
cout << "Length:" << lenth;
}
int main(){
point a;
a.Set();
a.Display();
a.Lenth();
return 0;
}