C language curriculum design templates

First, look at a self-made design framework:
Here Insert Picture Description

I personally feel that the design c language information management system, the basic framework is above
example:
1, student information management system
2, hotel management system
3, train ticketing management system
4, staff information management system
5, library information management system
such systems, nothing more than to come up with data from the disk, the data show about it again after the operation, and written to disk, so that the whole is much easier to look
similar to the above system, you can apply the following template itself modify use

Wherein the main difficulties:
1, array of structures, chain structures
2, the standard file I / O operations
3, programming skilled

Next on the code:

//编译环境:Dev c++编译器 (不同的编译器可能有细微的差别,可自行改动调试) 
//编译语言:C
//数据结构:结构体、单链表
//可以套用大多数的信息管理系统

//框架与设计介绍:

//1、登录模块
//2、功能子模块
//3、主菜单调用 

//其中可扩展部分在框内自行添加,无需改动的会标注 

#include<stdio.h>
struct Information
{
	char name[20];	//姓名
	char number[20];//编号
	int age;		//年龄
	//**自行添加**
	struct Information *next;//连接下一个节点
};
const unsigned char allchar[63] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生字符串使用 
char Str[10];//随机产生字符串使用 


/////////////////////////////////////////  1、登陆模块  ////////////////////////////////////////// 
void Verification()		//随机产生字符串
{
	int i=6;
	srand((unsigned int)time(NULL));
	while(i--)
	{
		Str[5-i]=allchar[rand()%62];
	}
	Str[5]='\0';
}
  

void Validation(char account[],char password[])	//函数用于验证账号密码的正确性
{
	char s_account[20],s_password[20],s_verification[10];
	while(1){	
	printf("请输入账户:");
	scanf("%s",&s_account);
	printf("请输入密码:"); 
	scanf("%s",&s_password);
	Verification();							//调用函数产生验证码 
	printf("请输入验证码(%s):",Str);
	scanf("%s",&s_verification);
	if((strcmp(s_account,account)==NULL)&&(strcmp(s_password,password)==NULL))	//如果账户和密码正确
	{
		if((strcmp(Str,s_verification)==NULL))
		{
			printf("登陆成功!\n");
			break;
		}
		printf("验证码错误!\n");
		Sleep(2000);
		system("cls"); 
		continue; 
	}
	printf("账户或密码验证失败!\n");
	Sleep(2000);
	system("cls"); 
	}
}
void Landing()		//函数用于从磁盘中读取账户和密码数据,并且传递给 Validation函数进行验证 
{
	//打开Landing.txt文件,读取账户和密码信息
	char account[20];	//账号
	char password[20];	//密码
	char Verification[10];	//验证码
	 
	FILE *fp;
	if((fp=fopen("Landing.txt","r"))==NULL)
	{
		printf("Landing.txt文件打开失败!\n");
		exit(0);
	}
	fscanf(fp,"账号: %s 密码: %s\n",&account,&password);
	fclose(fp);
	Validation(account,password);	//把磁盘中的账户和密码传入验证函数
}


////////////////////////////////////  2、功能子模块   //////////////////////////////////////// 

//**自行添加别的函数模块**

struct Information * Initialize(struct Information *head)	//调用函数进行初始化(从磁盘文件中读取信息)
{
	FILE *fp;
	struct Information *p,*q;		//head指向链表头,q和q来进行尾插法 
	p=q=NULL;
	if((fp=fopen("data.txt","r"))==NULL)//判断文件是否正确打开 
	{
		printf("file open fail !\n");
		exit(0);
	}
	if(fgetc(fp)==EOF)		//判断文件是否为空 
	{
		printf("磁盘文件为空!\n");
		return(head);
	}
	rewind(fp);			//由于上面fgetc使文件位置指针移动,让其还原 
	head=q=p=(struct Information *)malloc(sizeof(struct Information));	//分配空间 
	
	//**fscanf按照结构体自行修改,不懂格式看链接:https://blog.csdn.net/qq_42856154/article/details/93225821 掌握文件I/O操作**
	fscanf(fp,"姓名:%s 编号:%s 年龄:%d\n",p->name,p->number,&p->age);		//已经判断文件有数据,读取第一个数据,作为头节点 
	
	while(!feof(fp))	//feof(fp):当返回值为1时,文件到达尾部当返回值为0时,文件未到达尾部  
	{
		p=(struct Information *)malloc(sizeof(struct Information));
		
		//**fscanf按照结构体自行修改
		fscanf(fp,"姓名: %s 编号:%s 年龄: %d\n",p->name,p->number,&p->age);	//格式读取信息,一次一行 
		q->next=p;
		q=p;
	}
	p->next=NULL;		//尾节点next域赋值为NULL 
	p=q=NULL;			//指针指向为NULL 
	fclose(fp);			
	return(head);		//返回链表的头指针 
}




struct Information * Increase(struct Information *head)		//增加节点(在链表尾增加) 
{
	struct Information *p,*q;
	q=head;
	
	p=(struct Information *)malloc(sizeof(struct Information));
	
	//**按照自己定义的结构体自行修改 
	printf("请输入姓名:");
	scanf("%s",p->name);
	printf("请输入编号:");
	scanf("%s",p->number);
	printf("请输入年龄:");
	scanf("%d",&p->age);
	
	p->next=NULL;
	if(head==NULL)		//当头节点不存在时,把创建的节点直接当作头节点 
	{
		head=p;
	}
	else
	{
		while(q->next!=NULL)
			q=q->next;
		q->next=p;
	}
	q=p=NULL;
	printf("增加成功!\n");
	return head;
}

struct Information * Delete(struct Information *head)		//删除节点 (按编号删除)
{
	int i=0;
	char num[20];
	struct Information *p,*q;
	p=q=head;
	printf("请输入要删除的编号:");
	scanf("%s",num);
	while(q!=NULL)
	{
		if(i>1)p=p->next;
		i++;
		if((strcmp(num,q->number)==NULL))
		{
			printf("已经找到是第 %d 个节点\n",i);
			goto delete;
		}
		q=q->next;
	}
	printf("该编号信息不存在!\n");
	return head;
	
delete:
	if(i==1)			//删除头节点 
	{
		head=q->next;
		printf("头节点删除成功!\n");
		return head;
	}
	if(q->next==NULL)	//删除尾节点 
	{
		p->next=NULL;
		p=q=NULL;
		return head;
	}
	p->next=q->next;	//删除中间节点  
	free(q);
	q=NULL;
	printf("删除成功!\n"); 
	return head;
}

int Find(struct Information *head)		//查找节点信息 (按编号查找)
{
	int i=0;
	char num[20];
	printf("请输入要查找的编号:");
	scanf("%s",num);
	while(head!=NULL)
	{
		i++;
		if((strcmp(num,head->number)==NULL))
		{
			printf("已经找到在: %d 个节点\n",i);
			
			//**printf按照结构体自行修改
			printf("姓名: %s 编号:%s 年龄: %d\n",head->name,head->number,head->age);
			return 0;
		}
		head=head->next;
	}
	printf("该编号信息不存在!\n");
	return 0;
}

int Change(struct Information *head)		//修改节点信息 (按编号修改)
{
	//该函数可以做成(按照名称、学好、编号查找,本例为编号查找),可以自行修改 
	char num[20];
	printf("请输入要修改的编号:");
	scanf("%s",num);
	while(head!=NULL)
	{
		if((strcmp(num,head->number)==NULL))
		{
			printf("姓名: %s 编号:%s 年龄: %d\n",head->name,head->number,head->age);
			printf("请输入除编号外的其它信息\n\n");
			printf("请输入姓名:");
			scanf("%s",head->name);
			printf("请输入年龄:");
			scanf("%d",&head->age);
			printf("姓名: %s 编号:%s 年龄: %d\n",head->name,head->number,head->age);
			printf("\n修改成功!\n");
			return 0;
		}
		head=head->next;
	}
	printf("该编号信息不存在!\n");
	return 0;
}

int Print_list(struct Information *head)	//打印链表数据 
{
	if(head==NULL)
	{
		printf("链表为空!\n");
		return 0;
	}
	while(head!=NULL)		//当头指针不为NULL,表示头节点有数据 
	{
		
		//**printf按照结构体自行修改
		printf("姓名: %s 编号:%s 年龄: %d\n",head->name,head->number,head->age);
		head=head->next;
	}
	return 0;
}


void Save(struct Information *head)
{
	FILE *fp;
	if((fp=fopen("data.txt","w"))==NULL)//判断文件是否正确打开 
	{
		printf("file open fail !\n");
		exit(0);
	}
	while(head!=NULL)
	{
		
		//**fprintf按照结构体自行修改
		fprintf(fp,"姓名: %s 编号:%s 年龄: %d\n",head->name,head->number,head->age);
		head=head->next;
	}
	printf("保存磁盘成功!\n");
	fclose(fp);
}


///////////////////////////////////     3、主菜单调用     ///////////////////////////////////////////
void Menu()
{
	Sleep(2000);
	system("cls");
	
	//**按照函数功能自行修改菜单目录
	 
	printf("\t\t\t*****功能主菜单*****\n\n\n");
	printf("\t\t*** 增加节点 ***\t\t--1\n");
	printf("\t\t*** 删除节点 ***\t\t--2\n");
	printf("\t\t*** 修改节点 ***\t\t--3\n");
	printf("\t\t*** 查询节点 ***\t\t--4\n");
	printf("\t\t*** 打印链表 ***\t\t--5\n");
	printf("\t\t*** 保存磁盘 ***\t\t--6\n");
	printf("\t\t*** 退出系统 ***\t\t--7\n");
} 


int main()
{
	//main函数内可能变化较大,所以根据自己的喜好进行排布代码 
	
	int flag = 1;
	int n;
	struct Information *Head = NULL;	//头指针(头指针为空,只是用来指向头节点)
	Landing();			//1、登陆模块调用,使用默认的账户和密码进行验证(**登陆**) 
	Head = Initialize(Head);	//2、调用函数进行初始化(**从磁盘文件中读取信息恢复链表**)
	
	while(flag)
	{
		Menu();
		printf("请选择:");
		scanf("%d",&n);
		switch(n)
		{
			case 1:Head = Increase(Head);break;
			case 2:Head = Delete(Head);break;
			case 3:Change(Head);break;
			case 4:Find(Head);break;
			case 5:Print_list(Head);break;
			case 6:Save(Head);break;
			case 7:flag = 0;break;
			default:printf("输入错误!\n");	
		}
	} 
	return 0;
}

Overall the code is not long, but the basic function is perfect, but there will be strict test data BUG, ​​but as learning is possible

Some of these procedures done, the greatest feeling is :( first planting trees, trimming branches and go landscaping leaves)

OK
code is generally introduced:
1, languages: C language
2, data structures: structure list as the core of the entire code
3, file operations: formatted text file write and read operations
4, landing module: Password authentication random + codes
5, the functional sub-modules:

  • Add a node
  • Delete Node
  • Modify the node
  • Query node
  • Initialization data is read from the disk initialization list
  • Save the entire list to the disk (not append, overwrite the file directly)
  • Print the whole list

(There are two txt format under the project directory files, data.txt and Landing.txt)
data.txt: save the linked list data
Landing.txt: Save login ID and password data
to establish two txt file in the project directory, data .txt data may not exist, but to exist, then it must be in this format, Landing.txt be sure to save the contents of the picture

Here Insert Picture Description

Recommendation:
1, landing module sub-user accounts and administrator accounts, permissions set when the operation function Functions can enhance the whole process
2, sub-modules can add some statistics class module
3, main function of the design, it should be continuous improvement of the order of call, so the code looks compact clarity, call control this one, determines the final operating results and experience

He said that, if you want to take, it is recommended to run a race specific, coupled with the (comments) add their own modifications
Finally, attach a chicken soup: must know that this practice is essential will

What questions, please leave a message in the comments section

Guess you like

Origin blog.csdn.net/qq_42856154/article/details/93376716