如何在C程序中使用sin,tan,cos和sec?

时间:2015-07-26 14:14:33

标签: c math sin cos

我正在使用此链接上的公式制作计算器: http://cereference.com/book/surveying-and-transportation-engineering/simple-curves-or-circular-curves#sthash.qrD1VOm6.08csgYq9.dpbs

https://www.easycalculation.com/engineering/civil/highways-horizontal-curve.php

编辑问题!

所以我使用math.h库来使用sin,tan,cos和sec函数,但答案是不正确的,基于我的公式...所以要测试,让我说我的角度为36并且半径为286 ...所以切线(utangent)的答案必须是92.927。我的下一个问题是如何使用sec函数?我评论它是因为它不会编译...还有tan,sin和cos。

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


int main(){

double length, angle, radius, tangent, chord, midordinate, external, degree;
double pcurve, pintersection, ptangent;
double ulength, uangle, uradius, utangent, uchord, umidordinate, uexternal;
double pi;
double choice, choice2, given;

pi = 3.14159;

printf("Enter radius: ");
scanf("%lf",&radius);

printf("Enter angle: ");
scanf("%lf",&angle);

utangent = radius * (tan(angle/2)); 
uchord = 2*radius*(sin(angle/2));
umidordinate = radius - (radius*(cos(angle/2)));
//uexternal = radius * (sec(angle/2)) - radius;

printf("tangent = %lf\n",utangent);
printf("chord = %lf\n",uchord);
printf("ordinate = %lf\n",umidordinate);
//printf("%lf\n",uexternal);

getch();
return 0;
}

1 个答案:

答案 0 :(得分:4)

如果您使用警告编译代码,您绝对应该这样做:

sintancos.c:15:13: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%f", &angle);
       ~~   ^~~~~~
       %lf

通过警告信息的建议将其重写为scanf("%lf", &angle);来解决此问题。

我假设你需要重新计算从度到弧度的输入,因为你要求学位。当然,在再次输出之前,您需要将其更改回度。

它通常在C中用宏完成,但我更喜欢函数。

double to_degrees(double rad)
{
  return rad * 180.0 / M_PI;
}
double to_radians(double deg)
{
  return deg * M_PI / 180.0;
}

M_PI几乎总是在math.h中定义,其精度高于pi。您还应该将输入和计算移动到自己的函数中,以便于阅读和测试。

sec不是标准C函数,因此您必须自己定义它。它会是这样的:

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

double to_degrees(double rad)
{
  return rad * 180.0 / M_PI;
}

double to_radians(double deg)
{
  return deg * M_PI / 180.0;
}

double sec(double z_r)
{
  return 1 / cos(z_r);
}

int main(){

  double angle, radius, angle_r;
  double utangent, uchord, umidordinate, uexternal;


  //printf("Enter radius: ");
  //scanf("%lf",&radius);
  radius = 286;

  //printf("Enter angle: ");
  //scanf("%lf",&angle);
  angle = 36;

  angle_r = to_radians(angle);

  utangent = radius * (tan(angle_r/2)); 
  uchord = 2*radius*(sin(angle_r/2));
  umidordinate = radius - (radius*(cos(angle_r/2)));
  uexternal = radius * (sec(angle_r/2)) - radius;

  printf("\nResults:\n");
  printf("tangent = %lf\n",utangent);
  printf("chord = %lf\n",uchord);
  printf("ordinate = %lf\n",umidordinate);
  printf("external %lf\n",uexternal);

  return 0;
}