C++根据行数列数左上右上右下坐标获取所有坐标点
时间: 2023-05-25 20:04:53 浏览: 44
以下是用C语言编写的获取所有坐标点的程序,需要输入行数、列数以及左上、右上、右下坐标。
```
#include<stdio.h>
int main() {
int row, col, left_top_row, left_top_col, right_top_row, right_top_col, right_bottom_row, right_bottom_col, i, j;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &row, &col);
printf("Enter the coordinates of the left top point: ");
scanf("%d %d", &left_top_row, &left_top_col);
printf("Enter the coordinates of the right top point: ");
scanf("%d %d", &right_top_row, &right_top_col);
printf("Enter the coordinates of the right bottom point: ");
scanf("%d %d", &right_bottom_row, &right_bottom_col);
// ensure left_top is leftmost and topmost
if (left_top_col > right_top_col) {
int tmp = left_top_col;
left_top_col = right_top_col;
right_top_col = tmp;
}
if (left_top_row > right_top_row) {
int tmp = left_top_row;
left_top_row = right_top_row;
right_top_row = tmp;
}
// ensure right_top is topmost and rightmost
if (right_top_col > right_bottom_col) {
int tmp = right_top_col;
right_top_col = right_bottom_col;
right_bottom_col = tmp;
}
if (right_top_row < right_bottom_row) {
int tmp = right_top_row;
right_top_row = right_bottom_row;
right_bottom_row = tmp;
}
// print all coordinates
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
if (i >= left_top_row && i <= right_bottom_row && j >= left_top_col && j <= right_bottom_col)
printf("(%d, %d) ", i, j);
}
printf("\n");
}
return 0;
}
```
例如,输入一个3x3的矩阵,左上角坐标为(0,0),右上角坐标为(0,2),右下角坐标为(2,2),则输出结果为:
```
Enter the number of rows and columns: 3 3
Enter the coordinates of the left top point: 0 0
Enter the coordinates of the right top point: 0 2
Enter the coordinates of the right bottom point: 2 2
(0, 0) (0, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (2, 1) (2, 2)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)