求大佬看看
#include <stdio.h>#include <math.h>
//double power(double x,double y);
voidmain()
{
double power(double x,double y);
double a,b,c;
printf("input two numbers: ");
scanf("%.2f,%.2f",&a,&b);
c=power(a,b);
printf("%.2f 的%.2f是%.2f\n",a,b,c);
}
double power(double x,double y)
{
doublez;
for(z=1;y>=1;--y)
{
z=z*x;
return z;
}
}
实现pow函数有错误但是我看不出来求大佬解决一下 #include <stdio.h>
double power(double x , int y)
{
doublez ;
for(z = 1 ; y > 0 ; y --) z *= x ;
return z ;
}
int main(void)
{
double a ;
int b ;
printf("input two numbers : ") ;
scanf("%lf%d" , & a , & b) ;
printf("%.2lf ^ %d = %.2f\n" , a , b , power(a , b)) ;
}
编译运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
input two numbers : 2 3
2.00 ^ 3 = 8.00
D:\\C> double power(double x,double y)
{
doublez;
for(z=1;y>=1;--y)
{
z=z*x;
return z;//把return 放在了for里面,执行1次就return 了
}
} #include <stdio.h>
#include <math.h>
extern double power(double x,double y);
intmain()
{
double a,c;
int b;
printf("input two numbers: ");
scanf("%lf%d",&a,&b);
c=power(a,b);
printf("%lf 的%d是%.2f\n",a,b,c);
return 0;
}
double power(double x,double y)
{
doublez;
for(z=1;y>=1;--y)
{
z=z*x;
}
return z;
}运行结果:
input two numbers: 10 2
10.000000 的2是100.000000
--------------------------------
Process exited after 3.355 seconds with return value 0
Press ANY key to exit... 两手空空儿 发表于 2022-11-17 09:36
改了 但是x,y的值传不到函数里 homeskating 发表于 2022-11-17 10:55
#include <stdio.h>
#include <math.h>
extern double power(double x,double y);
//double power(double x,double y);
intmain()
{
// double power(double x,double y);
double a,c;
int b;
printf("input two numbers: ");
scanf("%f%d",&a,&b);
c=power(a,b);
printf("%f的%d是%.2f\n",a,b,c);
return 0;
}
double power(double x,double y)
{
doublez;
for(z=1;y>=1;y--)
{
z=z*x;
}
return z;
}
改的跟你的差不多了但是不知道为什么识别不了x
本帖最后由 jhq999 于 2022-11-17 16:29 编辑
{:5_105:} power方法的for循环写的有问题,第一次循环z=1时候就直接return了,所以要是这么写,power函数最后的返回值就是输入的第一个参数的值
页:
[1]