?? city1.4.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 + -