sqrt()函式的用法,求三角形面積
今天刷題的時候看到了三角形面積,突然想到老師上課講過,這不是好簡單?
就直接上代碼.
#include <stdio.h>
int main() {
float a = 0;
float b = 0;
float c = 0;//三角形的三條邊
printf("輸入三角形的三條邊\n");
scanf("%f %f %f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
float s = (a + b + c) / 2.0;
float mianji = s * (s - a) * (s - b) * (s - c);//三角形面積公
printf("三角形的面積為%.2f", mianji);//保留兩位小數輸出三角形面積
}
else {
printf("三角形之和要大于第三邊,請重新輸入邊長");
}
return 0;
}
然后怎么測驗就是不過,就去找老師作業去看沒問題啊!直到我找到了老師的PPT一看,???


然后我發現老師的題是三角形面積的平方,然后我拿筆一算尷尬了,敲代碼沒想那么多,我想在C語言怎么開平方,上網一找解決了,
那就是sqrt函式,sqrt函式需要#include<math.h>問題完美解決,代碼如下
#include <stdio.h>
#include <math.h>//用sqrt函式需要的頭檔案
int main() {
float a = 0;
float b = 0;
float c = 0;//三角形的三條邊
printf("輸入三角形的三條邊\n");
scanf("%f %f %f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
float s = (a + b + c) / 2.0;
float mianji = sqrt(s * (s - a) * (s - b) * (s - c));//三角形面積公,sqrt()函式用來開平方根
printf("三角形的面積為%.2f", mianji);//保留兩位小數輸出三角形面積
}
else {
printf("三角形之和要大于第三邊,請重新輸入邊長");
}
return 0;
}
兩個小改動就行了,有點小尷尬,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/249470.html
標籤:其他
