6-5 直角坐标*
分数 15
全屏浏览
切换布局
作者 李祥
单位 湖北经济学院
请设计直角坐标 COORD 类。它包含两个私有数据成员:x (横坐标) 和 y (纵坐标)。
#include <iostream> using namespace std; /* 你提交的代码将被嵌在这里 */ int main() { COORD a; a.Input(); a.Output(); cout << endl; return 0; }
输入样例
(2.5, 4.9)
输出样例
(2.5, 4.9)
要求:
- 设计构造函数,达到以下效果。
COORD a; // 坐标 a 初始化为 (0, 0) COORD b(2.5, 4.9); // 坐标 b 初始化为 (2.5, 4.9)
- 设计 Input 和 Output 函数用于输入和输出坐标
COORD a; a.Input(); // 输入: (2.5, 4.9) a.Output(); // 输出: (2.5, 4.9)
- 设计 Set 和 Get 函数用于设置和读取坐标
COORD a; double x, y; a.Set(2.5, 4.9) a.Get(x, y); cout << x << ' ' << y << endl; // 输出: 2.5 4.9
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class COORD//定义
{
double x,y;
public:
COORD(double x1 = 0,double y1 = 0)//构造函数
{
x=x1;
y=y1;
}
void Input()
{
char a;
cin >> a >> x >> a >> y >> a;//输入
}
void Output()const
{
// cout << "(" <<fixed << setprecision(1)<< x << ", " << y << ")";
//<<fixed << setprecision(1),要加这个的话头文件要加一个#include<bits/stdc++.h>
cout << "(" << x << ", " << y << ")";//不要加endl,会有格式错误
}
void Set(double x2,double y2)
{
x = x2;
y = y2;
}
void Get(double &x3,double &y3)const//引用,让main函数可以直接输出
{
x3 = x;
y3 = y;
}
};