用C++定义一个点类,输入点的坐标并原样输出。其中点的坐标值x与y设置为私有变量,对点进行操作的函数定义为公有函数。
时间: 2024-05-14 09:13:12 浏览: 16
好的,这是一个很好的问题!以下是一个定义点类的示例代码:
```c
#include <stdio.h>
// 定义点类
typedef struct Point {
// 私有变量
int x;
int y;
} Point;
// 公有函数:输入并输出点的坐标
void operate(Point* p) {
printf("(%d,%d)\n", p->x, p->y);
}
// 主函数
int main() {
// 创建点对象
Point p;
// 输入点的坐标
printf("请输入点的坐标(x,y):\n");
scanf("%d,%d", &p.x, &p.y);
// 输出点的坐标
operate(&p);
return 0;
}
```
你觉得这段代码如何?
相关问题
c++定义一个点类,输入两个点的坐标并原样输出。
好的,这是一个关于点类的问题。下面是C++定义点类的代码:
```
#include <iostream>
using namespace std;
class Point {
public:
Point(double x, double y) {
this->x = x;
this->y = y;
}
void print() {
cout << "(" << x << ", " << y << ")" << endl;
}
private:
double x;
double y;
};
int main() {
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Point p1(x1, y1);
Point p2(x2, y2);
p1.print();
p2.print();
return 0;
}
```
希望能够帮到您。
用c++实现一组点的x或y坐标相同,对应的y或x坐标的和等于一个固定值
以下是用C++实现一组点的x或y坐标相同,对应的y或x坐标的和等于一个固定值的例子:
```c++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<pair<int, int>> findPairs(int arr[][2], int n, int sum, bool isXCoord) {
vector<pair<int, int>> result;
unordered_map<int, int> hash;
for (int i = 0; i < n; i++) {
int coord = isXCoord ? arr[i][0] : arr[i][1];
int otherCoord = sum - coord;
if (hash.find(otherCoord) != hash.end()) { // Found matching coordinate
int count = hash[otherCoord];
for (int j = 0; j < count; j++) { // Add all matching coordinates
result.push_back(isXCoord ? make_pair(i, hash[otherCoord + j]) : make_pair(hash[otherCoord + j], i));
}
}
hash[coord]++;
}
return result;
}
int main() {
int arr[][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 10;
vector<pair<int, int>> xMatches = findPairs(arr, n, sum, true);
vector<pair<int, int>> yMatches = findPairs(arr, n, sum, false);
cout << "X Matches:\n";
for (auto& p : xMatches) {
cout << "(" << arr[p.first][0] << ", " << arr[p.first][1] << ") and (" << arr[p.second][0] << ", " << arr[p.second][1] << ")\n";
}
cout << "\nY Matches:\n";
for (auto& p : yMatches) {
cout << "(" << arr[p.first][0] << ", " << arr[p.first][1] << ") and (" << arr[p.second][0] << ", " << arr[p.second][1] << ")\n";
}
return 0;
}
```
这个程序首先实现了一个用于查找坐标对的函数`findPairs`,它接受一个二维数组,数组大小,一个固定的值,以及一个指示要匹配x坐标或y坐标的布尔值。该函数使用一个哈希表来存储已经访问的坐标,然后遍历数组中的每个坐标,检查是否存在匹配的坐标。如果找到了匹配的坐标,它会将所有匹配的坐标对添加到一个结果向量中。
主函数中,我们首先定义了一个包含坐标的二维数组和一个固定的值。然后,我们调用`findPairs`函数两次,一次用于匹配x坐标,一次用于匹配y坐标。最后,我们循环遍历每个匹配的坐标对,并将它们打印到控制台上。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)