安装图形库
参考
#include "graphics.h" // 就是需要引用这个图形库
#include <conio.h>
#include <stdio.h>
#include <math.h>// 判断是否可以构成三角形
int isTriangle(int a, int b, int c) {return (a + b > c) && (a + c > b) && (b + c > a);
}// 绘制三角形图形
void drawTriangle(int a, int b, int c) {initgraph(1000, 800); // 初始化,显示一个窗口// 设置背景色为蓝色setbkcolor(BLUE);// 用背景色清空屏幕cleardevice();settextstyle(16, 0, _T("Consolas"));if (a == b && b == c) {outtextxy(200,0,_T("这是一个等边三角形"));int hight = 0.75 * a;int points[] = {a / 2, 0, 0,hight, a,hight};printf("%d \n",hight);printf("%d %d %d %d %d %d",points[0],points[1],points[2],points[3],points[4],points[5]);if(a < 100){for(int i = 0; i < 6; i++){points[i] *= 10;}}drawpoly(3, points);} else if (a == b || b == c || a == c) {outtextxy(200,0,_T("这是一个等腰三角形"));int yaoL = a;int bottonL = c;if(b == c){yaoL = b;bottonL = a;}if(c == a){yaoL = a;bottonL = b;}int hight = sqrt(a*a - (bottonL * 0.5) * (bottonL * 0.5)); int points[] = {bottonL / 2, 0, 0,hight, bottonL,hight};printf("%d \n",hight);printf("%d %d %d %d %d %d",points[0],points[1],points[2],points[3],points[4],points[5]);if(a < 100){for(int i = 0; i < 6; i++){points[i] *= 10;}}drawpoly(3, points);} else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) {outtextxy(200,0,_T("这是一个直角三角形"));int l1= a,l2 = b,l3 = c;if (a * a + c * c == b * b){l1= a,l2 = c,l3 = b;}if(b * b + c * c == a * a){l1= b,l2 = c,l3 = a;}int points[] = {l1,l1,l1,l3,l3,l3};if(a < 100){for(int i = 0; i < 6; i++){points[i] *= 10;}}drawpoly(3, points);} else {outtextxy(200,0,_T("这是一个普通三角形"));// a > b > cint temp = a; if(a < b){temp = b;b = a;a = temp;} if(a < c){temp = c;c = a;a = temp;}//海伦公式double p = 0.5 * (a + b + c);double s = sqrt(p * (p - a) * (p - b) * (p - c));// s = 0.5 * a * hdouble h = s * 2.0 / a;printf("%lf \n",p);printf("%lf \n",s);printf("%lf \n",h);int ih = int(h);int otherH = sqrt((a * a) - (ih * ih));int points[] = {0,0,0,a,ih,otherH};if(a < 100){for(int i = 0; i < 6; i++){points[i] *= 10;}}drawpoly(3, points);}//circle(200, 200, 100); // 画圆,圆心(200, 200),半径 100 // int points2[] = {50, 200, 200, 200, 200, 50};
// drawpoly(3, points2);getch(); // 暂停一下等待用户按键 closegraph(); // 关闭图形界面
}int main() {int a, b, c;printf("请输入三条边的长度(用空格分隔):");scanf("%d %d %d", &a, &b, &c);if (isTriangle(a, b, c)) {printf("可以形成一个三角形。\n");drawTriangle(a, b, c);} else {printf("无法形成一个三角形。\n");}return 0;
}