⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydefine.cpp

📁 用户信息的增加、修改、删除、查询等功能
💻 CPP
字号:
#include "stdafx.h"
#include "MyDefine.h"

LNode *Head;
char names[10000][10]={""};//用户存放用户姓名
int usernum=0;//用户个数

////////////////
//读取文本文件内容到链表中
void ReadTxtFile(CString FileName)
{
	FILE *in;
	in=fopen((const char*)FileName,"rt");
	if(!in)
	{
		AfxMessageBox("不能打开该文件!");
		fclose(in);
		return; 
	}
	CStdioFile infile(in);
	char s[2048];
	while(infile.ReadString(s,2048))
	{
		if(s[strlen(s)-1]=='\n')
			s[strlen(s)-1]='\0';
		Head=InsertDataList(Head,s);
	}
	infile.Close();
}

///////////////
//写数据到文本文件
void WirteTxtFile(CString FileName)
{
	FILE *out;
	out=fopen((const char*)FileName,"wt");
	if(!out)
	{
		AfxMessageBox("不能打开该文件!");
		fclose(out);
		return; 
	}
	CStdioFile outfile(out);
	CString line;
	int length=LengthOfList(Head);
	for(int i=0;i<length;i++)
	{
		line=names[i];
		outfile.WriteString(line+'\n');
	}
	outfile.Close();
	AfxMessageBox("文件保存成功!");
}

///////////////
//创建空链表
LNode *CreateList(LNode *Head)
{
	Head=(LNode *)malloc(sizeof(LNode));
	strcpy(Head->name,"");
	Head->next=NULL;
	return Head;
}

///////////////
//向链表中插入数据
LNode *InsertDataList(LNode *Head,char *data)
{
	if(strcmp(data,"")==0)
	{
		AfxMessageBox("插入数据不能为空!");
		return Head;
	}
	LNode *p0,*p1,*p2;
	p1=Head;
	p0=(LNode *)malloc(sizeof(LNode));
	strcpy(p0->name,data);
	while((strcmp(p0->name,p1->name)>0)&&(p1->next!=NULL))
	{
		p2=p1;p1=p1->next;
	}
	if(strcmp(p0->name,p1->name)<0)
	{
		if(p1==Head)
		{
			p0->next=p1;
			Head=p0;
		}
		else
		{
			p2->next=p0;
			p0->next=p1;
		}
	}
	else if(strcmp(p0->name,p1->name)>0)
	{
		p1->next=p0;
		p0->next=NULL;
	}
	else
	{
		AfxMessageBox("数据重复!");
	}
	return Head;
}

//////////////
//删除链表中特定数据
LNode *DeleteDataList(LNode *Head,char *data)
{
	if(strcmp(data,"")==0)
	{
		AfxMessageBox("不能删除空数据!");
		return Head;
	}
	LNode *p1,*p2;
	p1=Head;
	while((strcmp(p1->name,data)!=0)&&(p1->next!=NULL))
	{
		p2=p1;p1=p1->next;
	}
	if(strcmp(p1->name,data)==0)
	{
		if(p1==Head)
		{
			Head=p1->next;
			free(p1);
		}
		else
		{
			p2->next=p1->next;
			free(p1);
		}
	}
	else
	{
		AfxMessageBox("被删除数据不能为空!");
	}
	return Head;
}

/////////////
//修改链表中数据
LNode *ModifyDataList(LNode *Head,char *olddata,char *newdata)
{
	if((strcmp(olddata,"")==0)||(strcmp(newdata,"")==0))
	{
		AfxMessageBox("修改数据时,源数据和目的数据不能为空!");
		return Head;
	}
	LNode *p1,*p2;
	p1=Head;
	while((strcmp(p1->name,olddata)!=0)&&(p1->next!=NULL))
	{
		p2=p1;p1=p1->next;
	}
	if(strcmp(p1->name,olddata)==0)
	{
		strcpy(p1->name,newdata);
	}
	else
	{
		AfxMessageBox("被修改数据不能为空!");
	}
	return Head;
}

/////////////
//读取链表中数据,将链表中数据放入中间状态缓存,如:names数组中,以便后面插入ListBox中去
LNode *ReadDataList(LNode *Head,char *data)
{//如果data为"*"则为全部读,否则按data具体值读
	if(strcmp(data,"")==0)
	{
		AfxMessageBox("查询不能为空!");
		return Head;
	}
	LNode *p;
	p=Head;
	if(strcmp(data,"*")==0)
	{
		while(p!=NULL)
		{
			strcpy(names[usernum++],p->name);
			p=p->next;
		}
	}
	else
	{
		while((strcmp(p->name,data)!=0)&&(p->next!=NULL))
		{
			p=p->next;
		}
		if(strcmp(p->name,data)==0)
		{
			strcpy(names[usernum++],p->name);
		}
		else
		{
			AfxMessageBox("不能找到该数据!");
		}
	}
	return Head;
}

////////////
//计算链表的长度
int LengthOfList(LNode *Head)
{
	LNode *p;
	p=Head;
	int num=0;
	while(p!=NULL)
	{
		num++;
		p=p->next;
	}
	return num;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -