[Functions]D. Liang 5.20 The MyTriangle header file.c

Description

Please implementation the following two functions in MyTriangle.h. Note that you need not write the main() function.

/* Returns true if the sum of any two sides is
 greater than the third side. */
 
bool isValid(double side1, double side2, double side3) 

/* Returns the area of the triangle. */

double area(double side1, double side2, double side3)

The formula for computing the area is
  s    = (side1 + side2 + side3) / 2;
  area = sqrt(s(s-side1)(s-side2)(s-side3)) 

Example Input1

1.0 2.0 3.0

Example Output1

Invalid!

Example Input2

3.0 4.0 5.0

Example Output2

6.00

The header files and main functions provided by the title are as follows:

#include<stdio.h>
#include "MyTriangle.h"

int main(){
	double side1, side2, side3;
	scanf("%lf%lf%lf", &side1, &side2, &side3);
	if(isValid(side1, side2, side3)){
		printf("%.2lf", area(side1, side2, side3));
	}else{
		printf("Invalid!");
	}
	return 0;
}
#include "MyTriangle.h"
#include<stdbool.h>

/* Returns true if the sum of any two sides is
 greater than the third side. */
bool isValid(double side1, double side2, double side3); 

/* Returns the area of the triangle. */
double area(double side1, double side2, double side3);

My code:

//   Date:2020/4/3 
//   Author:xiezhg5
#include<stdbool.h>   //bool函数包含着这个头文件中 
#include<stdio.h>
#include<math.h>     //一定要加这个头文件 
bool isValid(double side1, double side2, double side3)
{
	bool a=false;   //bool类型C99标准中有 
	if((side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1))
	a=true;
    return a;
}
double area(double side1, double side2, double side3)
{
	double s=0;
	if (isValid(side1, side2, side3))
    {
    	double b=(side1+side2+side3)/2;
	    s=sqrt(b*(b-side1)*(b-side2)*(b-side3));
    }
    return s;
}

Boolean type support library

C Type support
The C programming language, as of C99, supports Boolean arithmetic with the built-in type _Bool (see _Bool). When the header <stdbool.h> is included, the Boolean type is also accessible as bool.
Standard logical operators &&, ||, ! can be used with the Boolean type in any combination.
A program may undefine and perhaps then redefine the macros bool, true and false.

在这里插入图片描述

Example

#include <stdio.h>
#include <stdbool.h>
 
int main(void)
{
    bool a=true, b=false;
    printf("%d\n", a&&b);
    printf("%d\n", a||b);
    printf("%d\n", !b);
}

Output:

0
1
1

Reference:https://en.cppreference.com/w/c/types/boolean

发布了165 篇原创文章 · 获赞 124 · 访问量 5620

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105289099
今日推荐