Demo entry 6781836

sinxcosx

   

Submitted by anonymous on Jan 05, 2019 at 01:25
Language: C. Code size: 1.2 kB.

//
//  main.c
//  sinxcosx
//
//  Created by 刘霁琳 on 2019/1/2.
//  Copyright © 2019年 670. All rights reserved.
//

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

#define N 6 // the bigger N is, the more accuracy result you will get. *RECOMMEND N = 6 FOR THE BEST ANSWER*.
#define pi 3.1415926

double result = 0;

int fac(int n){
    int output = 1, i;
    for (i = 2; i <= n; i++){
        output *= i;
    }
    return output;
}

void _sin(double x){
    int begin = 1, i = 0, jec = 1;
    for (i = 0; i < N; i++){
        result = result + jec * (pow(x, begin) / fac(begin));
        jec *= -1;
        begin += 2;
    }
}

void _cos(double x){
    result = 1;
    int begin = 2, i = 0, jec = -1;
    for (i = 0; i < N; i++){
        result = result + jec * (pow(x, begin) / fac(begin));
        jec *= -1;
        begin += 2;
    }
}

int main(){
    double n;
    int mode = 0;
    printf("INPUT:\n");
    scanf("%lf",&n);
    printf("INPUT 0 FOR SIN, 1 FOR COS:\n");
    scanf("%d",&mode);
    while (n > 2 * pi) {
        n -= 2 * pi;
    }
    if (mode == 0){
        _sin(n);
    }else _cos(n);
    printf("The result is : %.3lf\n", result);
    return 0;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).