多线程实现变色龙
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <Windows.h> 5 #include <process.h> 6 #include <time.h> 7 8 void timeset(void *p)//线程的main函数 9 { 10 int i = 0; 11 while (1) 12 { 13 i++; 14 char str[100] = { 0 }; 15 sprintf(str, "title 当前时间第%d秒", i); 16 system(str); 17 Sleep(1000); 18 } 19 20 21 } 22 23 void colorall(void *p) 24 { 25 time_t ts; 26 unsigned int num = time(&ts); 27 srand(num); 28 29 for (;;) 30 { 31 int num1 = rand() % 16; 32 Sleep(10); 33 int num2 = rand() % 16; 34 char str[50] = { 0 }; 35 sprintf(str, "color %x%x", num1, num2);//%x 转化成十六进制 36 system(str); 37 } 38 } 39 40 void main() 41 { 42 _beginthread(timeset, 0, NULL); 43 _beginthread(colorall, 0, NULL); 44 45 system("color 3f"); 46 system("pause"); 47 }