C++时间间隔问题

sinat_35512698 2016-07-24 10:22:54
如何实现每1s输出一个a, 每2s输出一个b,每3s输出一个c。本人小白,求大神提供思路,感谢。如果用sleep函数,不知道怎样是三个函数同时进行
...全文
426 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
penghuahuijuan 2016-07-27
  • 打赏
  • 举报
回复
获取开始时间,获取当前时间,时间间隔除以3取余,分情况处理就可以了
赵4老师 2016-07-26
  • 打赏
  • 举报
回复
仅供参考:
// processor: x86
#include <windows.h>
#include <process.h>    /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>

void Bounce( void *ch );
void CheckKey( void *dummy );

/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

BOOL repeat = TRUE;     /* Global repeat flag and video variable */
HANDLE hStdOut;         /* Handle for console window */
CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */

int main()
{
    CHAR    ch = 'A';

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

    /* Get display screen's text row and column information. */
   GetConsoleScreenBufferInfo( hStdOut, &csbi );

    /* Launch CheckKey thread to check for terminating keystroke. */
    _beginthread( CheckKey, 0, NULL );

    /* Loop until CheckKey terminates program. */
    while( repeat )
    {
        /* On first loops, launch character threads. */
        _beginthread( Bounce, 0, (void *) (ch++)  );

        /* Wait one second between loops. */
        Sleep( 1000L );
    }
    return 0;
}

/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
void CheckKey( void *dummy )
{
    _getch();
    repeat = 0;    /* _endthread implied */

}

/* Bounce - Thread to create and and control a colored letter that moves
 * around on the screen.
 *
 * Params: ch - the letter to be moved
 */
void Bounce( void *ch )
{
    /* Generate letter and color attribute from thread argument. */
    char    blankcell = 0x20;
    char    blockcell = (char) ch;
    BOOL    first = TRUE;
   COORD   oldcoord, newcoord;
   DWORD   result;


    /* Seed random number generator and get initial location. */
    srand( _threadid );
    newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
    newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
    while( repeat )
    {
        /* Pause between loops. */
        Sleep( 100L );

        /* Blank out our old position on the screen, and draw new letter. */
        if( first )
            first = FALSE;
        else
         WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
         WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );

        /* Increment the coordinate for next placement of the block. */
        oldcoord.X = newcoord.X;
        oldcoord.Y = newcoord.Y;
        newcoord.X += GetRandom( -1, 1 );
        newcoord.Y += GetRandom( -1, 1 );

        /* Correct placement (and beep) if about to go off the screen. */
        if( newcoord.X < 0 )
            newcoord.X = 1;
        else if( newcoord.X == csbi.dwSize.X )
            newcoord.X = csbi.dwSize.X - 2;
        else if( newcoord.Y < 0 )
            newcoord.Y = 1;
        else if( newcoord.Y == csbi.dwSize.Y )
            newcoord.Y = csbi.dwSize.Y - 2;

        /* If not at a screen border, continue, otherwise beep. */
        else
            continue;
        Beep( ((char) ch - 'A') * 1000, 500 );
    }
    /* _endthread given to terminate */
    _endthread();
}


paschen 2016-07-25
  • 打赏
  • 举报
回复
引用 2 楼 sinat_35512698 的回复:
啊,感谢,不过我只是举了个例子所以给1、2、3s,不过我在想如果是0.7,0.8,0.9这样不是倍数关系的该怎么处理呢?我想到的是用最大公因数比如0.1进行循环,思路和你的差不多,所以还是谢谢你了,我想得太复杂了,只要利用两个频率之间的数字关系就可以解了,非常感谢!不过还是希望有人能给出更加本质的解释,把时间间隔换成x\y\z这样,如果间隔大小未知,怎样进行判断?还是利用这个思路吗?

	int iCnt = 0;
	while(!_kbhit())
	{
		Sleep(100);

		printf("\n%d", iCnt);

		if(iCnt % 7 == 0) //0.7秒
			printf(" a ");

		if(iCnt % 8 == 0) //0.8秒
			printf(" b ");

		if(iCnt % 9 == 0) //0.9秒
			printf(" c ");   

		iCnt++;
	}
sinat_35512698 2016-07-25
  • 打赏
  • 举报
回复
啊,感谢,不过我只是举了个例子所以给1、2、3s,不过我在想如果是0.7,0.8,0.9这样不是倍数关系的该怎么处理呢?我想到的是用最大公因数比如0.1进行循环,思路和你的差不多,所以还是谢谢你了,我想得太复杂了,只要利用两个频率之间的数字关系就可以解了,非常感谢!不过还是希望有人能给出更加本质的解释,把时间间隔换成x\y\z这样,如果间隔大小未知,怎样进行判断?还是利用这个思路吗?
zgl7903 2016-07-24
  • 打赏
  • 举报
回复

  int iCnt = 0;
  while(!_kbhit())
  {
    Sleep(1000);

    printf("\n%d", iCnt);

    if(iCnt % 1 == 0)
      printf(" a ");

    if(iCnt % 2 == 0)
      printf(" b ");

    if(iCnt % 3 == 0)
      printf(" c ");   

    iCnt++;
  }

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧