69,368
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include<conio.h>/*屏幕操作函数库*/
#define header1 "|***************************************************************通讯录***************************************************************|\n"
#define header2 "| 编号 | 姓名 | 单位 | 固定电话 | 移动手机 | 分组 | 地址 | 电子邮箱 | QQ |\n"
#define header3 "|----------|--------------------|------------------------------|------------------------------|------------------------------|--------------------|------------------------------|----------|\n"
#define end "|-------------------------------------------------------------------------------------结束--------------------------------------------------------------------------------------------------|\n"
int input();
int show();
void dele();
void save();
int change();
int cx();
int find1();
int find2();
int find3();
void quit();
///////////结构体//////////
struct telebook
{
int num[10];
char name[20];
char unit[30];
char telephone[30];
char mobilephone[30];
char code[20];
char addr[30];
char E_mail[30];
int QQ[10];
};
void printheader() /*格式化输出菜单头*/
{
printf(header1);
printf(header2);
printf(header3);
}
///////////////////////////////////////
///通讯信息录入//////////////
/////////////////////////////////////
int input(struct telebook temp[])
{
char sign,x[20];
int i=0;
FILE *fp;/*定义文件指针*/
system("cls");
if((fp=fopen("telebook.txt","ab+"))==NULL) {
printf("打开文件有误!");
exit(0);
}
while(sign!='n'&&sign!='N')
{
printf("\t编号:");
scanf("%d", temp[i].num);
printf("\t姓名:");
scanf("%s", temp[i].name);
printf("\t单位:");
scanf("%s", temp[i].unit);
printf("\t固定电话:");
scanf("%s", temp[i].telephone);
printf("\t移动手机:");
scanf("%s", temp[i].mobilephone);
printf("\t分组:");
scanf("%s", temp[i].code);
printf("\t通讯住址:");
scanf("%s", temp[i].addr);
printf("\t电子邮箱:");
scanf("%s", temp[i].E_mail);
printf("\tQQ号码:");
scanf("%d", temp[i].QQ);
fwrite(&temp[i], sizeof(struct telebook),1,fp);
//gets(x);
printf("\n\t是否继续添加?(Y/N)");
getchar(); /* for '\n' */
scanf("%c",&sign);
i++;
}
fclose(fp);
system("cls");
return 0;
}
////////////////////////////////////////
///通讯信息显示///////////////
///////////////////////////////////////
int show(struct telebook temp[])
{
int i;
printheader();
for(i=0;i<100;i++)
{
if(temp[i].num=='\0')
break;
printf("%10s-%20s-%30s-%30s-%30s-%20s-%30s-%30s-%10s\n",temp[i].num,temp[i].name,temp[i].unit,temp[i].telephone,temp[i].mobilephone,temp[i].code,temp[i].addr,temp[i].E_mail,temp[i].QQ);
}
printf("-----------------------------------------------------\n");
system("pause");
system("cls");
return 0;
}
//////////////////////////////////////
///通讯信息删除/////////////
/////////////////////////////////////
void dele(struct telebook temp[])
{
char s[20];
int i=0,j;
show (temp);
printf("\t请输入想删除记录中的名字:");
scanf("%s",s);
for(i=0;i<100;i++)
{
if(strcmp(temp[i].name,s)==0)
{
for(j=i;j<20;j++)
{
temp[j]=temp[j+1];
}
printf("\t\t\t已经成功删除!\n");
getch();
system("cls");
return ;
}
}
printf("\t通讯录中没有此人!\n");
getch();
system("cls");
}
////////////////////////////////////
///通讯信息保存////////////
////////////////////////////////////
void save(struct telebook temp[])
{
int i;
FILE*fp;
if((fp=fopen("telebook.txt","w"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
for (i=0;strlen(temp[i].name)!=0;i++)
fprintf(fp,"%d%s%s%s%s%s%s%s%d\n",temp[i].num,temp[i].name,temp[i].unit,temp[i].telephone,temp[i].mobilephone,temp[i].code,temp[i].addr,temp[i].E_mail,temp[i].QQ);
fclose(fp);
}
////////////////////////////////////////
///通讯信息修改///////////////
/////////////////////////////////////
int change(struct telebook temp[])
{
char s[20];
int i=0;
show (temp);
printf("\t请输入想修改的记录中的名字:");
scanf("%s",s);
for(i=0;i<100;i++)
{
if(strcmp(temp[i].name,s)==0)
{
printf("\t编号:");
scanf("%s", temp[i].num);
printf("\t姓名:");
scanf("%s", temp[i].name);
printf("\t单位:");
scanf("%s", temp[i].unit);
printf("\t固定电话:");
scanf("%s", temp[i].telephone);
printf("\t移动手机:");
scanf("%s", temp[i].mobilephone);
printf("\t分组:");
scanf("%s", temp[i].code);
printf("\t通讯住址:");
scanf("%s", temp[i].addr);
printf("\t电子邮箱:");
scanf("%s", temp[i].E_mail);
printf("\tQQ号码:");
scanf("%d", temp[i].QQ);
printf("\t修改成功!");
getch();
system("cls");
return 0;
}
}
if (i >= 100)
{
printf("\t通讯录中没有此人!\n");
return -1;
}
getch();
system("cls");
}
///////////////////////////////////
///通讯信息查询//////////
//////////////////////////////////
int cx(struct telebook temp[])
{
int n,flag=1;
printf("你想通过什么方式查找?\n");
printf("\t1.编号\t\t2.姓名\t\t3.电话号码\t4.返回\n");
scanf("%d", &n);
do{
if(n==1)
find1(temp);
if(n==2)
find2(temp);
if(n==3)
find3(temp);
if(n==4)
system("cls");
else{
flag=0;
printf("输入不正确,请重新输入:");
scanf("%d",&n);
}
}
while(flag==0);
return 0;
}
/////////////////////////////////////////
///按编号查询////////////////////
////////////////////////////////////////
int find1(struct telebook temp[])
{
int i=0,s=0;
printf("\t请输入想查询的编号:");
scanf("%d",&s);
for(i=0;i<100;i++)
{
if(temp[i].num=s)
{
printheader();
printf("%-10s%-20s%-20s%-20s\n",temp[i].num,temp[i].name,temp[i].phonenum,temp[i].addr);
getch();
system("cls");
return 0;
}
}
if (i >= 100)
printf("\t通讯录中没有此人!\n");
getch();
system("cls");
return 1 ;
}
//////////////////////////////////////
///按姓名查询////////////////
///////////////////////////////////
int find2(struct telebook temp[])
{
char s[20];
int i=0;
printf("\t请输入想查询的姓名:");
scanf("%s",s);
for (i=0;i<100;i++)
{
if(strcmp(temp[i].name,s)==0)
{
printheader();
printf("%-10d%-20s%-20s%-20s\n",temp[i].num,temp[i].name,temp[i].phonenum,temp[i].addr);
getch();
system("cls");
return 0;
}
}
if (i > 100)
printf("\t通讯录中没有此人!\n");
getch();
system("cls");
return 1;
}
///////////////////////////////////////
///按电话号码查询///////////
/////////////////////////////////////
int find3(struct telebook temp[])
{
char s[20];
int i=0;
printf("\t请输入想查询的电话:");
scanf("%s",s);
for (i=0;i<100;i++)
{
if(strcmp(temp[i].phonenum,s)==0)
{
printheader();
printf("%-10d%-20s%-20s%-20s\n",temp[i].num,temp[i].name,temp[i].phonenum,temp[i].addr);
getch();
system("cls");
return 0;
}
}
if (i >= 100)
printf("\t通讯录中没有此人!\n");
getch();
system("cls");
return 1;
}
/////////////////////
////退出/////////
void quit()
{ printf("\n");
printf("\n");
printf("\t\t---------------------------------------------------------------\n");
printf("\t\t---------------------感谢您的使用------------------------\n");
printf("\t\t-----------------------欢迎再次用-------------------------\n");
printf("\t\t---------------------------谢谢------------------------------\n");
exit(0);
getch();
system("cls");
}
///////////////////////////////////
///主函数//////////////////////
//////////////////////////////////
int main()
{
struct telebook mess[100];
char xx;
system("color 2E");/*改变控制台颜色*/
while(1)
{
printf("\t 【欢迎进入通讯录管理系统】\n");
printf("\t\t******************menu********************\n");
printf("\t\t----------------------------------------------------\n");
printf("\t\t\t a、 通讯信息录入\n");
printf("\t\t\t b、 通讯信息显示\n");
printf("\t\t\t c、 通讯信息保存\n");
printf("\t\t\t d、 通讯信息删除\n");
printf("\t\t\t e、 通讯信息修改\n");
printf("\t\t\t f、 通讯信息查询\n");
printf("\t\t\t .Ⅰ、按编号查询\n");
printf("\t\t\t . Ⅱ、按姓名查询\n");
printf("\t\t\t . Ⅲ、按电话号码查询\n");
printf("\t\t\t g、 退出系统\n");
printf("\t\t-------------------------------------------------\n");
printf("\t\t******************************************\n");
printf("请输入您的选择\n");
scanf("%c",&xx);
system("cls");
switch(xx)
{
case 'a':input(mess);break;
case 'b':show(mess);break;
case 'c':save(mess);break;
case 'd':dele(mess);break;
case 'e':change(mess);break;
case 'f':cx(mess);break;
case 'g':quit();break;
default:return 0;
}
getchar();
}
return 0;
}
参考一下吧
解决语法错误是最基本的编程能力体现,建议还是努力学好基本的编程语法