VC++生成矩形,圆形,三角形
时间: 2023-07-11 19:43:25 浏览: 53
可以使用MFC框架下的CDC类来实现图形绘制,具体实现如下:
1. 生成矩形:
```cpp
void CMyView::OnDraw(CDC* pDC)
{
// 绘制矩形
CRect rect(50, 50, 200, 200);
pDC->Rectangle(rect);
}
```
2. 生成圆形:
```cpp
void CMyView::OnDraw(CDC* pDC)
{
// 绘制圆形
CRect rect(50, 50, 200, 200);
pDC->Ellipse(rect);
}
```
3. 生成三角形:
```cpp
void CMyView::OnDraw(CDC* pDC)
{
// 绘制三角形
POINT points[3] = { {100, 50}, {150, 150}, {50, 150} };
pDC->Polygon(points, 3);
}
```
以上代码在MFC框架下实现,如果你不使用MFC框架,可以考虑使用其他图形库。
相关问题
矩形,三角形
好的,下面是简单的C++代码实现矩形和三角形的判断:
1. 判断矩形
```cpp
#include <iostream>
using namespace std;
bool isRectangle(int a, int b, int c, int d) {
if (a < 0 || b < 0 || c < 0 || d < 0) { // 判断边长是否合法
return false;
}
if (a == c || b == d) { // 判断是否为正方形
return false;
}
return true;
}
int main() {
int a, b, c, d;
cout << "请输入矩形的边长:";
cin >> a >> b >> c >> d;
if (isRectangle(a, b, c, d)) {
cout << "这是一个矩形" << endl;
} else {
cout << "这不是一个矩形" << endl;
}
return 0;
}
```
2. 判断三角形
```cpp
#include <iostream>
using namespace std;
bool isTriangle(int a, int b, int c) {
if (a + b <= c || a + c <= b || b + c <= a) { // 判断是否符合三角形的条件
return false;
}
return true;
}
int main() {
int a, b, c;
cout << "请输入三角形的三条边长:";
cin >> a >> b >> c;
if (isTriangle(a, b, c)) {
cout << "这是一个三角形" << endl;
} else {
cout << "这不是一个三角形" << endl;
}
return 0;
}
```
希望能够帮助到您!
Java方法实现求矩形 三角形 圆形面积系统
可以使用Java的面向对象编程思想,将矩形、三角形、圆形抽象成类,并在类中定义求面积的方法。
以矩形为例,可以定义一个矩形类(Rectangle),其中包含矩形的长和宽属性,以及求矩形面积的方法(getArea()):
```
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}
```
同样的,可以定义一个三角形类(Triangle),其中包含三角形的底边和高属性,以及求三角形面积的方法(getArea()):
```
public class Triangle {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return base * height / 2;
}
}
```
还可以定义一个圆形类(Circle),其中包含圆形的半径属性,以及求圆形面积的方法(getArea()):
```
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
```
在主程序中,可以根据用户的输入创建不同的矩形、三角形、圆形对象,并调用它们的getArea()方法求面积:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要计算的图形:");
System.out.println("1. 矩形");
System.out.println("2. 三角形");
System.out.println("3. 圆形");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入矩形的长和宽:");
double length = scanner.nextDouble();
double width = scanner.nextDouble();
Rectangle rectangle = new Rectangle(length, width);
System.out.println("矩形的面积为:" + rectangle.getArea());
break;
case 2:
System.out.print("请输入三角形的底边和高:");
double base = scanner.nextDouble();
double height = scanner.nextDouble();
Triangle triangle = new Triangle(base, height);
System.out.println("三角形的面积为:" + triangle.getArea());
break;
case 3:
System.out.print("请输入圆形的半径:");
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
System.out.println("圆形的面积为:" + circle.getArea());
break;
default:
System.out.println("输入有误!");
break;
}
}
}
```
这样就可以实现一个简单的求矩形、三角形、圆形面积的系统了。