为什么本人这个程序运行不对。感觉没有进入循环。小白

C语言 码拜 7年前 (2017-04-18) 984次浏览
#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;
}
解决方案

60

你把%lf写成了%1f所以导致读入的数字不对

30

看书不仔细吧,把这一句

while(scanf("%1f %d",&x,&exp)==2)

改成

while(scanf("%lf %d",&x,&exp)==2)

试试
是”%lf”而不是”%1f”,是字母而不是数字


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明为什么本人这个程序运行不对。感觉没有进入循环。小白
喜欢 (0)
[1034331897@qq.com]
分享 (0)