吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1040|回复: 12
上一主题 下一主题
收起左侧

[C&C++ 转载] 账单助手--一个很奇怪的账单助手

[复制链接]
跳转到指定楼层
楼主
再贱就再见 发表于 2026-10-25 12:44 回帖奖励
最近邮箱安全吗用多了,ai了个一个很奇怪的邮箱记账助手{:1_907:}

就是你自己给你自己邮箱发主题为账单的主题,内容比如苹果5斤10元,这个开在电脑上就会自动记账

没有单位也行,比如补胎30

邮箱内容要换行

苹果5斤10元
补胎30
吃饭10
晚餐1

要这种格式

{:1_896:}



https://wwbmj.lanzouu.com/iDVLx4dagsre
密码:2z1g


[C] 纯文本查看 复制代码

static int name_is_sane(const char *n) {
    size_t len = strlen(n);
    const unsigned char *p;
    if (len == 0 || len > 96) return 0;
    for (p = (const unsigned char *)n; *p; p++) {
        unsigned char c = *p;
        if (c == '<' || c == '>' || c == '=' || c == '*' ||
            c == '{' || c == '}' || c == '\\' || c == '|' ||
            c == '"' || c == '`')
            return 0;
    }
    if (strstr(n, "http://") || strstr(n, "https://")) return 0;
    return 1;
}

/*
 * 解析一行账单。require_currency=1 时必须有“元/块/¥/&#165;/$”,
 * 用于普通邮件,避免广告里的数字被误记。
 *
 * 支持:
 *   香蕉5斤20元        -> 数量 5,单位 斤,金额 20
 *   砌砖5平方米2400元  -> 数量 5,单位 平方米,金额 2400
 *   砌砖2400元         -> 数量 1,单位 项,金额 2400
 */
static int parse_bill_line_ex(const char *line, BillItem *out, int require_currency) {
    const char *p = line;
    char name[MAX_NAME];
    char unit[MAX_UNIT];
    size_t ni = 0, ui = 0;
    double qty = 0, amount = 0;
    char *endp;
    int skip, had_currency = 0, single_amount = 0;

    memset(out, 0, sizeof *out);
    memset(name, 0, sizeof name);
    memset(unit, 0, sizeof unit);

    while (*p && isspace((unsigned char)*p)) p++;
    if (!*p) return 0;

    /* 名称:一直读到第一个数字 */
    while (*p && !isdigit((unsigned char)*p) && *p != '.' && ni + 1 < sizeof name) {
        if (isspace((unsigned char)*p) && ni > 0) {
            const char *q = p;
            while (*q && isspace((unsigned char)*q)) q++;
            if (isdigit((unsigned char)*q) || *q == '.') break;
            name[ni++] = ' ';
            p = q;
            continue;
        }
        if (isspace((unsigned char)*p)) { p++; continue; }
        name[ni++] = *p++;
    }
    name[ni] = '\0';
    trim(name);
    if (!name[0]) return 0;

    while (*p && isspace((unsigned char)*p)) p++;
    if (!isdigit((unsigned char)*p) && *p != '.') return 0;

    /* 第一个数字先按数量解析 */
    qty = strtod(p, &endp);
    if (endp == p) return 0;
    p = endp;
    while (*p && isspace((unsigned char)*p)) p++;

    skip = is_money_suffix((const unsigned char *)p);
    if (skip) {
        /* 名称+金额,没有数量单位:砌砖2400元 -> 1 项 */
        amount = qty;
        qty = 1;
        single_amount = 1;
        had_currency = 1;
        p += skip;
    } else if (*p == '\0' && !require_currency) {
        /* 本地输入允许省略“元”:砌砖2400 */
        amount = qty;
        qty = 1;
        single_amount = 1;
    } else {
        /* 名称+数量+单位+金额:香蕉5斤20元 */
        while (*p && !isdigit((unsigned char)*p) && *p != '.' && ui + 1 < sizeof unit) {
            skip = is_money_suffix((const unsigned char *)p);
            if (skip) break;
            if (isspace((unsigned char)*p)) { p++; continue; }
            unit[ui++] = *p++;
        }
        unit[ui] = '\0';
        trim(unit);

        while (*p && isspace((unsigned char)*p)) p++;
        if (!isdigit((unsigned char)*p) && *p != '.') return 0;
        amount = strtod(p, &endp);
        if (endp == p) return 0;
        p = endp;
        while (*p && isspace((unsigned char)*p)) p++;
        skip = is_money_suffix((const unsigned char *)p);
        if (skip) {
            had_currency = 1;
            p += skip;
        }
    }

    if (qty <= 0 || amount < 0 || !name[0]) return 0;
    if (qty > 1000000.0 || amount > 100000000.0) return 0;
    if (require_currency && !had_currency) return 0;
    if (!name_is_sane(name)) return 0;

    /* 拒绝 MIME 分隔线、HTML 残留 */
    if (name[0] == '-' || name[0] == '=' || name[0] == '<' ||
        name[0] == '_' || name[0] == '.') return 0;
    {
        int has_text = 0;
        const unsigned char *q;
        for (q = (const unsigned char *)name; *q; q++) {
            if (isalpha(*q) || *q >= 0x80) { has_text = 1; break; }
        }
        if (!has_text) return 0;
    }

    if (!unit[0]) snprintf(unit, sizeof unit, single_amount ? "项" : "个");
    if (!unit_is_sane(unit)) return 0;

    snprintf(out->name, sizeof out->name, "%s", name);
    out->qty = qty;
    snprintf(out->unit, sizeof out->unit, "%s", unit);
    out->amount = amount;
    return 1;
}

/* 本地输入:允许省略“元” */
static int parse_bill_line(const char *line, BillItem *out) {
    return parse_bill_line_ex(line, out, 0);
}

/* ===================== 3. bills.csv 读取 / 排序 / 写入 ===================== */

/* 解析 CSV 一行:名称可能带引号和逗号 */
static int parse_bill_row(const char *line, BillRow *r) {
    const char *p = line;
    char *endp;
    size_t ni = 0, ui = 0;

    memset(r, 0, sizeof *r);
    while (*p == ' ' || *p == '\t') p++;

    if (*p == '"') {
        p++;
        while (*p && ni + 1 < sizeof r->name) {
            if (*p == '"') {
                if (p[1] == '"') { r->name[ni++] = '"'; p += 2; continue; }
                p++;
                break;
            }
            r->name[ni++] = *p++;
        }
        r->name[ni] = '\0';
        if (*p == ',') p++;
    } else {
        while (*p && *p != ',' && ni + 1 < sizeof r->name)
            r->name[ni++] = *p++;
        r->name[ni] = '\0';
        if (*p == ',') p++;
    }
    trim(r->name);

    r->qty = strtod(p, &endp);
    if (endp == p) return 0;
    p = endp;
    if (*p == ',') p++;

    while (*p && *p != ',' && ui + 1 < sizeof r->unit)
        r->unit[ui++] = *p++;
    r->unit[ui] = '\0';
    trim(r->unit);
    if (*p == ',') p++;

    r->amount = strtod(p, &endp);
    if (endp == p) return 0;
    p = endp;
    if (*p == ',') p++;

    snprintf(r->time, sizeof r->time, "%s", p);
    trim(r->time);
    return 1;
}

/* 时间倒序;同一秒保持文件顺序,避免删除重写后翻转 */
static int bill_cmp_newest(const void *a, const void *b) {
    const BillRow *x = (const BillRow *)a;
    const BillRow *y = (const BillRow *)b;
    int c = strcmp(y->time, x->time);
    if (c != 0) return c;
    if (x->seq < y->seq) return -1;
    if (x->seq > y->seq) return 1;
    return 0;
}

static void free_bill_rows(void) {
    int i;
    for (i = 0; i < g_nbills; i++) free(g_bills[i].raw);
    free(g_bills);
    g_bills = NULL;
    g_nbills = 0;
}

static void load_bill_rows(void) {
    FILE *fp;
    char line[4096];
    int first = 1;

    free_bill_rows();
    fp = fopen(g_csvPath, "r");
    if (!fp) return;

    while (fgets(line, sizeof line, fp)) {
        size_t n = strlen(line);
        BillRow *nr;

        while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r'))
            line[--n] = '\0';

        if (first) {
            first = 0;
            if ((unsigned char)line[0] == 0xEF &&
                (unsigned char)line[1] == 0xBB &&
                (unsigned char)line[2] == 0xBF) {
                memmove(line, line + 3, strlen(line + 3) + 1);
            }
            if (strncmp(line, "名称,", 7) == 0) continue; /* 跳过表头 */
        }
        if (!line[0]) continue;

        nr = (BillRow *)realloc(g_bills, ((size_t)g_nbills + 1) * sizeof *nr);
        if (!nr) break;
        g_bills = nr;
        memset(&g_bills[g_nbills], 0, sizeof g_bills[g_nbills]);
        if (!parse_bill_row(line, &g_bills[g_nbills])) {
            snprintf(g_bills[g_nbills].name, sizeof g_bills[g_nbills].name,
                     "(无法解析)");
            g_bills[g_nbills].qty = 0;
            g_bills[g_nbills].amount = 0;
            g_bills[g_nbills].unit[0] = '\0';
            g_bills[g_nbills].time[0] = '\0';
        }
        g_bills[g_nbills].raw = strdup(line);
        g_bills[g_nbills].seq = g_nbills;
        g_nbills++;
    }
    fclose(fp);
    if (g_nbills > 1)
        qsort(g_bills, (size_t)g_nbills, sizeof g_bills[0], bill_cmp_newest);
}

static int ensure_csv_header(void) {
    FILE *fp = fopen(g_csvPath, "r");
    if (fp) { fclose(fp); return 0; }
    fp = fopen(g_csvPath, "w");
    if (!fp) return -1;
    fputs("\xEF\xBB\xBF", fp);
    fprintf(fp, "名称,数量,单位,金额,时间\n");
    fclose(fp);
    return 0;
}

static int append_csv(const BillItem *it) {
    char tbuf[64];
    FILE *fp;
    if (ensure_csv_header() != 0) return -1;
    fp = fopen(g_csvPath, "a");
    if (!fp) return -1;
    now_str(tbuf, sizeof tbuf);
    fprintf(fp, "\"%s\",%.4g,%s,%.2f,%s\n",
            it->name, it->qty, it->unit, it->amount, tbuf);
    fclose(fp);
    return 0;
}

/* ===================== 4. 搜索 ===================== */

/* ASCII 不区分大小写;中文按 UTF-8 字节直接匹配 */
static int ci_contains(const char *hay, const char *needle) {
    size_t n = strlen(needle);
    if (n == 0) return 1;
    for (; *hay; hay++) {
        size_t i;
        for (i = 0; i < n; i++) {
            int a = (unsigned char)hay[i];
            int b = (unsigned char)needle[i];
            if (a == 0) break;
            if (a >= 'A' && a <= 'Z') a += 32;
            if (b >= 'A' && b <= 'Z') b += 32;
            if (a != b) break;
        }
        if (i == n) return 1;
    }
    return 0;
}

static int bill_matches_search(const BillRow *r) {
    char num[64];
    if (!g_search_text[0]) return 1;
    if (ci_contains(r->name, g_search_text)) return 1;
    if (ci_contains(r->unit, g_search_text)) return 1;
    if (ci_contains(r->time, g_search_text)) return 1;
    snprintf(num, sizeof num, "%.4g", r->qty);
    if (ci_contains(num, g_search_text)) return 1;
    snprintf(num, sizeof num, "%.2f", r->amount);
    if (ci_contains(num, g_search_text)) return 1;
    return 0;
}

/* ===================== 5. 表格刷新 ===================== */

static void refresh_bill_table(void) {
    int i, shown = 0;

    if (!g_hList) return;
    ListView_DeleteAllItems(g_hList);
    load_bill_rows();

    for (i = 0; i < g_nbills; i++) {
        wchar_t wname[MAX_NAME], wqty[32], wunit[MAX_UNIT], wamt[32], wtime[64];
        LVITEMW item;

        if (!bill_matches_search(&g_bills[i])) continue;

        u8_to_wbuf(g_bills[i].name, wname, sizeof wname / sizeof wname[0]);
        swprintf(wqty, sizeof wqty / sizeof wqty[0], L"%.4g", g_bills[i].qty);
        u8_to_wbuf(g_bills[i].unit, wunit, sizeof wunit / sizeof wunit[0]);
        swprintf(wamt, sizeof wamt / sizeof wamt[0], L"%.2f", g_bills[i].amount);
        u8_to_wbuf(g_bills[i].time, wtime, sizeof wtime / sizeof wtime[0]);

        memset(&item, 0, sizeof item);
        item.mask = LVIF_TEXT | LVIF_PARAM;
        item.iItem = shown;
        item.lParam = (LPARAM)i;   /* 记住在 g_bills 中的原始下标,删除时要用 */
        item.pszText = wname;
        ListView_InsertItem(g_hList, &item);
        ListView_SetItemText(g_hList, shown, 1, wqty);
        ListView_SetItemText(g_hList, shown, 2, wunit);
        ListView_SetItemText(g_hList, shown, 3, wamt);
        ListView_SetItemText(g_hList, shown, 4, wtime);
        shown++;
    }
}

/* ===================== 6. 删除选中行 ===================== */

/* 按删除标记重写 bills.csv,未删除的行原样保留 */
static int rewrite_bills_without(const char *del) {
    char tmp[MAX_PATH + 8];
    FILE *fp;
    int i;

    snprintf(tmp, sizeof tmp, "%s.tmp", g_csvPath);
    fp = fopen(tmp, "w");
    if (!fp) return -1;
    fputs("\xEF\xBB\xBF", fp);
    fprintf(fp, "名称,数量,单位,金额,时间\n");
    for (i = 0; i < g_nbills; i++) {
        if (!del[i] && g_bills[i].raw)
            fprintf(fp, "%s\n", g_bills[i].raw);
    }
    fclose(fp);
    if (!MoveFileExA(tmp, g_csvPath, MOVEFILE_REPLACE_EXISTING)) return -1;
    return 0;
}

static void on_delete_selected(void) {
    int sel = (int)ListView_GetSelectedCount(g_hList);
    char *del;
    int idx, removed = 0;

    if (sel <= 0) {
        set_status("请先在表格中选中要删除的行");
        return;
    }
    if (MessageBoxW(g_hwndMain, L"确定删除选中的账单记录?", L"删除账单",
                    MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2) != IDYES)
        return;

    del = (char *)calloc((size_t)g_nbills, 1);
    if (!del) return;

    /* ListView 可能在搜索状态下只显示部分行,lParam 才是真实下标 */
    idx = -1;
    while ((idx = (int)ListView_GetNextItem(g_hList, idx, LVNI_SELECTED)) != -1) {
        LVITEMW it;
        memset(&it, 0, sizeof it);
        it.mask = LVIF_PARAM;
        it.iItem = idx;
        if (ListView_GetItem(g_hList, &it)) {
            int ri = (int)it.lParam;
            if (ri >= 0 && ri < g_nbills && !del[ri]) {
                del[ri] = 1;
                removed++;
            }
        }
    }

    if (removed == 0 || rewrite_bills_without(del) != 0) {
        free(del);
        set_status("删除失败:无法写入 bills.csv");
        return;
    }
    free(del);
    refresh_bill_table();
    {
        char s[160];
        snprintf(s, sizeof s, "已删除 %d 条,剩余 %d 条", removed, g_nbills);
        set_status(s);
    }
}

/* ===================== 7. 汇总统计 ===================== */

typedef struct {
    char name[MAX_NAME];
    int count;
    double sum;
} SummaryItem;

/* 按名称汇总笔数和金额;搜索状态下只统计匹配项 */
static void summary_collect(SummaryItem *items, int *nitems,
                            int *count, double *total) {
    int i, j;
    *nitems = 0;
    *count = 0;
    *total = 0;
    for (i = 0; i < g_nbills; i++) {
        if (g_search_text[0] && !bill_matches_search(&g_bills[i])) continue;
        for (j = 0; j < *nitems; j++) {
            if (strcmp(items[j].name, g_bills[i].name) == 0) break;
        }
        if (j == *nitems) {
            if (*nitems >= 256) continue;
            snprintf(items[j].name, sizeof items[j].name, "%s", g_bills[i].name);
            items[j].count = 0;
            items[j].sum = 0;
            (*nitems)++;
        }
        items[j].count++;
        items[j].sum += g_bills[i].amount;
        (*count)++;
        *total += g_bills[i].amount;
    }
}

/* 填充汇总表格:名称 / 笔数 / 金额 */
static void summary_fill(void) {
    SummaryItem items[256];
    wchar_t wname[MAX_NAME], wcount[32], wamt[32], wtotal[192];
    int n = 0, count = 0, i;
    double total = 0;

    if (!g_summaryList) return;
    ListView_DeleteAllItems(g_summaryList);
    summary_collect(items, &n, &count, &total);

    for (i = 0; i < n; i++) {
        LVITEMW it;
        u8_to_wbuf(items[i].name, wname, sizeof wname / sizeof wname[0]);
        swprintf(wcount, sizeof wcount / sizeof wcount[0], L"%d", items[i].count);
        swprintf(wamt, sizeof wamt / sizeof wamt[0], L"%.2f", items[i].sum);
        memset(&it, 0, sizeof it);
        it.mask = LVIF_TEXT;
        it.iItem = i;
        it.pszText = wname;
        ListView_InsertItem(g_summaryList, &it);
        ListView_SetItemText(g_summaryList, i, 1, wcount);
        ListView_SetItemText(g_summaryList, i, 2, wamt);
    }

    if (g_search_text[0])
        swprintf(wtotal, sizeof wtotal / sizeof wtotal[0],
                 L"搜索结果:共 %d 笔,合计 %.2f 元", count, total);
    else
        swprintf(wtotal, sizeof wtotal / sizeof wtotal[0],
                 L"共 %d 笔,合计 %.2f 元", count, total);
    if (g_summaryTotal) SetWindowTextW(g_summaryTotal, wtotal);
}

/* ===================== 8. 关键 UI 处理 ===================== */

static void on_local_add(void) {
    char line[MAX_LINE];
    BillItem item;

    get_edit_utf8(g_hLocal, line, sizeof line);
    trim(line);
    if (!line[0]) {
        set_status("请输入账单,例如:香蕉5斤20元 或 砌砖2400元");
        return;
    }
    if (!parse_bill_line(line, &item)) {
        set_status("无法解析,请用「香蕉5斤20元」或「砌砖2400元」格式");
        return;
    }
    if (append_csv(&item) != 0) {
        set_status("写入 bills.csv 失败");
        return;
    }

    SetWindowTextW(g_hLocal, L"");
    if (g_hSearch && g_search_text[0])
        SetWindowTextW(g_hSearch, L"");
    refresh_bill_table(); /* 重新按时间倒序,新记录会在最上面 */
    {
        char msg[256];
        snprintf(msg, sizeof msg,
                 "已记账: %s %.4g%s %.2f元(共 %d 条)",
                 item.name, item.qty, item.unit, item.amount, g_nbills);
        set_status(msg);
    }
}

static void on_search_change(void) {
    get_edit_utf8(g_hSearch, g_search_text, sizeof g_search_text);
    trim(g_search_text);
    refresh_bill_table();
    {
        char s[192];
        if (g_search_text[0])
            snprintf(s, sizeof s, "搜索到 %d 条", ListView_GetItemCount(g_hList));
        else
            snprintf(s, sizeof s, "共 %d 条", g_nbills);
        set_status(s);
    }
}

/* 主窗口消息处理中与核心相关的部分 */
static void core_commands(HWND hwnd, WPARAM wParam) {
    switch (LOWORD(wParam)) {
    case IDOK: /* 本地账单输入框里按回车 */
        if (GetFocus() == g_hLocal) on_local_add();
        break;
    case ID_BTN_ADD:
        on_local_add();
        break;
    case ID_BTN_DEL:
        on_delete_selected();
        break;
    case ID_BTN_SUM:
        refresh_bill_table();
        show_summary_dialog(hwnd);
        break;
    case ID_SEARCH:
        if (HIWORD(wParam) == EN_CHANGE) on_search_change();
        break;
    case ID_BTN_CLEAR:
        SetWindowTextW(g_hSearch, L"");
        break;
    }
}

免费评分

参与人数 2吾爱币 +8 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Kayay + 1 我很赞同!

查看全部评分

发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
wufashihu 发表于 2026-10-25 16:58
yi0819 发表于 2026-10-25 16:32
为什么要往邮箱发账单这么麻烦,手机上装个记账软件不是更方便吗?随时随地都能记。

你说的那种是个人记账,记录当天自己开销多少。他这个我觉得应该是记录一些公共账目,而且每一分钟收发邮件以后就可以自动记录,根本不用人工介入,挺好的这个软件。
沙发
yi0819 发表于 2026-10-25 16:32
为什么要往邮箱发账单这么麻烦,手机上装个记账软件不是更方便吗?随时随地都能记。
4#
yaochangfu 发表于 2026-10-25 17:24
5#
syl7388 发表于 2026-10-25 19:26
授权码是邮箱密码吗
6#
gaoxiaoao 发表于 2026-10-25 22:04
有这种使用场景吗
7#
 楼主| 再贱就再见 发表于 2026-10-25 22:51 |楼主
syl7388 发表于 2026-10-25 19:26
授权码是邮箱密码吗

邮箱设置里有授权码
8#
 楼主| 再贱就再见 发表于 2026-10-25 22:52 |楼主
gaoxiaoao 发表于 2026-10-25 22:04
有这种使用场景吗

应该有吧哈哈,我自己就在用啊
9#
Zhangys2026 发表于 2026-10-26 09:42
D:\360MoveData\Users\G1313959\Desktop 乱码了

5946a79a-1cf5-40f2-8f7a-2440d02473f0.png (14.15 KB, 下载次数: 1)

5946a79a-1cf5-40f2-8f7a-2440d02473f0.png
10#
laoniu2026 发表于 2026-10-26 09:43
一般人用不到这个.exe,放源码给大家下载研究参考差不多
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-11-8 14:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表