2013-04-04 124 views
1

我刚开始接受这个C++课程就像一个月前。 现在我被分配编写一个程序来计算这个。我不知道我做错了什么。C++(函数,余弦)没有给出正确的答案

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

float gatherl1(); 
float gatherl2(); 
float gatheran(); 
void values(float,float,float); 
float findlength(float,float,float); 
float findan2(float,float,float); 
float findan3(float,float,float); 
void name(float,float,float); 


int main(void) 
{ 
    float length1,length2; 

    float length3; 

    float angle1,angle2,angle3; 

    length1 = gatherl1(); 
    length2 = gatherl2(); 
    angle1 = gatheran(); 

    values(length1,length2,angle1); 

    length3 = findlength(length1,length2,angle1); 

    angle2 = findan2(length1,length2,length3); 
    angle3 = findan3(length1,length2,length3); 

    name(angle1,angle2,angle3); 
} 

float gatherl1() 
{ 
     float l1; 

     printf("Enter the length of one of the sides of any triangle\n"); 

     scanf("%f",&l1); 

     return l1; 
} 

float gatherl2() 
{ 
     float l2; 


     printf("Enter the length of the other side\n"); 

     scanf("%f",&l2); 



     return l2; 
} 

float gatheran() 
{ 
     float angle; 

     printf("Enter the angle between them.\n"); 

     scanf("%f",&angle); 

     return angle; 
} 



void values(float l1, float l2, float angle) 
{ 
    printf("\n The two sides are %f and %f. The angle between them is %f \n",l1,l2,angle); 
} 
float findlength(float l1, float l2, float angle) 
{ 
    float l3,pyt,boy; 

    if (angle==90) 
    { 
     pyt = pow(l1,2) + pow(l2,2); 
     l3 = sqrt(pyt); 

    } 
    else 
    { 
     boy = pow(l1,2) + pow(l2,2) - 2*l1*l2*cos(angle); 
     l3 = sqrt(boy); 
    } 


    printf("\nthe third side is = %f",l3); 
    return l3; 
} 

float findan2(float l1, float l2, float l3) 
{ 
    float cosangle2,angle2; 

    cosangle2 = (pow(l2,2) + pow(l3,2) - pow(l1,2))/(2*l2*l3); 
    angle2 = acos(cosangle2); 
    return angle2; 

} 
float findan3(float l1, float l2, float l3) 
{ 
    float cosangle3,angle3; 

    cosangle3 = (pow(l1,2) + pow(l3,2) - pow(l2,2))/(2*l1*l3); 
    angle3 = acos(cosangle3); 
    return angle3; 

} 

void name(angle,angle2,angle3) 
{ 
    printf("\n\n\n the other two angles are %f and %f",angle2,angle3); 

    printf("\n\n\n The angle you put is %f",angle); 


    if(angle == 90) 
    { 
     printf("\n The triangle is a right triangle\n"); 
    } 
    else if(angle < 90) 
    { 
     printf("\n The triangle is a acute triangle\n"); 
    } 
    else 
    { 
     printf("\n The triangle is a obtuse triangle\n"); 
    } 


} 

我从来没有使用cos和arccos函数,所以我不确定这是否是原因。 或功能,因为我也是新功能。请帮帮我!!谢谢。

+4

你真的需要''pow''来计算广场吗? – gongzhitaao 2013-04-04 22:41:13

回答

4

您以弧度方式传递给函数的值是多少?因为cos和arccos将弧度视为输入。

+2

只是挑剔,但在'acos'的情况下,这是输出,而不是输入。 – Barmar 2013-04-04 22:43:24

+0

感谢您的支持 – Fred 2013-04-04 23:14:48

1

C三角函数以弧度而非度数运行。您必须乘以pi/180才能将度数转换为弧度。