怎么用编程求三角形的斜边 (编程三角形的三边求三角形面积)

如图:

数学知三角形一条边求面积,已知三角形三条边求角度代码

更一般地,可以得出余弦定理。

余弦定理是解三角形中的一个重要定理,可应用于以下三种需求:

① 当已知三角形的两边及其夹角,可由余弦定理得出已知角的对边。

② 当已知三角形的三边,可以由余弦定理得到三角形的三个内角。

③ 当已知三角形的三边,可以由余弦定理得到三角形的面积。

在三角形ABC中,设AB=c,BC=a,CA=b,且a、b、c所对的内角分别是A、B、C,则:

cosA=[b²+c²-a²]/(2bc)

cosB=[a²+c²-b²]/(2ac)

cosC=[a²+b²-c²]/(2ab)

数学知三角形一条边求面积,已知三角形三条边求角度代码

因为余弦函数在[0,pi]上的单调性,可以得到反余弦函数,从而求得三角形的三个内角。

按代码组织的方式,可以考虑结构化程序设计或面向对象程序设计方式。

1 结构体+函数的结构化程序设计方式

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI 3.1415926
#define a2d (180/PI)    // arc to degree

typedef struct triangle
{
    double a,b,c;
}Tri,*PTri;

int initTri(double a,double b,double c,PTri pti)
{
    if(a<0 || b<0 || c<0 || a+b<=c || a+c<=b || b+c<=a)
        return 0;
    pti->a = a;
    pti->b = b;
    pti->c = c;
    return 1;
}

typedef struct triangle_degree
{
    double A,B,C;
}Trid;

Trid calcTri(Tri ti)
{
    Trid td;
    td.A = (ti.b*ti.b+ti.c*ti.c-ti.a*ti.a)/(2*ti.b*ti.c);
    td.B = (ti.a*ti.a+ti.c*ti.c-ti.b*ti.b)/(2*ti.a*ti.c);
    td.C = (ti.a*ti.a+ti.b*ti.b-ti.c*ti.c)/(2*ti.a*ti.b);
    td.A = acos(td.A)*a2d;
    td.B = acos(td.B)*a2d;
    td.C = acos(td.C)*a2d;
    return td;
}
typedef struct triangle2 // 两边一夹角确定一三角形
{
    double a,b,r;
}Trir;

int initTrir(double a, double b, double r,Trir *pt)
{
    if(a<0 || b<0 || r<0)
        return 0;
    pt->a = a;
    pt->b = b;
    pt->r = r;
    return 1;
}
double calcSide(Trir tr)
{
    return sqrt(tr.a*tr.a+tr.b*tr.b-2*tr.a*tr.b*cos(tr.r/180*PI));
}

double sideA(double a,double b,double angleA)
{
    return sqrt(a*a+b*b-2*a*b*cos(angleA/180*PI));
}

double triArea(Tri ti)
{
    double s = (ti.a+ti.b+ti.c)/2;
    return sqrt(s*(s-ti.a)*(s-ti.b)*(s-ti.c));
}

double triPerimeter(Tri ti)
{
    return ti.a+ti.b+ti.c;
}

Trid calcTriEstimate(Tri ti)
{
    Trid td;
    double pm = ti.a+ti.b+ti.c; // perimeter
    td.A = ti.a/pm*180;
    td.B = ti.b/pm*180;
    td.C = ti.c/pm*180;
    return td;
}

void printTriD(PTri pti)
{
    printf("三角形的三条边分别是:%6.2f, %6.2f, %6.2f\n",pti->a,pti->b,pti->c);
    Trid td = calcTri(*pti);
    printf("三角形的三个角分别是:%6.2f, %6.2f, %6.2f\n",td.A,td.B,td.C);
    printf("三角形的面积和周长:  %6.2f, %6.2f\n",triArea(*pti),triPerimeter(*pti));
    Trid td2 = calcTriEstimate(*pti);
    printf("三角形的三个角分别是:%6.2f, %6.2f, %6.2f(按周长比的估算)\n",td2.A,td2.B,td2.C);
}

int main()
{
    int choice;
    do{
    printf("\n  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗\n");
    printf("  ┋\t1___已知三边,求三角                      ┋\n");
    printf("  ┋\t2___已知两边和其夹角(角度角),求夹角对边  ┋\n"); 
    printf("  ┋\t0___退出系统                              ┋\n"); 
    printf("  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝\n");
    printf("   请输入菜单序号(0~2):");  

        scanf("%d",&choice);
        getchar();  // get the '\n'
        switch(choice)
        {
        case 1:
            {
                Tri ti;
                double a,b,c;
                printf("请输入三角形的三条边(如3 5 7):");
                scanf("%lf %lf %lf",&a,&b,&c);
                getchar();  // get the '\n'
                if(initTri(a,b,c,&ti))
                    printTriD(&ti);
                else
                    printf("三条边的长度关系不能构成一个三角!\n");
                break;
            }
        case 2:
            {
                double sa = 4;
                double sb = 5;
                double angle = 36.87;
                printf("请输入三角形的两边夹一角(如4 5 36.87):");
                scanf("%lf %lf %lf",&sa,&sb,&angle);
                getchar();  // get the '\n'
                printf("两边(%.2f,%.2f)夹一角(%.2f度),角对应的边的边长是:%.2f\n",sa,sb,angle,sideA(sa,sb,angle));
                Trir tr;
                if(initTrir(sa,sb,angle,&tr))
                    printf("两边(%.2f,%.2f)夹一角(%.2f度),角对应的边的边长是:%.2f\n",sa,sb,angle,calcSide(tr));
                else
                    printf("两边(%.2f,%.2f)夹一角(%.2f度),无法构成三角!\n",sa,sb,angle,calcSide(tr));
                break;
            }
        case 0 :
            exit(1);
        default:
            printf("输入错误,请重新输入!");
        }
    }while(choice != 0);
    return 0;
}
/*

  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗
  ┋    1___已知三边,求三角                      ┋
  ┋    2___已知两边和其夹角(角度角),求夹角对边  ┋
  ┋    0___退出系统                              ┋
  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
   请输入菜单序号(0~2):1
请输入三角形的三条边(如3 5 7):3 4 5
三角形的三条边分别是:  3.00,   4.00,   5.00
三角形的三个角分别是: 36.87,  53.13,  90.00
三角形的面积和周长:    6.00,  12.00
三角形的三个角分别是: 45.00,  60.00,  75.00(按周长比的估算)

  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗
  ┋    1___已知三边,求三角                      ┋
  ┋    2___已知两边和其夹角(角度角),求夹角对边  ┋
  ┋    0___退出系统                              ┋
  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
   请输入菜单序号(0~2):2
请输入三角形的两边夹一角(如4 5 36.87):4 5 36.87
两边(4.00,5.00)夹一角(36.87度),角对应的边的边长是:3.00
两边(4.00,5.00)夹一角(36.87度),角对应的边的边长是:3.00

  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗
  ┋    1___已知三边,求三角                      ┋
  ┋    2___已知两边和其夹角(角度角),求夹角对边  ┋
  ┋    0___退出系统                              ┋
  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
   请输入菜单序号(0~2):
*/

2 类方法

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI 3.1415926
#define a2d (180/PI)    // arc to degree

class triangle
{
    double a,b,c;
    double A,B,C;
public:
    triangle(double a,double b,double c);       // 三边定三角形
    triangle(double b,double c,double A,int);   // 两边夹一角
    double triPerimeter();
    double triArea();
    void print();
};

triangle::triangle(double a,double b,double c)
{
    if(a<0 || b<0 || c<0 || a+b<=c || a+c<=b || b+c<=a)
    {
        printf("三条边的边长无法构成一个三角形!");
        a=b=c=0;
    }
    this->a = a;
    this->b = b;
    this->c = c;
    this->A = (b*b+c*c-a*a)/(2*b*c);
    this->B = (a*a+c*c-b*b)/(2*a*c);
    this->C = (a*a+b*b-c*c)/(2*a*b);
    this->A = acos(this->A)*a2d;
    this->B = acos(this->B)*a2d;
    this->C = acos(this->C)*a2d;
}

triangle::triangle(double b, double c, double A,int)
{
    if(b<0 || c<0 || A<0)
    {
        printf("两边夹一角无法构成一个三角形!");
        a=b=c=0;
    }
    this->a = sqrt(b*b+c*c-2*b*c*cos(A/180*PI));
    this->b = b;
    this->c = c;
    this->A = A;
    this->B = (a*a+c*c-b*b)/(2*a*c);
    this->C = (a*a+b*b-c*c)/(2*a*b);
    this->B = acos(this->B)*a2d;
    this->C = acos(this->C)*a2d;
}

double triangle::triArea()
{
    double s = (a+b+c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

double triangle::triPerimeter()
{
    return a+b+c;
}

void triangle::print()
{
    printf("三角形的三条边分别是:%6.2f, %6.2f, %6.2f\n",a,b,c);
    printf("三角形的三个角分别是:%6.2f, %6.2f, %6.2f\n",A,B,C);
    printf("三角形的面积和周长:  %6.2f, %6.2f\n",triArea(),triPerimeter());
}

int main()
{
    int choice;
    do{
    printf("\n  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗\n");
    printf("  ┋\t1___由三边构造三角形                      ┋\n");
    printf("  ┋\t2___通过两边和其夹角(角度角)来构造三角形  ┋\n"); 
    printf("  ┋\t0___退出系统                              ┋\n"); 
    printf("  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝\n");
    printf("   请输入菜单序号(0~2):");  

        scanf("%d",&choice);
        getchar();  // get the '\n'
        switch(choice)
        {
        case 1:
            {
                double a,b,c;
                printf("请输入三角形的三条边(如3 5 7):");
                scanf("%lf %lf %lf",&a,&b,&c);
                getchar();  // get the '\n'
                triangle tri(a,b,c);
                tri.print();
                break;
            }
        case 2:
            {
                double b,c,A;
                printf("请输入三角形的两边夹一角(如4 5 36.87):");
                scanf("%lf %lf %lf",&b,&c,&A);
                getchar();  // get the '\n'
                triangle tri(b,c,A,0);
                tri.print();
                break;
            }
        case 0 :
            exit(1);
        default:
            printf("输入错误,请重新输入!");
        }
    }while(choice != 0);
    return 0;
}
/*

  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗
  ┋    1___由三边构造三角形                      ┋
  ┋    2___通过两边和其夹角(角度角)来构造三角形  ┋
  ┋    0___退出系统                              ┋
  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
   请输入菜单序号(0~2):1
请输入三角形的三条边(如3 5 7):3 5 7
三角形的三条边分别是:  3.00,   5.00,   7.00
三角形的三个角分别是: 21.79,  38.21, 120.00
三角形的面积和周长:    6.50,  15.00

  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗
  ┋    1___由三边构造三角形                      ┋
  ┋    2___通过两边和其夹角(角度角)来构造三角形  ┋
  ┋    0___退出系统                              ┋
  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
   请输入菜单序号(0~2):2
请输入三角形的两边夹一角(如4 5 36.87):4 5 36.87
三角形的三条边分别是:  3.00,   4.00,   5.00
三角形的三个角分别是: 36.87,  53.13,  90.00
三角形的面积和周长:    6.00,  12.00

  ╔┉┉┉操作菜单(输入前面数字即运行对应功能┉┉╗
  ┋    1___由三边构造三角形                      ┋
  ┋    2___通过两边和其夹角(角度角)来构造三角形  ┋
  ┋    0___退出系统                              ┋
  ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
   请输入菜单序号(0~2):
*/

-End-