// 杂货店订购程序.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();
}