2015-06-01 75 views
-3

我想在C程序中转换此方程来求解方程,但结果总是错误的。我想将所有三个包围方程转换为C代码。我已经对前两个代码进行了编码,但请检查是否有问题。将方程转换为c程序

Equations Here

代码第一方程

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

int main() 
{ 
    /* Define temporary variables */ 
    double r1,r2,u; 
    double upper,lower,value,result; 

    printf("Enter coefficients r1"); 
    scanf("%f",&r1); 

    printf("Enter coefficients r2 "); 
    scanf("%f,&r2); 

    printf("Enter coefficients u "); 
    scanf("%f",&u); 

    /* Assign the value we will find the cosh of */ 
    value = r1*r2; 

    /* Calculate the Hyperbolic Cosine of value */ 
    upper = acos(value); 

    lower = sqrt((u*u)+1) - u; 

    result = upper/lower; 

    /* Display the result of the calculation */ 
    printf("The spinner rotaiton angle is %f",result); 

    return 0; 
} 

代码第二方程

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

int main() 
{ 
    /* Define temporary variables */ 
    double r,w,u; 
    double a1,result; 

    printf("Enter coefficients R"); 
    scanf("%f",&r); 

    printf("Enter coefficients w angular velocity "); 
    scanf("%f,&w); 

    printf("Enter coefficients u cofficient of friction"); 
    scanf("%f",&u); 

    a1 = sqrt((u*u)+1) - u; 
    a2 = a1*a1; 
    a3 = sqrt (1 + a2); 
    result = r * w * a3; 


    /* Display the result of the calculation */ 
    printf("The departure velocity is %f",result); 
    scanf(%f); 

    return 0; 
} 
+0

也ACOSH即COS双曲线的示值误差 – Nitin

+0

你有什么问题的倒数。 –

+2

'scanf(“%f”,&r1);' - >'scanf(“%lf”,&r1);'etc. – chux

回答

4

线条

scanf("%f",&r1); 
scanf("%f",&w); 
scanf("%f",&u); 

使用错误的格式说明吨o阅读double s。

需要的可以

scanf("%lf",&r1); // Use %lf instead of %f 
scanf("%lf",&w); 
scanf("%lf",&u); 

此外,它始终是检查IO操作的返回值,并打印输入数据,以确保该值被正确地读出一个好主意。

if (scanf("%lf",&r1) != 1) 
{ 
    // Deal with the error. 
} 
printf("The value of r1: %lf\n", lf); 

此外,你必须

/* Calculate the Hyperbolic Cosine of value */ 
upper = acos(value); 

评论是否正确的代码是对目前尚不清楚。您指向的链接表示您应该使用反双曲余弦,acosh

upper = acosh(value); 
+0

但acosh(价值);是错误的。“功能acosh应该有一个原型”这意味着 – Nitin

+0

现在acosh给我做主要错误和结果是-NAN – Nitin

+1

@Nitin,请参阅http://en.cppreference.com/w/c/numeric/math/acosh上的**错误处理**部分。 –

1

声明

/* Calculate the Hyperbolic Cosine of value */ 
upper = acos(value); 

会给你的value反余弦值,那么该公式需要反双曲余弦值。

将其改为:upper = acosh(value);由于acosh()函数计算value的反余弦余弦(余弦的反双曲线)。

收到值:

scanf("%lf",&r1); 
scanf("%lf",&w); 
scanf("%lf",&u);