黑马程序员技术交流社区

标题: 一个C语言的问题 [打印本页]

作者: Mike001    时间: 2015-4-19 16:30
标题: 一个C语言的问题
#include<stdio.h>
#include<math.h>
void main()
{
int a,p,c;
float area;

printf("Input a side of triangle: ");
scanf("%d",&a);
p=(3*a)/2;
c=3*a;
area=sqrt(p*(p-a)^3);
printf("The area of triangle is %.2f, the circle of triangle is %d.",area,c);
}根据“海伦-秦九韶”公式,area=√p(p-a)(p-b)(p-c),其中p=(a+b+c)/2计算三角形的面积和周长,结果周长是错误的,面积是错误的,求各位解释下
作者: 流风124    时间: 2015-4-19 18:33
恩,大概理解了一下你的题目,周长的结果应该是对的(但周长只能是整数),面积那里计算错了
  1. area=sqrt(p*(p-a)^3);
复制代码


^ 这个符号是位运算的异或,不是你以为的3次方,改成下面的就ok
  1. area=sqrt(p*pow((p-a),3));
复制代码


把你的完整代码给改了一下,你可以看看,不知道我的理解对不对

  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5.     int a,p,c;
  6.     float area;
  7.    
  8.     printf("Input a side of triangle: ");
  9.     scanf("%d",&a);
  10.     c=3*a;
  11.     p=c/2;
  12.    
  13.     area=sqrt(p*pow((p-a),3));
  14.     printf("The area of triangle is %.2f, the circle of triangle is %d.",area,c);

  15.     return 0;

  16. }
复制代码






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2