面对循环中的问题 [英] Facing Problem in Loop

查看:107
本文介绍了面对循环中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本X权力N的程序。问题是当

给出循环中指定的范围时它运行良好,但是当按下任何

字符时,它会进入无限循环。


第二个问题是它适用于较小的X或N值而不是

更高的基数15功率20.我怎样才能得到真正的值显示

具有如此高的基数或功率。?

任何人都可以帮我纠正这个程序正确的循环和

以获得高价值的电力。


#include< stdio.h>

#include< conio.h>


长双倍功率(双x,双n) );


双基数,pwr;

长双倍结果;

// int pwr;


void main()

{

clrscr();

gotoxy(22,11);

printf(" Enter Base Number:");

scanf("%lf"& base);

gotoxy(22,13 );

printf(输入功率数字:);

scanf("%lf ",& pwr);

printf("%lf%lf",base,pwr);


result = power(base,pwr) );

gotoxy(22,15);

printf(基础%f的结果,权力%d是\ n%60.10Lf。,基数,

pwr,结果);

getch();

}


长双倍功率(双x,双n)

{

long double res;

if(n< 0)

返回1 / power(x,-n);

否则if(n == 0)

返回1.0;

else

res =(x * power(x,n-1));

printf(" \ n%60.10Lf", res);

return(res);

// return(x * power(x,n-1));

}

解决方案

文章< 11 ********************* *@e65g2000hsc.googlegroups .com> ;,

Vijaykumar Dave< vi ************ @ gmail.comwrote:


>我有一个基本X权力N的程序。问题是,当给出循环中指定的范围时,它运行良好,但是当按下任何
字符时,它会进入无限循环。


> #include< stdio.h>
#include< conio.h>



对不起,标准C中没有conio.h,也没有

gotoxy()或getch()函数。因为那些看起来有些东西需要用你的I / O来处理,而你的I / O有问题

(它会进入无限循环)你将需要咨询新闻组

,它涉及您正在使用的任何操作系统。或者,更好

,重写程序而不使用这些功能。

-

有些想法错了,只有非常聪明这个人

可以相信他们。 - George Orwell


文章< 11 ********************** @ e65g2000hsc.googlegroups .com>,

Vijaykumar Dave< vi ************ @ gmail.comwrote:


>我有一个基本X权力N的程序。


> double base,pwr;


> printf(具有功率%d的基础%f的结果是\ n%60.10Lf。,base,
pwr,结果);



你有一个双倍的pwr,但你试图用

a%d格式打印它,这只对int有效。如果你的任何输出是正确的,那将是令人惊讶的

,除非pwr是0.

-

如果你撒谎到编译器,它将得到它的报复。 - Henry Spencer


2007年4月11日星期三14:03:42 -0700,Vijaykumar Dave写道:


我有一个基本X权力N的程序。问题是当

给出循环中指定的范围时它运行良好,但是当按下任何

字符时,它会进入无限循环。


第二个问题是它适用于较小的X或N值而不是

更高的基数15功率20.我怎样才能得到真正的值显示

具有如此高的基数或功率。?

任何人都可以帮我纠正这个程序正确的循环和

以获得高价值的电力。


#include< stdio.h>

#include< conio.h>



conio.h不是标准的。


long double power(double x,double n );


双基数,pwr;

长双倍结果;

// int pwr;


void main()



main返回int。现在和永远。永远有。


{

clrscr();



C中没有clrscr()这样的函数。


gotoxy(22,11);



C中没有gotoxy这样的函数。


printf(" Enter Base Number:" );

scanf("%lf"& base);

gotoxy(22,13);

printf("输入电源号码:);

scanf("%lf",& pwr);

printf("%lf%lf",base,pwr );


result = power(base,pwr);

gotoxy(22,15);

printf("具有功率%d的基数%f的结果是\ n%60.10Lf。,基数,

pwr,结果);

getch();



没有像C.中的getch()这样的函数。


}


long double power(double x,double n)

{

long double res;

if(n< 0)

返回1 / power(x,-n);

else if(n == 0)



注意n是双精度数。不知怎的,我怀疑这些测试中至少有一个是因为你想的应该是失败的,比如0,实际上是

0.0000000014376或其他一些。


返回1.0;

else

res =(x * power(x,n-1));

printf(" \ n%60.10Lf",res);

return(res);

// return(x * power( x,n-1));

}



如果我是你,我会尝试改变第二个功率参数( )某种味道的
整数。还有,%lf?我认为应该只是%f。


根据我的测试,代码 - 在将n更改为int之后 - 并不是
工作正常。结果:


您的代码:332525673007965060202496.000000

bc:332525673007965087890625

^^^^^^^^


我假设这是使用浮点值时的常见问题。

确实,用长双打测试它再买了另外四位数正确的

回答。


I have a program for base X power N as under. The problem is that when
the range specified in loop is given it works well, but when any
character is pressed, it goes to infinite loop.

Second problem is it works fine for smaller value of X or N but not
for higher say base 15 power 20. How can I get true value displayed
with such higher base or power.?
Can any one help me to correct this program putting proper loop and
for high value power.

#include<stdio.h>
#include<conio.h>

long double power(double x, double n);

double base, pwr;
long double result;
//int pwr;

void main()
{
clrscr();
gotoxy(22,11);
printf("Enter Base Number : ");
scanf("%lf", &base);
gotoxy(22, 13);
printf("Enter Power Number : ");
scanf("%lf", &pwr);
printf("%lf %lf", base,pwr);

result=power(base,pwr);
gotoxy(22, 15);
printf("Result of Base %f having power %d is\n %60.10Lf.", base,
pwr, result);
getch();
}

long double power(double x, double n)
{
long double res;
if (n < 0)
return 1/power(x, -n);
else if (n == 0)
return 1.0;
else
res=(x*power(x,n-1));
printf("\n%60.10Lf", res);
return(res);
// return (x * power(x, n-1));
}

解决方案

In article <11**********************@e65g2000hsc.googlegroups .com>,
Vijaykumar Dave <vi************@gmail.comwrote:

>I have a program for base X power N as under. The problem is that when
the range specified in loop is given it works well, but when any
character is pressed, it goes to infinite loop.

>#include<stdio.h>
#include<conio.h>

Sorry, there is no conio.h in standard C, and there is no
gotoxy() or getch() functions. As those appear to have something to
do with your I/O and you are having problems with your I/O
("it goes to infinite loop") you will need to consult a newsgroup
that deals with whatever operating system you are using. Or, better
yet, rewrite the program without using those functions.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


In article <11**********************@e65g2000hsc.googlegroups .com>,
Vijaykumar Dave <vi************@gmail.comwrote:

>I have a program for base X power N as under.

>double base, pwr;

> printf("Result of Base %f having power %d is\n %60.10Lf.", base,
pwr, result);

You have pwr as a double, but you are attempting to print it with
a %d format, which is only valid for int . It would be surprising
if any of your outputs were correct, other than when pwr was 0.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer


On Wed, 11 Apr 2007 14:03:42 -0700, Vijaykumar Dave wrote:

I have a program for base X power N as under. The problem is that when
the range specified in loop is given it works well, but when any
character is pressed, it goes to infinite loop.

Second problem is it works fine for smaller value of X or N but not
for higher say base 15 power 20. How can I get true value displayed
with such higher base or power.?
Can any one help me to correct this program putting proper loop and
for high value power.

#include<stdio.h>
#include<conio.h>

conio.h isn''t standard.

long double power(double x, double n);

double base, pwr;
long double result;
//int pwr;

void main()

main returns int. Now and forever. Always has.

{
clrscr();

No such function as clrscr() in C.

gotoxy(22,11);

No such function as gotoxy in C.

printf("Enter Base Number : ");
scanf("%lf", &base);
gotoxy(22, 13);
printf("Enter Power Number : ");
scanf("%lf", &pwr);
printf("%lf %lf", base,pwr);

result=power(base,pwr);
gotoxy(22, 15);
printf("Result of Base %f having power %d is\n %60.10Lf.", base,
pwr, result);
getch();

No such function as getch() in C.

}


long double power(double x, double n)
{
long double res;
if (n < 0)
return 1/power(x, -n);
else if (n == 0)

Note that n is a double. Somehow I suspect at least one of these tests is
going to fail as the n you think should be, say, 0 is actually
0.0000000014376 or some such.

return 1.0;
else
res=(x*power(x,n-1));
printf("\n%60.10Lf", res);
return(res);
// return (x * power(x, n-1));
}

If I were you, I''d try changing the second parameter of power() to an
integer of some flavour. Also, %lf? I believe that should simply be %f.

According to my testing, the code - after changing n to an int - doesn''t
work right. Results:

Your code: 332525673007965060202496.000000
bc: 332525673007965087890625
^^^^^^^^

I''m assuming this is the usual problem when using floating point values.
Indeed, testing it with long doubles bought another four digits of correct
answer.


这篇关于面对循环中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆