gpt4 book ai didi

java - 将我的 Java 代码正确转换为 C (错误 : no matching function for call to 'power1' )

转载 作者:行者123 更新时间:2023-11-30 17:05:46 25 4
gpt4 key购买 nike

我用 Java 编写了这个递归程序,但将其转换为 C 时遇到问题。(刚刚开始学习 C)

我的java代码

public class Power
{
static int count;

public static void main( String[] args )
{
double[] base = { 1.4, 1.3, 1.2, 1.1, 1.0 };
int[] index = { 5, 20, 63, 73, -1 };
double value;

System.out.println( "\nTest two algorithms for powering\n" );
for( int i=0 ; i<base.length ; i++ )
{
count = 0;
value = power1( base[i], index[i] );
System.out.println( "1: " + base[i] + "^" + index[i] + " = " + value + ", used " + count + " multiplies" );

count = 0;
value = power2( base[i], index[i] );
System.out.println( "2: " + base[i] + "^" + index[i] + " = " + value + ", used " + count + " multiplies" );
System.out.println();
}


}

public static double power1( double base, int index )
{
double retValue;

assert( index>=0 );

if( index == 0 )
{
retValue = 1;
}
else
{
retValue = base * power1( base, index-1 );
count++;
}

return retValue;
}

public static double power2( double base, int index )
{
double retValue;
double temp;

assert( index>=0 );

if( index == 0 )
{
retValue = 1;
}
else if( index%2 == 1 )
{
retValue = base * power2( base, index-1 );
count++;
}
else
{
temp = power2( base, index/2 );
retValue = temp*temp;
count++;
}

return retValue;
}
}

我正在处理的 C 代码...

#include <stdio.h>
#include <string.h>
#include <assert.h>

//#define count;
#define ARRAY_LENGTH 5

static int count;

double power1(double base, int index);
double power2(double base, int index);

int main(int argc, char *argv[]){
double base[5] = {1.4, 1.3, 1.2, 1.1, 1.0};
int index[5] = {5, 20, 63, 73, -1};
double value;

printf("%s\n", "test two algorithms for powering\n" );
for(double i = 0; i < ARRAY_LENGTH; i++){
count = 0;
//value = power1(base[i], index[i]);
value = power1(base, index);

printf("1: %f^%d = %f, used %d multiples", base,index,value,count);

count =0;

value = power2(base, index);

printf("2: %f^%d = %f, used %d multiples", base[i],index[i],value,count);

}




}

double power1(double base[5], int index[5]){
double retValue;
double temp;

assert(index>=0);

if(index ==0){
retValue=1;
}else if(index%2 ==1){
retValue = base * power2(base, index-1);
count++;
}else{
temp = power2(base, index/2);
retValue = temp * temp;
count++;
}

return retValue;
}


double power2(double base[5], int index[5]){
double retValue;
double temp;

assert(index >=0);

if(index ==0){
retValue=1;

}else if( index %2 ==1){
retValue = base * power2(base, index-1);
count++;
}else{
temp = power2(base, index/2);
retValue = temp * temp;
count++;
}
return retValue;
}

这些是我的错误和警告(其中大部分是重复的):

/Users/elle/Desktop/power.c:22:11: error: no matching function for call to 'power1'
value = power1(base, index);
^~~~~~
/Users/elle/Desktop/power.c:10:8: note: candidate function not viable: no known conversion from 'double [5]' to 'double' for 1st argument
double power1(double base, int index);
^
/Users/elle/Desktop/power.c:25:46: warning: format specifies type 'double' but the argument has type 'double *' [-Wformat]
printf("1: %f^%d = %f, used %d multiples", base,index,value,count);
~~ ^~~~
/Users/elle/Desktop/power.c:25:51: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("1: %f^%d = %f, used %d multiples", base,index,value,count);
~~ ^~~~~
/Users/elle/Desktop/power.c:29:11: error: no matching function for call to 'power2'
value = power2(base, index);
^~~~~~
/Users/elle/Desktop/power.c:11:8: note: candidate function not viable: no known conversion from 'double [5]' to 'double' for 1st argument
double power2(double base, int index);
^
/Users/elle/Desktop/power.c:32:50: error: array subscript is not an integer
printf("2: %f^%d = %f, used %d multiples", base[i],index[i],value,count);
^~
/Users/elle/Desktop/power.c:32:59: error: array subscript is not an integer
printf("2: %f^%d = %f, used %d multiples", base[i],index[i],value,count);
^~
/Users/elle/Desktop/power.c:49:16: error: invalid operands to binary expression ('int *' and 'int')
}else if(index%2 ==1){
~~~~~^~
/Users/elle/Desktop/power.c:50:21: error: no matching function for call to 'power2'
retValue = base * power2(base, index-1);
^~~~~~
/Users/elle/Desktop/power.c:11:8: note: candidate function not viable: no known conversion from 'double *' to 'double' for 1st argument; dereference the argument with *
double power2(double base, int index);
^
/Users/elle/Desktop/power.c:53:28: error: invalid operands to binary expression ('int *' and 'int')
temp = power2(base, index/2);
~~~~~^~
/Users/elle/Desktop/power.c:71:18: error: invalid operands to binary expression ('int *' and 'int')
}else if( index %2 ==1){
~~~~~ ^~
/Users/elle/Desktop/power.c:72:19: error: invalid operands to binary expression ('double *' and 'double')
retValue = base * power2(base, index-1);
~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~
/Users/elle/Desktop/power.c:75:28: error: invalid operands to binary expression ('int *' and 'int')
temp = power2(base, index/2);
~~~~~^~
2 warnings and 10 errors generated.
[Finished in 0.1s with exit code 1]

输出应该是这样的

Test two algorithms for powering

1: 1.4^5 = 5.378239999999998, used 5 multiplies
2: 1.4^5 = 5.378239999999998, used 4 multiplies

1: 1.3^20 = 190.04963774880824, used 20 multiplies
2: 1.3^20 = 190.04963774880818, used 6 multiplies

1: 1.2^63 = 97368.50480227199, used 63 multiplies
2: 1.2^63 = 97368.50480227212, used 11 multiplies

1: 1.1^73 = 1051.1531995000591, used 73 multiplies
2: 1.1^73 = 1051.1531995000587, used 9 multiplies

Exception in thread "main" java.lang.AssertionError
at Power.power1(Power.java:31)
at Power.main(Power.java:15)

我在大多数情况下都将 java 与 c 混合在一起,几个小时以来我一直在寻找如何修复这些错误。

如有任何帮助,我们将不胜感激!

最佳答案

您并不是想将数组传递给您的函数:

double power1(double base[5], int index[5])

与Java版本不匹配:

public static double power1( double base, int index )

应该很简单:

double power1(double base, int index)

power2相同。

不知何故,你的声明是正确的,但定义是错误的。

您还需要执行 value = power1( base[i], index[i] ); 就像 Java 版本中一样。

关于java - 将我的 Java 代码正确转换为 C (错误 : no matching function for call to 'power1' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108189/

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