+ -
当前位置:首页 → 问答吧 → 让一排字一个个输出,除了用sleep还有什么好方法吗

让一排字一个个输出,除了用sleep还有什么好方法吗

时间:2011-12-09

来源:互联网

RT

作者: freeicy2007   发布时间: 2011-12-09

什么意思。。。

作者: qq120848369   发布时间: 2011-12-09

就是在控制台一个字一个字输出

作者: freeicy2007   发布时间: 2011-12-09

这个要看你的平台了,windows下可用SetTimer
看下以下程序运行效果
C/C++ code

#include   <windows.h> 
#include   <iostream> 
using   namespace   std; 

static   BOOL   bExitApp   =   FALSE; 
const     UINT   uiTimerID   =   10; 
static const char *p="hello Word!";

VOID   CALLBACK   FooTimerFun(   HWND,   UINT,   UINT,   DWORD   ) 
{ 
static int i=0; 
if (p[i]==0)
  bExitApp   =   TRUE; 
else
     cout << p[i++];
} 

int   main() 
{ 
MSG   msgFoo; 
SetTimer(   NULL   ,   uiTimerID   ,   500   ,  (TIMERPROC) FooTimerFun   ); 
while(   !bExitApp   &&   GetMessage(   &msgFoo   ,   NULL   ,   0   ,   0   )   ) 
{ 
TranslateMessage(   &msgFoo   ); 
DispatchMessage(   &msgFoo   ); 
} 
KillTimer(   NULL   ,   uiTimerID   ); 
return   0; 
}

作者: keiy   发布时间: 2011-12-09

这么复杂啊,还没学到后面的线程和消息,有简单点的方法吗

作者: freeicy2007   发布时间: 2011-12-09

试试这个?
C/C++ code

#include   <stdio.h> 
#include   <stdlib.h>
#include <time.h>
void delay(int tseconds)   //1/10秒
  
{  
   clock_t start = clock();  
  
   clock_t lay = (clock_t)tseconds * CLOCKS_PER_SEC/10;  
  
   while ((clock()-start) < lay)  
  
     ;  
 }    
  
int main()
{
     static const char *p="hello word!";
     while(*p)
     {
            putchar(*p++);
            delay(5);
     }
}

作者: keiy   发布时间: 2011-12-09

把需求说清楚

是要更精确控制时间?还是嫌Sleep会挂起线程使得cpu不够忙碌?

否则的话Sleep就是最好而且最简单的方法

作者: yisikaipu   发布时间: 2011-12-09