| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
个人感觉所写代码没问题 
但不知道是不是数据类型的原因 
每次输入进去一个小数都会有问题 
无法正确得到base的值 
求大佬指点 
代码如下: 
#include<math.h> 
#include<stdio.h> 
double a; 
int b; 
float result=1; 
 
double power(double base,int exponent) 
{ 
    for(int i = 1;i <= exponent;i++) 
        result *= base; 
    //result = pow(base,exponent); 
    return result; 
} 
 
int main() 
{ 
    printf("Please input the base:\n"); 
    scanf("%f",&a); 
    printf("Please input the exponent:\n"); 
    scanf("%d",&b); 
    power(a,b); 
    printf("The result of the pow is %f",result); 
    return 0; 
}
 
代码格式化错了,double  数据类型应该用 %lf ,而不是 %f 
 
float 用 %f 哈,参考代码: 
 
- #include<math.h>
 
 - #include<stdio.h>
 
 - double a;
 
 - int b;
 
 - float result=1;
 
  
- double power(double base,int exponent)
 
 - {
 
 -     for(int i = 1;i <= exponent;i++)
 
 -         result *= base;
 
 -     //result = pow(base,exponent);
 
 -     return result;
 
 - }
 
  
- int main()
 
 - {
 
 -     printf("Please input the base:\n");
 
 -     scanf("%lf",&a);
 
 -     printf("Please input the exponent:\n");
 
 -     scanf("%d",&b);
 
 -     power(a,b);
 
 -     printf("The result of the pow is %.2lf",result);
 
 -     return 0;
 
 - }
 
  复制代码 
 
 
 |   
 
 
 
 |