关于C语言通讯录保存用户输入数据的问题~30分敬上,好的话再加~懂的来,谢谢~
我用C语言编的通讯录程序,怎样才能实现保存输入的数据的功能?就是我输入,退出之后下次进来还可以载入以前的数据~懂的人好好给弄弄,有时间的话顺便给我提一些合理化建议。O(∩...
我用C语言编的通讯录程序,怎样才能实现保存输入的数据的功能?就是我输入,退出之后下次进来还可以载入以前的数据~懂的人好好给弄弄,有时间的话顺便给我提一些合理化建议。O(∩_∩)O谢谢
#include<stdlib.h>
#include<string.h>
#define NEW (struct node*)malloc(sizeof(struct node))
struct node
{ char name[20],tel[9],class[10];
struct node *next;
};
struct node *create()
{ struct node *h;
struct node *p,*q;
char name[20],class[10];
h=q=NULL;
printf("name:");
gets(name);
while(strlen(name)!=0)
{ p=NEW;
if(p==NULL)
{printf("Allocation failure\n");
exit(0);
}
strcpy(p->name,name);
printf("tel:");
gets(p->tel);
printf("class:");
gets(class);
gets(p->class,class);
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
printf("name:");
gets(name);
}
return h;
};
void prlist(struct node *head)
{ struct node *p;
p=head;
while(p!=NULL)
{printf("%s %s %s\n",p->name,p->tel,p->class);
p=p->next;
}
}
struct node *delnode(struct node *head,char *x)
{struct node *p,*q;
if(head==NULL)
{printf("This is a empty list.");
return head; }
p=head;
while(strcmp(x,p->name)!=0&&p->next!=NULL)
{q=p,p=p->next;}
if(strcmp(x,p->name)==0)
{if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
}
else
printf("Not found.");
return head;
}
struct node *insert(struct node *head,struct node *p0,char *x)
{ struct node *p,*q;
if(head==NULL)
{head=p0;
p0->next=NULL; }
else
{p=head;
while(strcmp(x,p->name)!=0&&p->next!=NULL)
{q=p,p=q->next;}
if(strcmp(x,p->next)==0)
{if(p==head)
head=p0;
else
q->next=p0;
p0->next=p; }
else
{p->next=p0;
p0->next=NULL; }
}
return head;
}
main()
{ struct node *create(),*delnode(struct node *,char *);
struct node *insert(struct node *,struct node *,char *);
void prlist(struct node *);
struct node *head=NULL,*stu;
char s[80],name[20];
int c;
do
{do {printf("\n*****MENU*****\n");
printf("1.Create a list \n");
printf("2.Print alist \n");
printf("3.Delete a node \n");
printf("4.insert a node \n");
printf("0.Quit \n");
printf("Enter your choice 0-4 :");
gets(s);
c=atoi(s);
}while(c<0||c>4);
switch(c)
{case 1:head=create();break;
case 2:prlist(head);break;
case 3:printf("\nInput a name deleted:\n");
gets(name);
head=delnode(head,name);break;
case 4:stu=NEW;
printf("\Input a new node\n");
printf("name:");
gets(stu->name);
printf("tel:");
gets(stu->tel);
printf("class:");
gets(stu->class);
stu->next=NULL;
printf("\nInsert position\n");
printf("name:");
gets(name);
head=insert(head,stu,name);
}
}while(c);
} 展开
#include<stdlib.h>
#include<string.h>
#define NEW (struct node*)malloc(sizeof(struct node))
struct node
{ char name[20],tel[9],class[10];
struct node *next;
};
struct node *create()
{ struct node *h;
struct node *p,*q;
char name[20],class[10];
h=q=NULL;
printf("name:");
gets(name);
while(strlen(name)!=0)
{ p=NEW;
if(p==NULL)
{printf("Allocation failure\n");
exit(0);
}
strcpy(p->name,name);
printf("tel:");
gets(p->tel);
printf("class:");
gets(class);
gets(p->class,class);
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
printf("name:");
gets(name);
}
return h;
};
void prlist(struct node *head)
{ struct node *p;
p=head;
while(p!=NULL)
{printf("%s %s %s\n",p->name,p->tel,p->class);
p=p->next;
}
}
struct node *delnode(struct node *head,char *x)
{struct node *p,*q;
if(head==NULL)
{printf("This is a empty list.");
return head; }
p=head;
while(strcmp(x,p->name)!=0&&p->next!=NULL)
{q=p,p=p->next;}
if(strcmp(x,p->name)==0)
{if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
}
else
printf("Not found.");
return head;
}
struct node *insert(struct node *head,struct node *p0,char *x)
{ struct node *p,*q;
if(head==NULL)
{head=p0;
p0->next=NULL; }
else
{p=head;
while(strcmp(x,p->name)!=0&&p->next!=NULL)
{q=p,p=q->next;}
if(strcmp(x,p->next)==0)
{if(p==head)
head=p0;
else
q->next=p0;
p0->next=p; }
else
{p->next=p0;
p0->next=NULL; }
}
return head;
}
main()
{ struct node *create(),*delnode(struct node *,char *);
struct node *insert(struct node *,struct node *,char *);
void prlist(struct node *);
struct node *head=NULL,*stu;
char s[80],name[20];
int c;
do
{do {printf("\n*****MENU*****\n");
printf("1.Create a list \n");
printf("2.Print alist \n");
printf("3.Delete a node \n");
printf("4.insert a node \n");
printf("0.Quit \n");
printf("Enter your choice 0-4 :");
gets(s);
c=atoi(s);
}while(c<0||c>4);
switch(c)
{case 1:head=create();break;
case 2:prlist(head);break;
case 3:printf("\nInput a name deleted:\n");
gets(name);
head=delnode(head,name);break;
case 4:stu=NEW;
printf("\Input a new node\n");
printf("name:");
gets(stu->name);
printf("tel:");
gets(stu->tel);
printf("class:");
gets(stu->class);
stu->next=NULL;
printf("\nInsert position\n");
printf("name:");
gets(name);
head=insert(head,stu,name);
}
}while(c);
} 展开
展开全部
可以建立一个txt文件,通过程序打开它,将每次输入的记录,写到txt文件中。
如程序:
#include "stdio.h"
#include "stdlib.h"
int main()
{FILE *fp;
char ch;
if((fp=fopen("f:\\1.txt","w"))==NULL)
/*生成文件1.txt在F盘中*/
{printf("can't open this filel");
exit(1);}
while((ch=getchar())!='\n')
fputc(ch,fp);
fclose(fp);
return 0;
}
如果,下次打开时还要用到上次输入的信息,加入一个导入文件的函数就可以了!
如程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{FILE *fp;
char c[20];
if((fp=fopen("f:\\1.txt","r"))==NULL)
{printf("can't open this filel");
exit(1);}
while(fgets(c,20,fp)!=NULL)
{printf("%s",c);}
fclose(fp);
return 0;
}
(在运行过第一个程序后,在运行此程序。)
这只是一个简单的提示,你个以根据此提示,写出你需要的程序。如有不明白的地方,看一下相关书籍,文件那一章就可以了!
如程序:
#include "stdio.h"
#include "stdlib.h"
int main()
{FILE *fp;
char ch;
if((fp=fopen("f:\\1.txt","w"))==NULL)
/*生成文件1.txt在F盘中*/
{printf("can't open this filel");
exit(1);}
while((ch=getchar())!='\n')
fputc(ch,fp);
fclose(fp);
return 0;
}
如果,下次打开时还要用到上次输入的信息,加入一个导入文件的函数就可以了!
如程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{FILE *fp;
char c[20];
if((fp=fopen("f:\\1.txt","r"))==NULL)
{printf("can't open this filel");
exit(1);}
while(fgets(c,20,fp)!=NULL)
{printf("%s",c);}
fclose(fp);
return 0;
}
(在运行过第一个程序后,在运行此程序。)
这只是一个简单的提示,你个以根据此提示,写出你需要的程序。如有不明白的地方,看一下相关书籍,文件那一章就可以了!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询