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

C语言如何实现用户输入商品名称价格并纳入预算计算

个人消费预算程序第4项功能实现方案

实现核心要点

  • C语言无内置string类型,使用字符数组存储额外商品名称即可
  • 读取整数后输入缓冲区会残留换行符,需先清理缓冲区再读取字符串,避免读取到空内容
  • 新增额外消费累计变量,录入的额外商品价格全部累加后,合并到总支出计算逻辑中
  • 支持多件额外商品批量录入,用循环实现即可

修改后完整可运行代码

#include <stdio.h>

int main(){
    int shirt,pant,jacket,snikers,jogers,tshirt;
    int budget,save,Total_spend,over_cost;
    // 新增额外商品相关变量
    int extra_count, extra_price, extra_total = 0;
    char extra_item[100];
    char has_extra;

    printf("Please at first Tell me your Total Budget:");
    scanf("%d",&budget);
    //shirt section
    shirt=500;
    printf("\nThe price of the shirt's price was predicted to : %d$",shirt);
    printf("\nPlease tell how much you spend on The shirt:");
    scanf("%d",&shirt);

    if (shirt==500)
    {
        printf("Hurray!! you spend %dTK which is absolutely same as you predict on shirt",shirt);
    }
    else if (shirt>500)
    {
        printf("ouh o!! shirt price is %d.\nthis is more than the budget. You spend %d$ more TK on shirt ",shirt,shirt-500);
    }
    else if (shirt<500)
    {
        printf("Mamma Osthir!! shirt price is %d.\nthis below budget. You save %d$ TK on shirt",shirt,500-shirt);
    }

    //pant section
    pant=700;
    printf("\n\nThe price of the pant's price was predicted to : %d$",pant);
    printf("\nPlease tell how much you spend on The pant:");
    scanf("%d",&pant);

    if (pant==700)
    {
        printf("Hurray!! you spend %dTK which is absolutely same as you predict on pant",pant);
    }
    else if (pant>700)
    {
        printf("ouh o!! pant price is %d.\nmore than the budget.You spend %d$ more TK on pant",pant,pant-700);
    }
    else if (pant<700)
    {
        printf("Mamma Osthir!! your pant price is %d.\nthis pant price is below budget. You save %d$ TK on pant ",pant,700-pant);
    }

    //Extra bought Section 实现部分
    // 先清理缓冲区残留的换行符
    while(getchar() != '\n');
    printf("\n\nDo you have any extra purchased items? (y/n): ");
    scanf("%c", &has_extra);
    if(has_extra == 'y' || has_extra == 'Y'){
        printf("Please enter the number of extra items: ");
        scanf("%d", &extra_count);
        // 再次清理缓冲区
        while(getchar() != '\n');
        for(int i=0;i<extra_count;i++){
            printf("\nEnter the name of extra item %d: ", i+1);
            scanf("%[^\n]", extra_item);
            printf("Enter the price of %s: ", extra_item);
            scanf("%d", &extra_price);
            extra_total += extra_price;
            // 清理当前输入行残留
            while(getchar() != '\n');
            printf("You bought %s, cost %dTK, extra total now is %dTK\n", extra_item, extra_price, extra_total);
        }
    }

    //Total i spend
    printf("\n\n\nTotal Budget was= %dTK",budget);
    // 总支出加上额外消费金额
    Total_spend=shirt+pant+extra_total;
    printf("\nThe total Amount you spend that is= %dTK", Total_spend);
    if (budget>Total_spend)
    {
        printf("\nyour total savings is =%dTK",budget-Total_spend);
    }
    else if(budget<Total_spend)
    {
        printf("\nYou Didn't make any savings, you Totally over_cost =%dTK",Total_spend-budget);
    }
    else{
        printf("\nYour spending is exactly equal to the budget, no savings and no overspending");
    }

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 15:18:06