当前位置:网站首页>C language course design (detailed explanation of clothing management system)

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

2022-06-21 05:58:00 Xiao Hou doesn't lie flat

I was working on last week and next week C Curriculum design of language , So updating new knowledge is a little slow , This blog will lead you to an in-depth understanding of c The operation of language files and the problems I encountered when writing code .

First, I set up the user login system 、 The administrator logs in to the system . After logging in to the system, the user will compare the size of the clothes filled in by the user with the rest in the clothing system, so as to recommend the clothes that meet her size to the user .

Implementation of this code , It may be because I have been clumsy for a long time and have been using it in reading files fwrite and fread. So I can't read the information in the clothing text to the correct position, so I keep thinking about why it read All the information will be read into the first character array of the structure , Then I found out in the course design that read This function reads one line of information , There is no way to store and read information one by one , So I'm going to put all of the fwrite and fread Change to fprintf and fscanf. After compiling, it is found that the user will normally recommend clothing information after logging in . This is my own thinking , Then it is found that it can only recommend one clothing type after output , But I also used it feof To determine if it's at the end, so I use a loop . I found that I couldn't get out of the recycling , And in if The loop doesn't know how to get out after judging equality in , So I listened to my friend's advice and used a counter , First count the whole , Then the next cycle will exit after the total number of clothes . But another problem is that after reading the file, the pointer points to the end of the file , So we have to use fseek() The function causes the pointer to point back to the beginning of the file in the garment text , Read it again . In fact, I think the function can also use the linked list, but I have a lot of code and functions. If I use the linked list, it will be very messy , And hard to find bug So I chose the counter method . After the lesson design, I will try to use the linked list to analyze its functions .

The code of this module is as follows :

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 The same size clothes as yours are still in stock :\n"); 
	printf("\t Clothing brand \t Clothing number \t Clothing type \t Clothing size \t Clothing prices \t Clothing inventory \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 the implementation of another function is a little difficult , For example, the data to be saved and the data to be read during user registration and login . And compare them for equality , The realization of the two functions is not bad , So let's go straight to the registration and login code !

void Regist()
{
	administrator a={0},b={0};
	char tmp[20]={-1};		// Used to determine whether the passwords are the same  
	FILE *pf = NULL;
	pf = fopen("administrator.txt","r");	// use pf To point to a file , A file is a file that already exists   read only mode  
	if(pf == NULL)
	{
		printf(" Failed to open file during registration \n");
		return ;
	}
	printf("\t\t\t Welcome to the registration screen \n\n");
	printf("\t\t\t Enter account ->");
	scanf("%s",a.id);
	printf(" Input successful !\n"); 

//【 The registration screen 】 
	printf("\t\t\t Please enter a name ->"); 
	scanf("%s",a.name);
	printf("\t\t\t Please enter gender : male / Woman ->"); 
	do
	{		// Enter the gender and see if it is correct  
		getchar();
		scanf("%s",a.sex);
	}while(1);
			
		printf("\t\t\t Please input a password ->"); 
		scanf("%s",a.paw);
		printf("\t\t\t Please enter the password again ->"); 
	do	// Determine whether the two passwords are equal  
	{
		scanf("%s",tmp);
		if(strcmp(tmp,a.paw) != 0)
			printf("\t\t\t The two passwords are not the same , Please enter the password again ->");
		else
			break;
	}while(1);
		// The two passwords are the same 
		fclose(pf);	
		pf = NULL;
		pf = fopen("administrator.txt","a");	// Write file as append  
		//fwrite Writes data at the position of the current file pointer 
		//"w"  open , The file pointer goes to the head , Just write ;"a"  open , Point to the end of the file , No coverage . 
		fprintf(pf,"%s %s %s %s",a.name,a.paw,a.id,a.sex);	// take a The data is stored in the file 
		printf("\t\t\t Registered successfully !\n"); 
		fclose(pf);	
		pf = NULL;
		system("cls");
		return;	
}


bool Login()	// The return value is a boolean variable  
{
	administrator a={0},b={0};
	FILE *pf = fopen("administrator.txt","r+");	// Open the file in read mode  
	if(pf == NULL)
	{
		printf(" File opening failure \n");
		return false;
	}
	printf(" Welcome to the login screen !\n");
	printf(" Please enter your account number ->");
	scanf("%s",a.id);
	fscanf(pf,"%s %s %s %s",b.name,b.paw,b.id,b.sex);	// Each read Users Length , Read it once . 
	while(1)
	{
		if(strcmp(a.id, b.id) != 0 )
		{
			if(feof(pf) == 0)// Not to the end of the file   Always look back  
			{
				fscanf(pf,"%s %s %s %s",b.name,b.paw,b.id,b.sex);
			}
			else
			{
				printf(" The account does not exist , Please register first \n");
				fclose(pf); 
				pf = NULL;
				return false;
			}
		}
		else// The account has been registered -> Skip to enter password  
		{
			break; // Exit infinite loop , Skip to enter password  
		}
		
	}
//【 Input password 】 
	printf(" Please input a password ->"); 
	do
	{
		scanf("%s",a.paw);
	}while(1);
	printf(" Login successful !\n");
	fclose(pf); 
	pf = NULL;
	system("cls");
	return true;
}

In order to prevent you from copying and pasting the code directly, I have made some cuts to the above code , If you need to set up relevant functions of the class, you can csdn Send me a private letter , I will certainly answer the question .

I personally think the implementation of other codes is very simple, so I won't introduce other codes in detail here . I will take a screenshot of my program , You can have a look. If you need any function, you can send me a private message .

My user login may add many functions later .

  The following figure shows how to recommend the price the user wants :

  The following are the related operations for exiting the system :

  This is the end of my blog , If you want the code for some of the functions here, you can confide in me , Or my blog has some unclear language or ideas , Welcome to help me point out thank you !

 

 

原网站

版权声明
本文为[Xiao Hou doesn't lie flat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210550122271.html