三角形的面积为 area=s*(s-a)*(s-b)*(s-c) .... 编写程序,在程序中用带实参的宏名来求面积。 C++

2016-06-22 08:48:22  分类: c++程序设计第三版谭浩强课后答案  参与:

三角形的面积为 area=s*(s-a)*(s-b)*(s-c)  其中,s=1/2*(a+b+c),  a , b, c 为三角形的三边。定义两个带参数的宏,一个用来求S,另一个用来求 area 。 编写程序,在程序中用带实参的宏名来求面积。

以下是此题的【c++源代码】

#include <iostream>
#include <cmath>
using namespace std;
#define S(a,b,c)  (a+b+c)/2
#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c))
int main()
 {float a,b,c;
  cout<<"input a,b,c:";
  cin>>a>>b>>c;
  if (a+b>c && a+c>b && b+c>a)
    cout<<"area="<<AREA(a,b,c)<<endl;
  else
    cout<<"It is not a triangle!"<<endl;
  return 0;
 }
 


 

来源:c++程序设计第三版谭浩强课后答案

本文链接:http://www.wb98.com/cjia/post/cjia_4.12.html


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接