📄 supermarket.cpp
字号:
printf("还没有记录,请加入记录!\n\n");
printf("输入1加入记录,其它退出程序\n");
scanf("%d",&on);
system("cls");
if(1!=on)
exit(0);
else
while(1)
{
printf("产品种类说明:");
printf(" A.生活用品 B.食品");
printf(" C.文化用品 D.其它\n\n");
if(!creat(head))
{
fflush(stdin);
break;
}
}
save();
return;
}
if((fp=fopen("fname","rb"))==NULL)
{
printf("can not open out file!\n");
exit(0);
}
fscanf(fp1,"%d",&mm);
fclose(fp1);
printf("已有商品数%d\n",mm); //用来查看mm的值是否正确的
system("pause"); //用来检查错误的
for( i=0;i<mm;i++)
{
if(head==NULL)
{
head=(struct sell*)malloc(sizeof(struct sell));
head->next=NULL ; //建立一个无效的头结点
}
pt=(struct sell*)malloc(sizeof(struct sell));
if(pt==NULL)
{
printf("申请内存失败!");
exit(0);
}
fread(pt,sizeof(struct sell),1,fp);
pt->next=head;
head=pt;
}
fclose(fp);
mm=0;
}
void save()
{
if(head==NULL)
{
printf("没有可保存的数据!");
system("pause");
return;
}
struct sell *savepian;
FILE *fp;
FILE *fp1;
if((fp1=fopen("fname1","wb+"))==NULL)
{
printf("没有数据");
exit(0);
}
if((fp=fopen("fname ", "wb+"))==NULL)
{
printf("can not open file\n");
exit(0);
}
for(savepian=head;savepian->next!=NULL;savepian=savepian->next)
{
fwrite(savepian,sizeof(struct sell),1,fp);
fflush(stdin);
mm++;
}
fclose(fp);
fprintf(fp1,"%d",mm);
fclose(fp1);
mm=0;
}
int xiugai(SELL *phead)
{
char c;
printf("输入y确认修改\n");
c=getchar();
fflush(stdin);
if(c=='y')
{printf("输入一个数据就按一次回车:\n");
printf("输入 商品名字、价格、编号(整型)");
printf("数量、入库日期(例040101)、种类\n\n");
printf("名字:");
scanf("%s",phead->name);
printf("进货价格:");
scanf("%f",&phead->gain);
printf("销售价格:");
scanf("%f",&phead->price);
printf("编号(整型):");
scanf("%d",&phead->number);
printf("数量:");
scanf("%d",&phead->amount);
printf("日期:");
scanf("%s",phead->date);
fflush(stdin);
printf("种类(A,B,C,D):");
while(1)
{
scanf("%c",&phead->kind);
if('A'==phead->kind)break;
if('B'==phead->kind)break;
if('C'==phead->kind)break;
if('D'==phead->kind)break;
printf("请输入正确种类符号:");
fflush(stdin);
}
system("cls");
printf("已改为如下数据\n");
printf(" 商品名字 价格 编号 数量 日期 种类\n");
printf("%10s%10.2f%10d",phead->name,phead->price,phead->number);
printf("%10d%10s%10c\n",phead->amount,phead->date,phead->kind);
system("pause");
}
else
return 1;
return 1;
}
void banchuan()
{
int k=0,m=3,n=1,t=0,ll=0;
printf(" 课");
for(ll=0;ll<=80000000;ll++);
printf("程");
for( ll=0;ll<=80000000;ll++);
printf("设");
for( ll=0;ll<=80000000;ll++);
printf("计\n\n");
//for( ll=0;ll<=80000000;ll++);
printf(" 计036班 邹英斌 学号:2003041256\n\n");
system("pause");
system("cls");
}
void user()
{
char choice='1' ;
if(present.user_kind!='A')
{
printf("对不起!你不是管理员,不能执行这项操作\n");
system("pause");
return ;
}
while(1)
{
system("cls");
printf(" 1.增加用户 2.删除用户\n");
printf(" 3.修改密码 0.返回上一级菜单\n");
while(1)
{
fflush(stdin);
printf("请输入你的选择:");
scanf("%c",&choice);
fflush(stdin);
if('1'==choice||'2'==choice||'3'==choice||'0'==choice)break ;
else
{
system("cls");
printf("输入有错,请在0--3之间选择\n");
}
}
switch(choice)
{
case '1' :
user_add();
break ;
case '2' :
user_del();
break ;
case '3' :
user_modify_password();
break ;
case '0' :
return ;
break ;
}
}
}
void user_read()
{
FILE*fp3,*fp4 ;
struct users*pt,*pian ;
char name[20],mima[7];
int t=0,i=0,j=0,k=0,flag=0 ;
if(user_head==NULL)
{
pt=(struct users*)malloc(sizeof(struct users));
pt->next=NULL ;
user_head=pt ;
}
if((fp3=fopen("username","rb"))!=NULL&&(fp4=fopen("usercount","rb"))!=NULL)
{
fscanf(fp4,"%d",&t);
fclose(fp4);
for(i=0;i<t;i++)
{
pt=(struct users*)malloc(sizeof(struct users));
fread(pt,sizeof(struct users),1,fp3);
pt->next=user_head ;
user_head=pt ;
}
fclose(fp3);
}
if(user_head->next==NULL)
{
printf("还没创建用户,请在进入后创建用户!\n");
present.user_kind='A' ;
}
else
{
while(1)
{
printf("输入用户名:");
scanf("%20s",name);
fflush(stdin);
for(pian=user_head;pian->next!=NULL;pian=pian->next)
{
if(!strcmp(name,pian->user_name))
{
while(1)
{
printf("输入密码:");
for(i=0;i<6;i++)
{
mima[i]=getch();
printf("*");
}
mima[6]='\0';
printf("\n");
fflush(stdin);
addpassword(mima);
if(!strcmp(mima,pian->user_password))
{
strcpy(present.user_name,pian->user_name);
strcpy(present.user_password,pian->user_password);
present.user_kind=pian->user_kind ;
printf("\n欢迎用户 %s!\n",pian->user_name);
flag=1 ;
pre=pian;
break ;
}
else printf("密码不正确!\n");
j++;
if(3==j)exit(0);
}
break ;
}
}
k++;
if(1==flag)break ;
else printf("用户名错误!\n");
if(3==k)exit(0);
}
}
fflush(stdin);
//system("pause");
}
void user_add()
{
FILE*fp3,*fp4 ;
int i=0;
struct users*u_pian,*add_user ;
add_user=(struct users*)malloc(sizeof(struct users));
if(add_user==NULL)
{
printf("申请内存失败!程序退出\n");
system("pause");
exit(0);
}
int count=0 ;
printf("输入用户名:\n");
scanf("%20s",add_user->user_name);
printf("输入密码:");
for(i=0;i<6;i++)
{
add_user->user_password[i]=getch();
printf("*");
}
add_user->user_password[6]='\0';
addpassword(add_user->user_password);
printf("\n");
fflush(stdin);
printf("用户种类A--管理员,其它为普通用户\n");
fflush(stdin);
while(1)
{
scanf("%c",&add_user->user_kind);
if(add_user->user_kind=='A'||add_user->user_kind=='B')break ;
else printf("输入错误!只能输入A或B\n");
}
add_user->next=user_head ;
user_head=add_user ;
if((fp3=fopen("username","wb+"))!=NULL)
//进行写文件操作
for(u_pian=user_head;u_pian->next!=NULL;u_pian=u_pian->next)
{
fwrite(u_pian,sizeof(struct users),1,fp3);
count++;
}
fclose(fp3);
if(count!=0)
{
if((fp4=fopen("usercount","wb+"))!=NULL)
fprintf(fp4,"%d",count);
fclose(fp4);
}
fflush(stdin);
printf("successfull!");
system("pause");
}
void user_del()
{
char del_name[20];
FILE*fp3,*fp4 ;
struct users*del_pian,*cd,*cc,*d_pian ;
int count=0 ,flag=0;
printf("输入你要删除的用户名:");
scanf("%20s",del_name);
fflush(stdin);
if(user_head!=NULL&&user_head->next!=NULL)
{
for(del_pian=user_head;del_pian->next!=NULL;cc=del_pian,del_pian=del_pian->next)
{
if(!strcmp(del_pian->user_name,del_name))
{
if(del_pian==user_head)
{
cd=user_head ;
user_head=user_head->next ;
free(cd);
}
else
{
cd=del_pian ;
cc->next=cd->next ;
//free(cd);
}
printf("删除成功!");
flag=1;
printf("\npress any key continu!");
getch();
break ;
}
}
if((fp3=fopen("username","wb+"))!=NULL)
for(d_pian=user_head;d_pian->next!=NULL;d_pian=d_pian->next)
{
fwrite(d_pian,sizeof(struct users),1,fp3);
count++;
}
fclose(fp3);
if(count!=0)
{
if((fp4=fopen("usercount","wb+"))!=NULL)
fprintf(fp4,"%d",count);
fclose(fp4);
}
else
{
if((fp4=fopen("usercount","wb+"))!=NULL)
fprintf(fp4,"%d",count);
fclose(fp4);
}
fflush(stdin);
if(flag==1)return;
}
else printf("还没有用户!");
printf("删除失败!\n");
system("pause");
}
void user_modify_password()
{
char secret[7],secrets[7];
int count=0,i=0;
struct users *m_pian;
FILE *fp3,*fp4;
printf("只能修改当前用户密码!\n");
printf("输入旧密码");
for(i=0;i<6;i++)
{
secret[i]=getch();
printf("*");
}
secret[6]='\0';
addpassword(secret);
printf("\n");
fflush(stdin);
if(!strcmp(secret,pre->user_password))
{
printf("输入新密码:");
for(i=0;i<6;i++)
{
secret[i]=getch();
printf("*");
}
secret[6]='\0';
printf("\n");
fflush(stdin);
printf("再次输入密码");
for(i=0;i<6;i++)
{
secrets[i]=getch();
printf("*");
}
secrets[6]='\0';
printf("\n");
fflush(stdin);
if(!strcmp(secrets,secret))
{
addpassword(secrets);
strcpy(pre->user_password,secrets);
printf("successfull!");
if((fp3=fopen("username","wb+"))!=NULL)
for(m_pian=user_head;m_pian->next!=NULL;m_pian=m_pian->next)
{
fwrite(m_pian,sizeof(struct users),1,fp3);
count++;
}
fclose(fp3);
if(count!=0)
{
if((fp4=fopen("usercount","wb+"))!=NULL)
fprintf(fp4,"%d",count);
fclose(fp4);
}
fflush(stdin);
}
else printf("两 次输入密码不一致!fail");
}
else printf("旧密码不正确!");
system("pause");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -