| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5469 人关注过本帖
标题:[求助] 用c编写学生通讯录管理系统
只看楼主 加入收藏
caishuzhou
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-7-15
收藏
得分:0 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
struct Record
{
int ID;
char name[20];
char phoneNum[11];
int team;
};
struct Record Records[300];
int num=0;
void AddNew();
int IsLoop();
void Display();
void ShowMenu();
void Search_name();
void Search_phoneNum();
void Search_team();
void ChkData(char name[],char phoneNum[],int team);
void Record_Modify();
void DeleteMenu();
void DeleteAll();
void DeleteOne();
void ReadFromFile();
void MainMenu();
void WriteToFile();
void main()
{
ReadFromFile();
while (1)
{
MainMenu();
WriteToFile();
}
}


//返回下标
int Record_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(Records[i].name,name)==0)
{
return i;
}
}
return -1;
}
//显示一条信息
void Record_DisplaySingle(int index)

{

printf("\n%s%s%s\n\n","姓名\t","号码\t","组名\t");

printf("--------------------------------------------------------------------------\n");

printf("%-6s%5s%7d\n",Records[index].name,Records[index].phoneNum,Records[index].team);
}
//主菜单
void MainMenu()
{
printf("------------------------主菜单-----------------------------\n");
int option;
printf("1-添加新记录\n");
printf("2-查看\n");
printf("3-修改\n");
printf("4-删除\n");
printf("5-退出程序\n");
printf("请选择:");
scanf("%d",&option);
getchar();
switch (option)
{
case 1:AddNew();break;
case 2:ShowMenu();break;
case 3:Record_Modify();break;
case 4:DeleteMenu();break;
case 5:exit(0);
}
}
//添加记录
void AddNew()
{
printf("------------------------添加新记录-----------------------------\n");
printf("输入姓名:");
scanf("%s",&Records[num].name);
getchar();
printf("输入电话号码:");
scanf("%s",&Records[num].phoneNum);
getchar();
printf("选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&Records[num].team);
getchar();
ChkData(Records[num].name,Records[num].phoneNum,Records[num].team);
printf("你所输入的是:\n姓名:%s 电话号码:%s ",Records[num].name,Records[num].phoneNum);
printf("分组:");
if (Records[num].team==1)
printf("家庭");
if (Records[num].team==2)
printf("朋友");
if (Records[num].team==3)
printf("其他");
printf("\n是否存储?(Y/N):");
if (getchar()=='y' || getchar()=='Y')
{
num++;
getchar();
}
}
//查询菜单
void ShowMenu()
{
printf("------------------------查询菜单-----------------------------\n");
int option;
printf("1-查看所有\n2-按姓名查询\n3-按号码查询\n4-按分组查询\n5-返回主菜单\n请选择:");
scanf("%d",&option);
getchar();
switch (option)
{
case 1:
Display();ShowMenu();
case 2:
Search_name();ShowMenu();
case 3:
Search_phoneNum();ShowMenu();
case 4:
Search_team();ShowMenu();
case 5:
MainMenu();
default:
ShowMenu();
}
}
//显示所有信息
void Display()
{
int i;
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%-20s %-11s",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
//按姓名查找
void Search_name()
{
printf("------------------------按姓名查找-----------------------------\n");
char name[20];
int i;
printf("请输入姓名:");
scanf("%s",name);
if (strlen(name)>20)
{
printf("输入的姓名不能超过20个字符!\n");
getchar();
ShowMenu();
}
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (strcmp(Records[i].name,name)==0)
{
printf("%-20s %-11s ",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
}
//按电话号码查找
void Search_phoneNum()
{
char number[11];
int i;
printf("请输入电话号码:");
scanf("%s",number);
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (strcmp(Records[i].phoneNum,number)==0)
{
printf("%-20s %-11s ",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
}
//按分组查找
void Search_team()
{
int op,i;
printf("选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&op);
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (Records[i].team==op)
{
printf("%-20s %-11s ",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
}
//修改记录
void Record_Modify()
{

char name[20];
int index;
printf("--------------------------修改信息----------------------------");
printf("\n请输入要修改的姓名:");
scanf("%s",&name);
if(strlen(name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
index=Record_SearchByName(name);
if (index==-1)
{
printf("\n该姓名不存在!\n");
}
else
{
printf("你要修改的信息为:\n");
Record_DisplaySingle(index);
printf("\n-----------------------------请输入新值--------------------------\n");
printf("\n请输入姓名:");
scanf("%s",&Records[index].name);
if(strlen(Records[index].name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
printf("\n请输入电话号码(7--11位):");
scanf("%s",&Records[index].phoneNum);
if(strlen(Records[index].phoneNum)>11||strlen(Records[index].phoneNum)<7)
{
printf("很抱歉!你输入的不是合法的电话号码!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
printf("\n选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&Records[index].team);
getchar();
}
if (IsLoop())
{
Record_Modify();
}
else
return;

}
//数据检测
void ChkData(char name[],char phoneNum[],int team)
{
if (strlen(name)>20)
{
printf("姓名的字符不能超过20个!\n");
MainMenu();
}
if (strlen(phoneNum)>11)
{
printf("电话号码不能超过11位!\n");
MainMenu();
}
else
{
double phoneNumLen;
phoneNumLen=strlen(phoneNum);
for (int i=0;i<phoneNumLen;i++)
{
if (isdigit(phoneNum[i])==0)
{
printf("电话号码应为数字!\n");
MainMenu();
}
}
}
if (team>3 || team<1)
{
printf("分组选择错误!\n");
MainMenu();
}
}
//删除菜单
void DeleteMenu()
{
printf("--------------------------删除菜单--------------------------\n");
printf("1-删除单条信息\n2-删除所有\n3-返回主菜单\n请输入:");
int op;
scanf("%d",&op);
switch (op)
{
case 1:
DeleteOne();break;
case 2:
DeleteAll();break;
case 3:
MainMenu();break;
default:
DeleteMenu();break;
}
}
//删除所有
void DeleteAll()
{
printf("是否删除?(Y/N)");
if (getchar()=='Y' || getchar()=='y')
{
printf("***********************删除所有******************\n");
system("copy none phone.txt");
printf("\r已经删除\n");
main();
}
else
return;
}
//删除一条信息
void DeleteOne()
{
int i;
char name[20];
int index;
printf("\n请输入要删除的姓名:");
scanf("%s",&name);
if(strlen(name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
DeleteMenu();
}
else
return;
}
getchar();
index=Record_SearchByName(name);
if (index==-1)
{
printf("\n该姓名不存在!\n");
}
else
{
printf("\n你要删除的记录为:\n");
Record_DisplaySingle(index);
printf("\n是否真的要删除?(y/n)\n");
char p;
scanf("\n%c",&p);
if (p=='Y'||p=='y')
{
for (i=index;i<num-1;i++)
{
Records[i]=Records[i+1];
}
num--;
}
getchar();
}
if (IsLoop())
{
DeleteMenu();
}
else
return;
}
//循环输入
int IsLoop()
{
printf("是否继续?(Y/N):");
if (getchar()=='y' || getchar()=='Y')
{
return 1;
}
else
return 0;
}
//从文件读取记录
void ReadFromFile()
{
FILE *fp;
int i;
if ((fp=fopen("phone.txt","rb"))==NULL)
{
printf("电话簿不存在,已重新建立!\n");
return;
}
if (fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&Records[i],sizeof(struct Record),1,fp);
}
}
fclose(fp);
}

//将记录写如文件
void WriteToFile()
{

FILE *fp;
int i;
if ((fp=fopen("phone.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<num;i++)
{
if (fwrite(&Records[i],sizeof(struct Record),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}

2007-07-16 09:12
fangfangff
Rank: 1
等 级:新手上路
威 望:2
帖 子:479
专家分:0
注 册:2006-12-22
收藏
得分:0 
以下是引用caishuzhou在2007-7-16 9:12:55的发言:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
struct Record
{
int ID;
char name[20];
char phoneNum[11];
int team;
};
struct Record Records[300];
int num=0;
void AddNew();
int IsLoop();
void Display();
void ShowMenu();
void Search_name();
void Search_phoneNum();
void Search_team();
void ChkData(char name[],char phoneNum[],int team);
void Record_Modify();
void DeleteMenu();
void DeleteAll();
void DeleteOne();
void ReadFromFile();
void MainMenu();
void WriteToFile();
void main()
{
ReadFromFile();
while (1)
{
MainMenu();
WriteToFile();
}
}


//返回下标
int Record_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(Records[i].name,name)==0)
{
return i;
}
}
return -1;
}
//显示一条信息
void Record_DisplaySingle(int index)

{

printf("\n%s%s%s\n\n","姓名\t","号码\t","组名\t");

printf("--------------------------------------------------------------------------\n");

printf("%-6s%5s%7d\n",Records[index].name,Records[index].phoneNum,Records[index].team);
}
//主菜单
void MainMenu()
{
printf("------------------------主菜单-----------------------------\n");
int option;
printf("1-添加新记录\n");
printf("2-查看\n");
printf("3-修改\n");
printf("4-删除\n");
printf("5-退出程序\n");
printf("请选择:");
scanf("%d",&option);
getchar();
switch (option)
{
case 1:AddNew();break;
case 2:ShowMenu();break;
case 3:Record_Modify();break;
case 4:DeleteMenu();break;
case 5:exit(0);
}
}
//添加记录
void AddNew()
{
printf("------------------------添加新记录-----------------------------\n");
printf("输入姓名:");
scanf("%s",&Records[num].name);
getchar();
printf("输入电话号码:");
scanf("%s",&Records[num].phoneNum);
getchar();
printf("选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&Records[num].team);
getchar();
ChkData(Records[num].name,Records[num].phoneNum,Records[num].team);
printf("你所输入的是:\n姓名:%s 电话号码:%s ",Records[num].name,Records[num].phoneNum);
printf("分组:");
if (Records[num].team==1)
printf("家庭");
if (Records[num].team==2)
printf("朋友");
if (Records[num].team==3)
printf("其他");
printf("\n是否存储?(Y/N):");
if (getchar()=='y' || getchar()=='Y')
{
num++;
getchar();
}
}
//查询菜单
void ShowMenu()
{
printf("------------------------查询菜单-----------------------------\n");
int option;
printf("1-查看所有\n2-按姓名查询\n3-按号码查询\n4-按分组查询\n5-返回主菜单\n请选择:");
scanf("%d",&option);
getchar();
switch (option)
{
case 1:
Display();ShowMenu();
case 2:
Search_name();ShowMenu();
case 3:
Search_phoneNum();ShowMenu();
case 4:
Search_team();ShowMenu();
case 5:
MainMenu();
default:
ShowMenu();
}
}
//显示所有信息
void Display()
{
int i;
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%-20s %-11s",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
//按姓名查找
void Search_name()
{
printf("------------------------按姓名查找-----------------------------\n");
char name[20];
int i;
printf("请输入姓名:");
scanf("%s",name);
if (strlen(name)>20)
{
printf("输入的姓名不能超过20个字符!\n");
getchar();
ShowMenu();
}
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (strcmp(Records[i].name,name)==0)
{
printf("%-20s %-11s ",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
}
//按电话号码查找
void Search_phoneNum()
{
char number[11];
int i;
printf("请输入电话号码:");
scanf("%s",number);
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (strcmp(Records[i].phoneNum,number)==0)
{
printf("%-20s %-11s ",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
}
//按分组查找
void Search_team()
{
int op,i;
printf("选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&op);
printf("姓名 电话号码 分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (Records[i].team==op)
{
printf("%-20s %-11s ",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf(" %-5s\n","家庭");
if (Records[i].team==2)
printf(" %-5s\n","朋友");
if (Records[i].team==3)
printf(" %-5s\n","其他");
}
}
}
//修改记录
void Record_Modify()
{

char name[20];
int index;
printf("--------------------------修改信息----------------------------");
printf("\n请输入要修改的姓名:");
scanf("%s",&name);
if(strlen(name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
index=Record_SearchByName(name);
if (index==-1)
{
printf("\n该姓名不存在!\n");
}
else
{
printf("你要修改的信息为:\n");
Record_DisplaySingle(index);
printf("\n-----------------------------请输入新值--------------------------\n");
printf("\n请输入姓名:");
scanf("%s",&Records[index].name);
if(strlen(Records[index].name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
printf("\n请输入电话号码(7--11位):");
scanf("%s",&Records[index].phoneNum);
if(strlen(Records[index].phoneNum)>11||strlen(Records[index].phoneNum)<7)
{
printf("很抱歉!你输入的不是合法的电话号码!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
printf("\n选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&Records[index].team);
getchar();
}
if (IsLoop())
{
Record_Modify();
}
else
return;

}
//数据检测
void ChkData(char name[],char phoneNum[],int team)
{
if (strlen(name)>20)
{
printf("姓名的字符不能超过20个!\n");
MainMenu();
}
if (strlen(phoneNum)>11)
{
printf("电话号码不能超过11位!\n");
MainMenu();
}
else
{
double phoneNumLen;
phoneNumLen=strlen(phoneNum);
for (int i=0;i<phoneNumLen;i++)
{
if (isdigit(phoneNum[i])==0)
{
printf("电话号码应为数字!\n");
MainMenu();
}
}
}
if (team>3 || team<1)
{
printf("分组选择错误!\n");
MainMenu();
}
}
//删除菜单
void DeleteMenu()
{
printf("--------------------------删除菜单--------------------------\n");
printf("1-删除单条信息\n2-删除所有\n3-返回主菜单\n请输入:");
int op;
scanf("%d",&op);
switch (op)
{
case 1:
DeleteOne();break;
case 2:
DeleteAll();break;
case 3:
MainMenu();break;
default:
DeleteMenu();break;
}
}
//删除所有
void DeleteAll()
{
printf("是否删除?(Y/N)");
if (getchar()=='Y' || getchar()=='y')
{
printf("***********************删除所有******************\n");
system("copy none phone.txt");
printf("\r已经删除\n");
main();
}
else
return;
}
//删除一条信息
void DeleteOne()
{
int i;
char name[20];
int index;
printf("\n请输入要删除的姓名:");
scanf("%s",&name);
if(strlen(name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
DeleteMenu();
}
else
return;
}
getchar();
index=Record_SearchByName(name);
if (index==-1)
{
printf("\n该姓名不存在!\n");
}
else
{
printf("\n你要删除的记录为:\n");
Record_DisplaySingle(index);
printf("\n是否真的要删除?(y/n)\n");
char p;
scanf("\n%c",&p);
if (p=='Y'||p=='y')
{
for (i=index;i<num-1;i++)
{
Records[i]=Records[i+1];
}
num--;
}
getchar();
}
if (IsLoop())
{
DeleteMenu();
}
else
return;
}
//循环输入
int IsLoop()
{
printf("是否继续?(Y/N):");
if (getchar()=='y' || getchar()=='Y')
{
return 1;
}
else
return 0;
}
//从文件读取记录
void ReadFromFile()
{
FILE *fp;
int i;
if ((fp=fopen("phone.txt","rb"))==NULL)
{
printf("电话簿不存在,已重新建立!\n");
return;
}
if (fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&Records[i],sizeof(struct Record),1,fp);
}
}
fclose(fp);
}

//将记录写如文件
void WriteToFile()
{

FILE *fp;
int i;
if ((fp=fopen("phone.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<num;i++)
{
if (fwrite(&Records[i],sizeof(struct Record),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}

我晕死你,把我的东西搬过来了还没标注一下出处啊!!!
小心我告你版权侵犯哦!!!!!


千里冰封---My Love 尽管相隔千里 , 依然拥有冰封
2007-07-16 11:20
fangfangff
Rank: 1
等 级:新手上路
威 望:2
帖 子:479
专家分:0
注 册:2006-12-22
收藏
得分:0 
11楼真的太过分了...............
竟然也没顶一下..........
郁闷.............
建议封他ID

千里冰封---My Love 尽管相隔千里 , 依然拥有冰封
2007-07-16 11:23
miruo
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-7-18
收藏
得分:0 
谢谢哦!

用新生活。加油!BAXIA!!!
2007-07-18 13:44
sky089
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-24
收藏
得分:0 
还有没有啊??

爱上你 就不会后悔!!
2010-04-24 14:37
miya829475
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-3-8
收藏
得分:0 
回复 楼主 lizhihua1022
能给发一份吗?参考下  谢谢
2011-03-10 17:21
我无痞气
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-5-5
收藏
得分:0 
感谢
2015-05-05 17:18
快速回复:[求助] 用c编写学生通讯录管理系统
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041067 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved