借鉴夏同学要努力
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
//商品信息
typedef struct shop
{
char type[20];//种类
char ID[30];//名称
char number[10]; //位置编号
char saltTime[20];//销售时间
char locality[20];//产地
char date[10];//生产日期
int salt;//销量
char day[20];//保质期
float price;//价格
int amount;// 库存数量
struct shop *next; //指针域
}SHOP;
SHOP *head = NULL;
SHOP *p1 = NULL; //全局变量 ,头指针head、p1
int count;//全局计数点
//注册信息
typedef struct The_users
{
char id[11]; //账号
char pwd[20]; //密码
char name[15];//姓名为字符串
char sex; //性别为单个字符
long phone; //电话号码为长整型
}users;
/*--------------
函数声明
----------------*/
//初始化界面
void set();//管理员
void set1();//用户
//进度条
void progress();
//注册账户
void registers();
//登陆账户
void Input_login();
//界面(动画)
void welcome();//管理员
void welcome1();//用户
//菜单显示
void menu();//管理员
void menu1();//用户
//显示Windows当前时间
void Windowstime();
//找回密码
void reback();
//录入商品信息
void inputInformation();//链表
void inputDate(SHOP *p);//商品详细信息
//光标定位
void cursor(int x,int y);
//显示商品信息
void showShop();
void showShop1();//用户
//保存信息到文件
void saveInformation(); //保存文件 (w)
void saveInformation2(); //追加文件 (a+)
//读取文件信息
void readInformation();//管理员
void readInformation1();//客户
//统计信息
void infoShop();
//查询信息
void findInformation(); //管理员
void findInformation1();//用户
//修改信息
void amendInformation();
//删除信息
void delInformation();
//排序
void sortInformation();
//退出系统
void reout();
void reout1();//用户
//备份文件
//int copy();
/*--------------
函数实现
----------------*/
/*======
主函数
======*/
int main()
{
int shopChoose;//选择登陆方式
int flag = 1;//判断
while (flag == 1)
{
printf("\t\t\t\t\t ╭ % ╮ ╭ ```╮ \n");
printf("\t\t\t\t\t (@^o^@) 超市管理系统 (⌒:⌒)\n");
printf("\t\t\t\t\t (~):(~) (~)v(~) \n");
printf("%70s","\t\t┏┅┅┅┅┅┅┅┅┅┅┅┅┅☆\n");
printf("%66s","[ 1.管理员登陆 ]\n");
printf("%66s","[ 2.客户登陆 ]\n");
printf("%70s","\t\t┗┅┅┅┅┅┅┅┅┅┅┅┅┅┛\n");
printf("请选择:");
scanf("%d",&shopChoose);
switch(shopChoose)
{
case 1://管理员
system("pause");
system("cls");
set();
break;
case 2://客户
system("cls");
set1();
break;
default:
printf("输入错误,请重新输入\n");
system("pause");
system("cls");
break;
}
}
printf("\n");
system("pause");
return 0;
}
/*----------
初始化界面
(管理员)
-----------*/
void set()
{
int choose; //用户输入的选择
int ture = 1; //循环判断真假
while(ture != 0)
{
printf("\t\t\t\t\t ╭ % ╮ ╭ ```╮ \n");
printf("\t\t\t\t\t (@^o^@) 超市管理系统 (⌒:⌒)\n");
printf("\t\t\t\t\t (~):(~) (~)v(~) \n");
printf("%70s","\t\t┏┅┅┅┅┅┅┅┅┅┅┅┅┅☆\n");
printf("%66s","[ 1.注册账户 ]\n");
printf("%66s","[ 2.登陆账户 ]\n");
printf("%66s","[ 3.找回密码 ]\n");
printf("%70s","\t\t┗┅┅┅┅┅┅┅┅┅┅┅┅┅┛\n");
printf("请选择:");
scanf("%d",&choose);
switch(choose)
{
case 1:
getchar();
system("cls");
registers(); //注册账户
break;
case 2:
progress(); //进度条
system("cls");
Input_login(); //登录账户
getchar();
break;
case 3:
getchar();
reback(); //找回密码
break;
default:
getchar();
rewind(stdin); //清除键盘缓存
printf("输入错误,请重新输入!\n");
system("pause");
system("cls");
break;
}
}
}
/*--------------------------
创建储存用户账号密码的文件
(二进制)
--------------------------*/
void Create_File()
{
FILE *fp;
if ((fp = fopen("users.txt","rb"))==NULL) /*判断文件是否存在*/
{
if ((fp = fopen("users.txt","wb+"))==NULL) /*读写二进制*/
{
printf("无法建立文件!\n");
exit(0);
}
}
}
/*-------
注册账号
---------*/
void registers()
{
//按结构体的大小,一次写入或者读入1个结构体大小
users a,b; //结构体 The_users 重命名定义
FILE *fp; //定义头文件 fp
char temp[20];//存放密码,二次确认
int count = 0;
int flag = 1;
printf("欢迎来到注册界面!\n");
fflush(stdin);
Sleep(1000);
fp = fopen("users.txt","r");
fread(&b, sizeof(struct The_users), 1, fp); //读出数据存放的地址 b
printf("请输入账号\n");
scanf("%s",&a.id);
while (flag)
{
if (strcmp(a.id, b.id)) /*如果两串不相等*/
{
if (!feof(fp)) /*如果未到文件尾*/
{
fread(&b, sizeof(struct The_users), 1, fp);
}
else
break;
}
else
{
printf("此用户名已存在!请重新选择!\n");
getchar();
rewind(stdin);
Sleep(800);
system("cls");
return;
}
}
printf("请输入姓名:\n");
scanf("%s",&a.name);
printf("请输入性别(f(女)/m(男)):\n");
scanf(" %c",&a.sex);
do
{
if(a.sex != 'f' && a.sex != 'm')
{
printf("性别输入错误!请重新输入!\n");
getchar();
scanf("%c",&a.sex);
}
}
while(a.sex != 'f' && a.sex != 'm');
getchar();
printf("请输入电话号码:\n");
scanf("%ld",&a.phone);
printf("请输入密码\n");
scanf(" %s",&a.pwd);
printf("请确认密码\n");
scanf(" %s",&temp);
do{
if(!strcmp(a.pwd,temp))
{
fp = fopen("users.txt","a");//追加一个账号
fwrite(&a, sizeof(struct The_users), 1, fp);/* a 待写入文件的指针 (地址)*/
printf("账号注册成功,请登录。。。\n");
Sleep(500);
fclose(fp);
return;
}
else
{
printf("两次密码不匹配!请重新输入!\n");
getchar();
scanf("%s",&a.pwd);
printf("请确认密码\n");
scanf("%s",&temp);
}
}while(1);
return;
}
/*---------
登录系统
---------*/
void Input_login()
{
users a,b; //定义结构体The_users别名
FILE *fp; //文件
int i = 0;
system("cls");
system("color 06"); //背景为黑色,前景为黄色
printf("\n\n\n\n");
printf("\t\t\t\t\t\t欢迎来到登录界面!\n");
Sleep(1500);
system("cls");//清屏
Sleep(800);
fp = fopen("users.txt","r");
fread(&b, sizeof(struct The_users), 1, fp); //读入一个结构体字符块 写入b
printf("\n\n");
printf("\t\t\t\t │============================================│\n");
printf("\t\t\t\t │ 请输入账号: │\n");
printf("\t\t\t\t │============================================│\n");
cursor(50,3);
scanf("%s",&a.id);
while (1)
{
if (strcmp(a.id, b.id)==0) //如果有此用户名
{
break;
}
else
{
if (fp == NULL)
{
printf("还未拥有注册信息,请注册\n");
system("pause");
system("cls");
set();
}
if (!feof(fp)) //如果文件没有读完
{
fread(&b, sizeof(struct The_users), 1, fp);
/*读出数据 b 存放的地址位置,内存大小,每次读取一个数据块 1 ,以读的方式打开文件*/
}
else
{
printf("此用户名不存在,请重新选择!\n");
Sleep(500);
fclose(fp);
getchar();
return;
}
}
}
printf("\n\n\n");
printf("\t\t\t\t │============================================│\n");
printf("\t\t\t\t │ 请输入密码: │\n");
printf("\t\t\t\t │============================================│\n");
cursor(50,8);
while( (a.pwd[i]=getch()) != 13 )//getch()返回值是一个键值,回车的键值是13
{ //将输入的密码存储在数组中在屏幕上输出‘* ’代替数字
i++;
printf("*");//隐藏密码
}
a.pwd[i]='\0';//把最后的回车变成结束符(数组)
printf("\n");
do{
if (strcmp(a.pwd, b.pwd)==0) /*如果密码匹配*/
{
fclose(fp);
Sleep(500);
printf("\n\n\t\t\t\t\t\t登录进行中");
Sleep(500);
printf(".");
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".\n");
Sleep(1000);
fflush(stdin);
printf("\n\n\t\t\t\t\t\t登 录 成 功 !");
Sleep(1000);
system("cls");
welcome(); //动画菜单
return;
}
else
{
printf("密码错误!");
printf("\n\n\n");
printf("\t\t\t\t │============================================│\n");
printf("\t\t\t\t │ 请输入密码: │\n");
printf("\t\t\t\t │============================================│\n");
cursor(50,13);
scanf("%s",&a.pwd);
}
}
while(strcmp(a.pwd, b.pwd)==0);
}
/*---------
找回密码
----------*/
void reback() //按结构体的大小,一次写入或者读入1个结构体大小
{
users a,b;//结构体 The_users 重命名定义
FILE *fp;
char temp[20];
//int count = 0;
printf("欢迎来到找回密码界面!\n");
Sleep(1000);
fp = fopen("users.txt","r");
fread(&b, sizeof(struct The_users), 1, fp); //读入一个结构体字符块 写入b
system("cls");
system("color 06"); //背景为黑色,前景为黄色
printf("\n\n");
printf("\t\t\t\t │============================================│\n");
printf("\t\t\t\t │ 请输入账号: │\n");
printf("\t\t\t\t │============================================│\n");
cursor(50,3);
scanf("%s",&a.id);
while (1)
{
if (strcmp(a.id, b.id)==0) //如果有此用户名
{
break;
}
else
{
if (!feof(fp)) //如果文件没有读完
{
fread(&b, sizeof(struct The_users), 1, fp);
}
else
{
printf("此用户名不存在,请重新选择!\n");
Sleep(500);
fclose(fp);
getchar();
return;
}
}
}
printf("\n\n");
printf("请输入姓名:\n");
scanf("%s",&a.name);
do
{
if(strcmp(a.name,b.name))
{
printf("姓名输入错误!请重新输入!\n");
getchar();
scanf("%s",&a.name);
}
}
while(strcmp(a.name,b.name));
printf("请输入电话号码:\n");
scanf("%ld",&a.phone);
do{
if(a.phone != b.phone)
{
printf("电话号码输入错误!请重新输入!\n");
getchar();
scanf("%ld",&a.phone);
}
}
while(a.phone != b.phone);
printf("您的密码是:%s\n",b.pwd);
Sleep(3000);
system("pause");
system("cls");
}
/*-----------
欢迎界面(动画)
-------------*/
void welcome()
{
system("color F4");//变色、白底红字
Windowstime();//显示当前Windows时间
printf("\n\n");
printf("\n\n\n\t\t\t\t");
printf("欢");
Sleep(120);
printf(" 迎");
Sleep(120);
printf(" 使");
Sleep(120);
printf(" 用");
Sleep(120);
printf(" X");
Sleep(120);
printf(" X");
Sleep(120);
printf(" X");
Sleep(120);
printf(" 超");
Sleep(120);
printf(" 市");
Sleep(120);
printf(" 商");
Sleep(120);
printf(" 品");
Sleep(120);
printf(" 管");
Sleep(120);
printf(" 理");
Sleep(120);
printf(" 系");
Sleep(120);
printf(" 统!");
Sleep(800);
printf("\n");
system("cls");
menu(); //菜单
}
/*-------
进度条
-------*/
void progress()
{
int i;
int j;
system("cls");
printf("\n\n\n\n\n\t\t\t\t \033[0;33;40m 正 在 进 入 系 统,请 稍 后 . . . \033[0m");
const char* LastStr = "[--------------------------------------------------] 100%";
// 进度条标志,可以改用"*"或其它符号
const char ProgressIcon = '*';
// 进度每加5,进度条就会加一格,注意Step值要和LastStr中的"-"数量相匹配,两者相乘等于100
const int Step = 2;
// 总共需要多少进度标志,即LastStr中"-"的数量
const int IconMaxNum = 50;
printf("\n\n \t\t\t\t");
for ( i = 0; i <= 100; ++i)
{
// -------------- 打印进度条 --------------
printf("<");
int currentIndex = i / Step;
for ( j = 0; j < IconMaxNum; ++j)
{
if (j < currentIndex)
{
printf("\033[0;33;1m%c", ProgressIcon); // 打印进度条标志
}
else
{
printf(" "); // 未达进度则打印空格
}
}
printf("> ");
// -------- 打印数字进度 ----------
printf("\033[0;35;0m%3d%%", i);
fflush(stdout);
// 每隔100ms打印一次,这里同时每隔100ms会让进度加1
Sleep(3 * 10);
for ( j = 0; j < strlen(LastStr); ++j) //strlen 字符串处理函数
{
printf("\b"); // 回删字符,让数字和进度条原地变化
}
fflush(stdout); //清空输出缓存区
}
printf("\n\n");
return;
}
/*------------------
显示当前Windows时间
-------------------*/
void Windowstime()
{
SYSTEMTIME sys; //所需头文件windows.h,获取windows的时间函数
printf("\t\t\t\t\t 当前时间为:");
GetLocalTime( &sys ); //获取系统时间到程序的函数
printf( "%4d-%02d-%02d %02d:%02d \n",
sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,
sys.wSecond,sys.wDayOfWeek); //按照年/月/日 时/分/秒 星期几格式打印
printf("\n");
return;
}
/*--------
光标定位
--------*/
void cursor(int x, int y)
{
COORD pos; //COORD是Windows API中定义的一种结构
HANDLE h; //HANDLE:句柄
pos.X = x;
pos.Y = y;
h = GetStdHandle(STD_OUTPUT_HANDLE); //获得当前输出窗口的句柄
SetConsoleCursorPosition(h, pos);
}
/*---------
管理员菜单
----------*/
void menu()
{
int select=1;
while(select!=0)
{
printf("\n\n\n");
printf("\t\t\t\t\t ╭ ╮ ╭ ```╮ \n");
printf("\t\t\t\t\t (@^o^@) 超市商品管理系统 (⌒:⌒)\n");
printf("\t\t\t\t\t (~):(~) (~)v(~) \n");
printf("\n\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 超市商品管理系统 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 1. 录入商品信息 2. 显示商品信息 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 3.*保存商品信息 4. 读取商品信息 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 5. 统计商品信息 6. 查询商品信息 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 7. 修改商品信息 8. 删除商品信息 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t │ 9. 商品信息排序 0. 退出管理系统 │\n");
printf("\t\t\t\t │ │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
Windowstime(); //windows当前时间
printf("\t\t\t\t 请您选择(0-9):");
scanf("%d",&select);
switch(select)
{
case 1://录入信息
inputInformation();
break;
case 2://打印信息
showShop();
break;
case 3://保存信息到文件
//saveInformation();
break;
case 4://读取文件信息
readInformation();
break;
case 5://统计信息
infoShop();
break;
case 6://查询信息
findInformation();
break;
case 7://修改信息
amendInformation();
break;
case 8://删除
delInformation();
break;
case 9://排序
sortInformation();
break;
case 0://退出程序
getchar();
reout();
return;
default:
getchar();
rewind(stdin);//清除键盘缓存
printf("输入错误,请重新输入!\n");
system("pause");
break;
}
}
}
/*-----------
录入商品信息
-----------*/
void inputInformation()
{
SHOP *p2 = NULL;//结构体指针
int choose = 1;
int chooses = 1;//循环判断真假
while (choose == 1)
{
system("cls");
p2 = (SHOP *)malloc(sizeof(SHOP)); //为p2申请内存单元
inputDate(p2);//录入信息界面
if (head == NULL) //判断头结点
{
head = p1 = p2;
p2->next = NULL;
}
else
{
p1->next = p2;
p2->next = NULL;
p1 = p2;
}
system("cls");
printf("是否要继续录入商品信息 1.继续 2.退出\n");
scanf("%d", &choose);
if(choose != 1 && choose != 2)
{
printf("选项有误,请重新输入!\n");
printf("是否继续录入:1.继续 2.退出\n");
scanf("%d",&choose);
if(choose == 1)
{
continue;
}
else
{
printf("输入有误,退出录入\n");
system("pause");
break;
}
}
}
system("cls");
}
/*--------
商品信息
参数(p)
---------*/
void inputDate(SHOP *p)
{
printf("\n\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 超市商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 种类: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 名称: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 编号: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 价格: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 库存: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销量: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销售时间: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 产地: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 生产日期: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 保质期: │\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
cursor(44,7);
scanf("%s",p->type);//种类
cursor(44,9);
scanf("%s",p->ID);//名称
cursor(44,11);
scanf("%s",p->number);//编号
cursor(44,13);
fflush(stdin);
scanf("%f",&p->price);//价格
getchar();
cursor(44,15);
fflush(stdin);
scanf("%d",&p->amount);//库存
cursor(44,17);
scanf("%d",&p->salt);//销量
cursor(47,19);
scanf("%s",p->saltTime);//销售时间
cursor(44,21);
scanf("%s",p->locality);//产地
cursor(47,23);
scanf("%s",p->date);//生产日期
cursor(45,25);
scanf("%s",p->day);//保质期
count++;//节点个数 +1
}
/*----------
显示商品信息
(管理员)
传递参数 (p) 商品的相关信息
-----------*/
void showShop(SHOP *p)
{
if(head==NULL)
{
printf("目前尚未录入商品信息! 按任意键返回.\n");
getch();
system("pause");
system("cls");
//作用:按任意键程序继续向下执行
return ;
}
printf("商品信息如下:\n");
p = head;
printf("\t编号\t\t名称\t\t种类\t\t 价格\t\t库存\t\t销售日期\t销量\t产地\t生产日期\t保质期\n");
while (p != NULL)
{
printf(" %s %5s %12s %.2f %3d %8s %5d %5s %5s %5s\n",p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
//getchar();
//getch();//按任意键继续打印
p=p->next;
}
printf("\n");
printf("商品信息已全部打印完毕!");
printf("\n");
system("pause");
system("cls");
}
/*------------
保存信息到文件
(管理员)
{w}
-------------*/
void saveInformation()
{
/*读取文件信息之后进行操作,将文件数据覆盖保存*/
FILE *fp;//文件指针
SHOP *p;//相关信息指针
p = head;
fp=fopen("e:\\shopping.txt","w");//写入文件信息(覆盖原数据)
if(fp==NULL)//判断文件是否正常打开
{
printf("不能打开文件!");
exit(1);
}
while(p != NULL)//p2不为空时进行数据的录入
{
fprintf(fp,"%10s %10s %10s %lf %d %10s %d %10s %10s %10s\n",p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
p=p->next;
}
printf("文件保存成功!\n");
Sleep(800);
// printf("文件备份成功!\n");
// Sleep(800);
// printf("\t如需操作备份数据,请将备份数据复制到数据丢失的文件夹中再执行操作. . . \n");
system("pause");
system("cls");
exit(1);
if(fclose(fp))
{
printf("不能关闭文件!");
exit(1);
}
}
/*----------------
保存信息到文件
(未读取文件信息)
{a+}
-----------------*/
void saveInformation2()
{
/*未进行读取文件的操作,进行其他的操作时为防止文件数据丢失在用户退出时进行数据的追加保存*/
FILE *fp;//文件指针
SHOP *p;//相关信息指针
p = head;
fp=fopen("e:\\shopping.txt","a+");//程序结束追加信息
if(fp==NULL)//判断文件是否正常打开
{
printf("不能打开文件!");
exit(1);
}
while(p != NULL)//p2不为空时进行数据的录入
{
fprintf(fp,"%10s %10s %10s %lf %d %10s %d %10s %10s %10s\n",
p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
p=p->next;
}
// copy(); //拷贝文件数据
printf("文件数据追加成功!\n");
Sleep(800);
// printf("文件备份成功!\n");
//
// Sleep(800);
// printf("\t如需操作备份数据,请将备份数据复制到数据丢失的文件夹中再执行操作. . . \n");
system("pause");
system("cls");
exit(1);
if(fclose(fp))
{
printf("不能关闭文件!");
exit(1);
}
}
/*----------
读取文件信息
-----------*/
void readInformation()
{
/*
//读取(非链表)
FILE * fp;
char ch;
if((fp=fopen("e:\\shopping.txt","r+"))==NULL)
{
printf("Cannot open the file!\n");
return ;
}
ch=fgetc(fp);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp);
}
fclose(fp);
printf("\n读取成功...\n");
system("pause");
return ;
}
*/
FILE *fp; /*文件指针*/
SHOP *p; /*结构体指针*/
int i=0; //节点个数
head=(SHOP *)malloc(sizeof(SHOP)); //申请内存单元
head->next=NULL;
fp=fopen("e:\\shopping.txt","r");
if(fp == NULL)
{
printf("打开文件失败!");
exit(1);
}
if(fp!=NULL)
{
p1 = head;
while(!feof(fp))//feof(始终在某个文件末尾)系统看光标位置后边是否有数据来选择是否继续读取
{
p = (SHOP *)malloc(sizeof(SHOP));
p->next = NULL;
fscanf(fp,"%10s %10s %10s %f %d %10s %d %10s %10s %10s\n",p->number,p->ID,p->type,&p->price,&p->amount,p->saltTime,&p->salt,p->locality,p->date,p->day);
printf("%10s %10s %10s %f %d %10s %d %10s %10s %10s\n",p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
p1->next = p;
p1 = p;
i++; //赋值循环给节点 (自增)
if(i==1)
{
head=p1;
}
}
}
if(fclose(fp))
{
printf("关闭文件失败");
exit(1);
}
//return head;
printf("文件读取成功. . .");
printf("\n\n\n");
system("pause");
system("cls");
return;
}
/*----------
统计商品信息
----------*/
void infoShop()
{
SHOP *p; //结构体指针
int temp = 0;//做判断
p = head;/*头结点赋值给 p */
printf("\t统计进行中,请稍后");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".\n");
Sleep(1000);
fflush(stdin);
printf("\t结果如下:\n");
while (p != NULL)
{
/*库存小于10的商品信息*/
if(p->amount < 50)
{
printf("\n");
printf("销量:%d 编号:%s 名称:%7s 种类:%7s 价格:%.2f 库存:%d 销售日期:%7s 产地:%7s 生产日期:%7s 保质期:%s \n",
p->salt,p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->locality,p->date,p->day);
printf("\n\t该进货了,亲^_^\n");
temp = 1;
}
p = p->next;
}
if (temp == 0)
{
printf("没有库存小于50的商品. . .\n");
}
printf("\n");
system("pause");
system("cls");
}
/*-------
查询信息
(管理)
-------*/
void findInformation()
{
SHOP *p2; //结构体
int opt_c; //用户输入选项
int ture = 1;
int flag = 0; //判断真假
char s_ID[10]; //用户输入要查询的商品名称
char s_number[10]; //用户输入要查询的商品编号
p2=head;//p2赋值为头结点
while(ture == 1)
{
printf("\n");
printf("\t┌-^-^-^-^-^-^-^-^-┐\n");;
printf("\t |1、按照编号查询 |\n");
printf("\t |2、按照名称查询 |\n");
printf("\t |3、退出查询 |\n");
printf("\t└ V-V-V-V-V-V-V-V-┘\n");
printf("请选择:\n");
fflush(stdin);//清空键盘缓存
scanf("%s",&opt_c);
switch (opt_c)
{
case '1'://编号查询
system("pause");
system("cls"); //暂停和清屏
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入编号: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(50,3);
scanf("%s",&s_number);
flag = 0;
while(p2 != NULL)
{
//flag = 1;
if(strcmp(s_number,p2->number) == 0)
{
flag = 1;
printf("\n\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 查询商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 种类:%s │\n",p2->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 名称:%s │\n",p2->ID);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 价格:%.2f \t│\n",p2->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 库存:%d │\n",p2->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销量:%d │\n",p2->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销售时间:%s │\n",p2->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 产地:%s │\n",p2->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 生产日期: %s │\n",p2->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 保质期:%s \t│\n",p2->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
}
p2=p2->next;
}
if(flag == 0)
{
printf("查无此商品. . .");
printf("\n");
break;
}
system("pause");
system("cls");
break;
case '2'://名称查询
system("pause");
system("cls");
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入名称: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(50,3);
scanf("%s",&s_ID);
flag = 0;
while(p2 != NULL)
{
//flag = 1;
if(strcmp(s_ID,p2->ID) == 0 )
{
flag = 1;
printf("\n\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 查询商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 编号:%s │\n",p2->number);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 种类:%s │\n",p2->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 价格:%.2f \t│\n",p2->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 库存:%d │\n",p2->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销量:%d │\n",p2->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销售时间:%s │\n",p2->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 产地:%s │\n",p2->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 生产日期: %s │\n",p2->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 保质期:%s │\n",p2->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
}
p2=p2->next;
}
if(flag == 0)
{
printf("查无此商品. . .");
printf("\n");
system("pause");
system("cls");
break;
}
system("pause");
system("cls");
break;
case '3'://退出
printf("感谢使用!\n");
return;
default:
getchar();
printf("输入错误,请重新输入:\n");
system("pause");
break;
}
}
}
/*-------
修改信息
-------*/
void amendInformation()
{
SHOP *p2; //初始化结构体指针p2
int opt_c; //用户输入选项
//int choose;
int flag; //判断真假
char s_ID[10]; //按照名称
char s_number[10]; //按照编号
int content; //用户输入选项
int content1; //用户输入选项
p2=head;
printf("\n");
printf("\t┌-^-^-^-^-^-^-^-^-┐\n");;
printf("\t |1、编号查询修改 |\n");
printf("\t |2、名称查询修改 |\n");
printf("\t |3、退出修改 |\n");
printf("\t└ V-V-V-V-V-V-V-V-┘\n");
//rewind(stdin);
printf("请选择:\n");
scanf("%d",&opt_c);
while(opt_c != 3)
{
if(opt_c == 1)//编号查询
{
system("pause");
system("cls");
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入商品编号: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(54,3);
scanf("%s",&s_number);
flag = 0;
while(p2 != NULL)
{
if(strcmp(s_number,p2->number) == 0)
{
flag = 1;
printf("\n\n");
//p2 = head;
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 修改商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 1.种类:%s │\n",p2->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 2.名称:%s │\n",p2->ID);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 3.价格:%.2f │\n",p2->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 4.库存:%d │\n",p2->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 5.销量:%d │\n",p2->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 6.销售日期:%s │\n",p2->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 7.产地:%s │\n",p2->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 8.生产日期:%s │\n",p2->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 9.保质期:%s │\n",p2->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
fflush(stdin);
printf("请输入要修改的内容选项:\n");
scanf("%d",&content);
switch (content)
{
case 1:
printf("请输入商品种类:\n");
scanf("%s",p2->type);
break;
case 2:
printf("请输入商品名称:\n");
scanf("%s",p2->ID);
break;
case 3:
printf("请输入商品价格:\n");
scanf("%f",&p2->price);
break;
case 4:
printf("请输入商品库存:\n");
scanf("%d",&p2->amount);
break;
case 5:
printf("请输入商品销量:\n");
scanf("%d",&p2->salt);
break;
case 6:
printf("请输入商品销售日期:\n");
scanf("%s",p2->saltTime);
break;
case 7:
printf("请输入商品产地:\n");
scanf("%s",p2->locality);
break;
case 8:
printf("请输入商品生产日期:\n");
scanf("%s",p2->date);
break;
case 9:
printf("请输入商品保质期:\n");
scanf("%s",p2->day);
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
p2 = p2->next;
}
if(flag == 0)
{
printf("无该商品相关信息. . .");
printf("\n");
break;
}
printf("修改进行中");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".\n");
Sleep(1000);
fflush(stdin);
printf("修改成功!\n");
Sleep(500);
printf("请返回打印查看. . . \n");
return menu();
}
else if(opt_c == 2 )//名称查询修改
{
system("pause");
system("cls");
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入名称: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(50,3);
scanf("%s",&s_ID);
flag = 0;
while(p2 != NULL)
{
if(strcmp(s_ID,p2->ID) == 0)
{
flag = 1;
printf("\n\n");
p2 = head;
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 修改商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 1.编号:%s │\n",p2->number);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 2.种类:%s │\n",p2->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 3.价格:%.2f \t│\n",p2->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 4.库存:%d │\n",p2->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 5.销量:%d │\n",p2->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 6.销售时间:%s │\n",p2->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 7.产地:%s │\n",p2->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 8.生产日期:%s │\n",p2->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 9.保质期:%s │\n",p2->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
fflush(stdin);
printf("请输入要修改的内容选项:\n");
scanf("%d",&content1);
switch (content1)
{
case 1:
printf("请输入商品编号:\n");
scanf("%s",p1->number);
break;
case 2:
printf("请输入商品种类:\n");
scanf("%s",p1->type);
break;
case 3:
printf("请输入商品价格:\n");
scanf("%f",&p2->price);
break;
case 4:
printf("请输入商品库存:\n");
scanf("%d",&p1->amount);
break;
case 5:
printf("请输入商品销量:\n");
scanf("%d",&p1->salt);
break;
case 6:
printf("请输入商品销售日期:\n");
scanf("%s",p1->saltTime);
break;
case 7:
printf("请输入商品产地:\n");
scanf("%s",p1->locality);
break;
case 8:
printf("请输入商品生产日期:\n");
scanf("%s",p1->date);
break;
case 9:
printf("请输入商品保质期:\n");
scanf("%s",p1->day);
break;
default:
printf("输入错误,请重新输入:\n");
system("pause");
break;
}
}
p2 = p2->next;
}
if(flag == 0)
{
printf("无该商品相关信息. . .");
printf("\n");
break;
}
printf("修改进行中");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".\n");
Sleep(1000);
fflush(stdin);
printf("修改成功!\n");
printf("请返回打印查看. . . \n");
return menu();
}
else if(opt_c == 3 )
{
printf("退出修改!\n");
return;
}
else
{
printf("输入错误、已退出!\n");
system("pause");
return;
}
}
}
/*--------
删除信息
--------*/
void delInformation()
{
SHOP *p2;//判断的当前节点
SHOP *pb;//要删除的节点数据
SHOP *pf;//要删除的节点的前趋节点
p2 = head;
pf = head;
pb = head;
//int count;//计数节点
int ture;
int flag;//判断真假
int option;//用户输入的选项
int s_option;//判断选项
char S_id[20];//临时存放名称
char s_num[10];//临时存放编号
printf("\n");
printf("\t┌-^-^-^-^-^-^-^-^-┐\n");;
printf("\t |1、查询编号删除 |\n");
printf("\t |2、查询名称删除 |\n");
printf("\t |3、退出删除 |\n");
printf("\t└ V-V-V-V-V-V-V-V-┘\n");
rewind(stdin);
printf("请选择:");
scanf("%s",&option);
switch(option)
{
case '1'://编号删除
system("pause");
system("cls");
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入商品编号: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(54,3);
scanf("%s",&s_num);
while(pb != NULL )
{
if(strcmp(s_num,pb->number) == 0)
{
flag = 1;
printf("\n\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 1.种类:%s │\n",pb->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 2.名称:%s │\n",pb->ID);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 3.价格:%.2f │\n",pb->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 4.库存:%d │\n",pb->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 5.销量:%d │\n",pb->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 6.销售日期:%s │\n",pb->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 7.产地:%s │\n",pb->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 8.生产日期:%s │\n",pb->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 9.保质期:%s │\n",pb->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("是否选择删除?\n");
printf("1--yse 0--no\n");
printf("请选择:");
scanf("%d",&s_option);
if(s_option == 1)
{
if(pb == head)//判断pb是否指向头结点
{
head=head->next;
}
else
{
pf->next=pb->next;//让pf的下一个节点等于pb的下一个节点
}
printf("删除进行中");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".\n");
Sleep(1000);
fflush(stdin);
printf("数据删除成功!");
saveInformation1();//更新文件信息
}
else if(s_option == 0)
{
printf("取消删除!");
}
}
pf=pb;
pb=pb->next;
}
if(flag == 0)
{
printf("无该商品相关信息. . .");
printf("\n");
}
break;
case '2':
system("pause");
system("cls");
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入商品名称: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(54,3);
scanf("%s",&S_id);
while(pb != NULL )
{
if(strcmp(S_id,pb->ID) == 0)
{
flag = 1;
printf("\n\n");
//p2 = head;
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 1.编号:%s │\n",pb->number);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 2.种类:%s │\n",pb->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 3.价格:%.2f │\n",pb->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 4.库存:%d │\n",pb->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 5.销量:%d │\n",pb->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 6.销售日期:%s │\n",pb->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 7.产地:%s │\n",pb->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 8.生产日期:%s │\n",pb->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 9.保质期:%s │\n",pb->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
printf("是否选择删除?\n");
printf("1--yse 0--no\n");
printf("请选择:");
scanf("%d",&s_option);
if(s_option == 1)
{
if(pb == head)//判断pb是否指向头结点
{
head=head->next;
}
else
{
pf->next=pb->next;//让pf的下一个节点等于pb的下一个节点
}
printf("删除进行中");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".");
Sleep(500);
printf(".\n");
Sleep(1000);
fflush(stdin);
printf("数据删除成功!");
saveInformation1();//更新文件信息
}
else if(s_option == 0)
{
printf("取消删除!");
}
else
{
printf("输入错误,请重新输入\n");
continue;
}
}
pf=pb;
pb=pb->next;
}
if(flag == 0)
{
printf("无该商品相关信息. . .");
printf("\n");
}
break;
case '3': //退出删除
printf("感谢使用!\n");
break;
default:
printf("输入错误,已退出!\n");
system("pause");
break;
}
}
/*-------
排序
-------*/
void sortInformation()
{
char choise; //用户选项
SHOP *pb;
SHOP *pf;
SHOP temp; //临时存放数据地址
SHOP *p2; //定义临时节点指针变量
p2 = head;
while (choise != '3')
{
printf("\n");
printf("\t┌-^-^-^-^-^-^-^-^-┐\n");;
printf("\t |1、依据库存排序 |\n");
printf("\t |2、依据价格排序 |\n");
printf("\t |3、退出 |\n");
printf("\t└ V-V-V-V-V-V-V-V-┘\n");
printf("请选择:");
scanf("%s",&choise);
//库存数量进行排序
if (choise == '1')
{
p2=head;
if(p2 == NULL)
{
//链表为空
printf("无相关数据. . .\n");
return ;
}
if(p2->next == NULL)
{
//链表有1个节点
printf("数据仅存在一条\n");
return ;
}
while(p2->next != NULL) //p2的下一个节点不为空
{
pb = p2->next; //将该节点数据赋值给pb
while(pb != NULL) //pb不为空
{
//从小到大排序
//冒泡排序
if(p2->amount > pb->amount)//pb与p2进行比较
{
//交换数据
temp = *p2; //取amount的值进行交换 放在临时空间
*p2 = *pb; //pb的数据给p2
*pb = temp; //临时存放的数据给pb
temp.next = p2->next;
p2->next = pb->next;
pb->next = temp.next;
}
pb = pb->next;
}
p2 = p2->next;
}
printf("排序成功!请返回打印查看. . .\n");
}
//价格进行排序
else if(choise == '2')
{
p2=head;
if(p2 == NULL)
{
//链表为空
printf("无相关数据\n");
return ;
}
if(p2->next == NULL)
{
//链表有1个节点
printf("数据仅存在一条\n");
return ;
}
while(p2->next != NULL)//以p2指向的节点为基准节点
{
pb = p2->next;//pb从基准点的下一个节点开始
while(pb != NULL)
{
//从小到大排序
//冒泡排序
if(p2->price > pb->price)
{
temp = *p2;
*p2 = *pb;
*pb = temp;
temp.next = p2->next;
p2->next = pb->next;
pb->next = temp.next;
}
pb = pb->next;
}
p2 = p2->next;
}
printf("排序成功!请返回打印查看. . .\n");
}
//退出
else if(choise == '3')
{
break;
//return 1;
}
else
{
printf("输入错误请重新输入!\n");
system("pause");
}
}
}
/*-------
退出系统
-------*/
void reout()
{
char reoutback; //定义选项选择变量
int flag = 1; //判断
while(flag)
{
printf("\n\n\n");
printf("\t\t\t 退出时系统会在保存的文件末尾进行文件数据的追加,如若读取了文件数据,请选择覆盖原数据退出");
printf("\n\n\n\n");
printf("确定要退出?(1.追加数据文件并退出(未读取数据) 2.覆盖原数据并退出(已读取数据))\n");
scanf("%d",&reoutback);
if(reoutback == 1)
{
printf("感谢使用!\n"); //退出系统
Sleep(1000);
saveInformation2(); //追加信息到文件
exit(0);
}
else if(reoutback == 2)
{
saveInformation();
}
else if(reoutback != 2)
{
printf("\n菜单选项有误,请重新输入\n");
}
}
}
void reout1()
{
char reoutback; //定义选项选择变量
int flag = 1; //判断
while(flag)
{
printf("\n\n\n");
printf("确定要退出?(1.是 2.否)\n");
scanf("%d",&reoutback);
if(reoutback == 1)
{
printf("感谢使用!\n"); //退出系统
Sleep(1000);
exit(0);
}
else if(reoutback == 2)
{
break;
}
else if(reoutback != 2)
{
printf("\n菜单选项有误,请重新输入\n");
}
}
}
/*===================================================
用户模块
==================================================*/
/*----------
初始化界面
(用户)
-----------*/
void set1()
{
system("color F4");//变色、白底红字
welcome1();
}
/*------------
用户欢迎界面
-------------*/
void welcome1()
{
//显示当前Windows时间
Windowstime();
printf("\n\n");
printf("\n\n\n\t\t\t\t\t\t");
printf(" 欢");
Sleep(120);
printf(" 迎");
Sleep(120);
printf(" 光");
Sleep(120);
printf(" 临");
Sleep(120);
printf(" 本");
Sleep(120);
printf(" 管");
Sleep(120);
printf(" 理");
Sleep(120);
printf(" 平");
Sleep(120);
printf(" 台");
Sleep(120);
printf(" !");
Sleep(1000);
printf("\n");
system("cls");
menu1(); //用户菜单
}
/*--------
用户菜单
--------*/
void menu1()
{
int choosen;//用户输入的选择
int selects = 1;//循环判断
while(selects!=0)
{
printf("\n\n");
printf("\t\t\t┃ *********************************************************** ┃\n");
printf("\t\t\t┃ * * * ┃\n");
printf("\t\t\t┃ * 1.读取信息 * 2.查询(打印)商品 * ┃\n");
printf("\t\t\t┃ * * * ┃\n");
printf("\t\t\t┃ *********************************************************** ┃\n");
printf("\t\t\t┃ * * * ┃\n");
printf("\t\t\t┃ * 3.排序商品 * 4.退出系统 * ┃\n");
printf("\t\t\t┃ * * * ┃\n");
printf("\t\t\t┃ *********************************************************** ┃\n");
Windowstime();
printf("\t\t\t\t请选择:");
scanf("%d",&choosen);
switch (choosen)
{
case 1://读取文件信息
readInformation1();
getchar();
break;
case 2://查询
findInformation1();
break;
case 3://排序
sortInformation();
break;
case 4://退出
reout1();
break;
default:
printf("输入错误,请重新输入!");
scanf("%d",&choosen);
break;
}
}
}
/*------------
读取文件信息
(用户)
------------*/
void readInformation1()
{
FILE *fp;//文件指针
SHOP *p;//结构体指针初始化
int i=0;//节点个数
head=(SHOP *)malloc(sizeof(SHOP));//申请内存单元
head->next=NULL;
fp=fopen("e:\\shopping.txt","r");
if(fp == NULL)
{
printf("打开文件失败!");
exit(1);
}
if(fp!=NULL)
{
p1 = head;
while(!feof(fp))//feof(始终在某个文件末尾)系统看光标位置后边是否有数据来选择是否继续读取
{
p = (SHOP *)malloc(sizeof(SHOP));
p->next = NULL;
fscanf(fp,"%10s %10s %10s %f %d %10s %d %10s %10s %10s\n",p->number,p->ID,p->type,&p->price,&p->amount,p->saltTime,&p->salt,p->locality,p->date,p->day);
printf("%10s %10s %10s %f %d %10s %d %10s %10s %10s\n",p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
p1->next = p;
p1 = p;
i++; //赋值循环给节点 (自增)
if(i==1)
{
head=p1;
}
}
}
if(fclose(fp))
{
printf("关闭文件失败");
exit(1);
}
//return head;
printf("文件读取成功. . .");
printf("\n");
printf("如需查看详细信息,请返回查询查看. . . ");
printf("\n");
system("pause");
system("cls");
return;
}
/*-------
查询信息
(用户)
-------*/
void findInformation1()
{
SHOP *p2; //结构体指针
int opt_c; //用户输入选项
int flag = 0; //判断
char s_ID[10]; //用户输入要查询的商品名称
char s_number[10]; //用户输入要查询的商品编号
p2=head;
printf("\n");
printf("\t┌-^-^-^-^-^-^-^-^-^-^-- \n");
printf("\t |1、显示全部商品信息 |\n");
printf("\t |2、按照名称查询 |\n");
printf("\t |3、退出查询 |\n");
printf("\t└ V-V-V-V-V-V-V-V-V-V-┘\n");
printf("请选择:\n");
scanf("%d",&opt_c);
switch (opt_c)
{
case 1: //全部商品信息
showShop1(); //商品信息
system("pause");
system("cls");
break;
case 2://名称查询
system("pause");
system("cls");
p2=head;
printf("\n\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 请输入名称: │\n");
printf("\t\t\t\t │========================================== │\n");
cursor(50,3);
scanf("%s",&s_ID);
flag = 0;
while(p2 != NULL)
{
//flag = 1;
if(strcmp(s_ID,p2->ID) == 0 )
{
flag = 1;
printf("\n\n");
printf("\t\t\t\t ╭═════════════════════════════════○●○●═══╮\n");
printf("\t\t\t\t │ 查询商品信息 │\n");
printf("\t\t\t\t ╰═══○●○●═════════════════════════════════╯\n");
printf("\t\t\t\t ┌───────────────────────────────────────────-┐\n");
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 编号:%s │\n",p2->number);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 种类:%s │\n",p2->type);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 价格:%.2f \t│\n",p2->price);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 库存:%d │\n",p2->amount);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销量:%d │\n",p2->salt);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 销售时间:%s │\n",p2->saltTime);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 产地:%s │\n",p2->locality);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 生产日期: %s │\n",p2->date);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t │ 保质期:%s │\n",p2->day);
printf("\t\t\t\t │========================================== │\n");
printf("\t\t\t\t └────────────────────────────────────────────┘\n");
}
p2=p2->next;
}
if(flag == 0)
{
printf("查无此商品. . .");
printf("\n");
break;
}
system("pause");
system("cls");
break;
default:
printf("输入错误,请重新输入:");
break;
}
}
/*------------
显示商品信息
(用户)
-----------*/
void showShop1()
{
SHOP *p; //结构体变量初始化
//FILE *fp;
//SHOP *p1 = NULL;
if(head==NULL)
{
printf("目前尚未录入商品信息! 按任意键返回.\n");
getch();
system("pause");
system("cls");
//作用:按任意键程序继续向下执行
return ;
}
printf("商品信息如下:\n");
p = head;
printf("\t编号\t\t名称\t\t种类\t\t价格\t\t库存\t\t销售日期\t销量\t产地\t生产日期\t保质期\n");
while (p != NULL)
{
printf(" %s %s %12s %.2f %d %s %d %s %s %s\n",
p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
//getch();//按任意键继续打印
getchar();
p=p->next;
}
printf("\n");
printf("商品信息已全部打印完毕!");
printf("\n");
system("pause");
system("cls");
}
/*--------------
保存文件
(更新文件)
----------------*/
int saveInformation1()
{
FILE *fp;//文件指针
SHOP *p;//商品信息指针
p = head;//p赋值为头结点
fp=fopen("e:\\shopping.txt","w");//写入文件信息
if(fp==NULL)//判断文件是否正常打开
{
printf("不能打开文件!");
exit(1);
}
while(p != NULL)//p2不为空时进行数据的录入
{
fprintf(fp,"%10s %10s %10s %.2f %d %10s %d %10s %10s %10s\n",
p->number,p->ID,p->type,p->price,p->amount,p->saltTime,p->salt,p->locality,p->date,p->day);
p=p->next;
}
Sleep(800);
printf("\n");
printf("文件数据更新成功!\n");
getch();
system("pause");
system("cls");
//break;
if(fclose(fp))
{
printf("不能关闭文件!");
exit(1);
}
}
/*--------------
拷贝文件
(备份数据)
----------------*/
//
//int copy()
//{
//
// char ch;//待 写入文件的字符
//
// FILE* fpr = fopen("e:\\shopping.txt", "r");
// FILE* fpw = fopen("e:\\copyshop.txt", "w");
//
// if (NULL == fpw)
// {
// //perror("open file test2.txt");//将函数的错误信息一起打印出来、eg: No such file or directory
// printf("不能打开文件\n");
//
// }
//
// if (NULL == fpr)
// {
// //perror("open file test1.txt");
// printf("不能打开文件!\n");
//
// }
//
//
//
// /*不断的从源文件中读取字符并写入目的文件中,直到遇到EOF结束这个过程
// 字符变量=fgetc(文件指针)
// */
//
// while ( ( ch=fgetc(fpr) ) != EOF) //将shopping文件夹信息赋值给ch,EOF是文件结束标志
// {
// fputc(ch,fpw);//将ch写入pfw,也就是 copyshop.txt 文件夹
// }
//
//
//
// //关闭流,使用完流后记得关闭,避免占用资源
// fclose(fpr);
// fclose(fpw);
//
// fpr = NULL;
// fpw = NULL;
//
// return 0;
//
//}