小型超市管理系統.c
阿新 • • 發佈:2018-12-22
printf("\t| 祝您使用愉快 |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t*******************************************************************\n");
SALE = (struct Sale *)malloc(sizeof(struct Sale));
GOOD = (struct Good *)malloc(sizeof(struct Good));
SALE->next = NULL;
GOOD->next = NULL;
SaleReadFromFile();//讀取銷售記錄
GoodsReadFromFile();//讀取商品記錄
_sleep(2000); //延遲
}
//使用者主程式
void UserWindows()
{
int choose = 0;
while (1)
{
UserMenu(); //使用者主選單
scanf("%d", &choose);
if (choose == 1)
{
UserScan();
}
else if (choose == 2)
{
UserCart(); //開啟購物車之旅
}
else if (choose ==close_passward)
{
SaveAndExit();
}
else if (choose == password)
break;
}
}
//使用者主選單
void UserMenu()
{
system("cls");
printf("\n\n");
printf("\t*************************************************************\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| 相信美好的事情即將發生 |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| 請選擇操作 |\n");
printf("\t| |\n");
printf("\t| 【1】瀏覽商品 【2】購買商品 |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t*************************************************************\n");
}
//使用者瀏覽
void UserScan()
{
if (GOOD->next == NULL)
{
printf("\n\n\t抱歉,商店商品已全售完,店主正在忙碌進貨中...");
_sleep(2000);
}
else
{
system("cls");
struct Good *goods_pointer = GOOD;
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
GoodsShow(goods_pointer);
}
printf("\n\t是否購買商品?(按【Y】確認)");
char flag;
fflush(stdin);
flag = getch();
if (flag == 'Y' || flag == 'y')
{
UserCart();//購物車
}
}
}
//商品資訊顯示
void GoodsShow(struct Good *goods)
{
printf("\n\n\t***************商品資訊******************");
printf("\n\t商品編號...%30d\n", goods->id);
printf("\n\t-----------------------------------------");
printf("\n\t商品名稱...%30s\n", goods->name);
printf("\n\t-----------------------------------------");
printf("\n\t商品數量...%30d\n", goods->num);
printf("\n\t-----------------------------------------");
printf("\n\t出售價格...%30.2f\n", goods->price2);
printf("\n\t-----------------------------------------");
printf("\n\t生產日期...%30.2f\n", goods->date);
printf("\n\t-----------------------------------------");
printf("\n\t保質期...%32d\n", goods->baozhiqi);
printf("\n\t-----------------------------------------\n\n");
}
//購物車
void UserCart()
{
if (GOOD->next == NULL)
{
printf("\n\n\t抱歉,商店商品已全售完,店主正在忙碌進貨中...");
_sleep(2000);
return ;
}
system("cls");
printf("\n\n\n\t即將進入選購介面,請稍等...");
_sleep(2000);
struct Sale *shopping_cart;//購物車
shopping_cart = (struct Sale *)malloc(sizeof(struct Sale));
shopping_cart->next = NULL;//初始化購物車列表
int i = 0;
struct Good *goods, *address[ShoppingCartMax];//存放商品地址;
for ( ; i < ShoppingCartMax; i++)
{
goods = GoodsFind();//查詢商品
if (goods == NULL)
{
printf("\n\n\n\t\t抱歉,沒有您需要的商品!");
_sleep(2000);
continue;
}
else if (goods == GOOD)//在選擇商品時選擇【結束操作】
{
system("cls");
if (shopping_cart->next == NULL)
{
printf("\n\n\n\n\t\t由於您購物車空空的,將結束本次購物!");
printf("\n\n\n\t購物系統正在關閉,請稍等...");
free(shopping_cart);//銷燬購物車
_sleep(4000);
break;
}
else
{
printf("\n\n\n\t您取消了本次選購,是否前往支付?");
printf("\n\n\t【Y】前往支付 【任意鍵】放棄購物");
char choose1;
choose1 = getch();
if (choose1 == 'Y' || choose1 == 'y')
{
free(goods);
UserPay(shopping_cart, address);//結賬
}
else
{
printf("\n\n\n\n\t\t購物系統正在關閉,請稍等...");
free(shopping_cart);//銷燬購物車
_sleep(2000);
break;
}
}
}
system("cls");
GoodsShow(goods);//顯示商品資訊
struct Sale *cart_pointer = shopping_cart;
int judge = 123;//判斷是否重複購買,初始化賦隨機值
while (cart_pointer->next != NULL)//查詢購物車是否已存在該商品
{
cart_pointer = cart_pointer->next;
judge = strcmp(cart_pointer->name, goods->name);
if (judge == 0)
{
printf("\t【購物車已有%d件該商品!,若要繼續購買,將自動為您合併!】\n", cart_pointer->num);
break;
}
}
struct Sale *sale;//可移動指標對銷售進行操作
sale = (struct Sale *)malloc(sizeof(struct Sale));
printf("\n\n請輸入要購買的數量:(輸入【0】重新選擇)\t");
scanf("%d", &sale->num);
if (sale->num == 0) continue;
if (judge == 0)//準備合併相同商品
{
sale->num = (cart_pointer->num) + (sale->num);
}
if ((sale->num) > (goods->num))//判斷是否超過最大商品數量
{
printf("\n\n\n\t【友情提示:很抱歉,該商品由於銷售太火爆,目前只剩下%d件了】", goods->num);
printf("\n\n\t\t【A】繼續,購買%d件 【B】重新選擇 【任意鍵】結束購物\n", goods->num);
char choose;
fflush(stdin);
choose = getch();
if (choose == 'A' || choose == 'a')
{
sale->num = goods->num;
printf("\n\n\n\t【謝謝您的理解和支援!】");
}
else if (choose == 'B' || choose == 'b') continue;
else break;
}
if (judge == 0)//購物車存在商品時,合併商品
{
cart_pointer->num = sale->num;
printf("\n\n\t【由於購物車存在該商品,已自動為您合併!】\n\n\n");
}
else//購物車不存在商品時,新增到購物車
{
address[i] = goods;// 記錄商品地址
strcpy(sale->name, goods->name);// 記錄商品名稱
sale->id = goods->id;
sale->price1 = goods->price1;
sale->price2 = goods->price2;
sale->next = NULL;
while (cart_pointer->next != NULL)
{
cart_pointer = cart_pointer->next;
}
cart_pointer->next = sale;//新增到購物列車中
printf("\n\n\t已成功新增商品到購物車!");
}
if (i == (ShoppingCartMax-1))//購物車滿時操作
{
system("cls");
printf("\n\n\n\n\t\t購物車已達到最大容量,將自動為您跳往支付介面!");
_sleep(2000);
UserPay(shopping_cart, address);//結賬
break;
}
printf("\n\n\n\t\t【1】:前往支付 【2】:繼續瀏覽 【3】:放棄購物\n");
char choice;
choice = getch();
if (choice == '1')
{
UserPay(shopping_cart, address);//結賬
break;
}
else if (choice == '3')
{
free(shopping_cart);
system("cls");
printf("\n\n\n\n\t\t正在退出購物系統,請稍等...");
_sleep(2000);
break;
}
}
}
//結賬
void UserPay(struct Sale *shopping_cart, struct Good *address[])
{
system("cls");
float money = 0;
struct Sale *sale_pointer = shopping_cart;
printf("\n\n安全交易系統已開啟!");
printf("\n\n訂單開始處理...\n...\n\n訂單稽核通過...\n\n正在生成訂單...\n\n訂單生成完成!");
printf("\n\n\n\t*************************訂單詳情**************************");
printf("\n\n\t商品名稱\t商品單價\t購買數量\t消費總價\n");
while (sale_pointer->next != NULL)
{
sale_pointer = sale_pointer->next;
printf("\n\t%s\t\t%.2f\t\t%d\t\t%.2f\t\t",sale_pointer->name,
sale_pointer->price2, sale_pointer->num, (sale_pointer->price2)*(sale_pointer->num));
money = money + (sale_pointer->price2)*(sale_pointer->num);
}
printf("\n\n\t***********************************************************");
printf("\n\t總共消費金額為:%.2f", money);
printf("\n\n\n\t確定完成交易?\t按【Y】確認支付 【任意鍵】取消支付\n\t");
fflush(stdin);
char flag;
flag = getch();
if (flag == 'Y' || flag == 'y')
{
char name[20];
printf("\n\n\t請輸入您的名字:");
scanf("%s", name);
printf("\n\n系統正在完成交易,請稍等...");
int i;
struct Good *goods_address;//臨時存放商品地址
sale_pointer = shopping_cart;
for (i = 0; sale_pointer->next != NULL; i++)
{
sale_pointer = sale_pointer->next;
strcpy(sale_pointer->buyer, name);//記錄購買者名字
_tzset();
_strdate(sale_pointer->time);//記錄購買時間
goods_address = address[i];
goods_address->num = (goods_address->num) - (sale_pointer->num);//更改商品數量
if (goods_address->num == 0)
{
DeleteOperation(goods_address);//清除數量為0的商品記錄
}
}
//增加商品銷售記錄
sale_pointer = SALE;
while (sale_pointer->next != NULL)//定位到銷售列表末尾
{
sale_pointer = sale_pointer->next;
}
sale_pointer->next = shopping_cart->next;
printf("\n\n\n\t交易完成,謝謝惠顧! O(∩_∩)O~\n\n");
printf("\n\n\t交易系統正在關閉,請稍後...");
_sleep(5000);
}
}
//管理員主程式
void AdministratorWindows()
{
int flag = 1; //判斷是否進入使用者介面的標誌
char choose;
while (flag)
{
AdministratorMenu();
choose = getch();
system("cls");
switch (choose)
{
case '1':
{
GoodsAdd(); //新購商品入庫
break;
}
case '2':
{
GoodsDetail();//商品資訊詳情
break;
}
case '3':
{
GoodsChange();//商品資訊更改
break;
}
case '4':
{
GoodsDelete();//商品資訊刪除
break;
}
case '5':
{
GoodsBrowse();//商品資訊瀏覽
break;
}
case '6':
{
SaleManage(); //商品銷售管理
break;
}
case '7':
{
system("cls");
flag = 0; //flag 設為0,準備進入使用者介面
printf("\n\n\n\t即將進入使用者介面,請稍等...");
_sleep(2000);
break; //進入使用者介面
}
case '0':
{
SaveAndExit();//儲存並退出
exit(0);
}
}
fflush(stdin);
}
}
//管理員主選單
void AdministratorMenu()
{
system("cls");
printf("\n\t|-------------------------------------------------------------- ----------------|");
printf("\n\t| |");
printf("\n\t| 【小型超市管理系統】 |");
printf("\n\t| |");
printf("\n\t|------------------------------------------------------------------- -----------|");
printf("\n\t| |");
printf("\n\t| 【1】∽∽∽∽∽∽∽【 新購商品入庫 】 |");
printf("\n\t| |");
printf("\n\t| 【2】∽∽∽∽∽∽∽【 商品資訊詳情 】 |");
printf("\n\t| |");
printf("\n\t| 【3】∽∽∽∽∽∽∽【 商品資訊更改 】 |");
printf("\n\t| |");
printf("\n\t| 【4】∽∽∽∽∽∽∽【 商品資訊刪除 】 |");
printf("\n\t| |");
printf("\n\t| 【5】∽∽∽∽∽∽∽【 商品資訊瀏覽 】 |");
printf("\n\t| |");
printf("\n\t| 【6】∽∽∽∽∽∽∽【 商品銷售管理 】 |");
printf("\n\t| |");
printf("\n\t| 【7】∽∽∽∽∽∽∽【 進入使用者介面 】 |");
printf("\n\t| |");
printf("\n\t| 【0】∽∽∽∽∽∽∽【 儲存退出程式 】 |");
printf("\n\t| |");
printf("\n\t|------------------------------------------------------------- -----------------|");
printf("\n\n\t\t\t\t請輸入您要選擇的選單....");
}
//新購商品入庫
void GoodsAdd()
{
while (1)
{
system("cls");
printf("\n\n\t\t r:日用品 w:文化用品 s:食品 d:電子產品");
printf("\n\n請輸入商品資訊:");
struct Good *goods,*exist;//指標 *exist 判斷是否已存在該商品
goods = (struct Good *)malloc(sizeof(struct Good));
printf("\n\n商品編號:");
scanf("%d", &goods->id);
exist = GoodsIdFind(goods->id);//檢查編號是否重複
if (exist != NULL)
{
printf("\n\n\t該商品資訊已存在!請選擇操作:\n\t【1】:重新輸入\t【2】:前往編輯");
char choose1;
choose1 = getch();
if (choose1 == '2')
{
ChangeOperation(exist);//更改操作
}
free(goods);
continue;
}
printf("商品名稱:");
scanf("%s", &goods->name);
exist = GoodsNameFind(goods->name);//檢查名稱是否重複
if (exist != NULL)
{
printf("\n\n\t該商品資訊已存在!請選擇操作:\n\t【1】重新輸入\t【2】前往編輯");
char choose2;
choose2 = getch();
if (choose2 == '2')
{
ChangeOperation(exist);//更改操作
}
free(goods);
continue;
}
fflush(stdin);
printf("商品型別:");
scanf("%c", &goods->kind);
goods->kind = tolower(goods->kind);//將商品型別轉換為小寫
printf("商品數量:");
scanf("%d", &goods->num);
printf("進貨價格:");
scanf("%f", &goods->price1);
printf("出售價格:");
scanf("%f", &goods->price2);
printf("生產日期:");
scanf("%f", &goods->date);
printf("保質期:");
scanf("%d", &goods->baozhiqi);
system("cls");
printf("\n\n\t************************************************************************");
printf("\n\n\t編號\t名稱\t型別\t數量\t進價\t售價\t生產日期\t保質期 \n\n");
printf("\t%d\t%s\t%c\t%d\t%.2f\t%.2f\t %.2f \t\t %d\n",
goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t************************************************************************");
printf("\n\n\t確定錄入此商品資訊?\t(按【Y】確認)\n\n");
struct Good *goods_pointer = GOOD;
char check1;
fflush(stdin);
check1 = getch();
if (check1 == 'Y' || check1 == 'y')
{
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;//定位到商品列表末尾
}
goods->next = goods_pointer->next;
goods_pointer->next = goods;
}
printf("\n\n\t是否繼續錄入商品資訊?\t(按【Y】確認)");
char check2;
check2 = getch();
if (check2 != 'Y' || check2 != 'y')
{
system("cls");
printf("\n\n\n\n\t正在結束商品資訊錄入,即將返回主程式...");
_sleep(2000);
break;
}
}
}
//商品資訊詳情
void GoodsDetail()
{
struct Good *goods;
goods = GoodsFind();//查詢是存在該商品
if (goods == NULL)
{
printf("\n\n\t【抱歉,不存在該商品!】");
_sleep(2000);
}
else if (goods == GOOD)
{
printf("\n\n\n\t\t正在返回程式,請稍等...");
_sleep(2000);
}
else
{
system("cls");
printf("\n\n\t***********************************************************************");
printf("\n\n\t 編號\t 名稱\t 型別\t 數量\t 進價\t 售價\t 生產日期\t 保質期 \n\n");
printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n",
goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t***********************************************************************");
printf("\n\n\t按任意鍵繼續...");
getch();
}
}
//商品資訊查詢選擇
struct Good *GoodsFind()
{
if (GOOD->next == NULL)
{
printf("\n\n\t\t抱歉,目前商店沒有商品資訊!");
_sleep(2000);
return GOOD;
}
while (1)
{
system("cls");
printf("\n\n\n\n");
printf("\t|----------------------------------------------------------------------|\n");
printf("\t| |\n");
printf("\t| 【A】根據商品編號id選擇 |\n");
printf("\t| |\n");
printf("\t| 【B】根據商品名稱name選擇 |\n");
printf("\t| |\n");
printf("\t| 【C】結束操作 |\n");
printf("\t| |\n");
printf("\t|----------------------------------------------------------------------|\n");
printf("\t| |\n");
printf("\t| ∞請選擇操作∞ |\n");
printf("\t| |\n");
printf("\t|----------------------------------------------------------------------|\n");
struct Good *goods;
char choose;
fflush(stdin);
choose = getch();
if (choose == 'A' || choose == 'a')
{
printf("\n\t請輸入商品編號:");
int goods_number;
scanf("%d", &goods_number);
goods = GoodsIdFind(goods_number);
return goods;
}
else if (choose == 'B' || choose == 'b')
{
printf("\n\t請輸入商品名稱:");
fflush(stdin);
char goods_name[20];//待輸入名稱
gets(goods_name);
goods = GoodsNameFind(goods_name);
return goods;
}
else if (choose == 'C' || choose == 'c') return GOOD;
}
}
//根據ID查詢
struct Good *GoodsIdFind(int goods_number)
{
struct Good *goods=GOOD;
while (goods->next != NULL)
{
goods = goods->next;
if (goods->id == goods_number)
{
return goods;
}
}
return NULL;
}
//根據名稱查詢
struct Good *GoodsNameFind(char goods_name[])
{
struct Good *goods=GOOD;
while (goods->next != NULL)
{
goods = goods->next;
if (strcmp(goods->name, goods_name) == 0)
{
return goods;
}
}
return NULL;
}
//商品資訊更改
void GoodsChange()
{
struct Good *goods;
goods = GoodsFind();//查詢是存在該商品
if (goods == NULL)
{
printf("\n\n\t【不存在該商品!】");
_sleep(2000);
}
else if (goods == GOOD)
{
printf("\n\n\n\t\t正在返回程式,請稍等...");
_sleep(2000);
}
else
{
ChangeOperation(goods);//更改操作
}
}
//更改操作
void ChangeOperation(struct Good *goods)
{
int end = 0;
char choose;
while (1)
{
system("cls");
printf("\n\n\t***************************************************************************");
printf("\n\n\t編號\t 名稱 \t 型別\t 數量\t 進價\t 售價\t 生產日期\t保質期 \n\n");
printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n", goods->id, goods->name,
goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t***************************************************************************");
printf("\n\n\t\t【r】:日用品 【w】:文化用品 【s】:食品 【d】:電子產品\n");
printf("\n\t【1】商品編號\t【2】商品名稱\t【3】商品型別\t【4】商品數量");
printf("\n\t【5】進貨價格\t【6】出售價格\t【7】生產日期\t【8】保質期\n\t【0】儲存退出\n");
printf("\n\t請選擇序號進行操作:\n");
choose = getch();
switch (choose)
{
case '1':
{
printf("\n\t請輸入商品編號:");
scanf("%d", &goods->id);
break;
}
case '2':
{
printf("\n\t請輸入商品名稱:");
scanf("%s", &goods->name);
break;
}
case '3':
{
printf("\n\t請輸入商品型別:");
fflush(stdin);
scanf("%c", &goods->kind);
break;
}
case '4':
{
printf("\n\t請輸入商品數量:");
scanf("%d", &goods->num);
break;
}
case '5':
{
printf("\n\t請輸入進貨價格:");
scanf("%f", &goods->price1);
break;
}
case '6':
{
printf("\n\t請輸入出售價格:");
scanf("%f", &goods->price2);
break;
}
case '7':
{
printf("\n\t請輸入生產日期:");
scanf("%f", &goods->date);
break;
}
case '8':
{
printf("\n\t請輸入保質期:");
scanf("%d", &goods->baozhiqi);
break;
}
case '0':
{
printf("\n\n\t正在儲存,請稍等...");
_sleep(2000);
end = 1;
break;
}
}
if (end == 1) break;
}
}
//商品資訊刪除
void GoodsDelete()
{
struct Good *goods;
goods = GoodsFind();//查詢是存在該商品
if (goods == NULL)
{
printf("\n\n\t【抱歉,不存在該商品!】");
_sleep(2000);
}
else if (goods == GOOD)
{
printf("\n\n\n\t\t正在返回程式,請稍等...");
_sleep(2000);
}
else
{
printf("\n\n\t************************************************************************");
printf("\n\n\t 編號\t 名稱\t 型別\t 數量\t 進價\t 售價\t 生產日期\t 保質期 \n\n");
printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n",
goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t************************************************************************");
printf("\n警告:是否確認刪除?!(輸入【Y】確認)");
char choose;
fflush(stdin);
choose = getch();
if (choose == 'Y' || choose == 'y')
{
DeleteOperation(goods);//刪除操作
}
}
}
//刪除操作
void DeleteOperation(struct Good *goods)
{
struct Good *goods_pointer = GOOD;
while (goods_pointer->next != goods)
{
goods_pointer = goods_pointer->next;
}
goods_pointer->next = goods->next;
free(goods);
printf("\n\n\t操作成功!");
_sleep(300);
}
//商品資訊瀏覽
void GoodsBrowse()
{
system("cls");
if (GOOD->next == NULL)
{
printf("\n\n\t\t抱歉,目前商店沒有商品資訊!");
_sleep(2000);
}
else
{
printf("\n\n\n\t\t請選擇瀏覽方式:\n\n\t【1】全部瀏覽\t【2】按型別瀏覽\n\n");
char choose;
while (1)
{
choose = getch();
if (choose == '1')
{
AllBrowse();
break;
}
else if (choose=='2')
{
BrowseByType();
break;
}
}
printf("\n\n\n\t按任意鍵繼續...");
getch();
}
}
//瀏覽所有商品
void AllBrowse()
{
system("cls");
struct Good *goods_pointer = GOOD;
printf("\n\n\t*********************************************************************************");
printf("\n\n\t 編號\t 名稱\t\t 型別\t 數量\t 進價\t 售價\t 生產日期\t保質期 \n\n");
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
printf("\t %d\t %s\t\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t%d \n", goods_pointer->id,
goods_pointer->name, goods_pointer->kind, goods_pointer->num, goods_pointer->price1,
goods_pointer->price2, goods_pointer->date, goods_pointer->baozhiqi);
}
printf("\n\n\t*********************************************************************************");
}
//按型別瀏覽商品
void BrowseByType()
{
system("cls");
printf("\n\n\t\t 【r】:日用品 【w】:文化用品 【s】:食品 【d】:電子產品");
printf("\n\n\n請選擇要瀏覽的商品型別:");
char Type;
Type = getch();
Type = tolower(Type);//將商品型別轉換為小寫
system("cls");
struct Good *goods_pointer = GOOD;
printf("\n\n\t*********************************************************************");
printf("\n\n\t 編號\t 名稱\t 型別\t 數量\t 進價\t 售價\t 生產日期\t 保質期 \n");
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
if (goods_pointer->kind == Type)
printf("\n\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d",
goods_pointer->id, goods_pointer->name, goods_pointer->kind, goods_pointer->num,
goods_pointer->price1, goods_pointer->price2, goods_pointer->date, goods_pointer->baozhiqi);
}
printf("\n\n\t*********************************************************************");
}
//商品銷售管理
void SaleManage()
{
system("cls");
printf("\n\n\t\t\t銷售情況檢視及利潤統計");
printf("\n\n\t【r】:日用品 【w】:文化用品 【s】:食品 【d】:電子產品");
printf("\n\n\t***********************************************************");
if (SALE->next == NULL)
{
printf("\n\n目前暫無銷售記錄!");
_sleep(2000);
}
else
{
printf("\n\n\t編號\t名稱\t進價\t售價\t數量\t名字\t時間\n\n");
int number = 0;//商品數量
float profit = 0;//商品利潤
struct Sale *sale_pointer = SALE;
while (sale_pointer->next != NULL)
{
sale_pointer = sale_pointer->next;
printf("\t %d\t %s\t %.2f\t %.2f\t %d\t %s\t %s\n",sale_pointer->id, sale_pointer->name,
sale_pointer->price1, sale_pointer->price2, sale_pointer->num, sale_pointer->buyer, sale_pointer->time);
profit = profit + ((sale_pointer->price2) - (sale_pointer->price1))*(sale_pointer->num);
number = number + (sale_pointer->num);
}
printf("\n\t***********************************************************");
printf("\n\n\t共銷售商品%d件,純利潤為%.2f", number, profit);
printf("\n\n\n\t按任意鍵繼續...");
getch();
}
}
//儲存並退出
void SaveAndExit()
{
SaleSaveToFile();//儲存銷售資訊
GoodsSaveToFile();//儲存商品資訊
system("cls");
printf("\n\n\n\n\n\n");
printf("\n\t\t#〓§〓〓〓〓〓§〓〓〓〓〓〓§〓〓〓〓〓§〓#");
printf("\n\t\t↓ ↓ ↓ ↓");
printf("\n\t\t ☆★☆ ☆★☆ ☆★☆ ☆★☆");
printf("\n\t\t☆ 謝 ☆ ☆ 謝 ☆ ☆ 使 ☆ ☆ 用 ☆");
printf("\n\t\t ☆★☆ ☆★☆ ☆★☆ ☆★☆");
printf("\n\t\t ↓ ↓ ↓ ↓");
printf("\n\t\t ※ ※ ※ ※");
printf("\n");
_sleep(3000);
exit(0);
}
//儲存商品資訊
int GoodsSaveToFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("goods_path", "wb")) == NULL)
{
sprintf(buff, "Can not open file:%s to create", "goods_path");
_sleep(2000);
return 0;
}
struct Good *goods_pointer = GOOD;//定位到連結串列頭部
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
fwrite(goods_pointer, sizeof(struct Good), 1, fp);//一個個的儲存到檔案中去
}
fclose(fp);
return 1;
}
//儲存銷售資訊
int SaleSaveToFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("sale_path", "wb")) == NULL)
{
sprintf(buff, "Can not open file:%s to create", "sale_path");
_sleep(2000);
return 0;
}
struct Sale *sale_pointer = SALE;//定位到連結串列頭部
while (sale_pointer->next != NULL)
{
sale_pointer = sale_pointer->next;
fwrite(sale_pointer, sizeof(struct Sale), 1, fp);//一個個的儲存到檔案中去
}
fclose(fp);
return 1;
}
//讀取商品記錄
int GoodsReadFromFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("goods_path", "rb")) == NULL)
{
sprintf(buff, "Can not open file:%s to read", "goods_path");
return 1;
}
int number;
fseek(fp, 0, 2);//定位到檔案末尾
number = ftell(fp) / sizeof(struct Good);//求出總結點數
fseek(fp, 0, 0);//定位到檔案的開始處
int i;
struct Good *goods;
struct Good *goods_pointer = GOOD;
for (i = 0; i < number; i++)
{
goods = (struct Good *)malloc(sizeof(struct Good));
fread(goods, sizeof(struct Good), 1, fp);
goods->next = NULL;
while (goods_pointer->next != NULL)//定位到商品列表末尾
{
goods_pointer = goods_pointer->next;
}
goods_pointer->next = goods;//插入到連結串列當中
}
fclose(fp);//關閉檔案
return 0;
}
//讀取銷售記錄
int SaleReadFromFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("sale_path", "rb")) == NULL)
{
sprintf(buff, "Can not open file:%s to read", "sale_path");
return 1;
}
int number;
fseek(fp, 0, 2);//定位到檔案末尾
number = ftell(fp) / sizeof(struct Sale);//求出總結點數
fseek(fp, 0, 0);//定位到檔案的開始處
int i;
struct Sale *sale;
struct Sale *sale_pointer = SALE;
for (i = 0; i < number; i++)
{
sale = (struct Sale *)malloc(sizeof(struct Sale));
fread(sale, sizeof(struct Sale), 1, fp);
sale->next = NULL;
while (sale_pointer->next != NULL)//定位到銷售列表末尾
{
sale_pointer = sale_pointer->next;
}
sale_pointer->next = sale;//插入到連結串列當中
}
fclose(fp); //關閉檔案
return 0;
}
printf("\t| |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t*******************************************************************\n");
SALE = (struct Sale *)malloc(sizeof(struct Sale));
GOOD = (struct Good *)malloc(sizeof(struct Good));
SALE->next = NULL;
GOOD->next = NULL;
SaleReadFromFile();//讀取銷售記錄
GoodsReadFromFile();//讀取商品記錄
_sleep(2000); //延遲
}
//使用者主程式
void UserWindows()
{
int choose = 0;
while (1)
{
UserMenu(); //使用者主選單
scanf("%d", &choose);
if (choose == 1)
{
UserScan();
}
else if (choose == 2)
{
UserCart(); //開啟購物車之旅
}
else if (choose ==close_passward)
{
SaveAndExit();
}
else if (choose == password)
break;
}
}
//使用者主選單
void UserMenu()
{
system("cls");
printf("\n\n");
printf("\t*************************************************************\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| 相信美好的事情即將發生 |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t| 請選擇操作 |\n");
printf("\t| |\n");
printf("\t| 【1】瀏覽商品 【2】購買商品 |\n");
printf("\t| |\n");
printf("\t| |\n");
printf("\t*************************************************************\n");
}
//使用者瀏覽
void UserScan()
{
if (GOOD->next == NULL)
{
printf("\n\n\t抱歉,商店商品已全售完,店主正在忙碌進貨中...");
_sleep(2000);
}
else
{
system("cls");
struct Good *goods_pointer = GOOD;
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
GoodsShow(goods_pointer);
}
printf("\n\t是否購買商品?(按【Y】確認)");
char flag;
fflush(stdin);
flag = getch();
if (flag == 'Y' || flag == 'y')
{
UserCart();//購物車
}
}
}
//商品資訊顯示
void GoodsShow(struct Good *goods)
{
printf("\n\n\t***************商品資訊******************");
printf("\n\t商品編號...%30d\n", goods->id);
printf("\n\t-----------------------------------------");
printf("\n\t商品名稱...%30s\n", goods->name);
printf("\n\t-----------------------------------------");
printf("\n\t商品數量...%30d\n", goods->num);
printf("\n\t-----------------------------------------");
printf("\n\t出售價格...%30.2f\n", goods->price2);
printf("\n\t-----------------------------------------");
printf("\n\t生產日期...%30.2f\n", goods->date);
printf("\n\t-----------------------------------------");
printf("\n\t保質期...%32d\n", goods->baozhiqi);
printf("\n\t-----------------------------------------\n\n");
}
//購物車
void UserCart()
{
if (GOOD->next == NULL)
{
printf("\n\n\t抱歉,商店商品已全售完,店主正在忙碌進貨中...");
_sleep(2000);
return ;
}
system("cls");
printf("\n\n\n\t即將進入選購介面,請稍等...");
_sleep(2000);
struct Sale *shopping_cart;//購物車
shopping_cart = (struct Sale *)malloc(sizeof(struct Sale));
shopping_cart->next = NULL;//初始化購物車列表
int i = 0;
struct Good *goods, *address[ShoppingCartMax];//存放商品地址;
for ( ; i < ShoppingCartMax; i++)
{
goods = GoodsFind();//查詢商品
if (goods == NULL)
{
printf("\n\n\n\t\t抱歉,沒有您需要的商品!");
_sleep(2000);
continue;
}
else if (goods == GOOD)//在選擇商品時選擇【結束操作】
{
system("cls");
if (shopping_cart->next == NULL)
{
printf("\n\n\n\n\t\t由於您購物車空空的,將結束本次購物!");
printf("\n\n\n\t購物系統正在關閉,請稍等...");
free(shopping_cart);//銷燬購物車
_sleep(4000);
break;
}
else
{
printf("\n\n\n\t您取消了本次選購,是否前往支付?");
printf("\n\n\t【Y】前往支付 【任意鍵】放棄購物");
char choose1;
choose1 = getch();
if (choose1 == 'Y' || choose1 == 'y')
{
free(goods);
UserPay(shopping_cart, address);//結賬
}
else
{
printf("\n\n\n\n\t\t購物系統正在關閉,請稍等...");
free(shopping_cart);//銷燬購物車
_sleep(2000);
break;
}
}
}
system("cls");
GoodsShow(goods);//顯示商品資訊
struct Sale *cart_pointer = shopping_cart;
int judge = 123;//判斷是否重複購買,初始化賦隨機值
while (cart_pointer->next != NULL)//查詢購物車是否已存在該商品
{
cart_pointer = cart_pointer->next;
judge = strcmp(cart_pointer->name, goods->name);
if (judge == 0)
{
printf("\t【購物車已有%d件該商品!,若要繼續購買,將自動為您合併!】\n", cart_pointer->num);
break;
}
}
struct Sale *sale;//可移動指標對銷售進行操作
sale = (struct Sale *)malloc(sizeof(struct Sale));
printf("\n\n請輸入要購買的數量:(輸入【0】重新選擇)\t");
scanf("%d", &sale->num);
if (sale->num == 0) continue;
if (judge == 0)//準備合併相同商品
{
sale->num = (cart_pointer->num) + (sale->num);
}
if ((sale->num) > (goods->num))//判斷是否超過最大商品數量
{
printf("\n\n\n\t【友情提示:很抱歉,該商品由於銷售太火爆,目前只剩下%d件了】", goods->num);
printf("\n\n\t\t【A】繼續,購買%d件 【B】重新選擇 【任意鍵】結束購物\n", goods->num);
char choose;
fflush(stdin);
choose = getch();
if (choose == 'A' || choose == 'a')
{
sale->num = goods->num;
printf("\n\n\n\t【謝謝您的理解和支援!】");
}
else if (choose == 'B' || choose == 'b') continue;
else break;
}
if (judge == 0)//購物車存在商品時,合併商品
{
cart_pointer->num = sale->num;
printf("\n\n\t【由於購物車存在該商品,已自動為您合併!】\n\n\n");
}
else//購物車不存在商品時,新增到購物車
{
address[i] = goods;// 記錄商品地址
strcpy(sale->name, goods->name);// 記錄商品名稱
sale->id = goods->id;
sale->price1 = goods->price1;
sale->price2 = goods->price2;
sale->next = NULL;
while (cart_pointer->next != NULL)
{
cart_pointer = cart_pointer->next;
}
cart_pointer->next = sale;//新增到購物列車中
printf("\n\n\t已成功新增商品到購物車!");
}
if (i == (ShoppingCartMax-1))//購物車滿時操作
{
system("cls");
printf("\n\n\n\n\t\t購物車已達到最大容量,將自動為您跳往支付介面!");
_sleep(2000);
UserPay(shopping_cart, address);//結賬
break;
}
printf("\n\n\n\t\t【1】:前往支付 【2】:繼續瀏覽 【3】:放棄購物\n");
char choice;
choice = getch();
if (choice == '1')
{
UserPay(shopping_cart, address);//結賬
break;
}
else if (choice == '3')
{
free(shopping_cart);
system("cls");
printf("\n\n\n\n\t\t正在退出購物系統,請稍等...");
_sleep(2000);
break;
}
}
}
//結賬
void UserPay(struct Sale *shopping_cart, struct Good *address[])
{
system("cls");
float money = 0;
struct Sale *sale_pointer = shopping_cart;
printf("\n\n安全交易系統已開啟!");
printf("\n\n訂單開始處理...\n...\n\n訂單稽核通過...\n\n正在生成訂單...\n\n訂單生成完成!");
printf("\n\n\n\t*************************訂單詳情**************************");
printf("\n\n\t商品名稱\t商品單價\t購買數量\t消費總價\n");
while (sale_pointer->next != NULL)
{
sale_pointer = sale_pointer->next;
printf("\n\t%s\t\t%.2f\t\t%d\t\t%.2f\t\t",sale_pointer->name,
sale_pointer->price2, sale_pointer->num, (sale_pointer->price2)*(sale_pointer->num));
money = money + (sale_pointer->price2)*(sale_pointer->num);
}
printf("\n\n\t***********************************************************");
printf("\n\t總共消費金額為:%.2f", money);
printf("\n\n\n\t確定完成交易?\t按【Y】確認支付 【任意鍵】取消支付\n\t");
fflush(stdin);
char flag;
flag = getch();
if (flag == 'Y' || flag == 'y')
{
char name[20];
printf("\n\n\t請輸入您的名字:");
scanf("%s", name);
printf("\n\n系統正在完成交易,請稍等...");
int i;
struct Good *goods_address;//臨時存放商品地址
sale_pointer = shopping_cart;
for (i = 0; sale_pointer->next != NULL; i++)
{
sale_pointer = sale_pointer->next;
strcpy(sale_pointer->buyer, name);//記錄購買者名字
_tzset();
_strdate(sale_pointer->time);//記錄購買時間
goods_address = address[i];
goods_address->num = (goods_address->num) - (sale_pointer->num);//更改商品數量
if (goods_address->num == 0)
{
DeleteOperation(goods_address);//清除數量為0的商品記錄
}
}
//增加商品銷售記錄
sale_pointer = SALE;
while (sale_pointer->next != NULL)//定位到銷售列表末尾
{
sale_pointer = sale_pointer->next;
}
sale_pointer->next = shopping_cart->next;
printf("\n\n\n\t交易完成,謝謝惠顧! O(∩_∩)O~\n\n");
printf("\n\n\t交易系統正在關閉,請稍後...");
_sleep(5000);
}
}
//管理員主程式
void AdministratorWindows()
{
int flag = 1; //判斷是否進入使用者介面的標誌
char choose;
while (flag)
{
AdministratorMenu();
choose = getch();
system("cls");
switch (choose)
{
case '1':
{
GoodsAdd(); //新購商品入庫
break;
}
case '2':
{
GoodsDetail();//商品資訊詳情
break;
}
case '3':
{
GoodsChange();//商品資訊更改
break;
}
case '4':
{
GoodsDelete();//商品資訊刪除
break;
}
case '5':
{
GoodsBrowse();//商品資訊瀏覽
break;
}
case '6':
{
SaleManage(); //商品銷售管理
break;
}
case '7':
{
system("cls");
flag = 0; //flag 設為0,準備進入使用者介面
printf("\n\n\n\t即將進入使用者介面,請稍等...");
_sleep(2000);
break; //進入使用者介面
}
case '0':
{
SaveAndExit();//儲存並退出
exit(0);
}
}
fflush(stdin);
}
}
//管理員主選單
void AdministratorMenu()
{
system("cls");
printf("\n\t|--------------------------------------------------------------
printf("\n\t| |");
printf("\n\t| 【小型超市管理系統】 |");
printf("\n\t| |");
printf("\n\t|-------------------------------------------------------------------
printf("\n\t| |");
printf("\n\t| 【1】∽∽∽∽∽∽∽【 新購商品入庫 】 |");
printf("\n\t| |");
printf("\n\t| 【2】∽∽∽∽∽∽∽【 商品資訊詳情 】 |");
printf("\n\t| |");
printf("\n\t| 【3】∽∽∽∽∽∽∽【 商品資訊更改 】 |");
printf("\n\t| |");
printf("\n\t| 【4】∽∽∽∽∽∽∽【 商品資訊刪除 】 |");
printf("\n\t| |");
printf("\n\t| 【5】∽∽∽∽∽∽∽【 商品資訊瀏覽 】 |");
printf("\n\t| |");
printf("\n\t| 【6】∽∽∽∽∽∽∽【 商品銷售管理 】 |");
printf("\n\t| |");
printf("\n\t| 【7】∽∽∽∽∽∽∽【 進入使用者介面 】 |");
printf("\n\t| |");
printf("\n\t| 【0】∽∽∽∽∽∽∽【 儲存退出程式 】 |");
printf("\n\t| |");
printf("\n\t|-------------------------------------------------------------
printf("\n\n\t\t\t\t請輸入您要選擇的選單....");
}
//新購商品入庫
void GoodsAdd()
{
while (1)
{
system("cls");
printf("\n\n\t\t r:日用品 w:文化用品 s:食品 d:電子產品");
printf("\n\n請輸入商品資訊:");
struct Good *goods,*exist;//指標 *exist 判斷是否已存在該商品
goods = (struct Good *)malloc(sizeof(struct Good));
printf("\n\n商品編號:");
scanf("%d", &goods->id);
exist = GoodsIdFind(goods->id);//檢查編號是否重複
if (exist != NULL)
{
printf("\n\n\t該商品資訊已存在!請選擇操作:\n\t【1】:重新輸入\t【2】:前往編輯");
char choose1;
choose1 = getch();
if (choose1 == '2')
{
ChangeOperation(exist);//更改操作
}
free(goods);
continue;
}
printf("商品名稱:");
scanf("%s", &goods->name);
exist = GoodsNameFind(goods->name);//檢查名稱是否重複
if (exist != NULL)
{
printf("\n\n\t該商品資訊已存在!請選擇操作:\n\t【1】重新輸入\t【2】前往編輯");
char choose2;
choose2 = getch();
if (choose2 == '2')
{
ChangeOperation(exist);//更改操作
}
free(goods);
continue;
}
fflush(stdin);
printf("商品型別:");
scanf("%c", &goods->kind);
goods->kind = tolower(goods->kind);//將商品型別轉換為小寫
printf("商品數量:");
scanf("%d", &goods->num);
printf("進貨價格:");
scanf("%f", &goods->price1);
printf("出售價格:");
scanf("%f", &goods->price2);
printf("生產日期:");
scanf("%f", &goods->date);
printf("保質期:");
scanf("%d", &goods->baozhiqi);
system("cls");
printf("\n\n\t************************************************************************");
printf("\n\n\t編號\t名稱\t型別\t數量\t進價\t售價\t生產日期\t保質期 \n\n");
printf("\t%d\t%s\t%c\t%d\t%.2f\t%.2f\t %.2f \t\t %d\n",
goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t************************************************************************");
printf("\n\n\t確定錄入此商品資訊?\t(按【Y】確認)\n\n");
struct Good *goods_pointer = GOOD;
char check1;
fflush(stdin);
check1 = getch();
if (check1 == 'Y' || check1 == 'y')
{
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;//定位到商品列表末尾
}
goods->next = goods_pointer->next;
goods_pointer->next = goods;
}
printf("\n\n\t是否繼續錄入商品資訊?\t(按【Y】確認)");
char check2;
check2 = getch();
if (check2 != 'Y' || check2 != 'y')
{
system("cls");
printf("\n\n\n\n\t正在結束商品資訊錄入,即將返回主程式...");
_sleep(2000);
break;
}
}
}
//商品資訊詳情
void GoodsDetail()
{
struct Good *goods;
goods = GoodsFind();//查詢是存在該商品
if (goods == NULL)
{
printf("\n\n\t【抱歉,不存在該商品!】");
_sleep(2000);
}
else if (goods == GOOD)
{
printf("\n\n\n\t\t正在返回程式,請稍等...");
_sleep(2000);
}
else
{
system("cls");
printf("\n\n\t***********************************************************************");
printf("\n\n\t 編號\t 名稱\t 型別\t 數量\t 進價\t 售價\t 生產日期\t 保質期 \n\n");
printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n",
goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t***********************************************************************");
printf("\n\n\t按任意鍵繼續...");
getch();
}
}
//商品資訊查詢選擇
struct Good *GoodsFind()
{
if (GOOD->next == NULL)
{
printf("\n\n\t\t抱歉,目前商店沒有商品資訊!");
_sleep(2000);
return GOOD;
}
while (1)
{
system("cls");
printf("\n\n\n\n");
printf("\t|----------------------------------------------------------------------|\n");
printf("\t| |\n");
printf("\t| 【A】根據商品編號id選擇 |\n");
printf("\t| |\n");
printf("\t| 【B】根據商品名稱name選擇 |\n");
printf("\t| |\n");
printf("\t| 【C】結束操作 |\n");
printf("\t| |\n");
printf("\t|----------------------------------------------------------------------|\n");
printf("\t| |\n");
printf("\t| ∞請選擇操作∞ |\n");
printf("\t| |\n");
printf("\t|----------------------------------------------------------------------|\n");
struct Good *goods;
char choose;
fflush(stdin);
choose = getch();
if (choose == 'A' || choose == 'a')
{
printf("\n\t請輸入商品編號:");
int goods_number;
scanf("%d", &goods_number);
goods = GoodsIdFind(goods_number);
return goods;
}
else if (choose == 'B' || choose == 'b')
{
printf("\n\t請輸入商品名稱:");
fflush(stdin);
char goods_name[20];//待輸入名稱
gets(goods_name);
goods = GoodsNameFind(goods_name);
return goods;
}
else if (choose == 'C' || choose == 'c') return GOOD;
}
}
//根據ID查詢
struct Good *GoodsIdFind(int goods_number)
{
struct Good *goods=GOOD;
while (goods->next != NULL)
{
goods = goods->next;
if (goods->id == goods_number)
{
return goods;
}
}
return NULL;
}
//根據名稱查詢
struct Good *GoodsNameFind(char goods_name[])
{
struct Good *goods=GOOD;
while (goods->next != NULL)
{
goods = goods->next;
if (strcmp(goods->name, goods_name) == 0)
{
return goods;
}
}
return NULL;
}
//商品資訊更改
void GoodsChange()
{
struct Good *goods;
goods = GoodsFind();//查詢是存在該商品
if (goods == NULL)
{
printf("\n\n\t【不存在該商品!】");
_sleep(2000);
}
else if (goods == GOOD)
{
printf("\n\n\n\t\t正在返回程式,請稍等...");
_sleep(2000);
}
else
{
ChangeOperation(goods);//更改操作
}
}
//更改操作
void ChangeOperation(struct Good *goods)
{
int end = 0;
char choose;
while (1)
{
system("cls");
printf("\n\n\t***************************************************************************");
printf("\n\n\t編號\t 名稱 \t 型別\t 數量\t 進價\t 售價\t 生產日期\t保質期 \n\n");
printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n", goods->id, goods->name,
goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t***************************************************************************");
printf("\n\n\t\t【r】:日用品 【w】:文化用品 【s】:食品 【d】:電子產品\n");
printf("\n\t【1】商品編號\t【2】商品名稱\t【3】商品型別\t【4】商品數量");
printf("\n\t【5】進貨價格\t【6】出售價格\t【7】生產日期\t【8】保質期\n\t【0】儲存退出\n");
printf("\n\t請選擇序號進行操作:\n");
choose = getch();
switch (choose)
{
case '1':
{
printf("\n\t請輸入商品編號:");
scanf("%d", &goods->id);
break;
}
case '2':
{
printf("\n\t請輸入商品名稱:");
scanf("%s", &goods->name);
break;
}
case '3':
{
printf("\n\t請輸入商品型別:");
fflush(stdin);
scanf("%c", &goods->kind);
break;
}
case '4':
{
printf("\n\t請輸入商品數量:");
scanf("%d", &goods->num);
break;
}
case '5':
{
printf("\n\t請輸入進貨價格:");
scanf("%f", &goods->price1);
break;
}
case '6':
{
printf("\n\t請輸入出售價格:");
scanf("%f", &goods->price2);
break;
}
case '7':
{
printf("\n\t請輸入生產日期:");
scanf("%f", &goods->date);
break;
}
case '8':
{
printf("\n\t請輸入保質期:");
scanf("%d", &goods->baozhiqi);
break;
}
case '0':
{
printf("\n\n\t正在儲存,請稍等...");
_sleep(2000);
end = 1;
break;
}
}
if (end == 1) break;
}
}
//商品資訊刪除
void GoodsDelete()
{
struct Good *goods;
goods = GoodsFind();//查詢是存在該商品
if (goods == NULL)
{
printf("\n\n\t【抱歉,不存在該商品!】");
_sleep(2000);
}
else if (goods == GOOD)
{
printf("\n\n\n\t\t正在返回程式,請稍等...");
_sleep(2000);
}
else
{
printf("\n\n\t************************************************************************");
printf("\n\n\t 編號\t 名稱\t 型別\t 數量\t 進價\t 售價\t 生產日期\t 保質期 \n\n");
printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n",
goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi);
printf("\n\n\t************************************************************************");
printf("\n警告:是否確認刪除?!(輸入【Y】確認)");
char choose;
fflush(stdin);
choose = getch();
if (choose == 'Y' || choose == 'y')
{
DeleteOperation(goods);//刪除操作
}
}
}
//刪除操作
void DeleteOperation(struct Good *goods)
{
struct Good *goods_pointer = GOOD;
while (goods_pointer->next != goods)
{
goods_pointer = goods_pointer->next;
}
goods_pointer->next = goods->next;
free(goods);
printf("\n\n\t操作成功!");
_sleep(300);
}
//商品資訊瀏覽
void GoodsBrowse()
{
system("cls");
if (GOOD->next == NULL)
{
printf("\n\n\t\t抱歉,目前商店沒有商品資訊!");
_sleep(2000);
}
else
{
printf("\n\n\n\t\t請選擇瀏覽方式:\n\n\t【1】全部瀏覽\t【2】按型別瀏覽\n\n");
char choose;
while (1)
{
choose = getch();
if (choose == '1')
{
AllBrowse();
break;
}
else if (choose=='2')
{
BrowseByType();
break;
}
}
printf("\n\n\n\t按任意鍵繼續...");
getch();
}
}
//瀏覽所有商品
void AllBrowse()
{
system("cls");
struct Good *goods_pointer = GOOD;
printf("\n\n\t*********************************************************************************");
printf("\n\n\t 編號\t 名稱\t\t 型別\t 數量\t 進價\t 售價\t 生產日期\t保質期 \n\n");
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
printf("\t %d\t %s\t\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t%d \n", goods_pointer->id,
goods_pointer->name, goods_pointer->kind, goods_pointer->num, goods_pointer->price1,
goods_pointer->price2, goods_pointer->date, goods_pointer->baozhiqi);
}
printf("\n\n\t*********************************************************************************");
}
//按型別瀏覽商品
void BrowseByType()
{
system("cls");
printf("\n\n\t\t 【r】:日用品 【w】:文化用品 【s】:食品 【d】:電子產品");
printf("\n\n\n請選擇要瀏覽的商品型別:");
char Type;
Type = getch();
Type = tolower(Type);//將商品型別轉換為小寫
system("cls");
struct Good *goods_pointer = GOOD;
printf("\n\n\t*********************************************************************");
printf("\n\n\t 編號\t 名稱\t 型別\t 數量\t 進價\t 售價\t 生產日期\t 保質期 \n");
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
if (goods_pointer->kind == Type)
printf("\n\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d",
goods_pointer->id, goods_pointer->name, goods_pointer->kind, goods_pointer->num,
goods_pointer->price1, goods_pointer->price2, goods_pointer->date, goods_pointer->baozhiqi);
}
printf("\n\n\t*********************************************************************");
}
//商品銷售管理
void SaleManage()
{
system("cls");
printf("\n\n\t\t\t銷售情況檢視及利潤統計");
printf("\n\n\t【r】:日用品 【w】:文化用品 【s】:食品 【d】:電子產品");
printf("\n\n\t***********************************************************");
if (SALE->next == NULL)
{
printf("\n\n目前暫無銷售記錄!");
_sleep(2000);
}
else
{
printf("\n\n\t編號\t名稱\t進價\t售價\t數量\t名字\t時間\n\n");
int number = 0;//商品數量
float profit = 0;//商品利潤
struct Sale *sale_pointer = SALE;
while (sale_pointer->next != NULL)
{
sale_pointer = sale_pointer->next;
printf("\t %d\t %s\t %.2f\t %.2f\t %d\t %s\t %s\n",sale_pointer->id, sale_pointer->name,
sale_pointer->price1, sale_pointer->price2, sale_pointer->num, sale_pointer->buyer, sale_pointer->time);
profit = profit + ((sale_pointer->price2) - (sale_pointer->price1))*(sale_pointer->num);
number = number + (sale_pointer->num);
}
printf("\n\t***********************************************************");
printf("\n\n\t共銷售商品%d件,純利潤為%.2f", number, profit);
printf("\n\n\n\t按任意鍵繼續...");
getch();
}
}
//儲存並退出
void SaveAndExit()
{
SaleSaveToFile();//儲存銷售資訊
GoodsSaveToFile();//儲存商品資訊
system("cls");
printf("\n\n\n\n\n\n");
printf("\n\t\t#〓§〓〓〓〓〓§〓〓〓〓〓〓§〓〓〓〓〓§〓#");
printf("\n\t\t↓ ↓ ↓ ↓");
printf("\n\t\t ☆★☆ ☆★☆ ☆★☆ ☆★☆");
printf("\n\t\t☆ 謝 ☆ ☆ 謝 ☆ ☆ 使 ☆ ☆ 用 ☆");
printf("\n\t\t ☆★☆ ☆★☆ ☆★☆ ☆★☆");
printf("\n\t\t ↓ ↓ ↓ ↓");
printf("\n\t\t ※ ※ ※ ※");
printf("\n");
_sleep(3000);
exit(0);
}
//儲存商品資訊
int GoodsSaveToFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("goods_path", "wb")) == NULL)
{
sprintf(buff, "Can not open file:%s to create", "goods_path");
_sleep(2000);
return 0;
}
struct Good *goods_pointer = GOOD;//定位到連結串列頭部
while (goods_pointer->next != NULL)
{
goods_pointer = goods_pointer->next;
fwrite(goods_pointer, sizeof(struct Good), 1, fp);//一個個的儲存到檔案中去
}
fclose(fp);
return 1;
}
//儲存銷售資訊
int SaleSaveToFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("sale_path", "wb")) == NULL)
{
sprintf(buff, "Can not open file:%s to create", "sale_path");
_sleep(2000);
return 0;
}
struct Sale *sale_pointer = SALE;//定位到連結串列頭部
while (sale_pointer->next != NULL)
{
sale_pointer = sale_pointer->next;
fwrite(sale_pointer, sizeof(struct Sale), 1, fp);//一個個的儲存到檔案中去
}
fclose(fp);
return 1;
}
//讀取商品記錄
int GoodsReadFromFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("goods_path", "rb")) == NULL)
{
sprintf(buff, "Can not open file:%s to read", "goods_path");
return 1;
}
int number;
fseek(fp, 0, 2);//定位到檔案末尾
number = ftell(fp) / sizeof(struct Good);//求出總結點數
fseek(fp, 0, 0);//定位到檔案的開始處
int i;
struct Good *goods;
struct Good *goods_pointer = GOOD;
for (i = 0; i < number; i++)
{
goods = (struct Good *)malloc(sizeof(struct Good));
fread(goods, sizeof(struct Good), 1, fp);
goods->next = NULL;
while (goods_pointer->next != NULL)//定位到商品列表末尾
{
goods_pointer = goods_pointer->next;
}
goods_pointer->next = goods;//插入到連結串列當中
}
fclose(fp);//關閉檔案
return 0;
}
//讀取銷售記錄
int SaleReadFromFile()
{
FILE *fp;
char buff[512];
if ((fp = fopen("sale_path", "rb")) == NULL)
{
sprintf(buff, "Can not open file:%s to read", "sale_path");
return 1;
}
int number;
fseek(fp, 0, 2);//定位到檔案末尾
number = ftell(fp) / sizeof(struct Sale);//求出總結點數
fseek(fp, 0, 0);//定位到檔案的開始處
int i;
struct Sale *sale;
struct Sale *sale_pointer = SALE;
for (i = 0; i < number; i++)
{
sale = (struct Sale *)malloc(sizeof(struct Sale));
fread(sale, sizeof(struct Sale), 1, fp);
sale->next = NULL;
while (sale_pointer->next != NULL)//定位到銷售列表末尾
{
sale_pointer = sale_pointer->next;
}
sale_pointer->next = sale;//插入到連結串列當中
}
fclose(fp); //關閉檔案
return 0;
}