#include<windows.h>
#include<process.h>
using namespace std
int flag = 0//控制是否停止击鼓
int i = 0//控制花在谁手里
int n//游戏人数
int stopgameflag = 0//是否结束游戏
{
while(!stopgameflag)
{
while(flag == 0)
{
i++
if (i == n)
{
i = 0
}
}
Sleep(10)
}
return 1
}
int main()
{
char str[20] = {0}
cout<<"请输入参加游戏人数:"
cin>>n
HANDLE hThread
unsigned threadID
hThread = (HANDLE) _beginthreadex(NULL, 0, Threadforflower, NULL, 0, &threadID)
if (hThread == NULL)
{
cout<<"传花线程创建失败!!"<<endl
return 0
}
cout<<"传花线程创建成功!!(按下字符s键并回车时,停止击鼓, 输入over并回车时结束游戏,输入其他则重新击鼓)"<<endl
while(1)
{
cout<<"开始击鼓..."<<endl
fflush(stdin)
cin>>str
if (!strcmp(str , "over") || !strcmp(str , "OVER"))
{
flag = 1
stopgameflag = 1
break
}
if (!strcmp(str, "s") || !strcmp(str , "S"))
{
flag = 1
cout<<"花在第"<<i<<"个人手中"<<endl
flag = 0
}
}
WaitForSingleObject(hThread, INFINITE )
cout<<"游戏结束"<<endl
return 0
}
1 #include <string.h>2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #define MAX_POS_NUM 100
6 #define MAX_STR_LEN 1024
7
8
9 //1. get all position of str_z in str_x
10 int get_sub_str_pos(const char * str_x, const char * str_z, int sub_str_pos[])
11 {
12 if (NULL == str_x || NULL == str_z)
13 {
14 printf("in error!\n")
15 return -1
16 }
17
18 const char * pos_ptr = NULL
19
20 pos_ptr = strstr(str_x,str_z)
21
22 int i=0
23 while(pos_ptr)
24 {
25 printf("substring positon:%d\n",pos_ptr-str_x+1)
26 sub_str_pos[i] = pos_ptr - str_x + 1
27 pos_ptr = strstr(pos_ptr+strlen(str_z),str_z)
28 i++
29 }
30
31 return 0
32 }
33
34 //2. get max length common string of str_x and str_y
35 char * get_max_com_str(const char * str_x, const char * str_y)
36 {
37 int x_len = strlen(str_x)
38 int y_len = strlen(str_y)
39
40 char * tmp_str = new char[y_len+1]
41
42 for(int i=y_leni>0i--) // i is substring length
43 {
44 if (i>x_len)
45 continue
46 for(int j=0j<=y_len-ij++) // j is substring start postion
47 {
48 snprintf(tmp_str,i+1,"%s",str_y)
49 if (strstr(str_x,tmp_str))
50 {
51 printf("%s\n",tmp_str)
52 printf("max common substring length:%d\n",i)
53 return tmp_str
54 }
55 }
56 }
57
58 return NULL
59 }
60
61 //3. replace all substring in question 1
62 char * replace_sub_str(const char * str_x, char * max_com_str, int sub_str_pos[], int sub_str_len)
63 {
64 char * replaced_str = new char[MAX_STR_LEN]
65
66 int sub_pos = sub_str_pos[0]
67 int l=0// l is sub_str_pos index
68 int i=0,j=0//i is str_x pos, j is replaced_str pos
69
70 while(*str_x)
71 {
72 if (i==sub_pos-1) // replace from this position
73 {
74 // printf ("i:%d,\n",i)
75 for (int k=0k<strlen(max_com_str)k++)
76 {
77 *(replaced_str + j) = * (max_com_str + k)
78 j++
79 }
80 i += sub_str_len
81 str_x += sub_str_len
82 l++
83 sub_pos = sub_str_pos[l]
84 continue
85 }
86 *(replaced_str+j) = *str_x++
87 i++
88 j++
89 }
90
91 * (replaced_str + j) = '\0'
92
93 return replaced_str
94 }
95
96 int main()
97 {
98 const char * str_x = "abcabcabc"
99 const char * str_y = "cabcd"
100 const char * str_z = "abc"
101
102 int sub_str_pos [MAX_POS_NUM] = {0}
103
104 char * max_com_str = NULL
105
106 char * replaced_str = NULL
107
108 get_sub_str_pos(str_x,str_z,sub_str_pos)
109 max_com_str = get_max_com_str(str_x,str_y)
110
111 printf("max common str: %s\n",max_com_str)
112
113 replaced_str = replace_sub_str(str_x, max_com_str, sub_str_pos, strlen(str_z))
114 printf("repalced str: %s\n",replaced_str)
115
116 return 0
117 }
void bubble_sort(int *x, int n){
int j, k, h, t
for (h=n-1,h=kh>0h--) /*循环到没有比较范围*/
{
for (j=0, k=0j<hj++) /*每次预置k=0,循环扫描后更新k*/
{
if (*(x+j) >*(x+j+1)) /*大的放在后面,小的放到前面*/
{
t = *(x+j)
*(x+j) = *(x+j+1)
*(x+j+1) = t/*完成交换*/
k = j/*保存最后下沉的位置。这样k后面的都是排序排好了的。*/
}
}
}
}