
一、程序分析
三角形面积海伦公式:√[ p ( p - a ) ( p - b ) ( p - c ) ] 。其中 p = (a + b + c) / 2 。a、b、c分别是三角形的三边长。
二、根据三角形面积计算公式用if语句编写程序如下:
#include "stdioh"
#include "mathh"
int main(void)
{
float a = 0, b = 0, c = 0, p = 0;
float area = 0;
printf("Please input three sides of triangle:");
scanf_s("%f %f %f", &a, &b, &c);
if((a + b) > c && (a + c) > b && (b + c) > a)
{
p = (a + b + c) / 2;
area = sqrt(p (p - a) (p - b) (p - c));
}
else
printf("Triangle does not exist!\n");
printf("The area of triangle is:%f\n", area);
return 0;
扩展资料:
还可以使用switch语句计算三角形的面积,编写程序如下
#include "stdioh"
#include "mathh"
int main(void)
{
float a = 0, b = 0, c = 0;
float p = 0;
printf("Please input three sides of triangle:");
scanf_s("%f %f %f", &a, &b, &c);
switch (a + b > c && a + c > b && b + c > a)
{
case 0:printf("Triangle does not exist!\n"); break;
case 1:
p = (a + b + c)05;
printf("The area of triangle is:%f\n", sqrt(p (p - a) (p - b) (p - c)));
break;
}
return 0;
}
参考资料:
参考资料:
class Triangle extends drawings//空心三角形类
{
void draw(Graphics2D g2d)
{g2dsetPaint(new Color(R,G,B));
g2dsetStroke(new BasicStroke(stroke,
BasicStrokeCAP_ROUND,BasicStrokeJOIN_BEVEL));
g2ddrawLine((int)((x1+x2)/2),Mathmin(y1,y2),Mathmax(x1,x2),Mathmax(y1,y2));
g2ddrawLine(Mathmax(x1,x2),Mathmax(y1,y2),Mathmin(x1,x2),Mathmax(y1,y2));
g2ddrawLine(Mathmin(x1,x2),Mathmax(y1,y2),(int)((x1+x2)/2),Mathmin(y1,y2));
}
}
以上是通过绘制三条直线作为三角形的三条边来绘制三角形
class fillTriangle extends drawings//实心三角形
{
void draw(Graphics2D g2d)
{g2dsetPaint(new Color(R,G,B));
g2dsetStroke(new BasicStroke(stroke));
int mx=(int)((x1+x2)/2);
int[] x={mx,Mathmax(x1,x2),Mathmin(x1,x2)};
int[] y={Mathmin(y1,y2),Mathmax(y1,y2),Mathmax(y1,y2)};
g2dfillPolygon(x,y,3);
}
}
以上是用填充多边形的方式填充一个三角形,如果把最后的:g2dfillPolygon(x,y,3)改为g2ddrawPolygon(x,y,3); 则是以绘制多边形的方式绘制空心三角形
这里说明一下:因为(x1,y1,x2,y2)只能确定一个矩形区域,即鼠标拉动的起点和终点确定的矩形区域所以可以有多种方式确定三角形的三个顶点,我这个用的三个顶点是:
点1( (x1+x2)/2, min(y) ) 点2( max(x),max(y) ) 点3( min(x),max(y) )
以上就是关于编程题:编写程序输入三角形的3条边长,计算并输出三角形的面积。全部的内容,包括:编程题:编写程序输入三角形的3条边长,计算并输出三角形的面积。、如何用程序绘制三角形、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)