Skip to content

Instantly share code, notes, and snippets.

@splitline
Created November 8, 2016 12:48
Show Gist options
  • Save splitline/36a4a1c816e907a59ad1aaa441c5cee8 to your computer and use it in GitHub Desktop.
Save splitline/36a4a1c816e907a59ad1aaa441c5cee8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
double f(int n){
return n<=1?1:n*f(n-1);
}
int main(){
char str[100]={0};
while(~scanf("%s",str)){
int count=0;
while(str[count])str[count++]=tolower(str[count]);
if(strcmp(str,"sin")&&strcmp(str,"cos")){
printf("Input Error.\n");
memset(str,0,sizeof(str));
continue;
}
double x;
int neg=0,n;
scanf("%lf%d",&x,&n);
while(x>=360 || x<0){
if(x>=360)
x-=360;
else if(x<0)
x+=360;
}
if(x>=180) {
x-=180;
neg=1;
}
double r=x*3.14/180,s=0.0;
if(!strcmp(str,"sin")){
for(int i=0;i<=n;i++)
s+=pow(-1,i)*powl(r,2*i+1)/f(2*i+1);
if(neg)s*=-1;
printf("%lf\n",s);
}
else if(!strcmp(str,"cos")){
for(int i=0;i<=n;i++)
s+=pow(-1,i)*powl(r,2*i)/f(2*i);
if(neg)s*=-1;
printf("%lf\n",s);
}
memset(str,0,sizeof(str));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment