需要帮助将简单的C代码编译成JavaScript [英] Need help compiling simple C code into JavaScript

查看:106
本文介绍了需要帮助将简单的C代码编译成JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如何将以下C代码编译成JavaScript:



Hello, how can I compile the following C code into JavaScript:

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

long long c[100];

void coef(int n)
{
    int i, j;

    if (n < 0 || n > 63) abort(); // gracefully deal with range issue

    for (c[i=0] = 1; i < n; c[0] = -c[0], i++)
        for (c[1 + (j=i)] = 1; j > 0; j--)
            c[j] = c[j-1] - c[j];
}

int is_prime(int n)
{
    int i;

    coef(n);
    c[0] += 1, c[i=n] -= 1;
    while (i-- && !(c[i] % n));

    return i < 0;
}

void show(int n)
{
    do printf("%+lldx^%d", c[n], n); while (n--);
}

int main(void)
{
    int n;

    for (n = 0; n < 10; n++) {
        coef(n);
        printf("(x-1)^%d = ", n);
        show(n);
        putchar('\n');
    }

    printf("\nprimes (never mind the 1):");
    for (n = 1; n <= 63; n++)
        if (is_prime(n))
            printf(" %d", n);

    putchar('\n');
    return 0;
}





这应输出:





This should output:

(x-1)^0 = +1x^0
(x-1)^1 = +1x^1-1x^0
(x-1)^2 = +1x^2-2x^1+1x^0
(x-1)^3 = +1x^3-3x^2+3x^1-1x^0
(x-1)^4 = +1x^4-4x^3+6x^2-4x^1+1x^0
(x-1)^5 = +1x^5-5x^4+10x^3-10x^2+5x^1-1x^0
(x-1)^6 = +1x^6-6x^5+15x^4-20x^3+15x^2-6x^1+1x^0
(x-1)^7 = +1x^7-7x^6+21x^5-35x^4+35x^3-21x^2+7x^1-1x^0
(x-1)^8 = +1x^8-8x^7+28x^6-56x^5+70x^4-56x^3+28x^2-8x^1+1x^0
(x-1)^9 = +1x^9-9x^8+36x^7-84x^6+126x^5-126x^4+84x^3-36x^2+9x^1-1x^0

primes (never mind the 1): 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61





谢谢你任何帮助! :)



Thanks you any help! :)

推荐答案

谷歌搜索素数javascript可能会产生一些有用的信息。或者,您可以从学习 http://www.w3schools.com/js/default.asp [ ^ ]。
A google search for "prime javascript" would probably yield some useful information. Alternatively you could start by studying http://www.w3schools.com/js/default.asp[^].


这篇关于需要帮助将简单的C代码编译成JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆