标题:看看这程序吧,顺便调一下,谢谢!!
只看楼主
qqqq0199181
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-4-3
结帖率:50%
 问题点数:0 回复次数:3 
看看这程序吧,顺便调一下,谢谢!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct address{
    char name[30];
    char street[40];
    char city[20];
    char state[3];
    char zip[11];
    struct address *next;
    struct address *prior;
}

struct address *start;
struct address *last ;
struct address *find(char *);

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),display(struct address *);
int menu_select(void);

int main(void)
{
    start=last=NULL;
    for(;;){
        switch(menu_select()){
        case 1:enter();
            break;
        case 2:mldelete(&start,&last);
            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(void)
{
    char s[80];
    int c;

    printf("1.Enter a name\n");
    printf("2.Delect 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.Out\n");
        do{
            printf("\nEnter you choice:");
            gets(s);
            c=atoi(s);
        }while(c<0||c>7);
        return c;
}

void enter(void)
{
    struct address *info;
    for(;;){
        info=(struct address *) malloc(sizeof(struct address));
        if(!info){
            printf("\nout of memory");
            return;
        }
        inputs("Enter name:",info->name,30);
        if(!info->name[0])break;
        inputs("Enter street:",info->street,40);
        inputs("Enter city:",info->city,20);
        inputs("Enter state:",info->state,3);
        inputs("Enter zip:",info->zip,10);

        dls_store(info,&start,&last);
    }
}

void inputs(char *ptompt,char*s,int count)
{
    char p[255];
    do{
        printf(ptompt);
        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 dls_store(
        struct address*i,
        struct address **srart,
        struct address **last
        )
    {
        struct address *old,*p;
        if(*last==NULL){
            i->next=NULL;
            i->prior=NULL;
            * last=i;
            * start=i;
            return;
        }
        p=* start;
        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;
                i->prior= NULL;
                p->prior= i;
                *start = i;
                return;
            }
        }
        old->next=i;
        i->next=NULL;
        i->prior=old;
        *last=i;
    }

    void mldelete(struct address **start,struct address **last)
    {
        struct address *info;
        char s[80];

        inputs("Enter name:",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);
        }
    }

    struct address *find(char *name)
    {
        struct address *info;
        info=start;
        while(info){
            if(!strcmp(name,info->name))return info;
            info=info->next;
        }
        printf("Name not find.\n");
        return NULL;
    }

    void list(void)
    {
        struct address *info;
        info=start;
        while(info){
            display(info);
            info=info->next;
        }
        printf("\n\n");
    }
    void display(struct address *info)
    {
        printf("%s\n",info->name);
        printf("%s\n",info->street);
        printf("%s\n",info->city);
        printf("%s\n",info->state);
        printf("%s\n",info->zip);
        printf("\n\n");
    }
    void search(void)
    {
        char name[40];
        struct address *info;
        printf("Enter name to find:");
        gets(name);
        info=find(name);
        if(!info)printf("Not Find\n");
        else display(info);
    }


    void save(void)
    {
        struct address *info;
        FILE *fp;
        fp=fopen("mlist","wb");
        if(!fp){
            printf("Can't open file.\n");
            exit(1);
        }
        printf("\nSaving File\n");
        info=start;
        while(info){
            fwrite(info,sizeof(struct address),1,fp);
            info=info->next;
        }
        fclose(fp);
    }



    void load()
    {
        struct address *info;
        FILE *fp;
        fp=fopen("mlist","fb");
        if(!fp){
            printf("Can't open file.\n");
            exit(1);
        }
        while(start){
            info=start->next;
            free(info);
            start=info;
        }
        start=last=NULL;
        printf("\nLoading File\n");
        while(!feof(fp)){
            info=(struct address*)malloc(sizeof(struct address));
            while(!info){
                printf("Out of memory");
                return;
            }
            if(1!=fread(info,sizeof(struct address),1,fp))break;
            dls_store(info,&start,&last);
        }
        fclose(fp);
    }
搜索更多相关主题的帖子: address include search start 
2010-04-03 20:07
ggvvcc
Rank: 3Rank: 3
来 自:山东临沂
等 级:论坛游侠
帖 子:50
专家分:119
注 册:2010-4-3
得分:0 
有点像毕业设计,开始就错了,你定义结构体语句后的分号哪去了?
2010-04-03 20:25
qqqq0199181
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-4-3
得分:0 
谢谢,已经该过来了,不过还有其他错误。
2010-04-03 20:32
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:0 
请写上 注释 功能 错误 谢谢
2010-04-03 20:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-301576-1-1.html




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

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