| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 232 人关注过本帖
标题:大家帮忙纠一下错
只看楼主 加入收藏
流星000
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-20
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
大家帮忙纠一下错
#include <stdlib.h>   
#include <string.h>
  
  
struct address{   
    char name[30];   
    char tel[20];
  
    struct address *next;                       /*  pointer to next entry  */  
    struct address *prior;                      /*  pointer to previous record  */  
};   
 
struct address *start;                          /*  pointer to first entry in list  */  
struct address *last;                           /*  pointer to last entry  */  
struct address *find(char*);   

int bIsModify=0;
  
void enter(void),search(void),save(void);   
void load(void),list(void);   
void mldelete(struct address **,struct address **);   
void dls_store(struct address *i,struct address **start,struct address **last);   
void inputs(char *,char *,int);   
void display(struct address *);
void free_nodes(void);  
void new_addrBook(void);
void quit(void);
void link_file(void);
int menu_select (void);
 
  
int main(void)   
{  printf("\n");
 
 printf("\t******************************************\n");
 printf("\t*       这是一个                 *\n");
 printf("\t*    简单的通讯录程序              *\n");
 printf("\t* 可以对通讯录进行简单管理         *\n");
 printf("\t*  欢迎使用通讯录          *\n");
 printf("\t******************************************\n");
    start = last = NULL;                         /*  initialize start and end pointers  */  
  
    for(;;){   
        switch(menu_select()){   
        case 1: enter ();                        /*  enter an address  */  
            break;   
        case 2: list();        /*  display the list  */
   break;   
        case 3: save();                          /*  save list to disk  */                           
            break;   
        case 4: load();                          /*  read from disk  */  
      break;   
        case 5: mldelete(&start,&last);          /*  remove an address  */  
            break;   
        case 6: new_addrBook();                        /*  find an address  */  
            break;
  case 7: search();
   break;
     case 8: link_file();
   break;
  case 9:
   quit();
        }   
    }   
    return 0;   
}   
  
/*  Select an operation.  */  
int menu_select(void)   
{   
    char s[80];   
    int c;   
 
    printf("1.增加记录 \n");   
    printf("2.显示记录 \n");   
    printf("3.保存记录 \n");   
    printf("4.读取记录 \n");   
    printf("5.删除记录 \n");   
    printf("6.新建通讯录 \n");   
    printf("7.查询记录 \n");
 printf("8.连接文件 \n");
 printf("9.结束运行\n");
 printf("\n数字对应功能选择,请选择1-9:");
    do {   
        gets(s);   
        c = atoi(s);
  if(c<1||c>9)
   printf("\n输入错误,重选择1-9:");
  else
   break;
    }while(1);   
    return c;   
}   
  
/*  Enter names and address.  */  
void enter(void)   
{   
    struct address *info;   
  
    for(;;){   
        info= (struct address *)malloc(sizeof(struct address));   
        if(!info){   
            printf("\n内存不足");   
            return;   
        }   
           
        inputs("姓名:", info->name,30);   
        if(!info->name[0]) break;                  /*  stop entering  */  
        inputs("电话:",info->tel,40);   
         
        dls_store(info,&start,&last);   
    }        /*  entry loop  */  
 bIsModify=1;
}   
  
/*  This function will input a string up to the length in count and will  
    prevent the string from being overrun . It will also display a prompting message.  */  
void inputs(char *prompt,char *s,int count)   
{   
    char p[255];   
  
    do {   
        printf(prompt);   
        fgets(p,254,stdin);   
        if(strlen(p) > (unsigned int)count) printf("\n Too Long \n");   
    }while(strlen(p) > (unsigned int)count);   
  
    p[strlen(p)-1] = 0;             // remove newline charater     
    strcpy(s,p);   
}   
  
  
/*  Create a doubly linked list in sorted order.  */  
void dls_store(   
               struct address *i,          /*  new element  */  
               struct address **start,     /*  firsr element in list  */  
               struct address **last       /*  last element in list  */  
)   
{   
    struct address *old,*p;   
  
    if(*last == NULL){           /*  first element in list  */  
        i->next = NULL;   
        i->prior = NULL;   
        *last = i;   
        *start = i;   
        return;   
    }   
    p = *start;                  /*  start at top list  */  
  
    old = NULL;   
    while (p){   
        if (strcmp(p->name,i->name)<0){   
            old = p;   
            p = p->next;   
        }   
        else {   
            if(p->prior){   
                p->prior->next = i;   
                i->next = p;   
                i->prior = p->prior;   
                p->prior = i;   
                return;   
            }   
            i->next = p;           /*  new first element  */  
            i->prior = NULL;   
            p->prior = i;   
            *start = i;   
            return ;   
        }   
    }   
    old->next = i;       /*  put on old  */  
    i->next = NULL;   
    i->prior = old;   
    *last = i;   
}   
  
/*  Remove an element from the list.  */  
void mldelete(struct address **start,struct address **last)   
{   
    struct address *info;   
    char s[80];   
  
    inputs("请输入姓名:",s,30);   
    info = find(s);   
    if(info){   
        if(*start == info){   
            *start = info->next;   
            if(*start)((*start)->prior) = NULL;   
            else *last = NULL;   
        }   
        else{   
            info->prior->next = info->next;   
            if(info!=*last)   
                info->next->prior = info->prior;   
            else  
                *last = info->prior;   
        }   
        free(info);                 /*  return memory to system  */  
    }   
 bIsModify=1;
}   
  
/*  Find an address.  */  
struct address *find(char *name)   
{   
    struct address *info;   
  
    info = start;   
    while (info){   
        if(!strcmp(name,info->name))return info;   
        info = info->next;       /*  get next address  */  
    }   
    printf("Name not found. \n");   
    return NULL;         /*  not found  */  
}   
  
/*  Display the entire list.  */  
void list(void)   
{   
    struct address *info;   
  
    info = start;   
    while(info){   
        display(info);   
        info = info->next;             /*  get next address  */  
    }   
    printf("\n \n");   
}   
  
/*  This function actually prints the fields in each address.  */  
void display(struct address *info)   
{   
    printf("姓名:%s \n",info->name);   
    printf("电话:%s \n",info->tel);   
   
    printf("\n \n");   
}   
  
/*  Look for a name in the list.  */  
void search(void)   
{   
    char name[40];   
    struct address *info;   
  
    printf("请输入你要寻找人得名字:");   
    gets(name);   
    info = find(name);   
    if (!info)printf("Not Found \n");   
    else display(info);   
}   
  
/*  Save the file to disk  */  
void save(void)   
{   
    struct address *info;   
  
    FILE *fp;
 char name[30];
 printf("输入文件名:");
 gets(name);
 if(strlen(name)==0)
  strcpy(name,"textbook.txt");
    fp = fopen(name,"wb");   
    if(!fp){   
        printf("不能打开文件 \n");   
        exit(1);   
    }   
    printf("\n 已经保存了文件 \n");   
  
    info = start;   
    while(info){   
        fwrite(info,sizeof(struct address),1,fp);   
        info = info->next;      /*  get next address  */  
    }   
    fclose(fp);
 bIsModify=0;
}   
  
/*  Load the address file.  */  
void load()   
{   
    struct address *info;   
  
    FILE *fp;  
 char name[30];
   printf("输入文件名:");
 gets(name);
 if(strlen(name)==0)
  strcpy(name,"textbook.txt");
    fp = fopen(name,"rb");   
    if(!fp){   
        printf("打开文件失败!!\n");   
        exit(1);   
    }   
  
    /*  free any previously allocated memory  */  
    while (start){   
        info = start->next;   
        //free(info);   
        start = info;   
    }   
  
    /*  reset top bottom pointers  */  
    start = last = NULL;   
  
    printf("\n 已读取记录 \n");   
    while(!feof(fp)){   
        info = (struct address *)malloc(sizeof(struct address));   
        if(!info){   
            printf("Out of Memory");   
            return;   
        }   
        if(1 != fread(info,sizeof(struct address),1,fp))break;   
        dls_store(info,&start,&last);   
    }   
    fclose(fp);   
}
void free_nodes(void)
{
 
}
void new_addrBook(void)
{
 char str[10];
 if(bIsModify)
 {
  printf("原来的通讯录已改动,是否保存?(Y/n)\n");
  gets(str);
  if(str[0]=='Y'||str[0]=='y'||str[0]=='\0')
   save();
 }
 start=NULL;
 last=NULL;
 enter();
}

void quit()
{
 char str[10];
 if(bIsModify)
 {
  printf("原来的通讯录已改动,是否保存?(Y/n)\n");
  gets(str);
  if(str[0]=='Y'||str[0]=='y'||str[0]=='\0')
   save();
 }
 exit(0);
}
void link_file(void)
{
 FILE *fp1,*fp2;
 struct address *p;
 char fname1[20],fname2[20];
 int num=0;
 printf("请输入要读取的文件名(直接回车选择文件textbook.txt):\n");
 gets(fname1);
 if(strlen(fname1)==0)
  strcpy(fname1,"textbook.txt");
 if((fp1=fopen(fname1,"ab"))==NULL)
 {
  printf("打不开文件!请输入选择\n");
  return;
 }
 printf("请输入另一个要连接的文件名:\n");
 gets(fname2);
 if((fp2=fopen(fname2,"rb"))==NULL)
 {
  printf("打不开文件!请重新选择\n");
  return;
 }
 p=(struct address*)malloc(sizeof(struct address));
 if(NULL==p){printf("内存不足!");exit(-1);}
 while(1)
 {
  if(1!=fread(p,sizeof(struct address),1,fp2)) break;
  if(feof(fp2)!=0) break;
  fwrite(p,sizeof(struct address),1,fp1);
  num++;
 }
 fclose(fp1);
 fclose(fp2);
 free(p);
 bIsModify=1;
 printf("追加%d条记录。\n",num);
}
2010-06-20 16:46
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:10 
少了个头文件
最上面加一行
#include<stdio.h>

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2010-06-20 23:45
key8714
Rank: 2
等 级:论坛游民
帖 子:48
专家分:87
注 册:2010-6-9
收藏
得分:0 
不自动保存
2010-06-20 23:55
快速回复:大家帮忙纠一下错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026305 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved