要用c语言设计一个个人通讯录,要求有步骤的截图,急急急,求大神帮帮忙啊
2个回答
展开全部
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define M 3
FILE *fp;
char fn[50] = {""};
typedef struct message {
char name[20];
char address[50];
char telephone[15];
int postcode;
struct message *next;
} *LinkList,*NODE;
void ReadData(NODE p) {
printf("姓名 : ");
scanf("%s",p->name);
printf("地址 : ");
scanf("%s",p->address);
printf("电话号码 : ");
scanf("%s",p->telephone);
printf("邮政编码 : ");
scanf("%d",&p->postcode);
}
void ShowLine(int n) {
int i;
for(i = 0; i < n; ++i)
printf("*");
printf("\n");
}
void ShowData(NODE p) {
printf("%s ",p->name);
printf("%s ",p->address);
printf("%s ",p->telephone);
printf("%d\n",p->postcode);
}
void ShowAll(LinkList head) {
NODE p;
if(head == NULL) return;
ShowLine(25);
for(p = head->next; p; p = p->next)
ShowData(p);
ShowLine(25);
}
LinkList CreateList(int n) {
NODE head,p;
int i;
head = p = (LinkList)malloc(sizeof(message));
for(i = 0; i < n; ++i) {
p->next = (LinkList)malloc(sizeof(message));
printf("(%d/%d)的信息 : \n",i + 1,n);
ReadData(p->next);
p = p->next;
}
p->next = NULL;
return head;
}
void Addmember(LinkList head) { // 头插法添加,可用于创建新链表
NODE p = (LinkList)malloc(sizeof(message));
ReadData(p);
if(head == NULL) {
head->next = p;
p->next = NULL;
}
else {
p->next = head->next;
head->next = p;
}
}
NODE Search(LinkList head,char name[]) {
NODE p;
if(head == NULL) return NULL;
for(p = head->next; p; p = p->next) {
if(strcmp(p->name,name) == 0)
ShowData(p);
return p;
}
return NULL;
}
void ModifyList(LinkList head) {
NODE p;
char name[20];
printf("被修改人的姓名 : ");
scanf("%s",name);
p = Search(head,name);
if(p == NULL) printf("查无此人。\n");
else {
ShowData(p);
ReadData(p);
}
}
int Erase(LinkList head,char name[]) { // 删除姓名为name的结点
int an;
NODE p,q;
if(head == NULL) return 0;
for(p = head; p->next; p = p->next) {
if(strcmp(p->next->name,name) == 0) {
ShowData(p);
printf("1.删除, 0.不删除 : ");
scanf("%d",&an);
if(an == 1) {
q = p->next;
p->next = q->next;
free(q);
return 1;
}
else return 0;
}
}
return 0;
}
void LoadData(LinkList head) { // 传送文件的内容到内存
NODE p,tmp;
int an;
if(fn[0] == '\0') {
printf("文件名 : ");
scanf("%s",fn);
}
else {
printf("加载文件%s? 1.是,0不是 :",fn);
scanf("%d",&an);
if(an == 1) {
fp = fopen(fn,"rb");
if(fp == NULL) {
printf("打开文件%s失败。\n",fn);
return;
}
}
else {
printf("文件名 : ");
scanf("%s",fn);
fp = fopen(fn,"rb");
if(fp == NULL) {
printf("打开文件%s失败。\n",fn);
return;
}
}
}
if(head == NULL) {
head = p = (NODE)malloc(sizeof(message));
tmp = (NODE)malloc(sizeof(message));
while(fread(tmp,sizeof(message),1,fp) == 1) {
p = (NODE)malloc(sizeof(message));
*p->next = *tmp;
p = p->next;
}
fclose(fp);
p->next = NULL;
free(tmp);
return;
}
printf("1.追加 0.覆盖 : ");
scanf("%d",&an);
if(an == 1) {
for(p = head->next; p->next; p = p->next);
tmp = (NODE)malloc(sizeof(message));
while(fread(tmp,sizeof(message),1,fp) == 1) {
p->next = (NODE)malloc(sizeof(message));
*p->next = *tmp;
p = p->next;
}
fclose(fp);
p->next = NULL;
free(tmp);
return;
}
if(an == 0) {
p = head;
tmp = (NODE)malloc(sizeof(message));
while(p->next) {
if(fread(tmp,sizeof(message),1,fp) == 1) {
*p->next = *tmp;
p = p->next;
}
else break;
}
if(p->next == NULL) {
while(fread(tmp,sizeof(message),1,fp) == 1) {
p->next = (NODE)malloc(sizeof(message));
*p->next = *tmp;
p = p->next;
}
fclose(fp);
p->next = NULL;
free(tmp);
return;
}
free(tmp);
tmp = p->next;
p->next = NULL;
while(tmp->next) {
p = tmp->next;
tmp->next = p->next;
free(p);
}
}
}
void SaveData(LinkList head) { // 保存内存到文件
int an;
char mode[5];
NODE p;
if(fn[0] == 0) {
printf("请输入文件名 : ");
scanf("%s",fn);
fp = fopen(fn,"wb");
if(fp == NULL) {
printf("打开文件%s失败。\n",fn);
return;
}
for(p = head->next; p; p = p->next)
fwrite(p,sizeof(message),1,fp);
fclose(fp);
return;
}
else {
printf("1.覆盖文件%s\n2.追加文件%s\n3.新建\n4.无操作返回\n");
printf("请选择 : ");
scanf("%d",&an);
if(an < 1 || an > 3) return;
if(an == 3) {
printf("请输入文件名 : ");
scanf("%s",fn);
fp = fopen(fn,"wb");
if(fp == NULL) {
printf("打开文件%s失败。\n",fn);
return;
}
for(p = head->next; p; p = p->next)
fwrite(p,sizeof(message),1,fp);
fclose(fp);
return;
}
else {
if(an == 1) strcpy(mode,"wb");
else if(an == 2) strcpy(mode,"ab");
fp = fopen(fn,"wb");
if(fp == NULL) {
printf("打开文件%s失败。\n",fn);
return;
}
for(p = head->next; p; p = p->next)
fwrite(p,sizeof(message),1,fp);
fclose(fp);
return;
}
}
}
void Sort(LinkList head) { // 按姓名增排序
NODE p,q,pt;
if(head == NULL) return;
p = head;
while(p->next) {
q = p->next->next;
while(q->next) {
if(strcmp(p->next->name,q->next->name) > 0) {
pt = p->next;
p->next = q->next;
q->next = q->next->next;
p->next->next = pt;
}
else q = q->next;
}
p = p->next;
}
}
void FreeList(LinkList head) {
NODE p = head,q;
while(p) {
q = p;
p = p->next;
free(q);
}
}
void menu(LinkList head) {
int choice,n;
do {
printf("┌─────────────────────────────────┐\n");
printf("│ │\n");
printf("│ 欢迎使用通讯录管理软件 │\n");
printf("│ │\n");
printf("│ 编写 xoalyg │\n");
printf("│ │\n");
printf("└─────────────────────────────────┙\n");
printf("\n\n\n\n");
printf("\t1、新建\n");
printf("\t2、浏览\n");
printf("\t3、修改\n");
printf("\t4、插入\n");
printf("\t5、排序\n");
printf("\t6、加载\n");
printf("\t7、保存\n");
printf("\t0、退出\n");
printf("\n\t请选择 : ");
scanf("%d",&choice);
switch(choice) {
case 1 : printf("\t人数 : ");
scanf("%d",&n);
head = CreateList(n);
break;
case 2 : ShowAll(head); break;
case 3 : ModifyList(head); break;
case 4 : Addmember(head); break;
case 5 : Sort(head); break;
case 6 : LoadData(head); break;
case 7 : SaveData(head); break;
case 0 : break;
}
}while(choice != 0);
}
int main() {
NODE head = NULL;
menu(head);
FreeList(head);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询