以下程序運行後看起來比較有意思,像壹個隨機選號程序,但不是完全按照問題所說的寫的 可供參考,要改很容易
//多線程隨機選號程序示例
#include <stdio.h>
#include <Windows.h>
#include <ctime>
#include <cstdlib>
#include <process.h>
bool g_run = true; //是否運行
void userInput(void*) //監視輸入的線程函數
{
while (true)
{
if (getchar()=='\n') //是否輸入回車
{
g_run = !g_run; //回車運行 回車暫停
}
Sleep(10); //延遲
}
}
int main()
{
srand(time(0)); //隨機數種子
_beginthread(userInput,0,NULL); //開線程
while (true)
{
if (g_run)
{
system("cls"); //清屏
int t = rand() % 1000+ 1;//1-1000的隨機數
printf("\n %d",t); //輸出
}
Sleep(50); //延遲50毫秒
}
return 0;
}