这个课程设计不够完美,仅仅实现了增删查改的功能,使用了指针操作。时间不够了,只有先这样了,过几天考完试再补写吧~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define LEN sizeof(struct infor)
struct infor
{
long num;
char adr[50];
char name[20];
char rela[20];
struct infor *next;
};
struct infor *Head;
struct infor *del(struct infor *head,long number)
{
void menu();
struct infor *pf,*pb;
if(head->next==NULL)
{
printf("你还没有好友\n");
goto end;
}
pb=head->next;
while(pb->num!=number&&pb->next!=NULL)
{
pf=pb;
pb=pb->next;
}
if(pb->num==number)
{
if(pb==head->next)
head->next=pb->next;
else
pf->next=pb->next;
printf("该好友已被删除\n");
}
else
{
printf("没有该好友\n");
}
printf("请按任意键继续!");
getchar();
getchar();
end:
menu();
return head;
}
void home()
{
system("color 2d");
printf("\n");
printf("\t o(∩_∩)o o(∩_∩)o o(∩_∩)o o(∩_∩)o o(∩_∩)o o(∩_∩)o \n");
printf("\n");
printf("\t ☆☆☆☆☆☆☆☆☆☆☆☆☆★☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ 欢迎使用通讯录管理系统 ☆\n");
printf("\t ☆ ☆\n");
printf("\t ★ ★\n");
printf("\t ☆ ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ 制作人; ☆\n");
printf("\t ☆ 2014.6.23 ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆☆☆☆☆☆☆☆☆☆☆☆☆★☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
getchar();
}
void display(struct infor *head)
{
void menu();
struct infor *p;
p = head->next;
printf("学生信息如下:\n");
printf("\t\t好友号码\t好友住址\t好友姓名\t你们的关系\n");
while(p!= NULL)
{
printf("\t\t%-8ld\t%-8s\t%-8s\t%-8s\n",p->num,p->adr,p->name,p->rela);
p = p->next;
}
printf("请按任意键继续!");
getchar();
getchar();
menu();
}
bool Find(struct infor *head,long ai)
{
struct infor *pi;
pi=head->next;
while(pi!=NULL&&pi->num!=ai)
pi=pi->next;
if(pi&&pi->num==ai)
return 0;
else
return 1;
}
struct infor *add(struct infor *head)
{
void menu();
int n,t,m;
struct infor *p,*pi,*flag;
printf("希望增添几个好友?\n");
scanf("%d",&n);
while(n--)
{
pi = head;
while(pi->next != NULL)
{
pi=pi->next;
}
printf("1.在通讯录顶部增添\t2.在通讯录底部增添\t\n");
scanf("%d",&t);
getchar();
char numstr[20];
if(t==1)
{
struct infor *pt =head->next;
p=(struct infor *)malloc(LEN);
p->next=NULL;
printf("请输入所要添加好友的号码: ");
gets(numstr);
long aa=atol(numstr);
if(!Find(head,aa))
{
printf("该好友号码已经存在!\n请按任意键继续!");
getchar();
getchar();
menu();
}
p->num=atol(numstr);
printf("请输入好友住址: ");
gets(p->adr);
printf("请输入好友姓名: ");
gets(p->name);
printf("请输入关系:");
gets(p->rela);
head->next=p;
p->next=pt;
}
if(t==2)
{
p=(struct infor *)malloc(LEN);
p->next=NULL;
printf("请输入所要添加好友的号码: ");
gets(numstr);
long aa=atol(numstr);
if(!Find(head,aa))
{
printf("该好友号码已经存在!\n请按任意键继续!");
getchar();
getchar();
menu();
}
p->num=atol(numstr);
printf("请输入好友住址: ");
gets(p->adr);
printf("请输入好友姓名: ");
gets(p->name);
printf("请输入关系:");
gets(p->rela);
pi->next=p;
pi=p;
}
}
menu();
}
struct infor *print(struct infor *Head)
{
void menu();
char cho;
struct infor *p,*pi;
pi=Head;
int num;
loop:
printf("请输入你所要查询的好友学号:");
scanf("%ld",&num);
getchar();
while(num!=pi->num&&pi->next!=NULL)
pi=pi->next;
if(pi->num==num)
{
printf("该好友的基本信息如下:\n\t\t好友号码:%d\n\t\t好友住址:%s\n\t\t好友姓名:%s\n\t\t你们的关系:%s\n",pi->num,pi->adr,pi->name,pi->rela);
printf("是否需要继续查找?\n是:Y\t否:N\n");
scanf("%c",&cho);
getchar();
if(cho=='Y'||cho=='y')
goto loop;
else if(cho=='N'||cho=='n')
goto end;
}
else
{
printf("没有该好友!是否继续查找?\n是:Y\t否:N\n");
scanf("%c",&cho);
getchar();
if(cho=='Y'||cho=='y')
goto loop;
else if(cho=='N'||cho=='n')
goto end;
}
end:
printf("请按任意键继续!");
getchar();
menu();
return Head;
}
struct infor *modify(struct infor *head)
{
void menu();
long numb;
struct infor *p,*pi;
printf("请输入所要修改好友的号码:");
scanf("%ld",&numb);
pi=head->next;
while(pi!=NULL&&numb!=pi->num)
{
pi=pi->next;
}
if(pi&&pi->num==numb)
{
printf("请输入修改后的好友\n号码:");
scanf("%ld",&pi->num);
printf("住址:");
scanf("%s",&pi->adr);
printf("姓名:");
scanf("%s",&pi->name);
printf("关系:");
scanf("%s",&pi->rela);
printf("修改完毕!");
}
else
{
printf("您没有改好友!");
}
printf("请按任意键继续!");
getchar();
getchar();
menu();
}
void menu()
{
int n;
system("cls");
printf("\n\t\t 欢迎使用好友管理系统 \n ");
printf("\t ☆☆☆☆☆☆☆☆☆☆☆☆☆★☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ 功能菜单 ☆\n");
printf("\t ☆ 1.添加好友 ☆\n");
printf("\t ☆ 2.删除好友 ☆\n");
printf("\t ★ 3.修改信息 ★\n");
printf("\t ☆ 4.查看资料 ☆\n");
printf("\t ☆ 5.显示资料 ☆\n");
printf("\t ☆ 6.退出系统 ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆ ☆\n");
printf("\t ☆☆☆☆☆☆☆☆☆☆☆☆☆★☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf("请选择功能:[ ]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:
add(Head);
break;
case 2:
long aim;
printf("请输入所要删除好友的号码:");
scanf("%d",&aim);
del(Head,aim);
break;
case 3:
modify(Head);
break;
case 4:
print(Head);
break;
case 5:
display(Head);
break;
case 6:
exit(0);
break;
}
}
int main()
{
Head = (struct infor*)malloc(LEN);
Head->next=NULL;
home();
menu();
return 0;
}