输出边长为【1,10】以内三角形的边长及面积(C语言)
// TriangleArea.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/*
* The function to caculate a triangle's area, which specified with the three edges.
* An example for my students, to demo how to programming, written by Yang shitong, 2017.10.
* IN: a,b,c ---- the three edge length of a triangle.
* OUT: no
* RET: s ---- the area of the triangle.
* 0 ---- ERROR, not a triangle
* no 0 ---- the area. *
*/
float triangleArea(float a, float b, float c);
int main(int argc, char* argv[])
{
float a;
float b;
float c;
float s;
// printf("Please input three edges' length of a triangle [a b c Enter]: ");
// scanf("%f %f %f", &a, &b, &c);
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
{
for(int k=1;k<=10;k++)
{
s=triangleArea(i, j, k);
if(s<0.000001)
{
continue;
}
else
{
printf("边长 %d,%d,%d,面积 %.6f\n",i,j,k,s);s
}
}
}
}
return 0;
}
/*
* The function to caculate a triangle's area, which specified with the three edges.
* An example for my students, to demo how to programming, written by Yang shitong, 2017.10.
* IN: a,b,c ---- the three edge length of a triangle.
* OUT: no
* RET: s ---- the area of the triangle.
* 0 ---- ERROR, not a triangle
* no 0 ---- the area. *
*/
float triangleArea(float a, float b, float c)
{
float t;
float s=0.0f;
if(a+b>c &&
b+c>a &&
c+a>b) {
t=(a+b+c)/2.0f;
s=(float)sqrt((double)(t*(t-a)*(t-b)*(t-c))); //海伦公式,用来计算三角形面积
}
return s;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这个程序的主要关注点在两个地方
1、三角形三条边成立的条件,即任意两条边的和大于第三边
三角形面积计算方法:海伦公式
2、通过三层嵌套循环找出所有符合的边长,后经过计算得出面积,并输出,这里输出时候的C语言输出控制符号

浙公网安备 33010602011771号