使用C语言链表实现商品管理系统
#include <stdio.h>#include <stdlib.h>#include <windows.h>#define bool char#define true 1#define false 0#define NUM 100typedef struct GoodsInfo{ char gid[20
·
#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;
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();
}
更多推荐
已为社区贡献1条内容
所有评论(0)