超市管理系统(C语言)课程设计(新手)

一个不是很完善的设计,目的是实现基础功能。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
struct AD
{
	int t;           //商品类别
	char name[20];   //商品名称
	int price;       //商品价格
	int stock;       //商品库存
	char factory[20];//商品厂家
	char id[20];     //商品品牌
};
struct clas   //1.食品 2.化妆品 3.日用品 4.饮料 
{
	struct AD a;
	struct clas *next;
};
	char a1[30]="靳凌霄";    //管理员账号信息 
	char b1[30]="12345678";
	char c1[30]="你是谁?";
	char d1[30]="我不知道";
	char e1[30]="17638837824";
void passrand()   //随机数验证码 
{
	char a[53]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int i,j;
	char T[5];
	loop:srand((int)time(0));
	for(i=0;i<4;i++)
	{
		j=rand()%52;
		T[i]=a[j];
		printf("%c",a[j]);
	}
	printf("\n请输入验证码:\n");
	char t[5];
	scanf("%s",t);
	int len=strcmp(t,T);
	if(len==0)
	{
		printf("输入正确。");
	}
	else if(len!=0)
	{
		printf("请重新输入\n"); 
		goto loop;
	}
}
void inputpassword(char passwd[])   //密码用*代替 
{
	char s[30];
	char temp[2];
	int n;
	strcpy(s," ");
	while(1)
		{
			n=getch();    //头文件conio.h 
			if(n == 13)   //13就是回车 
			{
				break;
			}
			printf("*");
			memset(temp,0,sizeof(temp));
			sprintf(temp,"%c",n);      // 把n打印成字符保存在temp 
			strcat(s,temp);
		}
	printf("\n");
	strcpy(passwd, s+1);
}
char a[30],b[30],c[30],d[30],k[30],j[30];
void password();
void window()                  //账号登陆
{
	system("color 78");
	printf("请输入系统账号:\n");
	char x[30];
	scanf("%s",&x);
	int len1=strcmp(a,x);
	if(len1==0)
	{    
        password();
	}
	else
	{
		int num; 
		printf("账号错误,输入“1”重新输入,输入“其它”退出程序\n");
		scanf("%d",&num);
		if(num==1)
		{
			window();
		} 
        else
		{
			exit(0) ;
		}
		window();
	}
}
void bestpassword();    //管理员密码
void bestwindow()       //管理员账号
{
	system("color 78");
	printf("请输入系统账号:\n");
	char x[30];
	scanf("%s",&x);
	int len1=strcmp(a1,x);
	if(len1==0)
	{    
        bestpassword();
	}
	else
	{
		int num; 
		printf("账号错误,输入“1”重新输入,输入“其它”退出程序\n");
		scanf("%d",&num);
		if(num==1)
		{
			bestwindow();
		} 
        else
		{
			exit(0) ;
		}
	}
}
void bestchangepass();   //管理员修改密码
void bestfindpass();     //管理员查找密码
void bestpassword()      //管理员密码密码
{
	char y[30];	
	printf("请输入账号密码:\n");
	inputpassword(y);
	int len2=strcmp(b1,y);
	if(len2==0)
	{
		return ;
	}
	else
	{
		printf("密码错误!\n");
		int num;
		printf("请输入您要选择的功能:1.重新输入\t2.修改密码\t3.查找密码\n");
		scanf("%d",&num);
		if(num==1)
		{
			bestpassword(); 
		}
		else if(num==2)
		{
			bestchangepass();
		}
		else if(num==3)
		{
			bestfindpass();
		 } 
        else
        {
        	printf("您选择的功能不存在!\n");
		}
	}
}
void bestchangepass()      //管理员修改密码
{
	char z[30];
	printf("请输入问题答案:\n%s\n",&c1);
	scanf("%s",&z);
	int len3=strcmp(d1,z);
	if(len3==0)
	{
		printf("请输入您要修改的密码:\n");
		inputpassword(z);
		strcpy(b1,z);
		printf("修改成功,请重新登陆。\n");
		bestwindow();
	}
	else
	{
		printf("回答错误,请重试。\n");
		bestchangepass();
	}
} 
void bestfindpass()    //管理员查找密码
{
	char v[20];
	printf("请输入您的手机号码:\n");
	scanf("%s",&v);
	int len=strcmp(e1,v);
	if(len==0)
	{
		puts(b1);
		printf("请重新登录:\n");
		window();
	}
	else
	{
		printf("手机号码错误,1.返回 2.重新输入:\n");
		int num;
		scanf("%d",&num);
		if(num==1)
		{
			bestpassword();
		}
		else if(num==2)
		{
			bestfindpass();
		}
		else
		{
			printf("输入错误!");
			return ;
		}
	}
}
void changepass();    //注册账号修改密码
void findpass();      //注册账号查找密码
void password()       //注册账号密码
{
	char y[30];	
	printf("请输入账号密码:\n");
	inputpassword(y);
	int len2=strcmp(b,y);
	if(len2==0)
	{
		return ;
	}
	else
	{
		printf("密码错误!\n");
		int num;
		printf("请输入您要选择的功能:1.重新输入\t2.修改密码\t3.查找密码\n");
		scanf("%d",&num);
		if(num==1)
		{
			password(); 
		}
		else if(num==2)
		{
			changepass();
		}
		else if(num==3)
		{
			findpass();
		 } 
        else
        {
        	printf("您选择的功能不存在!\n");
		}
	}
}

void changepass()
{
	char z[30];
	printf("请输入问题答案:\n%s\n",&k);
	scanf("%s",&z);
	int len3=strcmp(c,z);
	if(len3==0)
	{
		printf("请输入您要修改的密码:\n");
		inputpassword(z);
		strcpy(b,z);
		printf("修改成功,请重新登陆。\n");
		window();
	}
	else
	{
		printf("回答错误,请重试。\n");
		changepass();
	}
}
void findpass()
{
	char v[20];
	printf("请输入您的手机号码:\n");
	scanf("%s",&v);
	int len=strcmp(d,v);
	if(len==0)
	{
		puts(b);
		printf("请重新登录:\n");
		window();
	}
	else
	{
		printf("手机号码错误,1.返回 2.重新输入:\n");
		int num;
		scanf("%d",&num);
		if(num==1)
		{
			password();
		}
		else if(num==2)
		{
			findpass();
		}
		else
		{
			printf("输入错误!");
			return ;
		}
	}
}
void createwindow()     //注册账号
{
	system("color 78");
	printf("请注册一个账号:\n");
    printf("请输入系统账号:(不超过30个字符)\n");
    scanf("%s",a);
    start : printf("请输入系统密码:(不超过30个字符)\n");
   	inputpassword(b);
    printf("请确认系统密码:\n");
   	inputpassword(j);
    int len9=strcmp(b,j);
    if(len9!=0)
    {
	   goto start;
    }
    printf("请设置一个密保问题:\n");
    scanf("%s",&k);
    printf("请输入问题答案:\n");
    scanf("%s",&c);
    printf("请输入您的手机号码:(以便用来找回密码)\n");
    scanf("%s",&d);
    printf("注册完成。\n");
    system("CLS"); 
}
int look(struct clas *head,int T);
int look1(struct clas *head,char F[]); 
void create(struct clas *head)      //添加一个不能输入相同信息的功能。 
{
	struct clas *p1=head;
	printf("请输入您想要添加的信息:\n\n");
	printf("(类别)1.食品 2.化妆品 3.日用品 4.饮料\n\n"); 
	int n=1;
	while(n==1) 
	{
		printf("是否开始录入商品信息?如果是请输入“1”,否则输入“2”\n");
		scanf("%d",&n);
		if(n==1)
		{	
	    	struct clas *p2=(struct clas *)malloc(sizeof(clas));
	    	printf("请输入商品信息:\n");
	    	printf("请输入商品类别: \t");
	    	scanf("%d",&p2->a.t);
	    	printf("请输入商品名称: \t");
	    	scanf("%s",&p2->a.name);
			if((look(head,p2->a.t))==1)
			{
				if((look1(head,p2->a.name))==0)
				{
					printf("请勿添加相同商品!\n");
					return ;
				}
			}
	    	printf("请输入商品价格: \t");
	    	scanf("%d",&p2->a.price);
	    	printf("请输入添加库存:\t");
	    	scanf("%d",&p2->a.stock);
	    	printf("请输入商品厂家: \t");
	    	scanf("%s",&p2->a.factory);
	    	printf("请输入商品品牌: \t");
	     	scanf("%s",&p2->a.id);
		    p1->next=p2;
	     	p2->next=NULL;
		    p1=p2;
		}
		else if(n==2)
		{
			printf("结束录入。\n");
			break;
		}
		else
		{
			printf("系统错误。\n");
			break;
		}
	}
}
void print(struct clas *head)    //输出链表
{
	struct clas *p1=head;
	printf("以下为所有的商品信息:((类别)1.食品 2.化妆品 3.日用品 4.饮料  \n");
	printf("商品类别\t商品名称\t商品价格\t商品库存\t商品厂家\t商品品牌\n");
	if(p1->next==NULL)
	{
		printf("未找到商品信息!\n"); 
	}
	while(p1->next!=NULL)
	{
		printf("%d\t\t%s\t\t%d\t\t%d\t\t%s\t\t%s\n",p1->next->a.t,p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
		p1=p1->next;
	}
}
void find(struct clas *head)      //查找商品
{
	int T;
	printf("请输入您要查找的商品类别: \t");
	scanf("%d",&T);
	char F[20];
	printf("请输入您要查找的商品名称: \t");
	scanf("%s",&F);
	struct clas *p1=head;
	int m=1;
	while(p1->next!=NULL)
	{
		int len=strcmp(F,p1->next->a.name);
		if(p1->next->a.t==T&&len==0)//这里多个分号会必定执行下面语句!!!!!!!!!
		{
			m=0;
			printf("您要查找的商品信息为: \n");
			printf("商品名称\t商品价格\t商品库存\t商品厂家\t商品品牌\n");
		    printf("%s\t\t%d\t\t%d\t\t%s\t\t%s\n",p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
		}
		p1=p1->next;
	}
	if(m==1)
	{
		printf("查找失败!");
	}
}
int look(struct clas *head,int T)    //查找类别是否存在 
{
	struct clas *p1=head;
	while(p1->next!=NULL)
	{
		if(p1->next->a.t==T)
		{
			return 1;
		}
		p1=p1->next;
	}
	return 0;
}
void sell(struct clas *head)        //购买功能
{
	struct clas *p1=head;
	int priceall=0;
	int T;
	printf("请输入您要购买的商品类别:\t");
	scanf("%d",&T);
	char F[20];
	printf("请输入您想要购买的商品名称:\t");
	scanf("%s",F);
	int A=look(head,T);
	if(A==1)
	{
		while(p1->next!=NULL)
		{		
			int len=strcmp(F,p1->next->a.name);
			if(len==0)
			{
			if(p1->next->a.t==T)
			{
			if(p1->next->a.stock>0)
		    {
			int total;
			printf("请输入您要购买商品的数量:\t");
			scanf("%d",&total);
			if(p1->next->a.stock>=total)
			{
				printf("商品总价格为:\t\n");
				priceall=p1->next->a.price*total+priceall;
		    	printf("人民币:\t%d\n",priceall);
		    	p1->next->a.stock=p1->next->a.stock-total;
		    	break;
			}
			else
			{
				printf("库存不足!\n");
				break;
		    }
	    	}
	    	else
	    	{
	    		printf("库存为0!\n");
	    		break;
			}
			}
			}
			else
			{
				printf("未找到您要购买的商品!\n");
				break;
			}
			p1=p1->next;
		}
	}
	else if(A==0) 
	{
		printf("未找到您要购买的商品!\n");
	}
	int num; 
	printf("是否继续购买?(是请输入“1”,否则请输入“2”)\n");
	scanf("%d",&num);
	if(num==1)
	{
			int T;
	printf("请输入您要购买的商品类别:\t");
	scanf("%d",&T);
	char F[20];
	printf("请输入您想要购买的商品名称:\t");
	scanf("%s",F);
	int A=look(head,T);
	if(A==1)
	{
		while(p1->next!=NULL)
		{		
			int len=strcmp(F,p1->next->a.name);
			if(len==0)
			{
			if(p1->next->a.t==T)
			{
			if(p1->next->a.stock>0)
		    {
			int total;
			printf("请输入您要购买商品的数量:\t");
			scanf("%d",&total);
			if(p1->next->a.stock>=total)
			{
				printf("商品总价格为:\t\n");
				priceall=p1->next->a.price*total+priceall;
		    	printf("人民币:\t%d\n",priceall);
		    	p1->next->a.stock=p1->next->a.stock-total;
		    	break;
			}
			else
			{
				printf("库存不足!\n");
				break;
		    }
	    	}
	    	else
	    	{
	    		printf("库存为0!\n");
	    		break;
			}
			}
			}
			else
			{
				printf("未找到您要购买的商品!\n");
				break;
			}
			p1=p1->next;
		}
	}
	else if(A==0) 
	{
		printf("未找到您要购买的商品!\n");
	}
	}
	else if(num==2)
	{
		printf("结束购买!\n");
	}
	else
	{
		printf("无效输入!\n");
	}
}
void change(struct clas *head)       //修改商品信息
{
	struct clas *p1=head;
	int T;
	printf("请输入您要修改的商品类别\t");
	scanf("%d",&T);
	char F[20];
	printf("请输入您要修改的商品名称:\t");
	scanf("%s",&F);
	while(p1->next!=NULL)
	{
		int len=strcmp(F,p1->next->a.name);
		if(p1->next->a.t==T&&len==0)
		{
			int z,c,v;
			char x[20],b[20],l[20];
	    	printf("请输入您要修改的商品类别: \t");
	    	scanf("%d",&z);
	    	printf("请输入您要修改的商品名称: \t");
	    	scanf("%s",&x); 
	    	printf("请输入您要修改的商品价格: \t");
	    	scanf("%d",&c);
	    	printf("请输入您要修改的库存:\t");
	    	scanf("%d",&v);
	    	printf("请输入您要修改的商品厂家: \t");
	    	scanf("%s",&b);
	    	printf("请输入您要修改的商品品牌: \t");
	     	scanf("%s",&l);
	     	p1->next->a.t=z;
	     	strcpy(p1->next->a.name,x);
	     	p1->next->a.price=c;
	     	p1->next->a.stock=v;
	     	strcpy(p1->next->a.factory,b);
	     	strcpy(p1->next->a.id,l);
		}
		p1=p1->next;
	}
}
void frees(struct clas *head)       //释放结构体 
{
	struct clas *p1=head;
	int T;
	printf("请输入您要删除的商品类别");
	scanf("%d",&T);
	char F[20];
	printf("请输入您要删除的商品名称");
	scanf("%s",F);
    if(look(head,T)==1)
    {
		while(p1->next!=NULL)
	
	{	
	    int len=strcmp(p1->next->a.name,F);
	    if(p1->next->a.t==T&&len==0)
		{
			struct clas *t;
			t=p1->next;
			t->next=p1->next->next;
			p1->next=t->next;         //这里是p1->next  不是  p1 !!!!! 
			free(t);
			t=NULL;
			printf("删除成功。\n");
			break;
		}
		p1=p1->next;
    }
	}
	else
	{
		printf("该商品不存在。\n");
	}
}
int look1(struct clas *head,char F[])        //查找名称是否存在 
{
	struct clas *p1=head;
	int m=1;
	while(p1->next!=NULL)
	{
		int len=strcmp(F,p1->next->a.name);
		if(len==0)
		{
			m=0;
			return 0;
		}
		p1=p1->next;
	}
	if(m==1)
	{
		return 1;
	}
}
void add(struct clas *head)      //添加商品
{
	int T;
	printf("请输入您要添加的类别:\t");
	scanf("%d",&T);
	char F[20];
	printf("请输入您要添加到哪个商品之后(请从此类别商品中选择):\t");//如果不存在出现BUG 
	scanf("%s",F);
	struct clas *p1=head;
	struct clas *p2=(struct clas *)malloc(sizeof(clas));	
	int m=1; 
	if(look(head,T)==1)
	{
		if(look1(head,F)==0)
		{
		
		while(p1->next!=NULL)
    	{
		int len=strcmp(p1->next->a.name,F);
		if(len==0)
		{
			m=0;
			if(p1->next->a.t==T)
			{
	    	printf("请输入您要添加的商品信息:\n");
	    	printf("请输入您要添加的商品类别: \t");
	    	scanf("%d",&p2->a.t);
	    	printf("请输入您要添加的商品名称: \t");		
			scanf("%s",&p2->a.name);
	    	if(look1(head,p2->a.name)==1)
			{
             ;
			}
			else if(look1(head,p2->a.name)==0)
			{
			printf("请勿添加相同商品,请使用修改功能更改库存。");
			break;
			}
	    	printf("请输入您要添加的商品价格: \t");
	    	scanf("%d",&p2->a.price);
	    	printf("请输入您要添加的添加库存:\t");
	    	scanf("%d",&p2->a.stock);
	    	printf("请输入您要添加的商品厂家: \t");
	    	scanf("%s",&p2->a.factory);
	    	printf("请输入您要添加的商品品牌: \t");
	     	scanf("%s",&p2->a.id);
	     	p2->next=p1->next->next;      //先连接在后面
			p1->next->next=p2;            //不能反 
			break;
			}
		}
    	}
    	p1=p1->next;
	}
	else
	{
		printf("该类别不存在。\n");
	}
	if(m==1)
	{
		printf("未找到该商品!\n");
		return ;
	}
}
}
void sorts(struct clas *head)      //商品排序
{
	int tt,price1,stock1;
	char name1[20],factory1[20],id1[20];
	char F[20];
	struct clas *p=head;
	struct clas *p1;
	struct clas *p2;
	int num;
	printf("请输入按照哪一项目排序:\t(1.类别 2.名称 3.价格 4.库存 5.厂家 6.品牌 \n)");
	scanf("%d",&num);
	if(num==1)
	{
		while(p->next!=NULL)
	{
		p1=p->next;
		p2=p1->next;
		while(p2!=NULL)
		{
			if(p1->a.t>p2->a.t)
			{
				tt=p1->a.t;
				p1->a.t=p2->a.t;
				p2->a.t=tt;
				strcpy(name1,p1->a.name);
				strcpy(p1->a.name,p2->a.name);
				strcpy(p2->a.name,name1);
				price1=p1->a.price;
				p1->a.price=p2->a.price;
				p2->a.price=price1;
				stock1=p1->a.stock;
				p1->a.stock=p2->a.stock;
				p2->a.stock=stock1;
				strcpy(factory1,p1->a.factory);
				strcpy(p1->a.factory,p2->a.factory);
				strcpy(p2->a.factory,factory1);
				strcpy(id1,p1->a.id);
				strcpy(p1->a.id,p2->a.id);
				strcpy(p2->a.id,id1);
				
			}
			p2=p2->next;   //选择排序 
		}
		p=p->next;
	}
	}
	else if(num==2)
	{
		while(p->next!=NULL)
	{
		p1=p->next;
		p2=p1->next;
		while(p2!=NULL)
		{
			int len=strcmp(p1->a.name,p2->a.name);
			if(len<0)
			{
				tt=p1->a.t;
				p1->a.t=p2->a.t;
				p2->a.t=tt;
				strcpy(name1,p1->a.name);
				strcpy(p1->a.name,p2->a.name);
				strcpy(p2->a.name,name1);
				price1=p1->a.price;
				p1->a.price=p2->a.price;
				p2->a.price=price1;
				stock1=p1->a.stock;
				p1->a.stock=p2->a.stock;
				p2->a.stock=stock1;
				strcpy(factory1,p1->a.factory);
				strcpy(p1->a.factory,p2->a.factory);
				strcpy(p2->a.factory,factory1);
				strcpy(id1,p1->a.id);
				strcpy(p1->a.id,p2->a.id);
				strcpy(p2->a.id,id1);
				
			}
			p2=p2->next;   //选择排序 
		}
		p=p->next;
	}
	}
	else if(num==3)
	{
		while(p->next!=NULL)
	{
		p1=p->next;
		p2=p1->next;
		while(p2!=NULL)
		{
			if(p1->a.price>p2->a.price)
			{
				tt=p1->a.t;
				p1->a.t=p2->a.t;
				p2->a.t=tt;
				strcpy(name1,p1->a.name);
				strcpy(p1->a.name,p2->a.name);
				strcpy(p2->a.name,name1);
				price1=p1->a.price;
				p1->a.price=p2->a.price;
				p2->a.price=price1;
				stock1=p1->a.stock;
				p1->a.stock=p2->a.stock;
				p2->a.stock=stock1;
				strcpy(factory1,p1->a.factory);
				strcpy(p1->a.factory,p2->a.factory);
				strcpy(p2->a.factory,factory1);
				strcpy(id1,p1->a.id);
				strcpy(p1->a.id,p2->a.id);
				strcpy(p2->a.id,id1);
				
			}
			p2=p2->next;   //选择排序 
		}
		p=p->next;
	}
	}
	else if(num==4)
	{
		while(p->next!=NULL)
	{
		p1=p->next;
		p2=p1->next;
		while(p2!=NULL)
		{
			if(p1->a.stock>p2->a.stock)
			{
				tt=p1->a.t;
				p1->a.t=p2->a.t;
				p2->a.t=tt;
				strcpy(name1,p1->a.name);
				strcpy(p1->a.name,p2->a.name);
				strcpy(p2->a.name,name1);
				price1=p1->a.price;
				p1->a.price=p2->a.price;
				p2->a.price=price1;
				stock1=p1->a.stock;
				p1->a.stock=p2->a.stock;
				p2->a.stock=stock1;
				strcpy(factory1,p1->a.factory);
				strcpy(p1->a.factory,p2->a.factory);
				strcpy(p2->a.factory,factory1);
				strcpy(id1,p1->a.id);
				strcpy(p1->a.id,p2->a.id);
				strcpy(p2->a.id,id1);
				
			}
			p2=p2->next;   //选择排序 
		}
		p=p->next;
	}
	}
	else if(num==5)
	{
		while(p->next!=NULL)
	{
		p1=p->next;
		p2=p1->next;
		while(p2!=NULL)
		{
			int len=strcmp(p1->a.factory,p2->a.factory);
			if(len<0)
			{
				tt=p1->a.t;
				p1->a.t=p2->a.t;
				p2->a.t=tt;
				strcpy(name1,p1->a.name);
				strcpy(p1->a.name,p2->a.name);
				strcpy(p2->a.name,name1);
				price1=p1->a.price;
				p1->a.price=p2->a.price;
				p2->a.price=price1;
				stock1=p1->a.stock;
				p1->a.stock=p2->a.stock;
				p2->a.stock=stock1;
				strcpy(factory1,p1->a.factory);
				strcpy(p1->a.factory,p2->a.factory);
				strcpy(p2->a.factory,factory1);
				strcpy(id1,p1->a.id);
				strcpy(p1->a.id,p2->a.id);
				strcpy(p2->a.id,id1);
				
			}
			p2=p2->next;   //选择排序 
		}
		p=p->next;
	}
	}
	else if(num==6)
	{
		while(p->next!=NULL)
	{
		p1=p->next;
		p2=p1->next;
		while(p2!=NULL)
		{
			int len=strcmp(p1->a.id,p2->a.id);
			if(len<0)
			{
				tt=p1->a.t;
				p1->a.t=p2->a.t;
				p2->a.t=tt;
				strcpy(name1,p1->a.name);
				strcpy(p1->a.name,p2->a.name);
				strcpy(p2->a.name,name1);
				price1=p1->a.price;
				p1->a.price=p2->a.price;
				p2->a.price=price1;
				stock1=p1->a.stock;
				p1->a.stock=p2->a.stock;
				p2->a.stock=stock1;
				strcpy(factory1,p1->a.factory);
				strcpy(p1->a.factory,p2->a.factory);
				strcpy(p2->a.factory,factory1);
				strcpy(id1,p1->a.id);
				strcpy(p1->a.id,p2->a.id);
				strcpy(p2->a.id,id1);
				
			}
			p2=p2->next;   //选择排序 
		}
		p=p->next;
	}
	}

}
void freeall(struct clas *head)       //全部释放
{
	struct clas *p1,*p2;
	p1=head;
	while(p1->next!=NULL)
	{
		p2=p1->next;
		p1->next=p2->next;
		free(p2);
                p2=NULL;             //防止野指针
	}
	printf("清除成功!\n");
}
void  filewrite(struct clas *head)     //文本写入
{
	struct clas *p1=head;
	FILE *fp;
	fp=fopen("shop.txt","w");
	int num;
	printf("请选择是否要存入已存入的商品信息:如果是请输入“1”,否则输入“2”.\t");
	scanf("%d",&num);
	fflush(stdin);                       //这个函数的功能具体是什么? 
//	fprintf(fp,"以下为所有的商品信息:((类别)1.食品 2.化妆品 3.日用品 4.饮料  \n商品类别\t商品名称\t商品价格\t商品库存\t商品厂家\t商品品牌\n"); 
	if(num==1)
	{
		if(fp==NULL)
		{
			printf("文件打开错误,程序无法继续运行!\n");
			exit(0);           //表示程序正常退出。 
		}
		while(p1->next!=NULL)
		{
			fprintf(fp,"%d %s %d %d %s %s ",p1->next->a.t,p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
			p1=p1->next;
		}
		fclose(fp);
		printf("数据存储完毕\n");
		return ;
	}
	else if(num==2)
	{
		return ;
	}
	else
	{
		printf("输入错误!\n");
	}
	p1=head;
}
void fileread(struct clas *head)      //文本读取
{
	struct clas *p1=head;
	FILE *fp;
	fp=fopen("shop.txt","r");
	if(fp==NULL)
	{
		printf("打开文件失败!\n");
	}
	else
	{
	    //printf("以下为所有的商品信息:((类别)1.食品 2.化妆品 3.日用品 4.饮料  \n商品类别\t商品名称\t商品价格\t商品库存\t商品厂家\t商品品牌\n"); 
		while(p1->next!=NULL)
		{
			fscanf(fp,"%d%s%d%d%s%s",&p1->next->a.t,p1->next->a.name,&p1->next->a.price,&p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
		    printf("%d%s%d%d%s%s",p1->next->a.t,p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
		    p1=p1->next;

		}
	}
	fclose(fp);
	p1=head;
}
void  filewrite1(struct clas *head)      //二进制写入
{
	struct clas *p1=head;
	FILE *fp;
	fp=fopen("shop.dat","wb");
	int num;
	printf("请选择是否要存入已存入的商品信息:如果是请输入“1”,否则输入“2”.\t");
	scanf("%d",&num);
	fflush(stdin);                       //这个函数的功能具体是什么? 
//	fprintf(fp,"以下为所有的商品信息:((类别)1.食品 2.化妆品 3.日用品 4.饮料  \n商品类别\t商品名称\t商品价格\t商品库存\t商品厂家\t商品品牌\n"); 
	if(num==1)
	{
		if(fp==NULL)
		{
			printf("文件打开错误,程序无法继续运行!\n");
			exit(0);           //表示程序正常退出。 
		}
		while(p1->next!=NULL)
		{
			//fprintf(fp,"%d\t\t%s\t\t%d\t\t%d\t\t%s\t\t%s\n",p1->next->a.t,p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
			fwrite(&p1->next->a,sizeof(struct clas *),1,fp);
			p1=p1->next;
		}
		fclose(fp);
		printf("数据存储完毕\n");
		return ;
	}
	else if(num==2)
	{
		return ;
	}
	else
	{
		printf("输入错误!\n");
	}
	p1=head;
}
void fileread1(struct clas *head)     二进制读取
{
	struct clas *p1=head;
	FILE *fp;
	fp=fopen("shop.dat","rb");
	if(fp==NULL)
	{
		printf("打开文件失败!\n");
	}
	else
	{
	 //   printf("以下为所有的商品信息:((类别)1.食品 2.化妆品 3.日用品 4.饮料  \n商品类别\t商品名称\t商品价格\t商品库存\t商品厂家\t商品品牌\n"); 
		while(p1->next!=NULL)
		{
		
		//	fscanf(fp,"%d%s%d%d%s%s",p1->next->a.t,p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
		    fread(&p1->next->a,sizeof(struct clas *),1,fp);
		  	printf("%d%s%d%d%s%s\n",p1->next->a.t,p1->next->a.name,p1->next->a.price,p1->next->a.stock,p1->next->a.factory,p1->next->a.id);
			p1=p1->next;
		}
	}
	fclose(fp);	
	p1=head;      //返回头不要忘不然只能一次读取
}
void y()            //返回登陆界面
{
	int num;
	loop:printf("请输入您要进去的登陆页面:1.管理员登陆 2.已注册账号登陆\n");
	scanf("%d",&num);
	if(num==1)
	{
		bestwindow();
		passrand();
	}
	else if(num==2)
	{
		int NUM;
		printf("若未注册账号,请输入3退出该界面,否则随意输入一个数字继续操作:\n");
		scanf("%d",&NUM);
		if(NUM==3)
		{
			goto loop;
		}
		window();
		passrand();
	}
	else
	{
 	printf("无效输入:\n");
 	y();
	}
}
int main()
{
	system("color 78");
	int num;
	loop1:printf("请输入您要使用的功能:1.管理员登录 2.注册账号登录(只能输入数字)\n");
	scanf("%d",&num);
	if(num==1)
	{
		loop2:bestwindow();
		passrand();

	}
	else if(num==2)
	{
		createwindow();	
		loop3:window();
		passrand();
	}
    else
    {
    	printf("输入错误,请重新输入:\n");
    	goto loop1;
	}
	struct clas *head=(struct clas *)malloc(sizeof(clas));
	struct clas *p1=head;
	p1->next=NULL;
	printf("1.食品 2.化妆品 3.日用品 4.饮料\n");
	system("color 3F");
	system("mode con cols=400 lines=30000 ");//调整系统Console控制台显示的宽度和高度,高度为?个字符,宽度为?个字符
    while(1)
	{
		system("date/t");
		system("time/t");
		printf(" ==============================================================================\n");
		printf("||                            超市商品管理系统                                ||\n");
		printf("||============================================================================||\n");
		printf("||                 ①--创建初始商品信息  (类别)1.食品 2.化妆品 3.日用品 4.饮料||\n");
		printf("||                 ②--删除某商品信息                                         ||\n");
		printf("||                 ③--查询某商品信息                                         ||\n");
		printf("||                 ④--修改某商品信息                                         ||\n");
		printf("||                 ⑤--添加某商品信息                                         ||\n");
		printf("||                 ⑥--清空商品信息                                           ||\n");
		printf("||                 ⑦--商品排序                                               ||\n");
		printf("||                 ⑧--查看所有商品信息                                       ||\n");
		printf("||                 ⑨--购买某商品                                             ||\n");
		printf("||                 ⑩--存储信息于文件(txt)                                  ||\n");
		printf("||                 ①①--读取文件信息(txt)                                  ||\n");
		printf("||                 ①②--存储信息于文件(dat)                                ||\n");
		printf("||                 ①③--读取文件信息(dat)                                  ||\n");
		printf("||                 ①④----返回登录界面(退出前请保存商品信息!)               ||\n");
		printf("||                 〇--退出系统                                               ||\n");
		printf(" ==============================================================================\n");
		printf("请选择对应服务种类:");
		int n;
		scanf("%d",&n);
		switch(n)
			{
				case 1 :system("CLS");create(head);system("pause");break;	
				case 2 :system("CLS");print(head);frees(head);system("pause");break;
				case 3 :system("CLS");find(head);system("pause");break;
				case 4 :system("CLS");print(head);change(head);system("pause");break;
				case 5 :system("CLS");print(head);add(head);system("pause");break;
				case 6 :system("CLS");freeall(head);system("pause");break;
				case 7 :system("CLS");print(head);sorts(head);print(head);system("pause");break;
				case 8 :system("CLS");print(head);system("pause");break;
				case 9 :system("CLS");print(head);sell(head);system("pause");break;
				case 10 :system("CLS");print(head);filewrite(head);system("pause");break;
				case 11 :system("CLS");fileread(head);system("pause");break; 
				case 12 :system("CLS");print(head);filewrite1(head);system("pause");break; 
				case 13 :system("CLS");fileread1(head);system("pause");break; 
				case 14 :system("CLS");y();break; 
				case 0 :system("CLS");printf("谢谢使用!!");exit(0);system("pause");
				default:system("CLS");printf("选择错误! 请重新选择!\n\n");system("pause");
			}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43678290/article/details/84643062
今日推荐