急求c++通讯录管理系统!限期三天!真急!

2025-03-04 07:54:05
推荐回答(2个)
回答1:

这是我写的一个程序 麻烦你看一下 看能不能帮上你的忙
#include
#include
#include
#define YN {do {yes_no=getch();printf("%c",yes_no);}while(yes_no!='y'&&yes_no!='Y'&&yes_no!='n'&&yes_no!='N');}
/*判断学号是否为空*/
#define XH {do {gets(temp.xh);}while(strcmp(temp.xh," ")==0);}
/*录入学生的学号、班级、姓名、性别、生日、籍贯、学院*/
#define SEX {do{printf("xingbie(w for woman,m for man):\n");gets(temp.xb);}while(temp.xb[0]!='w'&&temp.xb[0]!='m');}
#define LR {printf("banji:");gets(temp.bj);printf("xingming:");gets(temp.xm); SEX printf("shengri:");gets(temp.sr);printf("jiguan:");gets(temp.jg);printf("xueyuan:");gets(temp.xy);}
/*判断文件是否打开*/
#define PDW {if(fp==NULL) {printf("\n The file can't be opened\n");return;}}
#define PD {while(temp.xh[0]!='#'&&temp.bj[0]!='#'&&temp.xm[0]!='#'&&temp.xb[0]!='#'&&temp.sr[0]!='#'&&temp.jg[0]!='#'&&temp.xy[0]!='#');}
/*将学生的档案放进文件fp中*/
#define GS0 {fprintf(fp,"%10s%10s%10s%10s%10s%10s%10s\n",temp.xh,temp.bj,temp.xm,temp.xb,temp.sr,temp.jg,temp.xy);}
/*如果学生的信息不为空,则将该学生的信息输入文件fp中*/
#define PD1 {if(temp.xh[0]!='#'&&temp.bj[0]!='#'&&temp.xm[0]!='#'&&temp.xb[0]!='#'&&temp.sr[0]!='#'&&temp.jg[0]!='#'&&temp.xy[0]!='#') GS0}
/*从文件fp中接收学生的档案信息*/
#define GS {fscanf(fp,"%10s%10s%10s%10s%10s%10s%10s\n",temp.xh,temp.bj,temp.xm,temp.xb,temp.sr,temp.jg,temp.xy);}
/*通过结构体数组接收文件fp中的学生档案信息*/
#define GS1 {fscanf(fp,"%10s%10s%10s%10s%10s%10s%10s\n",stu[i].xh,stu[i].bj,stu[i].xm,stu[i].xb,stu[i].sr,stu[i].jg,stu[i].xy);}
/*通过结构体数组将学生信息传送到文件fp中*/
#define GS2 {fprintf(fp,"%10s%10s%10s%10s%10s%10s%10s\n",stu[i].xh,stu[i].bj,stu[i].xm,stu[i].xb,stu[i].sr,stu[i].jg,stu[i].xy);}
#define SRW {while(feof(fp)==0){GS1 i++;n++;}} /*判断是否已执行到文件尾,并用i,n记录学生人数*/
#define PT {printf("%c",choose);} /*回显所选数*/
#define PTF {printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n",temp.xh,temp.bj,temp.xm,temp.xb,temp.sr,temp.jg,temp.xy);}
#define PTF1 {printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n",stu[i].xh,stu[i].bj,stu[i].xm,stu[i].xb,stu[i].sr,stu[i].jg,stu[i].xy);}
#define GS3 {printf("\nxuehao banji xingming xingbie shengri jiguan xueyuan \n");}
#define N 100
struct student
{char xh[10];
char bj[10];
char xm[10];
char xb[2];
char sr[10];
char jg[10];
char xy[10];

};
void menu(); /*函数声明*/
void creat();
void refert();
void delete();
void add();
void modify();
void show();
void tongji();
void order();
void password();
main()
{ char choose,yes_no;
do
{ menu(); /*调用菜单函数*/
printf("\n\t\t please choose(1-9):");
choose=getch();
switch(choose)
{case '1':PT creat();break;
case '2':PT refert();break;
case '3':PT password();
delete();break;
case '4':PT add();break;
case '5':PT password();
modify();break;
case '6':PT show();break;
case '7':PT tongji();break;
case '8':PT order();break;
case '9':PT break;
default:printf("\n %c choose wrong!\n",choose);
}
if(choose=='9')
break;
printf("do you want to cotinue?(y/n)\n");
YN
}while(yes_no=='y'||yes_no=='Y');
}
/*制作菜单*/
void menu()
{clrscr(); /*清屏*/
printf("\n\n\n");
printf("\t\tWelcome to the student information manage system!\n");
printf("\t\t the designer--------yourname \n");
printf("\t\t *********************************\n");
printf("\t\t * please input order number *\n");
printf("\t\t * 1----------------creat *\n"); /*录入学生档案*/
printf("\t\t * 2----------------refer to *\n"); /*查询学生档案*/
printf("\t\t * 3----------------delete *\n"); /*删除学生档案*/
printf("\t\t * 4----------------add *\n"); /*添加学生档案*/
printf("\t\t * 5----------------modify *\n"); /*修改学生档案*/
printf("\t\t * 6----------------show *\n"); /*显示学生档案*/
printf("\t\t * 7----------------tongji *\n"); /*统计同一个学院的学生人数*/
printf("\t\t * 8----------------order *\n"); /*学生档案的排序*/
printf("\t\t * 9----------------exit *\n"); /*退出*/
printf("\t\t *********************************\n");

}
/*录入学生档案*/
void creat()
{int i=1;
struct student temp;
FILE *fp;
fp=fopen("f:\\123.txt","w"); /*以只写方式打开文件,且测试*/
PDW
do{ clrscr();
printf(" every pace end with '#'\n ");
printf(" please input number %d information:\n",i);
printf(" xuehao:");
XH /*判断学号是否为空*/
LR /*录入学生的学号、班级、姓名、性别、生日、籍贯、学院*/
PD1 /*判断学生信息是否为空,并放进文件fp中*/
i++;
} while(temp.xh[0]!='#'&&temp.bj[0]!='#'&&temp.xm[0]!='#'&&temp.xb[0]!='#'&&temp.sr[0]!='#'&&temp.jg[0]!='#'&&temp.xy[0]!='#');
fclose(fp); /*关闭文件*/
}
/*查询学生档案*/
void refert()
{char c,mid[10],b[10];
FILE *fp;
int flag=0,n=0;
struct student temp;
fp=fopen("f:\\123.txt","r"); /*打开文件*/
PDW
clrscr();

printf("\n which way to refetr to ?number(1) or name(2)?choose(1-2):\n");
c=getch();

if(c=='2') /*按姓名查询*/
{ printf("\nPlease input the student's name\n");
gets(mid);
}
else if(c=='1') /*按学号查询*/
{ printf("\nPlease input number\n");
gets(mid);
}
else { printf("Error!");
return;
}
while(feof(fp)==0)
{ GS
if(c=='1')
strcpy(b,temp.xh); /*将学生的学号复制到b中*/
else
strcpy(b,temp.xm); /*将学生的姓名复制到b中*/
if(strcmp(mid,b)==0) /*比较大小函数*/
{ if(flag==0)
{ GS3
PTF
}
flag=1;
}
n++;
}
if(n==0) printf("no list!\n"); /*已经到文件尾*/
else if(flag==0)
printf("no find\n"); /*没有到文件尾,但没有此人*/
}

void delete() /*删除函数*/
{ char c,mid[10],b[10];
int n=0,i=0,k;
FILE *fp;
struct student stu[N];
fp=fopen("f:\\123.txt","r"); /*打开并测试文件*/
PDW /*判断文件是否打开*/
SRW /*判断是否已执行到文件尾,并用i,n记录学生人数*/
fclose(fp);
clrscr();
printf("\nPlease input the deleting student's way\n");
printf("\n1----number,2----name please choose(1-2):");
c=getch();
if(c=='1')
{ printf("\nplease input the student's number:");
gets(mid);
}
else if(c=='2')
{ printf("\nplease input name:");
gets(mid);
}
else {printf("Error!");return;}

for(i=0;i { if(c=='1')
strcpy(b,stu[i].xh); /*将学生的学号复制到b中*/
else
strcpy(b,stu[i].xm); /*将学生的姓名复制到b中*/
if(strcmp(mid,b)==0) break; /*将要删除的学生信息和原有的信息作比较*/
}
if(i for(k=i;k stu[k]=stu[k+1];
else {printf("No the student!\n");return; }
fp=fopen("f:\\123.txt","w");
PDW
for(i=0;i GS2 /*将学生信息传送到文件fp中*/
fclose(fp);
}

void add() /*添加学生信息函数*/
{
char c,yes_no;
int n=0,i=0;
struct student temp,stu[N];
FILE *fp;
fp=fopen("f:\\123.txt","r");
PDW /*判断文件是否打开*/
SRW /*判断是否已执行到文件尾,并用i,n记录学生人数*/
clrscr();
for(i=0;i PTF1
do{ printf("\nplease input the new information:\n");
printf("xuehao:");
XH
LR
for(i=0;i if(strcmp(temp.xh,stu[i].xh)==0) /*将要添加的学生学号和原有的学生学号做比较*/
{ printf("\nThe number is repeat do you want to input again(y/n)?:");
YN
putchar('\n');
break;
}
if(i==n) {stu[i]=temp;break;}
}while(yes_no=='y'||yes_no=='Y');
if(i fp=fopen("f:\\123.txt","w");
PDW
for(i=0;i<=n;i++)
GS2 /*将学生信息传送到文件fp中*/
fclose(fp);
}

void modify() /* 修改学生信息函数*/
{char c,yes_no;
int n=0,i=0;
struct student *find,temp,stu[N],*p;
FILE *fp;
fp=fopen("f:\\123.txt","rb"); /*打开文件*/
PDW
SRW
fclose(fp);
if(n==0)
{ printf("\n No the information!\n");return;}
clrscr();
printf("\nPlease input the number of the modifying student:");
gets(temp.xh);
for(p=stu;p if(strcmp(temp.xh,p->xh)==0)
{ find=p;break;} /*用find记录要修改的学生学号*/
if(p==stu+n)
{ printf("No the person!\n");return;}
do
{ printf("\nPlease input the new student's information:\n");
printf("xuehao:");
gets(temp.xh);
XH /*判断学号是否为空*/
LR /*录入学生的学号、班级、姓名、性别、生日、籍贯、学院*/
for(p=stu;p if(strcmp(temp.xh,p->xh)==0&&p!=find)
{ printf("\nThe number is repeat, do you want to cotinue(y/n)?");
YN
putchar('\n');
break;
}
if(p==stu+n)
{ *find=temp; /*将新的学生信息赋给被修改的学生*/
break;
}
}while(yes_no=='y'||yes_no=='Y');
fp=fopen("f:\\123.txt","w");
PDW
for(i=0;i GS2
fclose(fp);
}

void show() /*显示学生信息函数*/
{int n=0,i=0;
struct student stu[N];
FILE *fp;
fp=fopen("f:\\123.txt","r");
PDW
clrscr();
GS3
SRW /*判断是否已执行到文件尾,并用i,n记录学生人数*/
if(n==0)
{
printf("No the student");
return;
}
else
{ for(i=0;i PTF1
}
printf("\nHave %d information",n);

fclose(fp);
}

void tongji() /*统计在同一个学院的学生人数*/
{int i=0,n=0,k=0;
char mid[10];
FILE *fp;
struct student stu[N];
fp=fopen("f:\\123.txt","r");
PDW
SRW
fclose(fp);
clrscr();
printf("\nplease input xueyuan name:");
gets(mid);

GS3
for(i=0;i if(strcmp(mid,stu[i].xy)==0)
{ PTF1
k++; /*记录符合条件的学生人数*/
}
printf("\nThere are %d students in the same university!\n",k);
}

void order() /*排序函数*/
{int i=0,n=0;
struct student stu[N],*p,*q,temp;
FILE *fp;
char c;
fp=fopen("f:\\123.txt","r");
PDW
SRW

if(n==0)
{ printf("no the student!\n");return; }
else {clrscr();
printf("\nWhich way to order ?number(1) or name(2):\n");
printf("\nPlease choose(1-2):");

c=getch();
putchar(c);

if(c=='1')

{ for(p=stu;p for(q=p+1;q if(strcmp(p->xh,q->xh)>0)
{ temp=*p;
*p=*q;
*q=temp;
}
}
else if(c=='2')

{
for(p=stu;p for(q=p+1;q if(tolower(p->xm[0])>tolower(q->xm[0])) /*按字母排序*/
{temp=*p;
*p=*q;
*q=temp;
}
}
else {printf("Error");return;}
}
printf("\n higher(1) or lower(2) order?\n"); /*按升序排(1)还是降序(2)*/
printf("\nplease choose(1-2):\n");
c=getch();
putchar(c);

if(c=='1')
{GS3
for(i=0;i {PTF1
GS2 /*将学生信息传送到文件fp中*/
}
}
else if(c=='2')
{GS3
for(i=n-1;i>=0;i--)
{PTF1
GS2
}
}
else {printf("Error");return;}
fclose(fp);

}
void password()
{char password[11]="123456",*p;
int count=0;
do
{if(count>0)
printf("Error!input again\n");
printf("please input password\n");
gets(p);
count++;
}while(strcmp(password,p)!=0&&count<3);
if(count>=3)
exit(1);
}

回答2:

第一个头文件 LINKLIST.H:
#include
#include
#include
#include

//预定义常量
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2

//单链表元素数据类型定义
typedef struct { //结点数据域类型定义
char num[5]; //编号
char name[10]; //姓名
char sex[3]; //性别
char phone[13]; //电话
char addr[30]; //地址
} ElemType;

typedef struct LNode{ //单链表的结点类型定义
ElemType data; //数据域
struct LNode *next; //指针域
} LNode , *LinkList;

//布尔类型定义
typedef int BOOL;

//函数返回类型定义
typedef int Status;

//单链表操作的函数原型声明
Status CreateList(LinkList &L); //构造带表头结点的通信录单链表
void Insert(LinkList &L,ElemType e); //人员记录的插入
LNode * Find(LinkList L); //通信录查询
void Delete(LinkList &L); //人员记录的删除
void PrintList(LinkList L); //遍历表,输出每个表元素值

第二个头文件 MENU.H:
#include

int menu_select(); //菜单选择函数
第一个C++文件 LINKLIST.CPP:
#include "linklist.h"

Status CreateList(LinkList &L)
{ //用尾插法建立带表头结点的通信录单链表L,输入记录按num有序
//system("cls");
int flag=1;
LNode *p,*rear;
L = (LinkList)malloc(sizeof(LNode));
if (!L) return ERROR;
L->next = NULL;
rear=L; //尾指针初始指向头结点
while (flag==1)
{
p=(LinkList)malloc(sizeof(LNode)); //申请新结点
printf("编号(4)姓名(8) 性别 电话(11) 地址(30)\n");
scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
rear->next =p;
rear=p;
printf("继续输入吗?(1/0)");
scanf("%d",&flag);
}
rear->next =NULL;
return OK;
}

void Insert(LinkList &L,ElemType e)
{ //人员记录的插入,使记录按num有序
LNode *p1,*p2,*p;
p1=L;
p2=p1->next ;
while (p2!=NULL && strcmp(p2->data.num,e.num)<0)
{
p1=p2;p2=p2->next;
}
p=(LinkList)malloc(sizeof(LNode)); //申请新结点
strcpy(p->data.num,e.num);
strcpy(p->data.name,e.name);
strcpy(p->data.sex,e.sex);
strcpy(p->data.phone,e.phone);
strcpy(p->data.addr,e.addr);
p1->next =p;
p->next =p2;
}

LNode * Find(LinkList L)
{ //通信录查询
LNode *p;
char num[5];
char name[10];
int select;
//system("cls");
printf("===============\n");
printf("1. 按编号查询 \n");
printf("2. 按姓名查询 \n");
printf("===============\n");
printf("请选择:\n");
p=L->next ;
scanf("%d",&select);
if (select==1)
{
printf("请输入要查找的编号:");
scanf("%s",num);
while (p && strcmp(p->data.num,num)<0)
p=p->next;
if (p==NULL || strcmp(p->data.num,num)>0)
{
p=NULL;
}
}
else
if (select==2)
{
printf("请输入要查找的姓名:");
scanf("%s",name);
while (p && strcmp(p->data.name,name)!=0)
p=p->next;
}
return p;
}

void Delete(LinkList &L)
{ //人员记录的删除
LNode *p,*q;
p=Find(L);
if (p==NULL)
{
printf("未查到要删除的记录!\n");
return;
}
q=L;
while (q!=NULL && q->next !=p)
q=q->next;
q->next =p->next ;
free(p);
printf("记录已被删除!\n");
}

void PrintList(LinkList L)
{ //遍历表,输出每个表元素值
LNode *p=L->next ;
system("cls");
printf("编号 姓 名 性别 电 话 地 址\n");
printf("-----------------------------------------------------\n");
while (p!=NULL)
{
printf("%s%s%s%s%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
printf("-----------------------------------------------------\n");
p=p->next ;
}
}
第二个C++文件 MENU.CPP:
#include "menu.h"

/***********************/
/* 菜单选择函数 */
/***********************/
int menu_select()
{
int sele;
printf("\n 通信录管理系统\n");
printf("===================================\n");
printf(" 1. 通信录单链表的建立\n");
printf(" 2. 人员记录的插入\n");
printf(" 3. 通信录的查询\n");
printf(" 4. 人员记录的删除\n");
printf(" 5. 通信录的输出\n");
printf(" 0. 退出系统\n");
printf("===================================\n");
printf(" 请选择 0--5:\n");
for(;;)
{
scanf("%d",&sele);
if (sele<0||sele>5)
printf("\n\t输入错误,重选0--5:");
else
break;
}
return sele;
}
主函数 MN.CPP:
#include "linklist.h"
#include "menu.h"

void main()
{
LinkList L,p;
ElemType e;
for (;;)
{
switch(menu_select())
{
case 1:
printf("****************************\n");
printf("* 通信录单链表的建立 *\n");
printf("****************************\n");
CreateList(L);
break;
case 2:
printf("****************************\n");
printf("* 人员记录的插入 *\n");
printf("****************************\n");
printf("编号(4)姓名(8) 性别 电话(11) 地址(30)\n");
scanf("%s%s%s%s%s",e.num,e.name,e.sex,e.phone,e.addr);
Insert(L,e);
break;
case 3:
printf("****************************\n");
printf("* 通信录的查询 *\n");
printf("****************************\n");
p=Find(L);
if (p!=NULL)
{
printf("编号 姓 名 性别 电 话 地 址\n");
printf("-----------------------------------------------------\n");
printf("%s%s%s%s%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
printf("-----------------------------------------------------\n");
}
else
printf("没有查到要查询的人员!\n");
break;
case 4:
printf("****************************\n");
printf("* 人员记录的删除 *\n");
printf("****************************\n");
Delete(L);
break;
case 5:
printf("****************************\n");
printf("* 通信录的输出 *\n");
printf("****************************\n");
PrintList(L);
break;
case 0:
printf("\t 退 出 !\n");
return;
}
}
}

注:楼主需先建立一个空的工程,然后新建上述的几个文件(复制粘贴就行),我已经编译运行过了,能够满足要求的说O(∩_∩)O~