写一段判断长方形的c++代码
时间: 2023-05-13 07:05:15 浏览: 175
以下是一段判断长方形的C代码:
```c
#include <stdio.h>
int main() {
int length, width;
printf("请输入长方形的长度和宽度:\n");
scanf("%d%d", &length, &width);
if (length == width) {
printf("这是一个正方形。\n");
} else {
printf("这是一个长方形。\n");
}
return 0;
}
```
该程序会要求用户输入长方形的长度和宽度,然后判断它是正方形还是长方形,并输出相应的结果。
相关问题
用c++代码写一个长方形类
好的,我可以帮您回答这个问题。以下是一个示例 C 代码,可以用来定义一个长方形类:
```
#include <stdio.h>
typedef struct {
float width;
float height;
} Rectangle;
void Rectangle_init(Rectangle* self, float width, float height) {
self->width = width;
self->height = height;
}
float Rectangle_getArea(Rectangle* self) {
return self->width * self->height;
}
int main() {
Rectangle r;
Rectangle_init(&r, 5.0, 10.0);
printf("The area of the rectangle is %.2f.\n", Rectangle_getArea(&r));
return 0;
}
```
这个代码定义了一个 Rectangle 结构体,它有两个属性:width 和 height。Rectangle_init 函数用于初始化一个 Rectangle 对象的属性值,Rectangle_getArea 函数用于计算一个 Rectangle 对象的面积。在 main 函数中,我们声明了一个 Rectangle 类型的变量 r,并且通过调用 Rectangle_init 函数来初始化它的属性。最后,我们调用 Rectangle_getArea 函数来计算该长方形的面积,并输出结果。
用C++写一段输入四个点判断是否为长方形的代码
以下是用 C 语言编写的判断四个点是否为长方形的代码:
```c
#include <stdio.h>
int main() {
int x1, y1, x2, y2, x3, y3, x4, y4;
printf("请输入四个点的坐标(x1, y1, x2, y2, x3, y3, x4, y4):\n");
scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
if ((x1 == x2 && y1 == y3 && x3 == x4 && y2 == y4) || (x1 == x4 && y1 == y2 && x2 == x3 && y3 == y4)) {
printf("这四个点是长方形的顶点!\n");
} else {
printf("这四个点不是长方形的顶点!\n");
}
return 0;
}
```
输入四个点的坐标,程序会判断这四个点是否为长方形的顶点。如果是,输出“这四个点是长方形的顶点!”;如果不是,输出“这四个点不是长方形的顶点!”
相关推荐
![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)