gpt4 book ai didi

javascript - 如何将 C 程序转换为等效的 JavaScript 程序?

转载 作者:行者123 更新时间:2023-11-30 20:21:14 26 4
gpt4 key购买 nike

给定

Base independent sequencer for A217626

/*
* ##########################################
* # Base independent sequencer for A217626 #
* ##########################################
*
* This program is free software.
* Written by R. J. Cano (remy at ula.ve, Or reemmmyyyy at gmail.com)
* On Jan 9 2014, for educational purposes and released under
* the terms of the General Public License 3.0 (GNU-GPL 3.0);
*
* There is NO warranty not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Note: For a large set of terms (>10!-1) the present program might be prone to data type overflows.
*
*/

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

long _base= 10;
long _showOffset= 1;
/** Standard output field width. An aid for comparisons using MD5 checksums. **/
long _normWidth= 13;
/** Set this to 0 for print everything in a single line. **/
long _onePerLine= 1;
/** 0 for the vector representation, 1 for the integer representation **/
long _objectToShow= 1;

long permute(long*, long*, long);
long vec2polyEval(long*, long, long);

int main(int argc, char *argv[]) {
long v1[100],v2[100],v3[100],u[100],n,k,l,offset=0;
_showOffset*= _onePerLine;
/* The size of the output (n!-1 items) is no longer read from the standard input.
scanf("%li",&n); -- Does stop silently, therefore it is avoided. */
n= strtol(argv[1], NULL, _base); /* Direct conversion from the command line parameter(s) */
for(k=0; k<100; k++) {
v1[k]= (k<n)*(k);
v2[k]= v1[k];
v3[k]= 0;
}
while(permute(v2,u,n)) {
for(k=0;k<n-1;k++) {
v3[k+1]=0;
for(l=k+1;l<n;l++) {
v3[k+1]+=(u[l]-v2[l]);
}
}
if (_showOffset) printf("%li ", ++offset);
if (!_onePerLine) printf(",");
if (!_objectToShow) {
for(k=0;k+n<_normWidth;k++) { printf(",0"); }
for(k=0;k<n;k++) { printf(",%li",v3[k]); }
printf(";");
} else {
printf("%li", vec2polyEval(v3,_base,n));
}
if (_onePerLine) printf("\n");
}
if (!_onePerLine) printf("\n");
return EXIT_SUCCESS;
}

long permute(long *data, long *previous, long Size) {
long tau, rho=Size-1, phi=Size-1;
for (tau=0;tau<Size;tau++) previous[tau]= data[tau];
while((rho > 0)&&(data[rho]<= data[rho-1])) rho--;
rho--;
if(rho<0) return 0;
while((phi > rho)&&(data[phi]<=data[rho])) phi--;
tau= data[rho];
data[rho]= data[phi];
data[phi]= tau;
Size--;
rho++;
while(Size>rho) {
tau= data[Size];
data[Size]= data[rho];
data[rho]= tau;
Size--;
rho++;
}
return 1;
}

long vec2polyEval(long* v, long B, long m) {
long ans=0, pow=1, k;
for(k=m-1;k>=0;k--) {
ans+= v[k]*pow;
pow*= B;
}
return ans;
}

到目前为止已经尝试过什么,导致 jsfiddle 出现明显的无限循环

var _base = 10;
var _showOffset = 1;
/** Standard output field width. An aid for comparisons using MD5 checksums. **/
var _normWidth = 13;
/** Set this to 0 for print everything in a single line. **/
var _onePerLine = 1;
/** 0 for the vector representation, 1 for the integer representation **/
var _objectToShow = 1;

// long permute(long*, long*, long);
// long vec2polyEval(long*, long, long);

function main( /*int argc, char **/ argv /*[]*/ ) {
var v1 = Array(100),
v2 = Array(100),
v3 = Array(100),
u = Array(100),
n, k, l, offset = 0;
_showOffset *= _onePerLine;
/* The size of the output (n!-1 items) is no longer read from the standard input.
scanf("%li",&n); -- Does stop silently, therefore it is avoided. */
n = parseInt(argv[1], _base); /* Direct conversion from the command line parameter(s) */
for (k = 0; k < 100; k++) {
v1[k] = (k < n) * (k);
v2[k] = v1[k];
v3[k] = 0;
}
while (permute(v2, u, n)) {
for (k = 0; k < n - 1; k++) {
v3[k + 1] = 0;
for (l = k + 1; l < n; l++) {
v3[k + 1] += (u[l] - v2[l]);
}
}
if (_showOffset) console.log("%li ", ++offset);
if (!_onePerLine) console.log(",");
if (!_objectToShow) {
for (k = 0; k + n < _normWidth; k++) {
console.log(",0");
}
for (k = 0; k < n; k++) {
console.log(",%li", v3[k]);
}
console.log(";");
} else {
printf("%li", vec2polyEval(v3, _base, n));
}
if (_onePerLine) console.log("\n");
}
if (!_onePerLine) console.log("\n");
return "EXIT_SUCCESS";
}

function permute(data, previous, Size) {
var tau, rho = Size - 1,
phi = Size - 1;
for (tau = 0; tau < Size; tau++) previous[tau] = data[tau];
while ((rho > 0) && (data[rho] <= data[rho - 1])) rho--;
rho--;
if (rho < 0) return 0;
while ((phi > rho) && (data[phi] <= data[rho])) phi--;
tau = data[rho];
data[rho] = data[phi];
data[phi] = tau;
Size--;
rho++;
while (Size > rho) {
tau = data[Size];
data[Size] = data[rho];
data[rho] = tau;
Size--;
rho++;
}
return 1;
}

function vec2polyEval(v, B, m) {
long ans = 0, pow = 1, k;
for (k = m - 1; k >= 0; k--) {
ans += v[k] * pow;
pow *= B;
}
return ans;
}

main([1,2,3]);

问题:

  • long v1[100],v2[100],v3[100],u[100]分配v1作为值 Array.length 100 ,或 Number有值(value)100

  • strtol 相当于 parseInt()

  • long permute(long*, long*, long)的目的是什么?和long vec2polyEval(long*, long, long)

  • int argc的目的是什么?在main ,其中argc好像没有在函数中使用?

  • 如何转换c代码为javascript等价?

最佳答案

  • Does long v1[100],v2[100],v3[100],u[100] assign v1 as the value as an Array having .length 100, or a Number having value 100?

长度为 100 的数组。

正常情况下,是的。
请注意,在 JS 代码中您不需要它,因为您传递的是实际整数而不是字符串。

  • What is purpose of int argc at main, where argc does not appear to be used in function?

在 C 中,您正在处理原始内存,因此为了不做一些愚蠢的事情,您需要知道数据在哪里结束(在本例中为 argv )。
C 代码应该使用 argc ,至少要检查是否 argv[1]甚至存在。
在我的机器上不带任何参数运行它会导致段错误。

  • How to convert the c code to javascript equivalent?

首先,将所有剩余的类型名称替换为 var ,否则会出现语法错误。
除此之外,您唯一需要注意的是printf的,...你不能只用 console.log 替换我害怕。
printf不会在其参数中附加换行符,而 console.log将所有内容打印在单独的行上。此外,printf 的第一个参数可以包含格式说明符 ( %... ),该说明符会被提供给它的附加参数替换,并以给定的方式格式化。

我会通过将函数更改为返回输出而不是直接打印来解决这个问题。为此,我将创建一个 ret值是一个字符串,而不是使用 printf ,您附加到该字符串。
至于格式说明符,如果你有类似的东西

printf(",%li",v3[k]);

那么这意味着“附加一个逗号和 v3[k] 的值,格式为 long int ( %li )”。在 JS 中,这只是:

ret += "," + v3[k];

最后,我会更改输入类型。您不需要argv当您只传递单个值时,在 JS 中使用数组(另请注意,在 C 中,argv[0] 是程序本身的路径)。

所有适用的内容,我得到:

var _base = 10;
var _showOffset = 1;
/** Standard output field width. An aid for comparisons using MD5 checksums. **/
var _normWidth = 13;
/** Set this to 0 for print everything in a single line. **/
var _onePerLine = 1;
/** 0 for the vector representation, 1 for the integer representation **/
var _objectToShow = 1;
// long permute(long*, long*, long);
// long vec2polyEval(long*, long, long);
function main( /*int argc, char **/ arg /*[]*/ )
{
var ret = "";
var v1 = Array(100),
v2 = Array(100),
v3 = Array(100),
u = Array(100),
n, k, l, offset = 0;
_showOffset *= _onePerLine;
/* The size of the output (n!-1 items) is no longer read from the standard input.
scanf("%li",&n); -- Does stop silently, therefore it is avoided. */
n = parseInt(arg, _base); /* Direct conversion from the command line parameter(s) */
for(k = 0; k < 100; k++)
{
v1[k] = (k < n) * (k);
v2[k] = v1[k];
v3[k] = 0;
}
while(permute(v2, u, n))
{
for(k = 0; k < n - 1; k++)
{
v3[k + 1] = 0;
for(l = k + 1; l < n; l++)
{
v3[k + 1] += (u[l] - v2[l]);
}
}
if(_showOffset) ret += (++offset) + " ";
if(!_onePerLine) ret += ",";
if(!_objectToShow)
{
for(k = 0; k + n < _normWidth; k++)
{
ret += ",0";
}
for(k = 0; k < n; k++)
{
ret += "," + v3[k];
}
ret += ";";
}
else
{
ret += vec2polyEval(v3, _base, n);
}
if(_onePerLine) ret += "\n";
}
if(!_onePerLine) ret += "\n";
return ret;
}

function permute(data, previous, Size)
{
var tau, rho = Size - 1,
phi = Size - 1;
for(tau = 0; tau < Size; tau++) previous[tau] = data[tau];
while((rho > 0) && (data[rho] <= data[rho - 1])) rho--;
rho--;
if(rho < 0) return 0;
while((phi > rho) && (data[phi] <= data[rho])) phi--;
tau = data[rho];
data[rho] = data[phi];
data[phi] = tau;
Size--;
rho++;
while(Size > rho)
{
tau = data[Size];
data[Size] = data[rho];
data[rho] = tau;
Size--;
rho++;
}
return 1;
}

function vec2polyEval(v, B, m)
{
var ans = 0,
pow = 1,
k;
for(k = m - 1; k >= 0; k--)
{
ans += v[k] * pow;
pow *= B;
}
return ans;
}

var ret = main('5');

// For demo:
document.body.innerText = ret;
document.body.style.whiteSpace = 'pre';

对于我测试过的值,它给出了与 C 程序相同的结果。

关于javascript - 如何将 C 程序转换为等效的 JavaScript 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com