| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖
标题:帮吗修改下加下注释 呵呵 谢了
只看楼主 加入收藏
新手NO1
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-28
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
帮吗修改下加下注释 呵呵 谢了
#include<stdio.h>
#include<stdlib.h>
struct address{
char name[20];
char phone[15];
char build[10];
struct address*next;
struct address*prior;
}list_entry;
struct address*start;/*point to first entry in list*/
struct address*last; /*point to last entry*/
void search();
main()
{
 char s[80],choice;
 struct address*info;
 start=0;  /*zero length list*/
 for(;;){
        choice=menu_select();
        switch(choice){
              case 1:enter();break;
              case 2:delete();break;
              case 3:list();break;
              case 4:search();break;
              case 5:save();break;
              case 6:load();break;
              case 7:exit(0);
              }
        }
    }

    menu_select()
    {
     char s[80];
     int c;
     printf("1.Enter a name\n");
     printf("2.Delete a name\n");
     printf("3.List the file\n");
     printf("4.Search\n");
     printf("5.Save the file\n");
     printf("6.Load the file\n");
     printf("7.Quit\n");
     do{
         printf("Enter your choice:");
         gets(s);
         c=atoi(s);
         }while(c<0||c>7);
         return c;
         }
         enter()
         {
          struct address*info,*dls_store();
          for(;;){
                  info=malloc(sizeof(list_entry));
                  if(info==0){
                             printf("\nout of memory");
                             return;
                             }
                   inputs("enter name:",info->name,20);
                   if(!info->name[0])break;
                   inputs("enter phone:",info->phone,15);
                   inputs("enter build:",info->build,10);
                   start=dls_store(info,start);
                   }
                 }
                 inputs(prompt,s,count)
                 char*prompt;
                 char*s;
                 int count;
                 {
                  char p[255];
                  do{
                    printf(prompt);
                    gets(p);
                    if(strlen(p)>count)printf("\ntoo long\n");
                    }while(strlen(p)>count);
                    strcpy(s,p);
                    }

                    struct address*dls_store(i,top);
                    struct address*i;
                    struct address*top;
                    {  static stuct address*last=0;
                       struct address*old,*p;
                       if(last==0){
                            i->next=0;
                            i->prior=0;
                            last=i;
                            return i;
                            }
                      p=top;
                      old=0;
                      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;
                                     p->prior=i;
                                     return i;
                                     }
                                  }
                       old->next=i;
                       i->next=0;
                       last=i;
                       return start;
                       }


                       delete()
                       {
                        struct address*info,*find();
                        char s[80];
                        printf("enter name:");
                        gets(s);
                        if(info){
                                 if(start==info){
                                            start=info->next;
                                            if(start)start->prior=0};
                                         else
                                           last=0;
                                           }
                                         else{
                                            info->prior->next=info->next;
                                            if(info!=last)
                                                    info->next->prior=info->prior;
                                                    else
                                                      last=info->prior;
                                                      }
                             free(info);
                             }


                      struct address*find(name)
                      char*name;
                      {
                        struct addrress*info;
                        info=start;
                        while(info){
                               if(!strcmp(name,info->name))return info;
                               info=info->next;
                               }
                             printf("name not found\n");
                             return 0;
                             }
                      list()
                      {
                        register int t;
                        struct address*info;
                        info=start;
                        while(info){
                            display(info);
                            info=info->next;
                            }
                          printf("\n");
                          }


                          display(info)
                          struct address*info;
                          {
                          printf("%s\n",info->name);
                          printf("%s\n",info->phone);
                          printf("%s\n",info->build);
                          printf("\n");
                        }

                        void search()
                        {
                        char name[20];
                        struct address*info,*find();
                        printf("enter name to fing:");
                        gets(name);
                        info=find(name);
                        if(!info)printf("not found\n");
                        else display(info);
                        }
                        save()
                        {
                         register int t,size;
                         struct address*info;
                         char*p;
                         FILE*fp;
                         if((fp=fopen("mlist","wb"))==0){
                           printf("cannot open file\n");
                           exit(0);
                           }
                           printf("\nsaving file\n");
                           size=sizeof(list_entry);
                           info=start;
                           while(info){
                               p=(char*)info;
                               for(t-0;t<size;t++)
                               putc(*p++,fp);
                               info=info->next;
                               }
                               putc(EOF,fp);
                               fclose(fp);
                               }

                               load()
                               {  register int t,size;
                                  struct address*info,*temp=0;
                                  char*p;
                                  FILE*fp;
                                  if(fp=fopen("mlist","r"))==0){
                                     printf("cannot open file\n");
                                     size=sizeof(list_entry);
                                     start=malloc(size);
                                     if(!start){
                                        printf("out of memory\n");
                                        return;
                                        }
                                  info=start;
                                  p=(char*)info;
                                  while((*p++=getc(fp))!=EOF){
                                     for(t=0;t<size-1;++t)
                                       *p++=getc(fp);
                                     info->next=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);
                                       }


                                       struct address*didelete(i,top)
                                       struct address*i;
                                       struct address*top;
                                       {
                                       if(i->prior)i->prior->next=i->next;
                                       else{
                                             top=i->next;
                                             if(top)top->prior=0;
                                             }
                                           if(i->next) i->next->prior=i->prior;
                                           return(top);
                                           }




搜索更多相关主题的帖子: 注释 
2010-04-06 09:13
新手NO1
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-28
收藏
得分:0 
有点长 帮忙改下错误啊 检查好久 就一个问题属实看不出来了
2010-04-07 08:12
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:9 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct address
{
    char name[20];
    char phone[15];
    char build[10];
    struct address*next;
    struct address*prior;
}list_entry;

struct address*start;/*point to first entry in list*/
struct address*last; /*point to last entry*/
int Menu_select();
void Search();

void Enter();//输入新的联系人
struct address*Dls_store(struct address*i, struct address*top);//保存新加入的结点
void Inputs(char*prompt, char*s, int count);

void Delete();//删除联系人
struct address*find(char *name);//查找联系人

void List();//输出全部联系人的信息
void display(struct address*info);//输出当前结点的单个信息

//查找联系人
void Search();

//保存全部信息
void Save();

//加载信息到链表中
void Load();

int main()
{
    int choice;
    //char s[80],choice;
    //struct address*info;
    start=0;  /*zero length list*/
    for(;;)
    {
        choice=Menu_select();
        switch(choice)
        {
        case 1:Enter();break;
        case 2:Delete();break;
        case 3:List();break;
        case 4:Search();break;
        case 5:Save();break;
        case 6:Load();break;
        case 7:exit(0);
        }
    }
    return 0;
}
//菜单选择函数
int Menu_select()
{
     char s[80];
     int c;
     printf("1.Enter a name\n");
     printf("2.Delete a name\n");
     printf("3.List the file\n");
     printf("4.Search\n");
     printf("5.Save the file\n");
     printf("6.Load the file\n");
     printf("7.Quit\n");
     do
     {
         printf("Enter your choice:");
         gets(s);
         c = atoi(s);
     }while(c<0||c>7);
     return c;
}
// 输入新的联系人
void Enter()
{
     struct address *info;
         info=(struct address *)malloc(sizeof(list_entry));
         if(!info)
         {
             printf("\nout of memory\n");
             return;
         }
         Inputs("enter name:",info->name,20);
         if(!info->name[0])return;
         Inputs("enter phone:",info->phone,15);
         Inputs("enter build:",info->build,10);
         start = Dls_store(info,start);

 }
//新加如的结点插入到双链表中
struct address*Dls_store(struct address*i, struct address*top)
//struct address*i;
//struct address*top;
{
    static struct address *last=0;//定义一个静态结构体变量
    struct address *old, *p;
    if(last==0)//第一次增加 把前驱和后继结点都指向NULL
    {
        i->next=0;
        i->prior=0;
        last=i;
        return i;
    }
    p=top;
    old=0;
    while(p)
    {
        if(strcmp(p->name,i->name)<0)//从小到大的顺序排列
        {
            old=p;
            p=p->next;
        }
        else
        {
            if(p->prior)//如果p的前驱存在 把i插入到p前面
            {
                p->prior->next=i;
                i->next=p;
                i->prior = p->prior;
                p->prior=i;
                return i;
            }
        }
        //最后面插入的情况
        old->next=i;
        i->next=0;
        i->prior = old;
        last=i;
        return start;
    }
}

void Inputs(char*prompt, char*s, int count)
//char*prompt;
//char*s;               
//int count;
{
    char p[15];
    do
    {
        printf(prompt);
        gets(p);
        if( (strlen(p)-count)<0 )
            printf("\ntoo long");
    }while( (strlen(p)-count)<0 ) ;
    strcpy(s,p);
}



//删除联系人                       
void Delete()
{
    //struct address*info,*find();
    struct address*info;
    char s[80];
    printf("enter name:");
    gets(s);
    info = find(s);
    if(info)//表示找到了名字的所在
    {
        if(start==info)//在第一个结点
        {
            start=info->next;
            if(start)//判断是否还存在结点
                start->prior=0;
            else
                last=0;
        }
        else
        {
            info->prior->next=info->next;
            if(info!=last)//删除的是中间的结点
                info->next->prior=info->prior;
            else//删除的是最后一个结点
                last=info->prior;
        }
    }
    free(info);
}
//在链表中查找当前输入的名字                    
struct address*find(char *name)
//char*name;
{
    struct address*info;
    info = start;
    while(info)
    {
        if(!strcmp(name,info->name))
            return info;
        info=info->next;
    }
    printf("name not found\n");
    return 0;
}
//输出全部联系人的信息
void List()
{
    //register int t;
    struct address*info;
    info=start;
    while(info)
    {
        display(info);
        info=info->next;
    }
    printf("\n");
}

//输出一个结点信息
void display(struct address*info)
//struct address*info;
{
    printf("%s\n",info->name);
    printf("%s\n",info->phone);
    printf("%s\n",info->build);
    printf("\n");
}

//查找联系人
void Search()
{
    char name[20];
    struct address*info;
    printf("enter name to fing:");
    gets(name);
    info = find(name);
    if(!info) ;
        //printf("not found\n");
    else
        display(info);
}
//保存全部信息
void Save()
{
    register int t,size;
    struct address*info;
    char*p;
    FILE*fp;
    if((fp=fopen("mlist","wb"))==0)
    {
        printf("cannot open file\n");
        exit(0);
    }
    printf("\nsaving file\n");
    size = sizeof(list_entry);
    info = start;
    while(info)
    {
        p=(char*)info;
        for(t=0;t<size;t++)
            putc(*p++,fp);
        info=info->next;
    }
    putc(EOF,fp);
    fclose(fp);
}
//加载信息到链表中
void Load()
{
    register int t,size;
    struct address*info,*temp=0;
    char*p;
    FILE*fp;
    if((fp=fopen("mlist","r"))==0)
    {
        printf("cannot open file\n");
        return;
    }
    size=sizeof(list_entry);
    start=(struct address*)malloc(size);
    if(!start)
    {
        printf("out of memory\n");
        return;
    }
    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);
}


/*
struct address*didelete(i,top)
struct address*i;
struct address*top;
{
    if(i->prior)
        i->prior->next=i->next;
    else
    {
        top=i->next;
        if(top)
            top->prior=0;
    }
    if(i->next)
        i->next->prior=i->prior;
    return(top);
}*/

void Save();
void Load();建议这两个函数的实现方法改过下 没有这么变来变去的 到头还是变错了

用fwrite()和fread()函数去实现  最后就是自己定义的函数尽量用头字母大写 加以区分



2010-04-07 10:54
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:1 
为了保持原编 后面两个函数 没有怎么改动
2010-04-07 11:00
新手NO1
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-28
收藏
得分:0 
  谢谢啦  我去改下
2010-04-08 08:45
快速回复:帮吗修改下加下注释 呵呵 谢了
数据加载中...
 
   



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

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