以下是给定边数时计算三角形面积的程序的源代码。
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float s,area;
clrscr();
printf("Enter the lengths of the sides of the triangle:\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area=%f",area);
getch();
}
我使用 Turbo C++ 编译器 3.0 版来编译程序。当我将边数设为 10、10 和 10 时,我得到的面积为 43.301270,这是正确的。但是当我将值插入为 1,1 和 1 时,程序给出的区域为 0.000000,这显然是错误的。此外,当我将值插入为 3,3 和 3 时,我得到的区域为 2.000000,这是错误的。
有谁知道程序不稳定行为的原因?如何纠正?我已将该程序作为 Zip 文件上传。
提前致谢。