大一暑假实训项目,主要对文件内容的增删改查,链表的基本应用。
编程小白,会的不多,只写了一些基本功能,函数大多是不带返回值的,避免错乱,简洁一些。
不过代码的健壮性还是不错滴。
代补功能:找回密码,各种信息独立修改。闲麻烦就没有打了OAO。
展示部分代码,如有错漏还望指正。
目录
文件存储格式
管理员 Administrator
依次:账号>密码>姓名>性别>电话号码
超级管理员 SuperAdministrator
依次:账号>密码,只有一个超级管理员。
商品 MobilePhone
依次:编号>品牌>价格>折扣>型号
用户 Registering
依次:账号>密码>姓名>性别>电话号码>余额
代码
提示注释和头文件及宏定义
// fflush(stdin);
// 清空输入缓冲区
// while((ch = getchar()) != '\n' && ch != EOF);
// 逐个读字符,遇到回车或者文本结尾为止
// 用于清空缓冲区
// strspn(char * str_1, char * str_2)
// str_1 中的字符在 str_2 中, 则加 1
// 遇到第一个不在 str_2 中的字符, 则停止比较
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h> //便于生成随机数
#define SIZE 8 //定义数组大小
#define ch_1 "龖" //用于生成仿二维码
#define ch_2 " " //二维码空白处
#define str "0123456789abcdefghijklmnopqrstuvwxyz" //字母和数字
结构体
typedef struct superAdministrator //超级管理员的结构体
{
int account; //超级管理员的账户 int 6位数
char password[SIZE*2]; //超级管理员的密码 int 9-15位数
}SuperAdministrator;
typedef struct administrtor //管理员的结构体
{
int account; //管理员账号 6位
char password[SIZE*2]; //密码 9-15位
char name[SIZE]; //姓名
char gender[SIZE]; //性别
char telNum[SIZE*2]; //电话号码 11位
struct administrtor * next;
}Administrator;
typedef struct mobile_phone //手机的结构体
{
int ID; //编号 6位
char type[SIZE]; //品牌 8位以内
int price; //价格
double discount; //折扣
char name[SIZE*2]; //型号 小于16位
struct mobile_phone * next;
}MobilePhone;
typedef struct customer //顾客的结构体
{
int account; //顾客账号 6位
char password[SIZE*2]; //密码 9-15位
char name[SIZE]; //姓名
char gender[SIZE]; //性别
char telNum[SIZE*2]; //电话号码 11位
int balance; //用户余额
struct customer * next;
}customer;
函数原型
一共21个。
void Initial_super_administrator(); //初始化超级管理员
void loginSuperAdmin(); //超级管理员入口
void loginAdmin(); //管理员入口
void loginUser(); //用户入口
void createAdministrator(); //注册管理员
void removeAdministrator(); //注销管理员
void reviseAdministratorPassword(); //修改管理员密码
void SearrchCt(); //查询顾客
void changeSuAdminPassword(); //修改超级管理员密码
void relevant_Product(); //商品相关函数
void showInfo(); //展示所有商品信息
void addProduct(); //添加商品
void deleteProduct(); //删除商品
void reviseProduct(); //修改商品信息
void merchandiseDiscount(); //商品折扣
void input_error(); //输入错误显示
void file_open_error(FILE * fp); //文件打开失败
void malloc_defeat(Administrator * A); //动态内存申请失败
void admi_Destrong_List(Administrator * L); //摧毁链表管理员
void prod_Destrong_List(MobilePhone * L); //摧毁链表商品
void Payment_QR_code(); //支付二维码
主函数
int main() //主函数
{
Initial_super_administrator(); //初始化超级管理员
while(1)
{
char sel; //选择
char ch; //用于清空缓冲区
printf("||------------------------------------------||\n");
printf("|| Welcome to Phone product system. ||\n");
printf("||------------------------------------------||\n");
printf("|| 【1】超级管理员登录 ||\n");
printf("|| 【2】管理员登录 ||\n");
printf("|| 【0】退出 ||\n");
printf("||------------------------------------------||\n");
printf("|| 请选择: ");
while((sel = getchar()) == '\n'); //不管输入什么都只读取第一个字符(除换行)
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区(清空第一个字符后的每一个字符)
system("cls"); //清屏
switch(sel)
{
case '1':
loginSuperAdmin(); //超级管理员入口
break;
case '2':
loginAdmin(); //管理员入口
break;
case '0':
printf("||------------------------------------------||\n");
printf("|| 系 统 已 关 闭 ||\n");
printf("||------------------------------------------||\n");
exit(0);
default:
input_error(); //输入错误显示
}
}
return 0;
}
初始化超级管理员密码
主要是怕在某次测试中把密码忘了,每次都初始化,方便一些。不要也没关系。
void Initial_super_administrator() //每次打开程序都会重新初始化超级管理员账号密码
{
FILE * fp;
const int account = 123456; //常量避免值被修改
const int password = 123456789;
fp = fopen("SuperAdministrator.txt", "w"); //只写,无文件就创建文件
file_open_error(fp); //文件是否打开失败
fprintf(fp, "%d\t%d\t", account, password);
fclose(fp); //关闭文件
}
超级管理员入口
void loginSuperAdmin() //超级管理员入口
{
char ch, sel;
SuperAdministrator sa_1; //超级管理员账号
FILE * fp; //文件指针
int account; //记录账号
char password[SIZE*2]; //记录密码
int len_1, len_2; //记录密码个数, 记录密码合法字符个数
while(1)
{
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消登录, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 超级管理员账号:");
scanf("%d", &account);
system("cls"); //清屏
if(account == 0) //输入 0 返回上一级
{
return;
}
if(account<=100000 || account>999999) //不是六位的数字
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 账号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消登录, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 超级管理员密码:");
scanf("%s", password);
system("cls"); //清屏
if(strcmp(password, "0") == 0)
{
return;
}
len_1 = strlen(password);
len_2 = strspn(password, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
fp = fopen("SuperAdministrator.txt", "r"); //只读,避免修改
file_open_error(fp); //文件是否打开失败
fscanf(fp, "%d\t%s\t", &sa_1.account, sa_1.password);
if(account != sa_1.account || strcmp(password, sa_1.password) != 0) //账号或密码错误
{
printf("||------------------------------------------||\n");
printf("|| 账号或密码错误, 请重新输入 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp);
continue;
}
printf("||------------------------------------------||\n");
printf("|| 登 陆 成 功 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 超 级 管 理 员 菜 单 ||\n");
printf("||------------------------------------------||\n");
printf("|| 【1】注册管理员 ||\n");
printf("|| 【2】注销管理员 ||\n");
printf("|| 【3】修改管理员密码 ||\n");
printf("|| 【4】查询顾客 ||\n");
printf("|| 【5】修改密码 ||\n");
printf("|| 【6】商品相关菜单 ||\n");
printf("|| 【0】退出登录 ||\n");
printf("||------------------------------------------||\n");
printf("|| 请选择: ");
while((sel = getchar()) == '\n'); //不管输入什么都只读取第一个字符(除换行)
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区(清空第一个字符后的每一个字符)
system("cls"); //清屏
switch(sel)
{
case '1':
createAdministrator(); //添加管理员
break;
case '2':
removeAdministrator(); //删除管理员
break;
case '3':
reviseAdministratorPassword(); //修改管理员密码
break;
case '4':
SearrchCt(); //查询顾客
break;
case '5':
changeSuAdminPassword(); //修改超级管理员密码
return;
case '6':
relevant_Product(); //商品相关函数
break;
case '0':
fclose(fp); //关闭文件
return; //返回上一级
default:
input_error(); //输入错误显示
}
}
}
}
管理员入口
void loginAdmin() //管理员入口
{
char ch, sel;
int flag = 0; //记录是否查找成功
Administrator a_1; //管理员账号 a_1
FILE * fp; //文件指针
int account;
char password[SIZE*2]; //存储输入的账号密码
int len_1, len_2; //记录密码个数, 记录密码合法字符个数
while(1)
{
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消登录, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员账号:");
scanf("%d", &account);
system("cls"); //清屏
if(account == 0) //输入 0 返回上一级
{
return;
}
if(account<=100000 || account>999999) //不是六位的数字
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 账号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消登录, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员密码:");
scanf("%s", password);
system("cls"); //清屏
if(strcmp(password, "0") == 0)
{
return;
}
len_1 = strlen(password);
len_2 = strspn(password, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
fp = fopen("Administrator.txt", "r"); //打开文件
file_open_error(fp); //文件是否打开失败
while(!feof(fp)) //文件指针到达末尾则返回 1
{
fscanf(fp, "%d\t%s\t%s\t%s\t%s\t\n", &a_1.account, a_1.password, a_1.name, a_1.gender, a_1.telNum);
if(account == a_1.account && strcmp(password, a_1.password) == 0) //账号密码都相同
{
flag = 1; //登录成功
break;
}
}
if(flag == 0)
{
printf("||------------------------------------------||\n");
printf("|| 账号或密码错误, 请重新输入 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp); //关闭文件
continue;
}
printf("||------------------------------------------||\n");
printf("|| 登 陆 成 功 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 管 理 员 菜 单 ||\n");
printf("||------------------------------------------||\n");
printf("|| 【1】展示所有商品信息 ||\n");
printf("|| 【2】添加商品 ||\n");
printf("|| 【3】删除商品 ||\n");
printf("|| 【4】修改商品价格 ||\n");
printf("|| 【5】添加商品折扣 ||\n");
printf("|| 【6】查询顾客 ||\n");
printf("|| 【0】退出登录 ||\n");
printf("||------------------------------------------||\n");
printf("|| 请选择: ");
while((sel = getchar()) == '\n'); //不管输入什么都只读取第一个字符(除换行)
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区(清空第一个字符后的每一个字符)
system("cls"); //清屏
switch(sel)
{
case '1':
showInfo(); //展示所有商品信息
system("pause");
system("cls");
break;
case '2':
addProduct(); //添加商品
break;
case '3':
deleteProduct(); //删除商品
break;
case '4':
reviseProduct(); //修改商品价格
break;
case '5':
merchandiseDiscount(); //添加商品折扣
break;
case '6':
SearrchCt(); //查询顾客
break;
case '0':
fclose(fp); //关闭文件
return; //返回上一级
default:
input_error(); //输入错误显示
}
}
}
}
注册管理员
每次注册管理员,实际就是在文件末尾追加一个,账号用随机时生成,所以说,可能会出现账号相同的情况。
void createAdministrator() //注册管理员
{
srand((unsigned)time(NULL)); //种子库,成逐渐增大或逐渐减小
char ch;
Administrator a_1; //管理员 a_1
FILE * fp;
int len_1, len_2; //记录密码个数, 记录密码合法字符个数
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可退出注册, 8 位以内字母 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员姓名: ");
scanf("%s", a_1.name);
system("cls"); //清屏
if(strcmp(a_1.name, "0") == 0) // 0 退出注册
{
return;
}
len_1 = strlen(a_1.name);
if(len_1 < 8)
{
break;
}
else
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 姓名格式不正确, 8 位以内字母 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可退出注册, 请输入 11 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员手机号码: ");
scanf("%s", a_1.telNum);
system("cls"); //清屏
if(strcmp(a_1.telNum, "0") == 0) // 0 退出注册
{
return;
}
if(strspn(a_1.telNum, "0123456789") == 11)
{
break;
}
else
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 手机号码格式不正确, 请输入 11 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可退出注册, 男 或 女 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员性别: ");
scanf("%s", a_1.gender);
system("cls"); //清屏
if(strcmp(a_1.gender, "0") == 0) // 0 退出注册
{
return;
}
if(strcmp(a_1.gender, "男") == 0 || strcmp(a_1.gender, "女") == 0)
{
break;
}
else
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 性别格式不正确, 请输入 男 或 女 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可退出注册, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员密码: ");
scanf("%s", a_1.password);
system("cls"); //清屏
if(strcmp(a_1.password, "0") == 0)
{
return;
}
len_1 = strlen(a_1.password);
len_2 = strspn(a_1.password, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
a_1.account = 100000 + rand()%900000;
fp = fopen("Administrator.txt", "a"); //只写,在末尾添加
file_open_error(fp); //文件是否打开失败
fprintf(fp, "%d\t%s\t%s\t%s\t%s\t\n", a_1.account, a_1.password, a_1.name, a_1.gender, a_1.telNum);
printf("||------------------------------------------||\n");
printf("|| 注 册 成 功 ||\n");
printf("||------------------------------------------||\n");
printf("|| 账号:%-8d 密码:%-16s ||\n", a_1.account, a_1.password);
printf("|| 姓名: %-8s 性别:%-4s ||\n", a_1.name, a_1.gender);
printf("|| 联系方式:%-16s ||\n", a_1.telNum);
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp);
}
注销管理员
void removeAdministrator() //注销管理员
{
char ch;
int i = 0;
int account;
int len_1, len_2; //记录密码个数, 记录密码合法字符个数
char password[SIZE*2];
Administrator * A; //管理员
Administrator * tail; //指向尾部的指针
FILE * fp; //文件指针
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可退出注销, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员账号: ");
scanf("%d", &account);
system("cls"); //清屏
if(account == 0)
{
return;
}
if(account<=100000 || account>999999)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 账号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可退出注销, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员密码: ");
scanf("%s", password);
system("cls"); //清屏
if(strcmp(password, "0") == 0)
{
return;
}
len_1 = strlen(password);
len_2 = strspn(password, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
A = (Administrator *)malloc(sizeof(Administrator)); //申请动态内存
malloc_defeat(A); //内存是否申请失败
A->next = NULL;
tail = A; //指向尾部的指针
fp = fopen("Administrator.txt", "r"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
while(!feof(fp)) //fp==EOF 返回 1
{
Administrator * p; //存放数据的指针
p = (Administrator *)malloc(sizeof(Administrator)); //申请动态内存
malloc_defeat(p); //内存是否申请失败
fscanf(fp,"%d\t%s\t%s\t%s\t%s\t\n", &p->account, p->password, p->name, p->gender, p->telNum);
if((account == p->account) && (strcmp(password, p->password) == 0))
{
i = 1; //删除计为 1
continue; //如果信息相同,则不接入链表
}
tail->next = p; //接到表尾
tail = p; //循环至表尾
tail->next = NULL; //表尾表尾赋空
}
printf("||------------------------------------------||\n");
if(i)
{
printf("|| 删 除 成 功 ||\n");
}
else
{
printf("|| 无该管理员或管理员信息错误 ||\n");
}
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp);
if(i == 0)
{
admi_Destrong_List(A); //摧毁链表
return;
}
fp = fopen("Administrator.txt", "w"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
tail = A->next; //指向尾部的指针
while(tail != NULL)
{
fprintf(fp,"%d\t%s\t%s\t%s\t%s\t\n", tail->account, tail->password, tail->name, tail->gender, tail->telNum);
tail = tail->next; //继续写入下一个
}
admi_Destrong_List(A); //摧毁链表
fclose(fp); //关闭文件
}
修改管理员密码
修改密码都要双重认证新密码是否相同。
void reviseAdministratorPassword() //修改管理员密码
{
char ch;
int i = 0;
int account;
int len_1, len_2; //记录密码个数, 记录密码合法字符个数
char password[SIZE*2];
char password_1[SIZE*2], password_2[SIZE*2];
FILE * fp; //文件指针
Administrator * A; //管理员
Administrator * tail; //指向尾部的指针
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 即可取消修改, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员账号: ");
scanf("%d", &account);
system("cls"); //清屏
if(account == 0)
{
return;
}
if(account<=100000 || account>999999)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 账号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消修改, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 管理员密码: ");
scanf("%s", password);
system("cls"); //清屏
if(strcmp(password, "0") == 0)
{
return;
}
len_1 = strlen(password);
len_2 = strspn(password, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
fp = fopen("Administrator.txt", "r"); //可读写
file_open_error(fp); //文件是否打开失败
A = (Administrator *)malloc(sizeof(Administrator)); //申请动态内存
malloc_defeat(A); //内存是否申请失败
A->next = NULL;
tail = A; //指向尾部的指针
while(!feof(fp)) //fp==EOF 返回 1
{
Administrator * p; //存放数据的指针
p = (Administrator *)malloc(sizeof(Administrator)); //申请动态内存
malloc_defeat(p); //内存是否申请失败
fscanf(fp, "%d\t%s\t%s\t%s\t%s\t\n", &p->account, p->password, p->name, p->gender, p->telNum);
while((account == p->account)&&(strcmp(password, p->password) == 0)) //账号密码相同
{
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消修改, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 新密码: ");
scanf("%s", password_1);
system("cls"); //清屏
if(strcmp(password_1, "0") == 0)
{
return;
}
len_1 = strlen(password_1);
len_2 = strspn(password_1, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 新密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消修改, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 请再次输入新密码: ");
scanf("%s", password_2);
system("cls"); //清屏
if(strcmp(password_2, "0") == 0)
{
return;
}
len_1 = strlen(password_2);
len_2 = strspn(password_2, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 新密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
if(strcmp(password_1, password_2) == 0)
{
i = 1;
strcpy(p->password, password_2);
}
else
{
printf("||------------------------------------------||\n");
printf("|| 密 码 不 相 同 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
}
}
tail->next = p; //接到表尾
tail = p; //循环至表尾
tail->next = NULL; //表尾表尾赋空
}
printf("||------------------------------------------||\n");
if(i)
{
printf("|| 修 改 成 功 ||\n");
}
else
{
printf("|| 无该管理员或管理员信息错误 ||\n");
}
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp);
if(i == 0)
{
admi_Destrong_List(A); //摧毁链表
return;
}
fp = fopen("Administrator.txt", "w"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
tail = A->next; //指向尾部的指针
while(tail != NULL)
{
fprintf(fp,"%d\t%s\t%s\t%s\t%s\t\n", tail->account, tail->password, tail->name, tail->gender, tail->telNum);
tail = tail->next; //继续写入下一个
}
admi_Destrong_List(A); //摧毁链表
fclose(fp); //释放文件指针
}
查询用户
根据用户姓名进行查询。目前只能用姓名,闲麻烦没有搞别的。
void SearrchCt() //查询顾客
{
customer u_1; //顾客
FILE * fp;
char name[SIZE];
fp = fopen("Registering.txt", "r"); //可读写
file_open_error(fp); //文件是否打开失败
printf("||------------------------------------------||\n");
printf("|| 顾客姓名: ");
scanf("%s", name);
system("cls"); //清屏
while(!feof(fp)) //fp==EOF 返回 1
{
fscanf(fp,"%d\t%s\t%s\t%s\t%s\t%d\t\n",&u_1.account, u_1.password,u_1.name,u_1.gender,u_1.telNum, &u_1.balance);
if(strcmp(name, u_1.name) == 0)
{
printf("||------------------------------------------||\n");
printf("|| 账号:%-8d 密码:%-16s ||\n", u_1.account, u_1.password);
printf("|| 姓名:%-8s 联系方式:%-11s ||\n", u_1.name, u_1.telNum);
printf("|| 性别:%-4s 余额:%-12d ||\n", u_1.gender, u_1.balance);
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp); //关闭文件
return;
}
}
printf("||------------------------------------------||\n");
printf("|| 无 此 顾 客 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp); //关闭文件
}
修改超级管理员密码
和修改管理员密码思路基本一致。
void changeSuAdminPassword() //修改超级管理员密码
{
char ch;
char password_1[SIZE*2], password_2[SIZE*2];
int len_1, len_2; //记录密码个数, 记录密码合法字符个数
SuperAdministrator sa_1;
FILE * fp;
fp = fopen("SuperAdministrator.txt", "r+"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
fscanf(fp, "%d\t%s\t", &sa_1.account, sa_1.password);
while(1)
{
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消修改, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 新密码: ");
scanf("%s", password_1);
system("cls"); //清屏
if(strcmp(password_1, "0") == 0)
{
return;
}
len_1 = strlen(password_1);
len_2 = strspn(password_1, str);
if(len_1<9 || len_1>15 || len_2<9 || len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 新密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消修改, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 请再次输入新密码: ");
scanf("%s", password_2);
system("cls"); //清屏
if(strcmp(password_2, "0") == 0)
{
return;
}
len_1 = strlen(password_2);
len_2 = strspn(password_2, str);
if(len_1<9 || len_1>15 || len_2<9|| len_2>15)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 新密码格式错误, 9-15 位字母或数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
if(strcmp(password_1, password_2) == 0)
{
strcpy(sa_1.password, password_2);
fseek(fp, 0, SEEK_SET);
fprintf(fp,"%d\t%s\t", sa_1.account, sa_1.password);
printf("||------------------------------------------||\n");
printf("|| 修 改 成 功, 请 重 新 登 录 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
break;
}
else
{
printf("||------------------------------------------||\n");
printf("|| 密 码 不 相 同 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
}
}
fclose(fp);
}
商品相关函数菜单
就是选择对商品的操作。功能有点少了。
void relevant_Product() //商品相关函数
{
char ch, sel;
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 商 品 相 关 菜 单 ||\n");
printf("||------------------------------------------||\n");
printf("|| 【1】所有商品 ||\n");
printf("|| 【2】添加商品 ||\n");
printf("|| 【3】删除商品 ||\n");
printf("|| 【4】修改商品价格 ||\n");
printf("|| 【5】添加商品折扣 ||\n");
printf("|| 【0】返回 ||\n");
printf("||------------------------------------------||\n");
printf("|| 请选择: ");
while((sel = getchar()) == '\n'); //不管输入什么都只读取第一个字符(除换行)
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区(清空第一个字符后的每一个字符)
system("cls"); //清屏
switch(sel)
{
case '1':
showInfo(); //展示所有商品信息
system("pause");
system("cls");
break;
case '2':
addProduct(); //添加商品
break;
case '3':
deleteProduct(); //删除商品
break;
case '4':
reviseProduct(); //修改商品价格
break;
case '5':
merchandiseDiscount(); //添加商品折扣
break;
case '0':
return; //返回上一级
default:
input_error(); //输入错误显示
}
}
}
展示所有商品
void showInfo() //展示所有商品信息
{
FILE * fp; //文件指针
MobilePhone mp_1;
fp = fopen("MobilePhone.txt", "r"); //只读,避免被修改
file_open_error(fp); //文件是否打开失败
printf("||------------------------------------------||\n");
printf("|| 编号 型号 价格 折扣||\n");
while(!feof(fp))
{
fscanf(fp,"%d\t%s\t%d\t%lf\t%s\t\n", &mp_1.ID, mp_1.type, &mp_1.price, &mp_1.discount, mp_1.name); //中转站
printf("||%-8d%-7s%-15s%-8d%.2lf||\n", mp_1.ID, mp_1.type, mp_1.name, mp_1.price, mp_1.discount);
}
printf("||------------------------------------------||\n");
fclose(fp);
}
添加商品
和注册管理员相似。
void addProduct() //添加商品
{
int len;
char ch;
FILE * fp;
MobilePhone mp_1;
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消添加, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 商品编号: ");
scanf("%d", &mp_1.ID);
system("cls"); //清屏
if(mp_1.ID == 0) //输入 0 返回上一级
{
return;
}
if(mp_1.ID<=100000 || mp_1.ID>999999) //不是六位的数字
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 编号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消添加, 8 位以内字母 ||\n");
printf("||------------------------------------------||\n");
printf("|| 商品品牌: ");
scanf("%s", mp_1.type);
system("cls"); //清屏
if(strcmp(mp_1.type, "0") == 0) //输入 0 返回上一级
{
return;
}
len = strlen(mp_1.type);
if(len >= 8)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 品牌格式错误, 8 位以内字母 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
printf("||------------------------------------------||\n");
printf("|| 商品型号: ");
scanf("%s", mp_1.name);
system("cls"); //清屏
printf("||------------------------------------------||\n");
printf("|| 商品价格: ");
scanf("%d", &mp_1.price);
system("cls"); //清屏
mp_1.discount = 1.0;
fp = fopen("MobilePhone.txt", "a"); //只写,追加
file_open_error(fp); //文件是否打开失败
fprintf(fp,"%d\t%s\t%d\t%lf\t%s\t\n", mp_1.ID, mp_1.type, mp_1.price, mp_1.discount, mp_1.name);
printf("||------------------------------------------||\n");
printf("|| 添 加 成 功 ||\n");
printf("||------------------------------------------||\n");
printf("|| 编号 型号 价格 折扣||\n");
printf("||%-8d%-7s%-15s%-8d%.2lf||\n", mp_1.ID, mp_1.type, mp_1.name, mp_1.price, mp_1.discount);
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp);
}
删除商品
void deleteProduct() //删除商品
{
char ch;
int i = 0;
int ID;
MobilePhone * MP; //商品
MobilePhone * tail; //指向尾部的指针
FILE * fp; //文件指针
MP = (MobilePhone *)malloc(sizeof(MobilePhone)); //申请动态内存
if(MP == NULL)
{
printf("||------------------------------------------||\n");
printf("|| 内 存 申 请 失 败 ||\n");
printf("||------------------------------------------||\n");
exit(0);
}
MP->next = NULL;
tail = MP; //指向尾部的指针
fp = fopen("MobilePhone.txt", "r"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消删除, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 商品编号: ");
scanf("%d", &ID);
system("cls"); //清屏
if(ID == 0) //输入 0 返回上一级
{
return;
}
if(ID<=100000 || ID>999999) //不是六位的数字
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 编号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
while(!feof(fp)) //fp==EOF 返回 1
{
MobilePhone * p; //存放数据的指针
p = (MobilePhone *)malloc(sizeof(MobilePhone)); //申请动态内存
if(p == NULL)
{
printf("||------------------------------------------||\n");
printf("|| 内 存 申 请 失 败 ||\n");
printf("||------------------------------------------||\n");
exit(0);
}
fscanf(fp,"%d\t%s\t%d\t%lf\t%s\t\n",&p->ID,p->type,&p->price,&p->discount,p->name);
if(ID == p->ID)
{
i = 1; //删除计为 1
continue; //如果信息相同,则不接入链表
}
tail->next = p; //接到表尾
tail = p; //循环至表尾
tail->next = NULL; //表尾表尾赋空
}
printf("||------------------------------------------||\n");
if(i)
{
printf("|| 删 除 成 功 ||\n");
}
else
{
printf("|| 无该商品或商品编号错误 ||\n");
}
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp);
if(i == 0) //摧毁指针,并且返回
{
prod_Destrong_List(MP); //销毁链表
return;
}
fp = fopen("MobilePhone.txt", "w"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
tail = MP->next; //指向尾部的指针
while(tail != NULL)
{
fprintf(fp,"%d\t%s\t%d\t%lf\t%s\t\n", tail->ID, tail->type, tail->price, tail->discount, tail->name);
tail = tail->next; //继续写入下一个
}
prod_Destrong_List(MP); //销毁链表
fclose(fp); //释放文件指针
}
修改商品价格
void reviseProduct() //修改商品价格
{
char ch;
int i = 0;
int ID;
MobilePhone * MP; //商品
MobilePhone * tail; //指向尾部的指针
FILE * fp; //文件指针
MP = (MobilePhone *)malloc(sizeof(MobilePhone)); //申请动态内存
if(MP == NULL)
{
printf("||------------------------------------------||\n");
printf("|| 内 存 申 请 失 败 ||\n");
printf("||------------------------------------------||\n");
exit(0);
}
MP->next = NULL;
tail = MP; //指向尾部的指针
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消修改, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 需改商品编号:");
scanf("%d", &ID);
system("cls"); //清屏
if(ID == 0) //输入 0 返回上一级
{
return;
}
if(ID<=100000 || ID>999999) //不是六位的数字
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 编号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
fp = fopen("MobilePhone.txt", "r"); //可读写
file_open_error(fp); //文件是否打开失败
while(!feof(fp)) //fp==EOF 返回 1
{
MobilePhone * p; //存放数据的指针
p = (MobilePhone *)malloc(sizeof(MobilePhone)); //申请动态内存
if(p == NULL)
{
printf("||------------------------------------------||\n");
printf("|| 内 存 申 请 失 败 ||\n");
printf("||------------------------------------------||\n");
exit(0);
}
fscanf(fp,"%d\t%s\t%d\t%lf\t%s\t\n",&p->ID,p->type,&p->price,&p->discount,p->name);
if(ID == p->ID) //编号相同
{
printf("||------------------------------------------||\n");
printf("|| 修 改 后 价 格 ||\n");
printf("||------------------------------------------||\n");
printf("|| 商品价格: ");
scanf("%d", &p->price);
system("cls"); //清屏
i = 1;
}
tail->next = p; //接到表尾
tail = p; //循环至表尾
tail->next = NULL; //表尾表尾赋空
}
printf("||------------------------------------------||\n");
if(i)
{
printf("|| 修 改 成 功 ||\n");
}
else
{
printf("|| 无该商品或商品编号错误 ||\n");
}
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp); //关闭文件
if(i == 0) //销毁链表,并且返回
{
prod_Destrong_List(MP);
return;
}
fp = fopen("MobilePhone.txt", "w"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
tail = MP->next; //指向尾部的指针
while(tail != NULL)
{
fprintf(fp, "%d\t%s\t%d\t%lf\t%s\t\n", tail->ID, tail->type, tail->price, tail->discount, tail->name);
tail = tail->next; //继续写入下一个
}
prod_Destrong_List(MP); //销毁链表
fclose(fp); //释放文件指针
}
添加商品折扣
用小数代替,相当于1是不打折,0.9是9折,以此类推。
void merchandiseDiscount() //添加商品折扣
{
char ch;
int i = 0;
int ID;
MobilePhone * MP; //商品
MobilePhone * tail; //指向尾部的指针
FILE * fp; //文件指针
MP = (MobilePhone *)malloc(sizeof(MobilePhone)); //申请动态内存
if(MP == NULL)
{
printf("||------------------------------------------||\n");
printf("|| 内 存 申 请 失 败 ||\n");
printf("||------------------------------------------||\n");
exit(0);
}
MP->next = NULL;
tail = MP; //指向尾部的指针
fp = fopen("MobilePhone.txt", "r"); //可读写
file_open_error(fp); //文件是否打开失败
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 输入 0 可取消添加, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
printf("|| 商品编号: ");
scanf("%d", &ID);
system("cls"); //清屏
if(ID == 0) //输入 0 返回上一级
{
return;
}
if(ID<=100000 || ID>999999) //不是六位的数字
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区
printf("||------------------------------------------||\n");
printf("|| 编号格式错误, 请输入 6 位数字 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break; //账号格式正确
}
}
while(!feof(fp)) //fp==EOF 返回 1
{
MobilePhone * p; //存放数据的指针
p = (MobilePhone *)malloc(sizeof(MobilePhone)); //申请动态内存
if(p == NULL)
{
printf("||------------------------------------------||\n");
printf("|| 内 存 申 请 失 败 ||\n");
printf("||------------------------------------------||\n");
exit(0);
}
fscanf(fp,"%d\t%s\t%d\t%lf\t%s\t\n",&p->ID,p->type,&p->price,&p->discount,p->name);
if(ID == p->ID) //编号相同
{
while(1)
{
printf("||------------------------------------------||\n");
printf("|| 添 加 折 扣, 小 数(例: 0.9) ||\n");
printf("||------------------------------------------||\n");
printf("|| 折扣: ");
scanf("%lf", &p->discount);
system("cls"); //清屏
if(p->discount<=0 || p->discount>1)
{
while((ch = getchar()) != '\n' && ch != EOF); //清空缓冲区(清空第一个字符后的每一个字符)
printf("||------------------------------------------||\n");
printf("|| 折扣格式错误, 请输入小于 1 的小数 ||\n");
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
continue;
}
else
{
break;
}
}
i = 1; //添加成功
}
tail->next = p; //接到表尾
tail = p; //循环至表尾
tail->next = NULL; //表尾表尾赋空
}
printf("||------------------------------------------||\n");
if(i)
{
printf("|| 添 加 成 功 ||\n");
}
else
{
printf("|| 无该商品或商品编号错误 ||\n");
}
printf("||------------------------------------------||\n");
system("pause"); //暂停
system("cls"); //清屏
fclose(fp); //关闭文件
if(i == 0) //销毁链表,并且返回
{
prod_Destrong_List(MP);
return;
}
fp = fopen("MobilePhone.txt", "w"); //可读写,写时清除原本内容
file_open_error(fp); //文件是否打开失败
tail = MP->next; //指向尾部的指针
while(tail != NULL)
{
fprintf(fp,"%d\t%s\t%d\t%lf\t%s\t\n", tail->ID, tail->type, tail->price, tail->discount, tail->name);
tail = tail->next; //继续写入下一个
}
prod_Destrong_List(MP); //销毁链表
fclose(fp); //释放文件指针
}
假二维码
其实这个二维码只是一个用循环结合if语句一个格子一个格子的打印出来的图像,利用随机数,除了固定位置,其余位置每次打印都会不一样。
所以说,是扫不出来的哦。
不过这个函数在这个项目里面没有调用,原本是购买商品时的支付二维码和余额充值的支付二维码,属于是对用户的操作了,就没有调用。感兴趣的小伙伴可以复制打印一下。
void Payment_QR_code() //支付二维码
{
const int n = 25;
int i, j;
srand((unsigned)time(NULL)); //种子库,成逐渐增大或逐渐减小
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
//定黑
if((i==1&&j<=7)||(i==1&&j>=n-6)||(i==7&&j<=7)||(i==7&&j>=n-6))
{
printf(ch_1);
continue;
}
if((j==1&&i<=7)||(j==1&&i>=n-6)||(j==7&&i<=7)||(j==7&&i>=n-6))
{
printf(ch_1);
continue;
}
if(((i==n-6||i==n)&&j<=7)||((j==n-6||j==n)&&i<=7))
{
printf(ch_1);
continue;
}
if((i==3||i==4||i==5||i==n-2||i==n-3||i==n-4)&&(j==3||j==4||j==5))
{
printf(ch_1);
continue;
}
if((i==3||i==4||i==5)&&(j==3||j==4||j==5||j==n-2||j==n-3||j==n-4))
{
printf(ch_1);
continue;
}
if((i==n-4||i==n-8)&&(j<=n-4&&j>=n-8))
{
printf(ch_1);
continue;
}
if((j==n-4||j==n-8)&&(i<=n-4&&i>=n-8))
{
printf(ch_1);
continue;
}
if(i==n-6&&j==n-6)
{
printf(ch_1);
continue;
}
//定白
if(i==2&&((j>=2&&j<=6)||(j<=n-1&&j>=n-5)))
{
printf(ch_2);
continue;
}
if(i==6&&((j>=2&&j<=6)||(j<=n-1&&j>=n-5)))
{
printf(ch_2);
continue;
}
if(j==2&&((i>=2&&i<=6)||(i<=n-1&&i>=n-5)))
{
printf(ch_2);
continue;
}
if(j==6&&((i>=2&&i<=6)||(i<=n-1&&i>=n-5)))
{
printf(ch_2);
continue;
}
if(i==8&&(j<=8||j>=n-7))
{
printf(ch_2);
continue;
}
if(j==8&&(i<=8||i>=n-7))
{
printf(ch_2);
continue;
}
if(i==n-7&&((j<=8)||(j<=n-5&&j>=n-7)))
{
printf(ch_2);
continue;
}
if(j==n-7&&((i<=8)||(i<=n-5&&i>=n-7)))
{
printf(ch_2);
continue;
}
if(i==n-5&&((j<=8)||(j<=n-5&&j>=n-7)))
{
printf(ch_2);
continue;
}
if(j==n-5&&((i<=8)||(i<=n-5&&i>=n-7)))
{
printf(ch_2);
continue;
}
if(i==n-1&&(j<=6&&j>=2))
{
printf(ch_2);
continue;
}
if(j==n-1&&(i<=6&&i>=2))
{
printf(ch_2);
continue;
}
else //随机
{
if(rand()%2)
{
printf(ch_1);
continue;
}
else
{
printf(ch_2);
continue;
}
}
}
printf("\n");
}
system("pause"); //暂停
system("cls"); //清屏
}
给个赞吧OAO,谢谢!
完结......