-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redenvelope.c
115 lines (92 loc) · 2.65 KB
/
Redenvelope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*************************************************************************
> File Name: Redenvelope.c
> Author:
> Mail:
> Created Time: 2017年09月22日 星期五 19时58分02秒
************************************************************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
#include<time.h>
#define MAX 200 //最大红包个数
double red[MAX]; //红包数组
pthread_mutex_t lock; //申请一个互斥锁
pthread_t threads[MAX]; //线程数组
struct RedEnvelo //定义红包 包括钱和数量
{
double money;
int num;
}re;
struct Person //定义抢红包人信息
{
int id;
double m; //取到的钱
int flag; //是否领取过红包
}per[MAX];
void CreatRedenve(double m,int n) //创建红包
{
pthread_mutex_lock(&lock);
re.money = m;
re.num = n;
pthread_mutex_unlock(&lock);
printf("红包已放入!\n");
}
void* DistrubuteRedenve(void *j) //分配红包
{
srand((int)time(0)); //随机数种子
double m =((struct RedEnvelo*)j)->money;
int n =((struct RedEnvelo*)j)->num;
double restmoney = m;
int restnum = n;
double redmax = m / n * 2;
for(int i=0; i < n-1; i++)
{
red[i] = (rand() % (int)(redmax * 100) + 1) / 100.0;
restmoney -= red[i];
redmax = restmoney / (n - i - 1) * 2;
}
red[n-1] = restmoney;
return (void*)2;
}
void* Getrede(void* j)
{
int i = *(int*)j;
pthread_mutex_lock(&lock);
if(per[i].flag == 0)
{
printf("第%d个人拿走的金额是:%.2lf\n",i+1,red[i]);
re.money = re.money - red[i];
re.num = re.num - 1;
printf("剩余金额:%.2lf 剩余红包个数:%d\n",re.money,re.num);
per[i].id = i;
per[i].m = red[i];
per[i].flag =1;
}
pthread_mutex_unlock(&lock);
return (void*)1;
}
int main()
{
struct RedEnvelo* p;
p = &re;
pthread_mutex_init(&lock,0);
pthread_t t;
double money = 0; //红包放入的钱
int num = 0; //要发的红包个数
printf("请输入红包金额:\n");
scanf("%lf",&money);
printf("请输入红包个数:\n");
scanf("%d",&num);
CreatRedenve(money,num); //创建红包
memset(per,0,sizeof(struct Person)*(num)); //初始化抢红包信息
pthread_create(&t,NULL,DistrubuteRedenve,p);
pthread_join(t,NULL);
for(int i=0; i < num; i++)
{
pthread_create(&threads[i],NULL,Getrede,(void*)&i);
pthread_join(threads[i],NULL);
}
pthread_mutex_destroy(&lock);
return 0;
}