juinda

Untitled

Nov 3rd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <ctype.h>
  4. #include<string.h>
  5. #define PI 3.14f
  6. #define TSF PI/180
  7.  
  8. double DegToRad(double deg) //角度轉弳度
  9. {
  10. double rad = 0;
  11. while (deg >= 360) deg -= 360;
  12. rad = deg*TSF;
  13. return rad;
  14. }
  15.  
  16. double factorial(int n) //階層
  17. {
  18. double a = 1;
  19. for (int i = 1; i <= n; i++)
  20. a *= i;
  21. return a;
  22. }
  23.  
  24. double sine(double x, int n)
  25. {
  26. int flag = 0;
  27. double value = 0;
  28. if (x < 0) x += 360;
  29. if (x > 180)
  30. {
  31. x -= 180;
  32. flag = 1;
  33. }
  34. x = DegToRad(x);
  35. for (int i = 0; i <= n; i++)
  36. {
  37. value += (pow(-1, i)*pow(x, 2 * i + 1) / factorial(2 * i + 1));
  38. //printf("%.6lf\n", value);
  39. }
  40. return (flag) ? -value : value;
  41. }
  42.  
  43. double cosine(double x, int n)
  44. {
  45. int flag = 0;
  46. double value = 0;
  47.  
  48.  
  49. x = DegToRad(x);
  50. for (int i = 0; i <= n; i++)
  51. {
  52. value += (pow(-1, i)*pow(x, 2 * i) / factorial(2 * i));
  53. }
  54. return value;
  55. }
  56.  
  57. int main()
  58. {
  59. char str[4] = { 0 };
  60. double x = 0;
  61. int n = 0;
  62. while (scanf("%s %lf %d", &str, &x, &n) != EOF)
  63. {
  64. for (int i = 0; i < strlen(str); i++)
  65. str[i] = toupper(str[i]);
  66. if (str[0] == 'S' && str[1] == 'I' && str[2] == 'N')
  67. printf("%.6lf", sine(x, n));
  68. else if (str[0] == 'C' && str[1] == 'O' && str[2] == 'S')
  69. printf("%.6llf", cosine(x, n));
  70. else
  71. printf("Input Error.");
  72. printf("\n");
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment