#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<io.h>
#define MAXNUM 2000
typedef struct{
char isbn[10];//书序号
char bkName[30];//书名
char author[20];//作者
float price;//单价
int num;//数量
} Book;
int bookNum=3;
Book bk[MAXNUM]={"001","C Primer Plus","Stephen Prata",89.00,10,
"002","python","John zelle",78.00,20,
"003","The Dream of Red Mansion","CaoXueQin",38.00,30};
void list(){
int i;
system("cls");
printf(".................\n");
for(i=0;i<bookNum;i++){
printf("%10s%30s %20s %.2f %d\n",bk[i].isbn,bk[i].bkName,
bk[i].author,bk[i].price,bk[i].num);
}
system("pause");
}
void myGets(char *ps){
fflush(stdin);
gets(ps);
}
float viewBuy(Book buy[],int buyNum,char bkNo[]){
float total=0;
system("cls");
printf("欢迎使用销售功能\n");
for(int i=0;i<buyNum;i++){
float t=buy[i].price*buy[i].num;
total+=t;
printf("%-10s%-30s%-8.2f%-5d%-8.2f\n",
buy[i].isbn,buy[i].bkName,
buy[i].price,buy[i].num,t);
}
printf("total=%.2f\n",total);
printf("请输入书号,000则退出:");myGets(bkNo);
return total;
}
int seekBkNo(char *bkNo){
int i;
for(i=0;i<bookNum;i++){
if(strcmp(bk[i].isbn,bkNo)==0) break;
}
if(i>=bookNum) return -1;
if(bk[i].num<=0) return -2;
return i;
}
void addBuy(Book buy[],int &buyNum,int index){
int i;
for(i=0;i<buyNum;i++){
if(strcmp(buy[i].isbn,bk[index].isbn)==0)
break;
}
if(i<buyNum) buy[i].num++;
else buy[buyNum]=bk[index];
buy[buyNum].num=1;
buyNum++;
}
void sale(){
system("cls");
Book buy[100]={};
int buyNum=0;
float inmoney,total;
char bkNo[20];
do{
total=viewBuy(buy,buyNum,bkNo);//显示销售界面并等待输入一个书号
if(strcmp(bkNo,"000")==0) break;//若书号为000则退出循环
int index=seekBkNo(bkNo);//在bk数组查找改书
if(index<0){
printf("该书不存在或未入库,");
system("pause");
continue;
}// 未找到,则做相应处理后continue
addBuy(buy,buyNum,index);//找到,把图书相关信息返回到buy数组
bk[index].num--;//bk数组对应的图书数量-1
}while(1);
printf("支付金额:"); scanf("%f",&inmoney);
printf("找零:%.2lf",inmoney-total);
}
void input(){
system("cls");
printf("入库ing\n