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

📄 city1.4.c

📁 通过单链表做得的课程设计作业
💻 C
字号:
/*film name:城市与人口*/
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

void read_func(void);/*读函数*/ 
void write_func(void);/*存储函数*/
void insert_func(void);/*插入函数*/
void search_func(void);/*查询函数*/
void sort_func(void);/*排序函数*/
void delete_func(void);/*删除函数*/
void display_func(void);/*输出函数*/
void modify_func(void);/*修改函数*/
void anykey_func(void);/*任意键*/
void add_func(void);/*汇总函数*/ 

struct city{
	char name[20];
	long pop;
	struct city *next;
};

struct city *ptr,*head,*this,*prev;
 
main(void)
{
	int option1;
	system("cls");
	read_func();
	while(1)
	{
		printf("******************************\n");
		printf("          1.插入\n");
		printf("          2.删除\n");
		printf("          3.查询\n"); 
		printf("          4.显示\n");
		printf("          5.修改\n");
		printf("          6.汇总\n"); 
		printf("          7.退出\n");
		printf("******************************\n");
		printf("    请输入你的选择(1-7)...");
		option1=getche();
		switch(option1)
		{
			case'1': 
				insert_func();
				break;
			case'2':
				delete_func();
				break;
			case'3':
				search_func();
				break;
			case'4':
				display_func();
				break;
			case'5':
				modify_func();
				break;
			case'6':
				add_func();
				break;
			case'7':
				write_func();
				exit(0);
		}
	}
}

void read_func(void)
{
	FILE *fptr;
	
	head=(struct city *)malloc(sizeof(struct city));
	head->next=NULL;
	
	/*打开文件,若文件不存在,则要求输入第一组数据*/
	if((fptr=fopen("city_list.dat","r"))==NULL)
	{
		printf("城市人口记录文件不存在\n");
		printf("请输入任何键以编辑第一个记录...\n");
		getch();
		insert_func();
	}
	/*文件存在*/ 
	else
	{
		ptr=(struct city *)malloc(sizeof(struct city));
		while(fscanf(fptr,"%s %d",ptr->name,&ptr->pop)!=EOF)
		{
				sort_func();
				ptr=(struct city *)malloc(sizeof(struct city));
		}
		fclose(fptr);
	}
}

void write_func(void)
{
	FILE *fptr;
	fptr=fopen("city_list.dat","w");/*创建一个用于写入的记录数据的文件*/ 
	this=head->next;
	while(this!=NULL)
	{
		fprintf(fptr,"%s %d\n",this->name,this->pop);
		this=this->next;
	}
	fclose(fptr);/*关闭输出文件流*/
}

void insert_func(void)
{
	char p_temp[10];/*创建临时的人口数量记录*/
	ptr=(struct city *)malloc(sizeof(struct city));
	printf("城市名称:");
	gets(ptr->name);
	prev=head;
	this=head->next;
	 
	printf("城市人口数量:");
	gets(p_temp);
	ptr->pop=atoi(p_temp);
	
	sort_func();
	
}

/*以城市人口高低由大排到小排列*/
void sort_func(void)
{
	prev=head;
	this=head->next;
	while((this!=NULL)&&(this->pop>ptr->pop))/*排序*/ 
	{
		prev=this;
		this=this->next;
	}
	ptr->next=this;
	prev->next=ptr;
}
	
void delete_func(void)
{
	char del_name[20];/*创建要删除的记录的临时记录*/
	printf("要删除的城市名称:");
	gets(del_name);
	
	prev=head;
	this=head->next;
	while((this!=NULL)&&(strcmp(this->name,del_name)!=0))/*查找要删除的城市记录*/ 
	{
		prev=this;
		this=this->next;
	}
	if(this!=NULL)
	{
		prev->next=this->next;
		free(this);
		printf("%s 城市的记录已删除\n",del_name);
	}
	else
		printf("城市 %s 没有找到\n",del_name);
		
		anykey_func();
}

void search_func(void)
{
	char sear_name[20];
	printf("\n要查询的城市名称:");
	gets(sear_name);
	
	prev=head;
	this=head->next;
	while((this!=NULL)&&(strcmp(this->name,sear_name)!=0))/*查找要查询的城市记录*/ 
	{
		prev=this;
		this=this->next;
	}
	if(this!=NULL)
		printf("    %s 城市的人口数量是 %d\n",sear_name,this->pop);
	else
		printf("    城市 %s 的记录没有找到\n",sear_name);
		
		anykey_func(); 
}

void modify_func(void)
{
	char n_temp[20],p_temp[10];
	printf("要修改的城市名称:");
	gets(n_temp);
	this=head->next;
	
	while((this!=NULL)&&(strcmp(this->name,n_temp)!=0))
	{
		prev=this;
		this=this->next;
	}
	if(this!=NULL) 
	{
		printf("******************************\n");
		printf("          城市名称:%s\n",this->name);
		printf("          城市人口数量:%d\n",this->pop);
		printf("******************************\n");
		printf("请输入新的人口数量数据:");
		gets(p_temp);
		this->pop=atoi(p_temp);
		printf("%s 城市的人口数量记录已修改\n",n_temp);
	}
	else
		printf("%s 城市 没有找到\n",n_temp);
		
	anykey_func();
}

void display_func(void)
{
	int count=0;
	system("cls");
	if(head->next==NULL)
		printf("没有任何城市人口记录\n");
	else 
	{
		printf("排名              城市名称              城市人口数量\n");
		printf("--------------------------------------\n");
		this=head->next;
		while(this!=NULL)
		{
			count++;
			printf("    %d    %-20s    %d\n",count,this->name,this->pop);
			this=this->next;
			if(count%20==0)
				getch();
		}
		printf("-------------------------------------\n");
		printf("总共有 %d 个城市的记录",(count));
	}
		 
	anykey_func();
}

void add_func(void)
{
	int sum=0,count=0;
	this=head->next;
	
	while(this!=NULL)
	{
		sum+=this->pop;
		count++;
		prev=this;
		this=this->next;
	}
	printf("\n记录中共有 %d 个城市\n共有人口 %ld 人\n平均人口为 %d\n",count,sum,sum/count);
	
	anykey_func();
}

void anykey_func(void)
{
	printf("请输入任何键以继续...");
	getch();
	printf("\n");
}

⌨️ 快捷键说明

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