我正在弄乱的另一个棘手的问题(不是家庭作业) [英] Another Tricky Problem I am Messing With (Not Homework)

查看:66
本文介绍了我正在弄乱的另一个棘手的问题(不是家庭作业)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题,我试图帮助一些朋友想出乐趣。我

我不知道怎么回事,但有些东西我不知道

这里。这是我们想要的输出:

需要从键盘读取一段时间(例如1.2秒,

3.4秒,或8.37秒)。一旦读完时间,我们想要打印出来的字样?bICK?然后等待指定的时间段和

打印这个词?TICK?再次。重复直到

屏幕上有6?TICK?s。如果我们知道如何在TICK和

TOCK之间进行变化,这将是真正的整洁,但这可能是输出。


输入时间== 2.27

TICK<等待2.27秒>

TICK<等待2.27秒>

TICK<等待2.27秒>

TICK<等待2.27秒>

TICK<等待2.27秒>


这是我开始的,我迷路因为我是新手大声笑......这是错误的任何想法?

This is a problem I was trying to help a few friends figure out for fun. I
am not sure how to go about this, but there is something I am missing
here. Here is what we want the output to be:
Need to read in a time period from the keyboard (for example 1.2 seconds,
3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to
print out the word ?TICK? and then wait the designated time period and
print the word ?TICK? again. Repeat until there are 6 ?TICK?s on the
screen. It would be real neat if we knew how to variate between TICK and
TOCK, however this might be the output.

Enter a time ==2.27
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>

Here is what I started, I am lost because I am a newbie lol... This is
wrong any ideas?

展开 | 选择 | Wrap | 行号

推荐答案

joebenjamin写道:
joebenjamin wrote:

这是一个问题我试图帮助一些朋友找出乐趣。我

我不知道怎么回事,但有些东西我不知道

这里。这是我们想要的输出:

需要从键盘读取一段时间(例如1.2秒,

3.4秒,或8.37秒)。一旦读完时间,我们想要打印出来的字样a ?? TICKa ??然后等待指定的时间段和

打印出来的字样?? ?? TICKa ??再次。重复直到

屏幕上有6个?? TICKa ?? s。如果我们知道如何在TICK和

TOCK之间进行变化,这将是真正的整洁,但这可能是输出。
This is a problem I was trying to help a few friends figure out for fun. I
am not sure how to go about this, but there is something I am missing
here. Here is what we want the output to be:
Need to read in a time period from the keyboard (for example 1.2 seconds,
3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to
print out the word a??TICKa?? and then wait the designated time period and
print the word a??TICKa?? again. Repeat until there are 6 a??TICKa??s on the
screen. It would be real neat if we knew how to variate between TICK and
TOCK, however this might be the output.



没有任何东西这样做我决定试一试...... :)


使代码尽可能简单;希望你能理解代码

的作用,因为我发现它不言自明。


我已经有一段时间了用C编码(自80年代后期开始),我不确定是否使用了任何非标准的C功能。希望这里的一些常客

如果有的话可以发现''和更正'。


#include< stdio.h>

#include< conio.h>

#include< time.h>


int main()

{

clock_t start,target,end;

浮动计时器;

int i,j;

char msg [] [5] = {" TICK"," TOCK"};

do

{

printf( \ n以秒为单位输入任何时间:;;

scanf("%f",& timer);


i = j = 0;


printf(" Start timing\\\
);

start = clock();

while( i ++< 6)

{

target = clock()+(clock_t)(timer *(float)CLOCKS_PER_SEC);


while(clock()< target);


printf("%s",msg [j]);


如果(++ j 1)

j = 0;

}

end = clock() - start;


printf(" \ n总时间经过:%0.3f秒,%0.3f /迭代\ n",

(浮动)结束/ 1000,(浮动)结束/((i-1)* 1000));


printf(另一个去? y / [n]");

} while(getch()==''y'');

}

Had nothing to do so I decided to give it a shot... :)

Made the code as simpler as I could; hopefully you''ll understand what the code
does, as I find it self-explanatory.

It''s been awhile since I''ve coded in C (since the late 80''s) and am not sure
whether I''ve used any non-standard C features. Hopefully some of the regulars
here can spot''em and correct''em if I have.

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

int main( )
{
clock_t start, target, end;
float timer;
int i, j;
char msg[][5] = {"TICK", "TOCK"};
do
{
printf("\nEnter any time in seconds: ");
scanf ("%f", &timer);

i = j = 0;

printf( "Start timing\n" );
start = clock();
while(i++ < 6)
{
target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);

while(clock() < target);

printf("%s ", msg[j]);

if(++j 1)
j = 0;
}
end = clock() - start;

printf( "\nTotal Time Elapsed : %0.3f seconds, %0.3f/iteration\n",
(float)end/1000, (float)end/((i-1)*1000));

printf("Another go? y/[n] ");
} while(getch() == ''y'');
}


Miguel Guedes写道:


< snip>
Miguel Guedes wrote:

<snip>

{

target = clock()+(clock_t)(timer *(float)CLOCKS_PER_SEC);

while(clock()< target);
{
target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);

while(clock() < target);



虽然这是一种标准的一致方式,等待一段时间

期间,但它实际上不是一个非常友好的方式 - 用户系统。

这个循环可能会消耗100%的CPU时间,而它什么都不做。


对于这种任务,建议寻找一个实现

的具体方法。想到Sleep(Windows)和sleep / usleep(unix)

等功能。


Bart v Ingen Schenau

- -

acllc-c ++常见问题解答: http:// www。 comeaucomputing.com/learn/faq

clc常见问题: http://www.eskimo.com/~scs/C-faq/top.html

clc ++常见问题: http://www.parashift.com/c++-faq-lite/

Although this is a standard conforming way to wait for a certain time
period, it is actually not a very friendly way on a multi-user system.
This loop probably consumes 100% CPU time while it is doing nothing.

For this kind of task, it is advisable to look for a implementation
specific method. Functions like Sleep (Windows) and sleep/usleep (unix)
come to mind.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/


Bart van Ingen Schenau< ba ** @ ingen.ddns.infowrites:
Bart van Ingen Schenau <ba**@ingen.ddns.infowrites:

Miguel Guedes写道:

< snip>
Miguel Guedes wrote:
<snip>

> {
target = clock()+(clock_t)(timer *(float)CLOCKS_PER_SEC);

while (clock()< target);
>{
target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);

while(clock() < target);



虽然这是一种标准的符合标准的方式等待一段时间

期间,但它实际上并不是一个非常友好的方式 - 用户系统。

这个循环可能会消耗100%的CPU时间,而它什么都不做。


Although this is a standard conforming way to wait for a certain time
period, it is actually not a very friendly way on a multi-user system.
This loop probably consumes 100% CPU time while it is doing nothing.



它并不一定按照你的意愿去做。时钟()

函数返回CPU时间的指示,而不是实时。因此,对于

示例,你的程序获得了25%的CPU,循环将等待4

倍,只要你可能想要它(浪费25 CPU的百分比

没有什么特别有用的东西。

And it doesn''t necessarily do what you want it to do. The clock()
function returns an indication of CPU time, not real time. So if, for
example, your program is getting 25% of the CPU, the loop will wait 4
times as long as you probably want it to (while wasting 25% of the CPU
doing nothing particularly useful).


对于这种任务,建议寻找实现

具体方法。睡眠(Windows)和睡眠/ usleep(unix)

等功能会浮现在脑海中。
For this kind of task, it is advisable to look for a implementation
specific method. Functions like Sleep (Windows) and sleep/usleep (unix)
come to mind.



的确如此。延迟指定的时间间隔就是使用标准C不能很好地完成的那些事情之一

,但可能使用某些系统可以很好地完成* b $ b特定的界面。


(标准定义一个sleep()

函数已经很容易了,但是需要这个功能的应用程序几乎总是如此

需要在便携式

界面中无法轻易定义的其他功能。)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti。 net / ~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

"我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

Indeed. Delaying for a specified time interval is one of those things
that cannot be done well using only standard C, but that can probably
be done *very* well using some system-specific interface.

(It would have been easy enough for the standard to define a sleep()
function, but applications that need that functionality almost always
need other functionality that can''t be define so easily in a portable
interface.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于我正在弄乱的另一个棘手的问题(不是家庭作业)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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