求解程式的問題并修改,孩實在改不過來了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct address
{ //定義結構
char name[10];
char street[50];
char city[10];
char nation[15];
char tel[7];
struct address*next; //后繼指標
struct address*prior; //前驅指標
}
struct address*start; //首結點
struct address*last; //尾節點
struct address*find(char*);//宣告查找函式
void enter(); //函式宣告
void search();
void save();
void load();
void list();
void ddelete(struct address**start,struct address**last);
void insert(struct address*i,struct address**start,struct address**last);
void inputs(char*,char*,int);
void display(struct address*);
int menu_select(void);
void main()
{
start=last=NULL;
for(;;)
{
switch(menu_select())
{
case1:enter();
continue;
case2:ddelete(&start,&last);
continue;
case3:list();
continue;
case4:search();
continue;
case5:save();
continue;
case6:load();
continue;
case7:exit(0);
}
}
}
int menu_select(void) //主目錄
{
char s[80];
int c;
printf("********^歡迎使用DOS通訊錄系統^*********\n");
printf("********請在其他操作之前先匯入**********\n");
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");
do{
printf("\n Please enter you choice:\n");
gets(s);
c=atoi(s);
}while(c<0||c>7);
return c; //回傳輸入值
}
void enter() //輸入函式,本函式回圈輸入資料,當輸入姓名為空時退出
{
struct address*info; //定義當前結點
for(;;)
{
info=(struct address*)malloc(sizeof(struct adress)); //為當前結點分配空間
if(!info)
{
printf("\n Out of memory");
exit(0); //如果分配空間失敗,退出程式
}
printf("輸入空姓名結束:\n");
inputs("請輸入 姓名:",info->name,10);
if(!info->name[0])break; //如果輸入姓名為空,結束回圈
inputs("請輸入 街道:",info->street,50);
inputs("請輸入 城市:",info->city,15);
inputs("請輸入 民族:",info->nation,15);
inputs("請輸入 電話:",info->tel,7);
insert(info,&start,&last); //呼叫結點插入函式
}
}
void inputs(char*prompt,char*s,int count) //輸入函式,有越界檢測功能
{
char p[255];
do
{print(prompt);
fgets(p,254,stdin);
if(strlen(p)>count)
printf("\nToo Long\n");
}
while(strlen(p)>count);
p[strlen(p)-1]=0;
strcpy(s,p);
}
void insert( struct address*i, struct address**start, struct address**last )
//資料插入函式
{
if(*last==NULL) //如果尾節點為空,意味著當前鏈表為空
{
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;
}
else
{
(*last)->next=i;
i->prior=*last;
i->next=NULL;
*last=(*last)->next;
}
}
void ddelete(struct address**start,struct address**last) //洗掉函式
{
struct address *info;
char s[80];
inputs("請輸入 姓名;",s,10); //輸入欲洗掉結點name域內容
info=find(s); //查找該內容
if(info) //如果找到
{
printf("Deleting……\n");
if(*start==info) //如果該結點為首結點,把該結點的下驅作為作為新的首結點(入口)
{
*start=info->next;
if(*start)
(*start)->prior=NULL;
else *last=NULL;
}
else //如果欲洗掉的結點不是首結點
{
info->prior->next=info->next; /*令該結點的前驅的next指標指向該結點的后驅,又 令該結點的后驅prior指點指向該結點的前驅*/
if(info!=*last) //如果該節點是尾結點,則令該節點的前驅為尾結點
info->next->prior=infor->prior;
else
*last=info->prior;
}
free(info); //釋放該結點所占用的記憶體
printf("-OK,洗掉成功!\n");
}
}
struct address *find(char *name) //查找函式,形參為欲查找結點的name域
{
struct address *info;
info=start;
while(info)
{
if(!strcmp(name,info->name))
return info;
info=info->next;
}
printf("未找到相關資訊,\n");
return NULL;
} //輸出整個鏈表
void list(void)
{
struct address*info;
info=start;
if(info ==NULL)
printf("當前記錄為空!");
else printf("姓名\t街道\t\t城市\t民族\t電話\t\n");
while(info)
{
display(info);
if(info->next==NULL){break;}info=info->next;
}
printf("\n\n");
}
void display(struct address*info) //輸出傳入結點函式
{
printf("%s\t",info->name);
printf("%s\t",info->street);
printf("%s\t",info->city);
printf("%s\t",info->nation);
printf("%s\t",info->tel);
printf("\n");
}
void search(void) //查找函式
{
char name[40];
struct address *info;
printf("輸入要查找的姓名:"); //輸入欲查找的姓名
gets(name);
info=find(name);
if(!info)
printf("姓名不存在\n"); //如果沒找到,顯示Not found
else
display(info); //如果找到,顯示該結點資料
}
void save(void) //保存函式
{
struct address*info;
FILE*fp;
fp=fopen("record,txt","wb"); //生成檔案
if(!fp)
{
printf("Cannot open file.\n");
return;
}
printf("\nSaveing……\n");
info=start;
while(info) //把鏈表寫入檔案
{
fwrite(info,sizeof(struct address),1,fp);
info=info->next;
}
printf("-ok!\n");
fclose(fp); //鏈表全部寫入檔案后,關閉檔案
}
void load() //呼叫預存檔案函式
{
register int t, size;
struct address *info,*temp=0;
char*p;
FILE*fp; //打開檔案
if((fp=fopen("record.txt", "r"))==NULL)
{
printf("Cannot open file!\n");
return;
}
printf("\n\nLoading...\n"); //呼叫檔案
size=sizeof(struct address); //為結點分配記憶體
start=(struct address*)malloc(size);
if(!start) //如果讀取失敗,回傳
{
printf("Out of memory!\n");
exit(0);
}
info=start;
p=(char*)info;
while((*p++=getc(fp))!=EOF)
{
for(t=0;t;<size-1;++t)
*p++=getc(fp);
info->next=(struct address*)malloc(size);
if(!info->next)
{
printf("Out of memory!\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
p=(char*)info;
}
temp->next=0;
last=temp;
start->prior=0;
fclose(fp);
printf("-OK!\n");
}
uj5u.com熱心網友回復:
沒看明白你的問題是啥啊?轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/30023.html
標籤:數據庫報表
上一篇:MSSQL批量替換
下一篇:課設作業(?ω?)
