C language course design (detailed explanation of clothing management system)

I am working on the course design of C language last week and next week, so it is a bit slow to update new knowledge. This blog will lead you to understand the operation of C language files and the problems that arise when I write code.

First of all, I set up the user login system and the administrator login system. After the user logs in, the system will recommend clothes that meet her size according to the comparison between the clothes size filled in by the user and the rest in the clothing system.

The realization of this code may be because I have been using fwrite and fread in file reading for a long time due to my clumsiness. Therefore, the information in the clothing text has not been read in the correct position, so I have been thinking about why it will read all the information into the first character array of the structure when it is read, and then I found in the course design When the read function reads information one by one, there is no way to store and read information one by one, so I changed all fwrite and fread in the code to fprintf and fscanf respectively. Then after compiling, it is found that after the user logs in, the clothing information will be recommended normally. This is my own way of thinking, and after the output, I found that it can only recommend one clothing type, but I also used feof to judge whether it is at the end, so I used a loop. I found that I couldn’t get out of the loop all the time, and I didn’t know how to get out of the loop after judging equality in if, so I listened to my friend’s suggestion and used a counter to count the whole first, and then exit after the next loop reaches the total number of clothes . But there is another problem that the pointer of the file points to the end of the file after reading, so the fseek() function is used to make the pointer point to the beginning of the file in the clothing text again, and read it again. In fact, I think this function can also use the linked list, but my code and functions are a bit too much. If I use the linked list, it will be messy, and it is difficult to find bugs, so I chose the counter method. After the class is over, I will try to analyze its function with a linked list.

The code for this module looks like this:

void userin()
{
	Users y={0};
	stu f={0}; 
	int count=0;
	FILE *pe=fopen("users.txt","r+");
	FILE *pi=fopen("fuzhuang.txt","r+");
	fscanf(pe,"%s %s %s %s %s",y.id,y.name,y.paw,y.sex,y.size);
	fscanf(pi,"%s %s %s %s %d %d",&f.brand,&f.name,&f.num,&f.size,&f.price,&f.stock);
	printf("\t跟您尺码相同的服装库存还有:\n"); 
	printf("\t服装品牌\t服装编号\t服装类型\t服装尺寸\t服装售价\t服装库存\n");
	while(1)
	{
		if(feof(pi)==0)
		{
			fscanf(pi,"%s %s %s %s %d %d",&f.brand,&f.name,&f.num,&f.size,&f.price,&f.stock);
			count++;
		}
		else
			break;

	}
	fseek(pi,0L,SEEK_SET);
	while(count--)
	{
		if(strcmp(y.size,f.size)!=0)
		{
			if(feof(pi)==0)
			{
				fscanf(pi,"%s %s %s %s %d %d",&f.brand,&f.name,&f.num,&f.size,&f.price,&f.stock);
			}
		}
		else
		{
			printf("\t%s\t\t%s\t\t%s\t\t%s\t\t%d\t\t%d\n",f.brand,f.num,f.name,f.size,f.price,f.stock);
			if(feof(pi)==0)
			{
				fscanf(pi,"%s %s %s %s %d %d",&f.brand,&f.name,&f.num,&f.size,&f.price,&f.stock);
			}
		}
	}
} 

Of course, in addition to this function, I think there is another function that is a little bit more difficult to implement, such as the data that needs to be saved and read when the user registers and logs in. And it is necessary to compare whether they are equal. The realization of the two functions is not bad, so let's directly look at the registration and login code!

void Regist()
{
	administrator a={0},b={0};
	char tmp[20]={-1};		//判断密码是否相同时使用的 
	FILE *pf = NULL;
	pf = fopen("administrator.txt","r");	//用pf去指向文件,文件是要已经存在的文件 只读模式 
	if(pf == NULL)
	{
		printf("注册时打开文件失败\n");
		return ;
	}
	printf("\t\t\t欢迎来到注册界面\n\n");
	printf("\t\t\t输入账号->");
	scanf("%s",a.id);
	printf("输入成功!\n"); 

//【注册界面】 
	printf("\t\t\t请输入姓名->"); 
	scanf("%s",a.name);
	printf("\t\t\t请输入性别:男/女->"); 
	do
	{		//输入性别并查看是否输入正确 
		getchar();
		scanf("%s",a.sex);
	}while(1);
			
		printf("\t\t\t请输入密码->"); 
		scanf("%s",a.paw);
		printf("\t\t\t请再输入一次密码->"); 
	do	//判断两次密码是否相等 
	{
		scanf("%s",tmp);
		if(strcmp(tmp,a.paw) != 0)
			printf("\t\t\t两次输入密码不一致,请再输入一次密码->");
		else
			break;
	}while(1);
		//两次密码一致
		fclose(pf);	
		pf = NULL;
		pf = fopen("administrator.txt","a");	//以追加的形式写入文件 
		//fwrite会在当前文件指针的位置写入数据
		//"w" 打开,文件指针指到头,只写;"a" 打开,指向文件尾部,不覆盖。 
		fprintf(pf,"%s %s %s %s",a.name,a.paw,a.id,a.sex);	//将a的数据存入文件之中
		printf("\t\t\t注册成功!\n"); 
		fclose(pf);	
		pf = NULL;
		system("cls");
		return;	
}


bool Login()	//返回值是一个布尔变量 
{
	administrator a={0},b={0};
	FILE *pf = fopen("administrator.txt","r+");	//以读的模式打开文件 
	if(pf == NULL)
	{
		printf("文件打开失败\n");
		return false;
	}
	printf("欢迎来到登录界面!\n");
	printf("请输入账号->");
	scanf("%s",a.id);
	fscanf(pf,"%s %s %s %s",b.name,b.paw,b.id,b.sex);	//每次读取Users个长度,读一次。 
	while(1)
	{
		if(strcmp(a.id, b.id) != 0 )
		{
			if(feof(pf) == 0)//未到文件尾 一直向后查找 
			{
				fscanf(pf,"%s %s %s %s",b.name,b.paw,b.id,b.sex);
			}
			else
			{
				printf("该账号不存在,请先注册\n");
				fclose(pf); 
				pf = NULL;
				return false;
			}
		}
		else//账号注册过->跳到输入密码 
		{
			break; //退出无限循环,跳到输入密码 
		}
		
	}
//【输入密码】 
	printf("请输入密码->"); 
	do
	{
		scanf("%s",a.paw);
	}while(1);
	printf("登录成功!\n");
	fclose(pf); 
	pf = NULL;
	system("cls");
	return true;
}

In order to prevent everyone from directly copying and pasting the code, I have made some deletions to the above code . If you have friends who need the relevant functions of the course design, you can private message me on csdn, and I will definitely answer the question.

For other codes, I personally think that the implementation of related functions is very simple, so I will not introduce other codes in detail here. I will take a screenshot of my program, you can take a look, and if you need any functions, you can private message me.

My user login terminal may add many functions in the future.

 The following figure is a recommendation to the user for the price that the user wants:

 The following are related operations to log out of the system:

 The explanation of my blog will end here first. If you want some codes for some functions here, you can private message me, or there are some unclear language or problems in my blog. Welcome to Thanks for pointing it out for me!

 

 

Guess you like

Origin blog.csdn.net/m0_61886762/article/details/125242155