You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发从stdin读取数据的C语言货币计算器遇到问题求助

帮助开发C语言Alkansiya货币计算器

我来帮你搞定这个Alkansiya货币计算器!根据你描述的交互需求,我整理了完整的实现方案,包括基础版和更易维护的改进版,同时会解释关键细节避免踩坑。

基础版实现代码

这个版本完全贴合你要的交互流程,逻辑清晰,适合新手理解:

#include <stdio.h>

int main() {
    // 打印欢迎信息
    printf("欢迎来到Alkansiya计算器!\n");

    // 定义变量存储每种货币的数量
    int count_1000, count_500, count_200, count_100, count_50;
    int count_20, count_10, count_5, count_1;
    int count_50cent, count_25cent, count_10cent, count_5cent, count_1cent;

    // 逐个读取用户输入
    printf("有多少张1000比索?");
    scanf("%d", &count_1000);
    printf("有多少张500比索?");
    scanf("%d", &count_500);
    printf("有多少张200比索?");
    scanf("%d", &count_200);
    printf("有多少张100比索?");
    scanf("%d", &count_100);
    printf("有多少张50比索?");
    scanf("%d", &count_50);
    printf("有多少张20比索?");
    scanf("%d", &count_20);
    printf("有多少张10比索?");
    scanf("%d", &count_10);
    printf("有多少枚5比索?");
    scanf("%d", &count_5);
    printf("有多少枚1比索?");
    scanf("%d", &count_1);
    printf("有多少枚50分?");
    scanf("%d", &count_50cent);
    printf("有多少枚25分?");
    scanf("%d", &count_25cent);
    printf("有多少枚10分?");
    scanf("%d", &count_10cent);
    printf("有多少枚5分?");
    scanf("%d", &count_5cent);
    printf("有多少枚1分?");
    scanf("%d", &count_1cent);

    // 以1分为单位计算总金额(避免浮点精度误差)
    long long total_cents = 0;
    total_cents += count_1000 * 1000 * 100;
    total_cents += count_500 * 500 * 100;
    total_cents += count_200 * 200 * 100;
    total_cents += count_100 * 100 * 100;
    total_cents += count_50 * 50 * 100;
    total_cents += count_20 * 20 * 100;
    total_cents += count_10 * 10 * 100;
    total_cents += count_5 * 5 * 100;
    total_cents += count_1 * 1 * 100;
    total_cents += count_50cent * 50;
    total_cents += count_25cent * 25;
    total_cents += count_10cent * 10;
    total_cents += count_5cent * 5;
    total_cents += count_1cent * 1;

    // 转换为比索+分的格式输出
    long long total_pesos = total_cents / 100;
    int remaining_cents = total_cents % 100;

    printf("您的余额是... %lld比索 %d分\n", total_pesos, remaining_cents);

    return 0;
}

关键细节说明

  • 避免浮点精度问题:我没有直接用浮点数计算总金额,而是把所有面值转换为1分的整数单位,最后再拆分回比索和分。这样能彻底避免0.1、0.25这类小数在二进制浮点数中无法精确表示的问题。
  • 使用long long存储总金额:如果用户输入的货币数量较多,总金额可能会超过int类型的上限(通常是21亿左右),long long能支持更大的数值范围,防止溢出。
  • 交互流程完全匹配:代码中的提示语和你给出的示例完全一致,用户输入后会按要求输出余额。

更易维护的改进版

如果以后需要添加或修改货币面值,重复写scanf会很麻烦,用结构体数组+循环的方式可以让代码更简洁、易扩展:

#include <stdio.h>

// 定义结构体存储每个面值的提示信息和对应1分单位的价值
typedef struct {
    char* prompt;
    long long value_cents;
} CurrencyDenomination;

int main() {
    printf("欢迎来到Alkansiya计算器!\n");

    // 把所有面值的信息存入数组,后续修改只需调整这里
    CurrencyDenomination denominations[] = {
        {"有多少张1000比索?", 1000 * 100},
        {"有多少张500比索?", 500 * 100},
        {"有多少张200比索?", 200 * 100},
        {"有多少张100比索?", 100 * 100},
        {"有多少张50比索?", 50 * 100},
        {"有多少张20比索?", 20 * 100},
        {"有多少张10比索?", 10 * 100},
        {"有多少枚5比索?", 5 * 100},
        {"有多少枚1比索?", 1 * 100},
        {"有多少枚50分?", 50},
        {"有多少枚25分?", 25},
        {"有多少枚10分?", 10},
        {"有多少枚5分?", 5},
        {"有多少枚1分?", 1}
    };

    int num_denominations = sizeof(denominations) / sizeof(denominations[0]);
    long long total_cents = 0;

    // 循环处理所有面值的输入和计算
    for (int i = 0; i < num_denominations; i++) {
        int count;
        printf("%s", denominations[i].prompt);
        scanf("%d", &count);
        total_cents += (long long)count * denominations[i].value_cents;
    }

    // 输出结果
    long long total_pesos = total_cents / 100;
    int remaining_cents = total_cents % 100;
    printf("您的余额是... %lld比索 %d分\n", total_pesos, remaining_cents);

    return 0;
}

编译运行方法

把代码保存为alkansiya.c,然后用gcc编译:

gcc alkansiya.c -o alkansiya

运行程序:

./alkansiya

就能看到你期望的交互效果啦!

内容的提问来源于stack exchange,提问作者temy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:40:25