能在C++下运行没有错误就散分!希望大家看看。

neebeen 2004-12-28 11:48:24
/* A simple mailing list program that illustrtates the use and maintenance of doubly linked lists.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct address
{ char name[30];
char street[40];
char city[20];
char state[40];
char zip_code[11];
struct address *next;/*pointer to next entry*/
struct address *prior;/*pointer previous record*/
};


struct address *start;/*pointer to first entry in list*/
struct address *last;/*pointer to last entry*/
struct address *find(char *);


void enter(void);
void search(void);
void save(void);
void load(void);
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 *);
int menu_select(void);


int main (void)
{
start=last=NULL;/*initialize start and end pointers*/
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;
}



/*select an operation*/
int menu_select(void)
{
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("\nEnter your choice:");
gets(s);
c=atoi(s);
}while(c<0||c>7);
return c;
}



/*Enter names and addresses.*/
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;/*stop entering*/
inputs("Enter street:",info->street,40);
inputs("Enter city:",info->city,20);
inputs("Enter state:",info->state,3);
inputs("Enter zip_code:",info->zip_code,10);


dls_store(info,&start,&last);
}/*entry loop*/
}



/*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)>count) printf("\nToo Long\n");
}while(strlen(p)>count);

p[strlen(p)-1]=0;/*remove newline character*/
strcpy(s,p);
}



/*Create a doubly linked list in sorted order.*/
void dls_store(struct address *i,struct address **start,struct **last)
/*new clement;first element in list;last element in list*/
{
struct address *old,*p;

if(*last==NULL)/*fist element in list*/
{
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;
}
p=*start;/*start at top of 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 fist element*/
i->prior=NULL;
p->prior=i;
*start=i;
return;
}
}
old->next=i;/*put on end*/
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("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);/*return memory to system*/
}
}


/*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 atually prints the fields in each address.*/
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_code);
printf("\n\n");
}



/*Look for a name in the list*/
void search(void)
{
char name[40];
struct address *info;

printf("Enter name to find:");
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;

fp=fopen("mlist","web");
if(!fp)
{
printf("Cannot open file.\n");
exit(1);
}
printf("\nSaving File\n");


info=start;
while(info)
{
fwrite(info,sizeof(struct address),1,fp);
info=info->next;/*get next address*/
}
fclose(fp);
}



/*Load the address file.*/
void load()
{
struct address *info;
FILE *fp;


fp=fopen("mlist","rb");
if(!fp)
{
printf("Cannot open file.\n");
exit(1);
}
/*free any previously allocated memory.*/
while(start)
{
info=start->next;
free(info);
start=info;
}
/*reset top and bottom pointers*/
start=last=NULL;

printf("\nLoading File\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);
}

...全文
159 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuyangqiang 2004-12-29
  • 打赏
  • 举报
回复
你的程序我决定用一下!
   不介意吧?
  

我就不写了!
chuyangqiang 2004-12-29
  • 打赏
  • 举报
回复
你好!
   才几分钟就改名字了! 
 让我好找啊!
icansaymyabc 2004-12-29
  • 打赏
  • 举报
回复
你在程序运行时选择 5,
那么你的数据就保存在 mlist 这个文件里了。
mlist 和你的exe可执行程序在同一个目录里。
minmaxlee 2004-12-28
  • 打赏
  • 举报
回复
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
在开头加上就没的警告了!
minmaxlee 2004-12-28
  • 打赏
  • 举报
回复
void dls_store(struct address *i,struct address **start,struct **last)
参数有问题!
==>
void dls_store(struct address *i,struct address **start,struct address **last)
neebeen 2004-12-28
  • 打赏
  • 举报
回复
希望大家支持!谢谢!
neebeen 2004-12-28
  • 打赏
  • 举报
回复
在整个链表中使用的是将文件保存在内存中,不知道要怎么改才能够将我输入的信息保存在磁盘文件中呢 ?
oyljerry 2004-12-28
  • 打赏
  • 举报
回复
编译一下,把错误贴一下
bigbee 2004-12-28
  • 打赏
  • 举报
回复
o,yeah,不懂,接分.
icansaymyabc 2004-12-28
  • 打赏
  • 举报
回复
void dls_store(struct address *i,struct **start,struct **last)
改为
void dls_store(struct address *i,struct address **start,struct address**last)

/*Save the file to disk*/
void save(void)
{
struct address *info;

FILE *fp;

fp=fopen("mlist","web"); = 改为 =>fp=fopen("mlist","wb");

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧