使用C語言鏈表實現商品管理系統

#include
#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();
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章