简易交互程序2

// 杂货店订购程序.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#define Length strlen("洋蓟                    ¥2.05/500g")
//
const char List[] = { "1、洋蓟                 ¥2.05/500g\n"
                      "2、甜菜                 ¥1.15/500g\n" };
const char Option[] = {
    "a)添加洋蓟\n"
    "b) 添加甜菜\n"
    "c) 结算\n"
    "d) 退出\n"
};
char shoppinglist[100];
//
void printline(int);
void printmanue();
void operation();
void additem(int);
void caculate();
//
int weight[2];
//
int main()
{
    printmanue();
    operation();
    return 0;
}

void printline(int n)
{
    for (int i = 0; i < n; i++)
    printf("*");
    printf("\n");
}

void printmanue()
{
    printline(Length);
    printf("\n");
    printf("%s", List);
    printline(Length);
}

void operation()
{
    int quit = 0;
    char ch;
    printf("%s",Option);
    while (quit != 1)
    {
        ch = getchar();
        scanf_s("%*[^\n]%*c");
        if (ch >= 'a' && ch <= 'd')
        {
            switch (ch)
            {
            case 'a':
                additem(0);
                break;
            case 'b':
                additem(1);
                break;
            case 'c' :
                caculate();
                break;
            case 'd':
                quit = 1;
                break;
            }
        }
        else
        {
            printf("please enter a opction among above\n");
            void printmanue();
            printf("%s\n", Option);
        }
    }

void additem(int a)
{
    int n;
    char item[2][5]{{"洋蓟"},{"甜菜"}};
    printf("how many %s you wish to take(negative to reduce)\n"
           "/'r /'to return\n",item[a]);
    while (1)
    {
        if (scanf_s("%d%*c",&n) != 0 )
        {
            weight[a] += n;
            printf("successfully added\n");
            printf("%s\n", Option);
            break;
        }
        else if (getchar() == 'r')
        {
            scanf_s("%*[^\n]%*c");
            printf("%s\n", Option);
            break;
        }
        else
        {
            scanf_s("%*[^\n]%*c");
            printf("Please enter a right value\n");
            printf("how many %c%c you wish to take(negative to reduce)\n"
                "/'r /'to return\n", item[n], item[n + 1]);
        }
    }
}

void caculate()
{
    float discount;
    float total = weight[0] * 2.05f + weight[1] * 1.15f;
    if (total >= 100)
    {
        discount = 0.05f;
    }
    else
        discount = 0;
    float pack;
    if ((weight[0] + weight[1]) > 0 && (weight[0] + weight[1]) <= 5)
        pack = 6.5;
    else if ((weight[0] + weight[1]) > 5 && (weight[0] + weight[1]) <= 20)
        pack = 14;
    else if ((weight[0] + weight[1]) >= 20)
        pack = 14 + ((weight[0] + weight[1]) - 14)*0.5f;
    else
        pack = 0;
    printline(strlen("item             cost            weight        total)"));
    printf("item             cost            weight        total\n\n");
        if (weight[0] > 0)
    printf("洋蓟          ¥2.05/500g        %6d            %5.2f\n\n",weight[0],weight[0]*2.05f);
        if (weight[0] > 0)
    printf("甜菜          ¥1.15/500g        %6d            %5.2f\n\n",weight[1], weight[1] * 1.15f);
    printf("discount : %.2f\n", total*discount);
    printf("packing fees : %.2f\n\n", pack);
    printf("                                                sum:¥%.2f\n", pack + weight[0] * 2.05f + weight[1] * 1.15f - total * discount);
    printline(strlen("item             cost            weight        total)"));
    printf("press any key to comfirm\n");
    getchar();
    printf("Thank you");
    getchar();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值