c語言計算三角形面積代碼_C 語言

來源:互聯網
上載者:User

複製代碼 代碼如下:

//面積公式s = (a+b+c) / 2   area = sqrt(s * (s - a) * (s - b) * (s - c));
//小作業 求三角形的面積

int check(double a);
int check2(double a, double b, double c);

#include <stdio.h>
#include <math.h>
int main(void)
{
    double area = 0;
    double s;
    double a,b,c;
    printf("請輸入三角形的三邊的長(a b c):");
    scanf("%lf %lf %lf", &a, &b, &c);
    if (check(a) == 1 && check(b) == 1 && check(c) ==1)
    {
        if (check2(a,b,c) == 0)
        {
            printf("您輸入的不是三角形.請重新輸入\n\r");
            return 0;
        }
        else if (check2(a,b,c) == 1)
        {
            s = (a+b+c) / 2;
            area = sqrt(s * (s - a) * (s - b) * (s - c));
            printf("該三角形的面積是:%g\n\r",area);
        }
    }
    else
        printf("輸入有誤,請重新輸入.\n\r");
    return 0;
}

 

//判斷輸入的資料是否有效
int check(double a)
{
    if (a > 0)
        return 1;
    else
        return 0;
}

//判斷三角形是否有效

int check2(double a, double b, double c)
{
    if ( ((a+b)<=c) | ((a+c)<=b) | ((c+b)<=a) )
        return 0;
    if (abs(a-b)>=c | abs(a-c)>=a | abs(c-b)>=a)
        return 0;
    else
        return 1;
}

 想了下,area不會是垃圾值啊.所以不用寫那麼多行聲明變數誒.....

修改了一些

複製代碼 代碼如下:

//面積公式s = (a+b+c) / 2   area = sqrt(s * (s - a) * (s - b) * (s - c));
//小作業 求三角形的面積

int check(double a);
int check2(double a, double b, double c);

#include <stdio.h>
#include <math.h>
int main(void)
{
    double area,s,a,b,c;
    printf("請輸入三角形的三邊的長(a b c):");
    scanf("%lf %lf %lf", &a, &b, &c);
    if (check(a) == 1 && check(b) == 1 && check(c) ==1)
    {
        if (check2(a,b,c) == 0)
        {
            printf("您輸入的不是三角形.請重新輸入\n\r");
            return 0;
        }
        else if (check2(a,b,c) == 1)
        {
            s = (a+b+c) / 2;
            area = sqrt(s * (s - a) * (s - b) * (s - c));
            printf("該三角形的面積是:%g\n\r",area);
        }
    }
    else
        printf("輸入有誤,請重新輸入.\n\r");
    return 0;
}

 

//判斷輸入的資料是否有效
int check(double a)
{
    if (a > 0)
        return 1;
    else
        return 0;
}

//判斷三角形是否有效

int check2(double a, double b, double c)
{
    if ( ((a+b)<=c) | ((a+c)<=b) | ((c+b)<=a) )
        return 0;
    if (abs(a-b)>=c | abs(a-c)>=a | abs(c-b)>=a)
        return 0;
    else
        return 1;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.