#include<stdio.h>
double power(double n,int p);
int main(void)
{
double x,xpow;
int exp;
printf(“enter a number and the positive integer power”);
printf(” to which\nthe number will be raised. enter q”);
printf(” to quit.\n”);
while(scanf(“%1f %d”,&x,&exp)==2)
{
xpow=power(x,exp);
printf(“%.3g to the power %d is %.5g\n”,x,exp,xpow);
printf(“enter next pair of numbers or q to quit.\n”);
}
printf(“hope you enjoyed this power trip–bye!\n”);
return 0;
}
double power(double n,int p)
{
double pow =1;
int i;
for(i=1;i<=p;i++)
pow*=n;
return pow;
}
double power(double n,int p);
int main(void)
{
double x,xpow;
int exp;
printf(“enter a number and the positive integer power”);
printf(” to which\nthe number will be raised. enter q”);
printf(” to quit.\n”);
while(scanf(“%1f %d”,&x,&exp)==2)
{
xpow=power(x,exp);
printf(“%.3g to the power %d is %.5g\n”,x,exp,xpow);
printf(“enter next pair of numbers or q to quit.\n”);
}
printf(“hope you enjoyed this power trip–bye!\n”);
return 0;
}
double power(double n,int p)
{
double pow =1;
int i;
for(i=1;i<=p;i++)
pow*=n;
return pow;
}
解决方案
60
你把%lf写成了%1f所以导致读入的数字不对
30
看书不仔细吧,把这一句
while(scanf("%1f %d",&x,&exp)==2)
改成
while(scanf("%lf %d",&x,&exp)==2)
试试
是”%lf”而不是”%1f”,是字母而不是数字