标题:C语言虚拟抢红包
只看楼主
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:3 
自己玩的话挺好弄的。

saber,别哭.
2019-01-17 20:14
漂流瓶子
Rank: 2
等 级:论坛游民
帖 子:22
专家分:77
注 册:2019-1-14
得分:0 
C语言模拟抢红包源码,楼下贴出
2019-01-19 22:53
漂流瓶子
Rank: 2
等 级:论坛游民
帖 子:22
专家分:77
注 册:2019-1-14
得分:0 
#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;
}
2019-01-19 22:53
do8do8do8
Rank: 10Rank: 10Rank: 10
来 自:沙滩
等 级:贵宾
威 望:17
帖 子:366
专家分:1845
注 册:2010-7-2
得分:0 
这代码,我真的一个字符一个字符看了。
有意思的地方,两个
一,顺着线走,能找到出口。
二,抢红包不让抢完,0.0-0.99之间,总会有0.0000....1存银行。

评个:
能根据思路做出来就不简单。
代码还可以优化,比如在匹对用户的代码可以写成一个函数,然后调用,这样就能缩减很多行。等等
(心思缜密)

学C语言从底层开始,学编程从问题开始,一日学会C!!!
2019-01-20 13:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492720-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053765 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved