Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <ctype.h>
- #include<string.h>
- #define PI 3.14f
- #define TSF PI/180
- double DegToRad(double deg) //角度轉弳度
- {
- double rad = 0;
- while (deg >= 360) deg -= 360;
- rad = deg*TSF;
- return rad;
- }
- double factorial(int n) //階層
- {
- double a = 1;
- for (int i = 1; i <= n; i++)
- a *= i;
- return a;
- }
- double sine(double x, int n)
- {
- int flag = 0;
- double value = 0;
- if (x < 0) x += 360;
- if (x > 180)
- {
- x -= 180;
- flag = 1;
- }
- x = DegToRad(x);
- for (int i = 0; i <= n; i++)
- {
- value += (pow(-1, i)*pow(x, 2 * i + 1) / factorial(2 * i + 1));
- //printf("%.6lf\n", value);
- }
- return (flag) ? -value : value;
- }
- double cosine(double x, int n)
- {
- int flag = 0;
- double value = 0;
- x = DegToRad(x);
- for (int i = 0; i <= n; i++)
- {
- value += (pow(-1, i)*pow(x, 2 * i) / factorial(2 * i));
- }
- return value;
- }
- int main()
- {
- char str[4] = { 0 };
- double x = 0;
- int n = 0;
- while (scanf("%s %lf %d", &str, &x, &n) != EOF)
- {
- for (int i = 0; i < strlen(str); i++)
- str[i] = toupper(str[i]);
- if (str[0] == 'S' && str[1] == 'I' && str[2] == 'N')
- printf("%.6lf", sine(x, n));
- else if (str[0] == 'C' && str[1] == 'O' && str[2] == 'S')
- printf("%.6llf", cosine(x, n));
- else
- printf("Input Error.");
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment