juinda

Untitled

Oct 27th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. // 20161027泰勒sin cos.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdio.h"
  5. #include<stdlib.h>
  6. #include<ctype.h>
  7. #include<math.h>
  8. float fsin(float,float,float);
  9. int main()
  10. {
  11.  
  12. char a[4], b = "sin";
  13. float angle, n;
  14.  
  15. while (scanf("%s", a)!= EOF)
  16. {
  17. scanf("%f %f", &angle, &n);
  18. int i = 0;
  19. while (a[i] != '\0')//to小字母
  20. {
  21. a[i] = tolower(a[i]);
  22. i++;
  23. }
  24. while (angle > 180 || angle < -180)//角度轉換
  25. {
  26. if (angle > 180)angle -= 360;
  27. if (angle < -180)angle += 360;
  28. }
  29. angle = angle*3.14 / 180;
  30. if (!strcmp(a, "sin"))
  31. {
  32. printf("%f", n);
  33. //system("pause");
  34. float sum = fsin(angle, n, 0);
  35. printf("\n%f",sum);
  36. }
  37. else if (!strcmp(a, "cos"))
  38. {
  39.  
  40. }
  41. else
  42. {
  43. puts("Input Error.");
  44.  
  45. }
  46. }
  47. //while (getchar() != '\n');
  48. }
  49.  
  50. float fsin(float angle, float n,float sum)
  51. {
  52. if ((int)n == -1)
  53. {
  54. printf("\n%f", sum);
  55. return sum;
  56. }
  57. int pn=-1;
  58. int div = 1;
  59. for (int i = 1; i <= 2 * n + 1; i++)
  60. div *= i;
  61. if ((int)n % 2 == 0)pn = 1;
  62. sum += pn*pow(angle, 2 * n + 1) / div;
  63. //printf("\n %f",sum);
  64. fsin(angle,--n,sum);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment