使用C語言連結串列實現商品管理系統
阿新 • • 發佈:2018-11-12
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define bool char
#define true 1
#define false 0
#define NUM 100
typedef struct GoodsInfo
{
char gid[20]; // 商品編號
char gname[20]; // 商品名稱
float price; // 商品價格
}Goods; // 結構體變數
typedef struct NodeList
{
Goods data;
struct NodeList *next;
}Node, *nodeList;
Node *head = NULL; // 頭節點
Node *last = NULL; // 尾節點
bool login(); // 使用者登入
void menu(); // 主選單
void setWindow(); // 介面設定
void addGood(); // 新增商品資訊
void init(); // 初始化
void view(); // 瀏覽資料
void search(); // 查詢
void update(); // 修改
void delGoods(); // 刪除
bool isEnd = false; // 是否退出系統
int main()
{
bool bl = false;
int count = 0; // 使用者記錄輸入輸入賬號和密碼的次數,最多允許輸入3次
do
{
//清除緩衝區的內容
fflush(stdin);
if (count == 3)
{
break; // 跳出while迴圈
}
if (count > 0)
{ // 說明不是第一次登陸
printf("\n\n\t賬號或密碼錯誤,請確認後重新登入...");
printf("\n\n\t您還有 %d 次機會<請按回車繼續>", 3-count);
getch();
}
count++;
bl = login(); // 呼叫登陸的方法,實現使用者登陸
}while( !bl );
// 問題:當用戶登陸成功也會跳出while,如果超過3次也會跳出while
if ( !bl )
{
printf("\n\n\t對不起,您暫無許可權...\n\t");
system("exit");
} else // 登陸成功
{
init(); // 初始化資料,即將資料從資料檔案讀取到系統中
fflush(stdin);
do{
//清除緩衝區的內容
menu(); // 顯示主選單
}while(!isEnd);
}
printf("\n\n\t");
system("exit");
}
// 初始化列表
void init()
{
head = last = (nodeList)malloc(sizeof(Node)); // 為頭結點分配空間
last -> next = NULL;
head -> next = NULL;
}
// 介面設定
void setWindow()
{
system("cls"); // 清屏
system("mode con:cols=80 lines=40"); //設定視窗大小
system("color 2f"); //設定視窗字型的顏色
}
// 使用者登入方法
bool login()
{
char name[20]; // 儲存使用者輸入的賬號
char pwd[20], ch; // 儲存使用者輸入的密碼
int index = 0; // 輸入密碼的位數
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************後臺登入介面************************");
printf("\n\n\t請輸入您的賬號:");
// 獲取使用者輸入的賬號
scanf("%s", name);
printf("\n\t請輸入您的密碼:");
// 獲取使用者輸入的密碼
while ( (ch=getch()) != 13 ) // 不是回車鍵
{
if ( ch ==8 ) // 如果是退格 Backspace
{
if(index <= 0)
{
index = 0;
}
else
{
printf("\b \b");
index --;
}
}
else
{
pwd[index++] = ch;
putch('*');
}
}
pwd[index] = '\0';
//scanf("%s", pwd); // 當用戶輸入密碼時,如何用掩碼的方式顯示 ***
// 比較使用者輸入的賬號和密碼是否正確。如果使用者輸入的賬號是yc 並且密碼是 123321,則認為是合法的使用者,那麼跳轉到主介面,否則提示錯誤。
if ( strcmp("yc",name) == 0 && strcmp("123", pwd) == 0)
{
return true;
} else
{
return false;
}
}
// 主選單
void menu()
{
int flag = 0 ;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************後臺管理介面************************");
printf("\n\t*************************1. 新增商品************************");
printf("\n\t*************************2. 查詢商品************************");
printf("\n\t*************************3. 瀏覽商品************************");
printf("\n\t*************************4. 修改商品************************");
printf("\n\t*************************5. 刪除商品************************");
printf("\n\t*************************6. 退出系統************************");
printf("\n\n\t請選擇您要進行的操作(1-6):");
scanf("%d",&flag);
switch(flag)
{
case 1: addGood(); break;
case 2: search(); break;
case 3: view(); break;
case 4: update(); break;
case 5: delGoods(); break;
case 6: isEnd=true; break;
default: break;
}
}
// 新增
void addGood()
{
//struct goodsInfo good; // 宣告結構體變數
Goods goods;
int result;
Node *node; // 建立一個新節點
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************商品資訊新增************************");
printf("\n\t******************** 請輸入以下資訊 *********************");
printf("\n\n\t請輸入商品編號:");
scanf("%s", goods.gid);
printf("\n\t請輸入商品名稱:");
scanf("%s", goods.gname);
printf("\n\t請輸入商品單價:");
scanf("%f", &goods.price);
result = MessageBox(NULL, "您確定要新增此商品資訊嗎?", "確認提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6) // 說明確定新增
{
node = (nodeList) malloc(sizeof(Node)); // 為新節點分配空間
node -> data = goods;
node -> next = NULL;
last -> next = node;
last = node;
last -> next = NULL;
//LENGTH++;
printf("\n\n\t商品資訊新增成功<請按回車鍵返回>\n\n\t");
}
getch();
}
// 瀏覽資料
void view()
{
Node *c;
c = head; // 從頭結點開始
int index = 0;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************瀏覽商品資訊************************");
printf("\n\n\t商品編號\t 商品名稱\t\t 商品價格");
while(c != NULL && c -> next != NULL)
{
c = c -> next;
index++;
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
}
printf("\n\n\t************************共 %d 條記錄************************", index);
printf("\n\n\t<請按回車鍵返回>\n");
getch();
}
void search()
{
Node *c;
c = head; // 從頭結點開始
int index = 0;
char gno[12];
bool isFind = false;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************查詢商品資訊************************");
printf("\n\n\t請輸入您要查詢的商品編號:");
scanf("%s", gno);
while(c != NULL && c -> next != NULL)
{
c = c -> next;
if (strcmp(c->data.gid,gno) == 0)
{
printf("\n\t商品編號\t 商品名稱\t\t 商品價格");
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
isFind = true;
break;
}
}
if (!isFind)
{
printf("\n\t對不起,暫無此商品資訊...");
}
printf("\n\n\t<請按回車鍵返回>");
getch();
}
// 修改
void update()
{
Node *c;
c = head; // 從頭結點開始
int index = 0, result = 0;
char gno[12], gname[20];
float price;
bool isFind = false;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************修改商品資訊************************");
printf("\n\n\t請輸入您要修改的商品編號:");
scanf("%s", gno);
while(c != NULL && c -> next != NULL)
{
c = c -> next;
if (strcmp(c->data.gid,gno) == 0)
{
printf("\n\t商品編號\t 商品名稱\t\t 商品價格");
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
printf("\n\n\t請輸入您要商品名稱:");
scanf("%s", gname);
printf("\n\t請輸入您要商品價格:");
scanf("%f", &price);
result = MessageBox(NULL, "您確定要修改此商品資訊嗎?", "確認提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6)
{
strcpy( c->data.gname, gname);
c->data.price = price;
printf("\n\n\t商品資訊修改成功...");
}
isFind = true;
break;
}
}
if (!isFind)
{
printf("\n\t對不起,暫無此商品資訊...");
}
printf("\n\n\t<請按回車鍵返回>");
getch();
}
// 刪除
void delGoods()
{
Node *c, *pre;
pre = c = head; // 從頭結點開始
int index = 0, result = 0;
char gno[12];
bool isFind = false;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************刪除商品資訊************************");
printf("\n\n\t請輸入您要刪除的商品編號:");
scanf("%s", gno);
while(c != NULL && c -> next != NULL)
{
c = c -> next;
if (strcmp(c->data.gid,gno) == 0)
{
printf("\n\t商品編號\t 商品名稱\t\t 商品價格");
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
result = MessageBox(NULL, "您確定要刪除此商品資訊嗎?", "確認提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6)
{
if (c -> next == NULL) // 說明是最有一個節點
{
last = pre;
}
pre -> next = c-> next;
free(c); // 釋放空間
printf("\n\n\t商品資訊刪除成功...");
}
isFind = true;
break;
}
pre = c;
}
if (!isFind)
{
printf("\n\t對不起,暫無此商品資訊...");
}
printf("\n\n\t<請按回車鍵返回>");
getch();
}
#include <stdlib.h>
#include <windows.h>
#define bool char
#define true 1
#define false 0
#define NUM 100
typedef struct GoodsInfo
{
char gid[20]; // 商品編號
char gname[20]; // 商品名稱
float price; // 商品價格
}Goods; // 結構體變數
typedef struct NodeList
{
Goods data;
struct NodeList *next;
}Node, *nodeList;
Node *head = NULL; // 頭節點
Node *last = NULL; // 尾節點
bool login(); // 使用者登入
void menu(); // 主選單
void setWindow(); // 介面設定
void addGood(); // 新增商品資訊
void init(); // 初始化
void view(); // 瀏覽資料
void search(); // 查詢
void update(); // 修改
void delGoods(); // 刪除
bool isEnd = false; // 是否退出系統
int main()
{
bool bl = false;
do
{
//清除緩衝區的內容
fflush(stdin);
if (count == 3)
{
break; // 跳出while迴圈
}
if (count > 0)
{ // 說明不是第一次登陸
printf("\n\n\t賬號或密碼錯誤,請確認後重新登入...");
printf("\n\n\t您還有 %d 次機會<請按回車繼續>", 3-count);
}
count++;
bl = login(); // 呼叫登陸的方法,實現使用者登陸
}while( !bl );
// 問題:當用戶登陸成功也會跳出while,如果超過3次也會跳出while
if ( !bl )
{
printf("\n\n\t對不起,您暫無許可權...\n\t");
system("exit");
} else // 登陸成功
{
init(); // 初始化資料,即將資料從資料檔案讀取到系統中
fflush(stdin);
//清除緩衝區的內容
menu(); // 顯示主選單
}while(!isEnd);
}
printf("\n\n\t");
system("exit");
}
// 初始化列表
void init()
{
head = last = (nodeList)malloc(sizeof(Node)); // 為頭結點分配空間
last -> next = NULL;
head -> next = NULL;
}
// 介面設定
void setWindow()
{
system("cls"); // 清屏
system("mode con:cols=80 lines=40"); //設定視窗大小
system("color 2f"); //設定視窗字型的顏色
}
// 使用者登入方法
bool login()
{
char name[20]; // 儲存使用者輸入的賬號
char pwd[20], ch; // 儲存使用者輸入的密碼
int index = 0; // 輸入密碼的位數
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************後臺登入介面************************");
printf("\n\n\t請輸入您的賬號:");
// 獲取使用者輸入的賬號
scanf("%s", name);
printf("\n\t請輸入您的密碼:");
// 獲取使用者輸入的密碼
while ( (ch=getch()) != 13 ) // 不是回車鍵
{
if ( ch ==8 ) // 如果是退格 Backspace
{
if(index <= 0)
{
index = 0;
}
else
{
printf("\b \b");
index --;
}
}
else
{
pwd[index++] = ch;
putch('*');
}
}
pwd[index] = '\0';
//scanf("%s", pwd); // 當用戶輸入密碼時,如何用掩碼的方式顯示 ***
// 比較使用者輸入的賬號和密碼是否正確。如果使用者輸入的賬號是yc 並且密碼是 123321,則認為是合法的使用者,那麼跳轉到主介面,否則提示錯誤。
if ( strcmp("yc",name) == 0 && strcmp("123", pwd) == 0)
{
return true;
} else
{
return false;
}
}
// 主選單
void menu()
{
int flag = 0 ;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************後臺管理介面************************");
printf("\n\t*************************1. 新增商品************************");
printf("\n\t*************************2. 查詢商品************************");
printf("\n\t*************************3. 瀏覽商品************************");
printf("\n\t*************************4. 修改商品************************");
printf("\n\t*************************5. 刪除商品************************");
printf("\n\t*************************6. 退出系統************************");
printf("\n\n\t請選擇您要進行的操作(1-6):");
scanf("%d",&flag);
switch(flag)
{
case 1: addGood(); break;
case 2: search(); break;
case 3: view(); break;
case 4: update(); break;
case 5: delGoods(); break;
case 6: isEnd=true; break;
default: break;
}
}
// 新增
void addGood()
{
//struct goodsInfo good; // 宣告結構體變數
Goods goods;
int result;
Node *node; // 建立一個新節點
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************商品資訊新增************************");
printf("\n\t******************** 請輸入以下資訊 *********************");
printf("\n\n\t請輸入商品編號:");
scanf("%s", goods.gid);
printf("\n\t請輸入商品名稱:");
scanf("%s", goods.gname);
printf("\n\t請輸入商品單價:");
scanf("%f", &goods.price);
result = MessageBox(NULL, "您確定要新增此商品資訊嗎?", "確認提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6) // 說明確定新增
{
node = (nodeList) malloc(sizeof(Node)); // 為新節點分配空間
node -> data = goods;
node -> next = NULL;
last -> next = node;
last = node;
last -> next = NULL;
//LENGTH++;
printf("\n\n\t商品資訊新增成功<請按回車鍵返回>\n\n\t");
}
getch();
}
// 瀏覽資料
void view()
{
Node *c;
c = head; // 從頭結點開始
int index = 0;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************瀏覽商品資訊************************");
printf("\n\n\t商品編號\t 商品名稱\t\t 商品價格");
while(c != NULL && c -> next != NULL)
{
c = c -> next;
index++;
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
}
printf("\n\n\t************************共 %d 條記錄************************", index);
printf("\n\n\t<請按回車鍵返回>\n");
getch();
}
void search()
{
Node *c;
c = head; // 從頭結點開始
int index = 0;
char gno[12];
bool isFind = false;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************查詢商品資訊************************");
printf("\n\n\t請輸入您要查詢的商品編號:");
scanf("%s", gno);
while(c != NULL && c -> next != NULL)
{
c = c -> next;
if (strcmp(c->data.gid,gno) == 0)
{
printf("\n\t商品編號\t 商品名稱\t\t 商品價格");
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
isFind = true;
break;
}
}
if (!isFind)
{
printf("\n\t對不起,暫無此商品資訊...");
}
printf("\n\n\t<請按回車鍵返回>");
getch();
}
// 修改
void update()
{
Node *c;
c = head; // 從頭結點開始
int index = 0, result = 0;
char gno[12], gname[20];
float price;
bool isFind = false;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************修改商品資訊************************");
printf("\n\n\t請輸入您要修改的商品編號:");
scanf("%s", gno);
while(c != NULL && c -> next != NULL)
{
c = c -> next;
if (strcmp(c->data.gid,gno) == 0)
{
printf("\n\t商品編號\t 商品名稱\t\t 商品價格");
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
printf("\n\n\t請輸入您要商品名稱:");
scanf("%s", gname);
printf("\n\t請輸入您要商品價格:");
scanf("%f", &price);
result = MessageBox(NULL, "您確定要修改此商品資訊嗎?", "確認提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6)
{
strcpy( c->data.gname, gname);
c->data.price = price;
printf("\n\n\t商品資訊修改成功...");
}
isFind = true;
break;
}
}
if (!isFind)
{
printf("\n\t對不起,暫無此商品資訊...");
}
printf("\n\n\t<請按回車鍵返回>");
getch();
}
// 刪除
void delGoods()
{
Node *c, *pre;
pre = c = head; // 從頭結點開始
int index = 0, result = 0;
char gno[12];
bool isFind = false;
setWindow();
printf("\n\t**********************商品資訊管理系統**********************");
printf("\n\t************************刪除商品資訊************************");
printf("\n\n\t請輸入您要刪除的商品編號:");
scanf("%s", gno);
while(c != NULL && c -> next != NULL)
{
c = c -> next;
if (strcmp(c->data.gid,gno) == 0)
{
printf("\n\t商品編號\t 商品名稱\t\t 商品價格");
printf("\n\t%-12s\t %-20s\t%.2f", c->data.gid, c->data.gname, c->data.price);
result = MessageBox(NULL, "您確定要刪除此商品資訊嗎?", "確認提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6)
{
if (c -> next == NULL) // 說明是最有一個節點
{
last = pre;
}
pre -> next = c-> next;
free(c); // 釋放空間
printf("\n\n\t商品資訊刪除成功...");
}
isFind = true;
break;
}
pre = c;
}
if (!isFind)
{
printf("\n\t對不起,暫無此商品資訊...");
}
printf("\n\n\t<請按回車鍵返回>");
getch();
}