#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct user_table{
int id;
char name[16];
float amount;
};
struct wallet_table{
int id;
int type;
int user_id;
time_t date;
float amount;
};
struct hb_table{
int id;
int max_count;
int count;
float amount;
};
struct user_table user_list[10];
struct wallet_table wallet_list[1000];
struct hb_table hb_list[1000];
int add_user(char name[], int len){
static int count = 0;
int i;
if(count > 9){
fprintf(stderr, "user is full\n");
return -1;
}
user_list[count].id = count;
user_list[count].amount = 0.0;
for(i=0;i<len;i++)
user_list[count].name[i] = name[i];
user_list[count].name[i] = '\0';
count++;
return count - 1;
}
int add_wallet(int type, int user_id, float amount){
static int count = 0;
if(count > 999){
fprintf(stderr, "wallet is full\n");
return -1;
}
wallet_list[count].id = count;
wallet_list[count].type = type;
wallet_list[count].user_id = user_id;
wallet_list[count].date = time(NULL);
wallet_list[count].amount = amount;
count++;
return count - 1;
}
int add_hb(int number, float amount){
static int count = 0;
if(count > 999){
fprintf(stderr, "hb is full\n");
return -1;
}
hb_list[count].id = count;
hb_list[count].max_count = number;
hb_list[count].count = number;
hb_list[count].amount = amount;
count++;
return count - 1;
}
void deposit(char name[], int len, float amount){
int i, j, status;
for(i=0;i<9;i++){
status = 0;
for(j=0;j<len;j++){
if(user_list[i].name[j] != name[j]){
status = 1;
break;
}
}
if(status == 0){
user_list[i].amount += amount;
printf("%s deposit %f Yuan\n", name, amount);
return;
}
}
fprintf(stderr, "no user name:%s\n", name);
}
void get_hb(char name[], int len, int hb_id){
int i, j, status, a;
float b;
for(i=0;i<9;i++){
status = 0;
for(j=0;j<len;j++){
if(user_list[i].name[j] != name[j]){
status = 1;
break;
}
}
if(status == 0)
goto a1;
}
fprintf(stderr, "no user name:%s\n", name);
return;
a1:
for(j=0;j<999;j++){
if(hb_list[j].id == hb_id)
goto a2;
}
fprintf(stderr, "no hb_id:%d\n", hb_id);
return;
a2:
a = rand() % 100;
b = (float)a / 100;
if(hb_list[j].count <= 0){
fprintf(stderr, "The hb is null\n");
return;
}
else if(hb_list[j].count == 1){
b = hb_list[j].amount;
}
else{
b *= hb_list[j].amount;
}
hb_list[j].amount -= b;
user_list[i].amount += b;
printf("%s get %f Yuan hb\n", name, b);
}
int put_hb(char name[], int len, float amount, int number){
int i, j, status, a;
int hb_id;
float b;
for(i=0;i<9;i++){
status = 0;
for(j=0;j<len;j++){
if(user_list[i].name[j] != name[j]){
status = 1;
break;
}
}
if(status == 0)
goto a1;
}
fprintf(stderr, "no user name:%s\n", name);
return -1;
a1:
if(user_list[i].amount >= amount){
add_wallet(2, user_list[i].id, amount);
hb_id = add_hb(number, amount);
user_list[i].amount -= amount;
printf("%s put %f Yuan hb.\n", name, amount);
return hb_id;
}
fprintf(stderr, "%s amount is too few\n", name);
}
int main(){
int hb_id;
add_user("Xiao Ming", 9);
add_user("Xiao Hong", 9);
add_user("Xiao Liang", 10);
add_user("Xiao Guang", 10);
deposit("Xiao Ming", 9, 100.0);
add_wallet(0, 0, 100.0);
hb_id = put_hb("Xiao Ming", 9, 5.0, 3);
srand(time(NULL));
get_hb("Xiao Hong", 9, hb_id);
get_hb("Xiao Liang", 10, hb_id);
get_hb("Xiao Guang", 10, hb_id);
return 0;
}