c語言電話簿管理系統
㈠ 求用C語言編寫一個 通訊錄管理系統 。設計要求如下
這個主要用到鏈表來做就可以的啦,鏈表的基本操作(創建、插入、刪除等)要掌握好。之前我自己寫有一個,和你的功能差不多,但是變數有所不同,給你參考參考吧,可以仿照。不懂的再問吧。代碼如下:
#include"stdafx.h"
using namespace std;
//結構體
struct contaxt
{
char name[10];
char sex[5];
char Adrre[50];
char Class[20];
char Mobi[20];
char qq[10];
struct contaxt *next;
};
//函數聲明
typedef struct contaxt CONT;
CONT *InsertCon(CONT *head);
CONT *AddContaxt(int n);
CONT *FindCon(CONT *head);
CONT *AmendMessage(CONT *head);
CONT *DeleteMessage(CONT *head);
void showmessage(CONT *head);
int menu(CONT *head);
CONT *saveFileCONT(CONT *head );
CONT *readFileCONT(CONT *head);
int choose;
//入口
void main()
{
//system("color F4");
CONT *head;
head = NULL;
menu(head);
}
//系統主菜單
int menu(CONT *head)
{
cout << "請選擇操作菜單!!!" << endl ;
do
{
cout << "---------------------------歡迎進入通訊錄管理系統-------------------------------"
<< "---------------------------------操作菜單---------------------------------------";
cout << "--------------------------------------------------------------------------------"
<< " 1 添加聯系人信息 "
<< " 2 插入聯系人信息 " << endl
<< " 3 查找聯系人信息 "
<< " 4 修改聯系人信息 " << endl
<< " 5 刪除聯系人信息 "
<< " 6 顯示聯系人信息 " << endl
<< " 7 退出本系統 " << endl
<< " 8 保存文件 " << endl
<< " 9 讀取文件 " << endl
<< "--------------------------------------------------------------------------------" << endl;
cout << "請選擇操作菜單!!!" << endl ;
int choose;
//choose = getchar();
fflush(stdin);
//return choose;
cin >> choose;
if(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7&&choose != 8)
{
cout << "輸入錯誤:"<<endl;
}
else
{
switch(choose)
{
case 1:
int n;
do
{
fflush(stdin);
cout << "請確定要添加幾個聯系人:";
cin >> n;
}while(n <= 0);
head = AddContaxt(n);
break;
case 2:
system("CLS");
head = InsertCon(head);
break;
case 3:
head = FindCon(head);
break;
case 6:
system("CLS");
showmessage(head);
break;
case 7:
exit(0);
break;
case 4:
system("CLS");
AmendMessage(head);
break;
case 5:
system("CLS");
DeleteMessage(head);
break;
case 8:
system("CLS");
head = saveFileCONT(head );
break;
case 9:
// head = readFileCONT(head);
default:
system("CLS");
cout << "你輸入錯誤,請重新選擇!!!" << endl;
break;
}
}
}while(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7);
return choose;
}
//添加聯系人
CONT *AddContaxt(int n)
{
CONT *p,*q,*head;
int i;
system("CLS");
p = (CONT *)malloc(sizeof(CONT));
cout << "開始添加聯系人信息:"<< endl;
cout << "輸入姓名:";
cin >> p->name;
cout << "輸入姓別:";
cin >> p->sex ;
cout << "輸入班級:";
cin >> p->Class ;
cout << "輸入住址:";
cin >> p->Adrre ;
cout << "輸入手機號:";
cin >> p->Mobi ;
cout << "輸入qq號:";
cin >> p->qq ;
head = p;
for(i = 2;i <= n;i++)
{
q = (CONT *)malloc(sizeof(CONT));
p->next = q ;
p = q;
system("CLS");
cout<< "總共需要輸入"<< n << "個聯系人的信息,"<<"現在你已輸入" << i-1<< "個聯系人信息."<< endl<< "請繼續輸入第"<<i << "個人的信息!!"<< endl;
cout << "輸入姓名:";
cin >> p->name;
cout << "輸入姓別:";
cin >> p->sex ;
cout << "輸入班級:";
cin >> p->Class ;
cout << "輸入住址:";
cin >> p->Adrre ;
cout << "輸入手機號:";
cin >> p->Mobi ;
cout << "輸入qq號:";
cin >> p->qq ;
}
p->next = NULL;
return head;
}
//插入聯系人信息
CONT *InsertCon(CONT *head)
{
CONT *pc,*p,*q;
int choose ;
char name[10];
if(head ==NULL)
{
cout << "還沒有添加任何聯系人,請先添加聯系人!!"<< endl;
return head;
}
pc = (CONT *)malloc(sizeof(CONT));
cout << "請依次輸入被插入人的信息:姓名 /性別/班級/住址/手機號/qq號"<< endl;
cin >> pc->name >> pc->sex >> pc->Class >> pc->Adrre >> pc->Mobi >> pc->qq ;
pc ->next = NULL;
cout << "請選擇自己喜歡的插入方式: "<< endl<< "1 插入在自己喜歡的人之前"<< endl << "2 插入在最後" << endl << "3 插入在開頭" << endl;
do {
cin >> choose;
switch(choose)
{
case 1:
cout << "請輸入自己喜歡的人的名字:";
cin >> name ;
if(strcmp(head->name ,name)==0)
{
pc->next = head;
head = pc;
return head;
}
else
{
q = head;
p = head->next ;
while(strcmp(p->name,name)!=0 && p->next !=NULL)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
pc->next = p ;
q = pc;
return head;
}
else
{
p->next = pc;
return head;
}
}
break;
case 2:
//cout << "請輸入自己喜歡的人的名字:";
//cin >> name ;
p = head;
while(p->next != NULL)
p = p->next ;
p->next = pc;
return head;
break;
case 3:
//cout << "請輸入自己喜歡的人的名字:";
//cin >> name ;
pc->next = head;
head = pc;
return head;
break;
default :
cout<<"error"<< endl;
break;
}
}while(choose!=1 &&choose != 2 && choose != 3);
}
//查找聯系人信息
CONT *FindCon(CONT *head)
{
system("CLS");
char na[20];
CONT *p,*q;
if(head == NULL)
{
cout << "沒有任何信息可查找,請先添加信息!!"<< endl;
return head;
}
cout << "請您輸入要查找人的姓名:"<< endl;
cin >> na;
p = head;
while(p!= NULL)
{
if(strcmp(p->name ,na)==0)
{
q = p;
cout << "找到聯系人,信息如下!!"<< endl;
cout << "姓名:" << q->name <<" 性別: " << q->sex << q->Class <<" 住址: " <<q->Adrre <<" 手機號: " <<q->Mobi <<" qq號: "
<< q->qq << endl;
break;
}
else
p = p->next ;
}
return head;
}
//修改聯系人信息
CONT *AmendMessage(CONT *head)
{
CONT *p;
char nam[10];
char sex[5];
char Adrre[50];
char Class[20];
char Mobi[20];
char qq[10];
if(head == NULL)
{
cout<< "沒有任何信息,請返回添加信息!!!"<< endl;
return head;
}
else
p = head;
cout << "請你輸入要修改人的名字:";
cin >> nam;
while(p != NULL && p->name != nam)
{
if(strcmp(p->name ,nam)==0)
{
strcpy(p->name ,nam);
int choose;
do
{
cout << "請選擇要修改哪一項的信息!!!"<< endl;
cout << "1 修改性別 2 修改班別 3 修改地址 4 修改手機號碼 5 修改qq號 " << endl;
cin >> choose;
switch(choose)
{
case 1:
cout << "請輸入要修改後的性別:";
cin >> sex;
strcpy(p->sex , sex);
cout << "修改成功!!!"<< endl;
system("CLS");
return head;
break;
case 2:
cout << "請輸入修改後的班別:";
cin >> Class;
strcpy(p->Class ,Class);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 3:
cout << " 請輸入修改後的地址:";
cin >> Adrre;
strcpy(p->Adrre ,Adrre);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 4:
cout << "請輸入修改後的手機號碼:";
cin >> Mobi;
strcpy(p->Mobi ,Mobi);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 5:
cout << "請輸入修改後的qq號:";
cin >> qq;
strcpy(p->qq ,qq);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
default:
cout << "輸入錯誤,重新選擇!!" << endl;
break;
}
}while(choose != 0);
}
else if(p->next != NULL && p->name != nam)
{
p = p->next ;
}
else
{
cout << "輸入名字錯誤,請重新選擇4修改!!!" << endl;
return head;
}
}
//return head;
}
//刪除聯系人
CONT *DeleteMessage(CONT *head)
{
CONT *p,*q,*t;
char name[20];
char elem[10];
cout << "請輸入要刪除聯系人的姓名:";
cin >> name;
if(head == NULL)
{
cout << "沒有任何信息可刪除,請添加信息!!"<< endl;
return head;
}
else if(strcmp(head->name,name)==0 )
{
t = head;
head = head->next ;
return head;
}
else
{
p = head->next ;
q = head;
}
while(p !=NULL && p->name != name)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
q->next = p->next ;
free(p);
//t = p;
}
else
{
cout<< "表中無該元素!" << endl;
return head;
}
if(t)
{
// elem = t->name ;
free(t);
}
return head;
}
//保存文件
CONT *saveFileCONT(CONT *head )
{
char *fileName;
FILE *fp;
CONT *p;
p = (CONT *)malloc(sizeof(CONT));
if(p=NULL)
{
cout <<"分配內存失敗"<< endl;
// exit(0);
}
if(head ==NULL)
{
cout <<"沒有任何信息,請先添加信息!!"<< endl;
return head;
}
else
{
p = head;
fileName = (char *)malloc(sizeof(char));
cout << "請輸入文件名:" << endl;
cin >> fileName;
fileName = strcat(fileName,".txt");
if( (fp = fopen(fileName,"a")) == NULL)
cout << "can't open file" << endl;
do
{
//fwrite(head,sizeof(CONT),1,fp);
//fprintf(fp,p->name,p->sex,p->Class,p->Adrre,p->Mobi,p->qq );
fprintf(fp," 姓名:");
fprintf(fp, p->name );
fprintf(fp," 性別:");
fprintf(fp, p->sex);
fprintf(fp," 班別:");
fprintf(fp, p->Class);
fprintf(fp," 地址:");
fprintf(fp, p->Adrre);
fprintf(fp," 手機號:");
fprintf(fp, p->Mobi);
fprintf(fp," qq號:");
fprintf(fp, p->qq);
fprintf(fp,"\n");
cout << endl;
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性別:" <<p->sex << " 班別:" <<p->Class <<" 住址:" <<p->Adrre <<"手機號:"
<< p->Mobi <<" qq號:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p=p->next ;
}while(p!=NULL);
}
fclose(fp);
return head;
}
//讀取文件
CONT *readFileCONT(CONT *head)
{
char *fileName;
CONT *p;
FILE *fp;
fileName = (char *)malloc(sizeof(char));
cout << "請輸入要打開的文件名:";
cin >> fileName;
fileName = strcat(fileName,".txt");
fp = fopen(fileName,"r");
if(fp == NULL)
{
cout << "文件沒有任何信息,請先輸入信息!!"<< endl;
}
else
{
while(!feof(fp))
{
p = (CONT *)malloc(sizeof(CONT));
if(p == NULL)
{
cout << "分配內存失敗!!"<< endl;
return head;
}
fscanf(fp,p->name );
fscanf(fp,p->sex );
fscanf(fp,p->Class );
fscanf(fp,p->Adrre );
fscanf(fp,p->Mobi );
fscanf(fp,p->qq );
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性別:" <<p->sex << " 班別:" <<p->Class <<" 住址:" <<p->Adrre <<"手機號:"
<< p->Mobi <<" qq號:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p->next = head;
head = p;
p = p->next ;
}
fclose(fp);
cout<< "讀取信息成功!!!"<< endl;
}
fclose(fp);
return head;
}
void showmessage(CONT *head)
{
CONT *p;
int i=0;
if(head == NULL )
{
cout << "沒有任何信息可輸出,請添加信息!!!"<< endl;
}
else
{
p = head;
cout << "*******************************聯系表如下***************************************";
do
{
i++;
cout << "--------------------------------------------------------------------------------";
cout << i << " "<<"姓名:" <<p->name <<" 性別:" <<p->sex << " 班別:" <<p->Class <<" 住址:" <<p->Adrre <<"手機號:"
<< p->Mobi <<" qq號:" << p->qq << endl;
cout << "--------------------------------------------------------------------------------";
p = p->next ;
}while(p!=NULL);
}
cout << "*********************************聯系表***************************************";
cout << endl;
}
㈡ C語言程序設計 通訊錄系統管理
// 頭文件部分
//============================
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
// 初始化數據部分
int Menu();
void Start();
void Write();
void Read();
void Search();
void Detele();
void beauty();
void Rework();
void Lock();
int Judge();
//============================
struct Student{
char name[20];
char Tel[20];
char QQ[20];
int age;
}xy[100];
char wenjian[10000];
char Number[10];
char number=0;
int GOTO;
//
int main()
{
Start();
for(;;)
{
GOTO=Menu();
if(GOTO==1)
{
for(;;)
{
Write();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==2)
{
for(;;)
{
Read();
system("pause");
break;
}
}
if(GOTO==3)
{
for(;;)
{
Search();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==4)
{
for(;;)
{
Rework();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==5)
{
Detele();
break;
}
if(GOTO==6)
{
beauty();
break;
}
if(GOTO==7)
{
}
if(GOTO==8)
{
printf("");
break;
}
if(GOTO==9)
{
printf("\t\t\t退出系統\n\n");
return 0;
}
if(GOTO==0)
{
printf("");
break;
}
}
// beauty();
//
// Write();
// Read();
// Search();
// Rework();
// Detele();
return 0;
}
void Write()
{
printf("\t\t\t請輸入好友名字\n");
scanf("%s",&xy[number].name);
printf("\t\t\t請輸入好友電話號碼\n");
scanf("%s",&xy[number].Tel);
printf("\t\t\t請輸入好友QQ號碼\n");
scanf("%s",&xy[number].QQ);
printf("\t\t\t請輸入好友年齡\n");
scanf("%d",&xy[number].age);
FILE *file;
if((file=fopen("Friend.txt","rt"))==NULL)
{
file=fopen("Friend.txt","wt");
fprintf(file,"%-20s%-20s%-20s%-20s","姓名","電話","QQ","年齡");
}
file=fopen("Friend.txt","at");
fprintf(file,"%-20s%-20s%-20s%-20d\n",xy[number].name,xy[number].Tel,xy[number].QQ,xy[number].age);
fclose(file);
number++;
Number[0]=number;
FILE * Sta;
if((Sta=fopen("number.txt","r"))=NULL)
{
Sta=fopen("number.txt","wt");
exit(1);
}
Sta=fopen("number.txt","wt");
fprintf(Sta,"%d",Number[0]);
fclose(Sta);
}
void Read()
{
printf("\t\t\t您通訊錄中一共有%d位好友\n",Number[0]);
FILE* read;
if((read=fopen("Friend.txt","r"))=NULL)
{
printf("\t\t\t您通訊中當前沒有好友,請返回添加\n");
}
read=fopen("Friend.txt","rt");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
for(int i=0;i<number;i++)
{
fscanf(read,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
/* fread(wenjian,10000,1,read);
for(int i=0;i<number*80+80;i++)
{
printf("%c",wenjian[i]);
}
printf("\n");
*/
fclose(read);
}
void beauty()
{
int d;
do{
printf("choose");
scanf("%d",&d);
}while(d<1&&d>5);
if(d==1)
{
system("color 27");
printf("===1===");
}
if(d==2)
{
system("color 37");
printf("===2===");
}
if(d==3)
{
system("color 47");
printf("===3===");
}
if(d==4)
{
system("color 57");
printf("===4===");
}
if(d==5)
{
system("color 67");
printf("===5===");
}
}
void Start()
{
FILE* Start;
if((Start=fopen("number.txt","r"))==NULL)
{
Start=fopen("number.txt","w");
number=0;
Number[0]=number;
fscanf(Start,"%d",&Number[0]);
printf("\t\t\t您的通訊錄中還沒有任何好友,趕快添加吧.此次操作需要重新運行程序。\n");
exit(1);
}
Start=fopen("number.txt","r");
fscanf(Start,"%d",&Number[0]);
number=Number[0];
fclose(Start);
}
void Search()
{
int change=0;
char Searchname[20];
printf("\t\t\t輸入要查找的好友的名字\n\n");
scanf("%s",&Searchname);
fflush(stdin);
printf("\t\t\t您要查找的好友是 %s \n\n",Searchname);
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Searchname)==0)
{
printf("\t\t\t下面是該好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
}
fclose(Search);
}
if(change==0)
{
printf("\t\t\t您要查找的好友不存在,請返回菜單添加。\n");
}
}
void Rework()
{
char Rework[20];
printf("\t\t\t輸入要修改的好友的名字\n\n");
scanf("%s",&Rework);
printf("\t\t\t您要修改 %s 的信息\n\n",Rework);
int Change=0;
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Rework)==0)
{
printf("\t\t\t下面是該好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
Change=1;
int Choose;
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
//---------------------------------------------------------------------
do
{
printf("\t\t\tchoose\t\t\t1-----3");
scanf("%d",&Choose);
}while(Choose>3||Choose<1);
if(Choose==1){
printf("\t\t\t請輸入好友電話號碼\n");
scanf("%s",&xy[i].Tel);
}
if(Choose==2)
{
printf("\t\t\t請輸入好友QQ號碼\n");
scanf("%s",&xy[i].QQ);
}
if(Choose==3)
{
printf("\t\t\t請輸入好友年齡\n");
scanf("%d",&xy[i].age);
}
Search=fopen("Friend.txt","wt");
for(int sta=0;sta<i;sta++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[sta].name,xy[sta].Tel,xy[sta].QQ,xy[sta].age);
}
if(sta==i)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
i++;
}
for(i;i<=number;i++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
printf("\t\t\t修改後的信息為:\n\n");
Read();
}
fclose(Search);
}
if(Change==0)
{
printf("\t\t\t您要查找的好友不存在,請返回菜單添加。\n");
}
}
void Detele()
{
int change=0;
char delete_friend[20];
printf("\t\t\t輸入要刪除的好友的名字\n\n");
scanf("%s",&delete_friend);
fflush(stdin);
printf("\t\t\t您要刪除的好友是 %s \n\n",delete_friend);
FILE *DeleteF;
DeleteF=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(DeleteF,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,delete_friend)==0)
{
printf("\t\t\t下面是該好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","電話 ","QQ","年齡");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
DeleteF=fopen("Friend.txt","wt");
for(int st=0;st<i;st++)
{
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[st].name,xy[st].Tel,xy[st].QQ,xy[st].age);
}
i++;
for(i;i<=number;i++)
{
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
}
//--------------------------------------------------------------------------------
if(change==1)
{
printf("\t\t\t刪除後的通訊錄信息為:\n\n");
number--;
Number[0]=number;
FILE * St;
if((St=fopen("number.txt","r"))=NULL)
{
St=fopen("number.txt","wt");
exit(1);
}
St=fopen("number.txt","wt");
fprintf(St,"%d",Number[0]);
fclose(St);
fclose(DeleteF);
Start();
Read();
}
}
if(change==0)
{
printf("\t\t\t您要刪除的好友不存在。\n");
}
}
void Lock()
{
}
int Menu()
{
printf("\t\t*****歡迎進入通訊管理界面*****\n\n");
printf("\t\t\t1.添加我的好友\n");
printf("\t\t\t2.顯示所有好友\n");
printf("\t\t\t3.查找我的好友\n");
printf("\t\t\t4.修改好友信息\n");
printf("\t\t\t5.刪除我的好友\n");
printf("\t\t\t6.設置背景顏色\n");
printf("\t\t\t7.設置管理密碼\n");
printf("\t\t\t8.使用注意事項\n");
printf("\t\t\t9.退出通訊系統\n");
printf("\t\t\t0.作者想說的話\n");
printf("\t\t******************************\n\n");
do{
printf("\t\t\t請輸入你的選擇:\n\n");
scanf("%d",&GOTO);
}while(GOTO>9||GOTO<1);
return GOTO;
}
int Judge()
{
char JUDGE;
do
{
printf("\t\t\t是否繼續當前操作\n\n Y(yes) / N(no)\n");
printf("\t\t\t輸入你的選擇\n");
JUDGE=getch();
if(JUDGE=='y'||JUDGE=='Y')
{
return 1;
}
if(JUDGE=='n'||JUDGE=='N')
{
return 2;
}
}while(JUDGE!='y'||JUDGE!='n');
}
㈢ 用C語言或C++做個通訊錄管理系統
//簡單的學生通訊錄管理系統
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
structrecord
{
charname[20];//姓名
charphone[12];//電話
charadress[50];//地址
charpostcode[8];//郵政編碼
chare_mail[20];//電子郵件。
}student[100];//假設最大數為100.
//定義全局變數num,表示已經輸入的人數。
intnum;//這里使用數組解決通訊錄的問題,實際上使用鏈表更好。
intmenu_select()
{
chars[80];
inta;/*定義整形變數*/
system("cls");
printf(" ***********歡迎進入通訊管理界面******** ");
printf(" 0.輸入記錄 ");
printf(" 1.顯示記錄 ");
printf(" 2.按姓名查找 ");
printf(" 3.按電話號碼查找 ");
printf(" 4.插入記錄 ");
printf(" 5.按姓名排序 ");
printf(" 6.刪除記錄 ");
printf(" 7.Quit ");
printf(" *********************************************** ");
do{
printf("Enteryouchoice(0~7):");
scanf("%s",s);
a=atoi(s);
}
while(a<0||a>7);
returna;
}
intadser()
{
printf(" ****************請輸入用戶信息**************** ");
printf(" 輸入姓名: ");
scanf("%s",student[num].name);
printf(" 輸入電話號碼: ");
scanf("%s",student[num].phone);
printf(" 輸入地址: ");
scanf("%s",student[num].adress);
printf(" 輸入郵編: ");
scanf("%s",student[num].postcode);
printf(" 輸入e-mail: ");
scanf("%s",student[num].e_mail);
num++;
printf(" 是否繼續添加?(Y/N): ");
if(getch()=='y'||getch()=='Y')
adser();
return(0);
}
voidlist()
{
inti;
system("cls");
if(num!=0)
{
printf(" ***************以下為通訊錄所有信息************ ");
for(i=0;i<num;i++)
{
printf(" 姓名:%s ",student[i].name);
printf(" 電話:%s ",student[i].phone);
printf(" 地址:%s ",student[i].adress);
printf(" 郵編:%s ",student[i].postcode);
printf(" e-mail:%s ",student[i].e_mail);
if(i+1<num)
{
system("pause");
}
}
printf(" ************************************************ ");
}
else
printf(" 通訊錄中無任何紀錄 ");
printf(" 按任意鍵返回主菜單: ");
getch();//這里是無回顯的輸入字元,你輸入的字元不會顯示在屏幕上。
return;
}
intsearchbyname()
{
intmark=0;
inti;
printf(" *****************按姓名查找******************* ");
charname[20];
printf(" 請輸入姓名: ");
scanf("%s",name);
for(i=0;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf(" *************以下是您查找的用戶信息*********** ");
printf(" 姓名:%s",student[i].name);
printf(" 電話:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ************************************************ ");
mark++;
if((i+1)<num)
{
printf(" 是否繼續查找相同名字的用戶信息:(y/n) ");
if(getch()=='y'||getch()=='Y')
{
continue;
}
else
return(0);
}
else
{
printf(" 按任意鍵返回主菜單");
getch();
return(0);
}
}
}
if(mark==0)
{
printf(" 沒有相同姓名的用戶紀錄 ");
printf(" 按任意鍵返回主菜單 ");
getch();
return(0);
}
return0;
}
intsearchbyphone()
{
intmark=0;
inti;
printf(" ******************按電話查找****************** ");
charphone[10];
printf(" 請輸入電話號碼: ");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if(strcmp(student[i].phone,phone)==0)
{
printf(" **************以下是您查找的用戶信息********** ");
printf(" 姓名:%s",student[i].name);
printf(" 電話:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ************************************************ ");
printf(" 按任意鍵返回主菜單 ");
mark++;
getch();
return(0);
}
}
if(mark==0)
{
printf(" 沒有改用戶的信息 ");
printf(" 按任意鍵返回主菜單 ");
getch();
return(0);
}
return(0);
}
voiddeletebyphone()
{
inti,j;
intdeletemark=0;
charphone[20];
printf(" 請輸入要刪除用戶電話號碼: ");
scanf("%s",phone);
if(num==0)
{
printf(" 對不起,文件中無任何紀錄 ");
printf(" 按任意鍵返回主菜單 ");
getch();
return;
}
for(i=0;i<num;i++)
{
if(strcmp(student[i].phone,phone)==0)
{
printf(" 以下是您要刪除的用戶紀錄: ");
printf(" 姓名:%s",student[i].name);
printf(" 電話:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" 是否刪除?(y/n)");
if(getch()=='y'||getch()=='Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf(" 刪除成功");
printf(" 是否繼續刪除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
else
return;
}
continue;
}
if(deletemark==0)
{
printf(" 沒有該用戶的紀錄");
printf(" 是否繼續刪除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
return;
}
voiddeletebyname()
{
inta=0;
intfindmark=0;
intj;
intdeletemark=0;
inti;
charname[20];
printf(" 請輸入要刪除用戶姓名: ");
scanf("%s",name);
for(i=a;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf(" 以下是您要刪除的用戶紀錄:");
findmark++;
printf(" ________________________________");
printf(" 姓名:%s",student[i].name);
printf(" 電話:%s",student[i].phone);
printf(" 地址:%s",student[i].adress);
printf(" e-mail:%s",student[i].e_mail);
printf(" ________________________________");
printf(" 是否刪除?(y/n)");
if(getch()=='y'||getch()=='Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf(" 刪除成功");
if((i+1)<num)
{
printf(" 是否繼續刪除相同姓名的用戶信息?(y/n)");
if(getch()=='y')
{
a=i;
continue;
}
}
printf(" 是否繼續刪除?(y/n)");
if(getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf(" 是否繼續刪除相同姓名的用戶信息?(y/n)");
if(getch()=='y'||getch()=='Y')
{
a=i;
continue;
}
}
}
else
continue;
}
if((deletemark==0)&&(findmark==0))
{
printf(" 沒有該用戶的紀錄");
printf(" 是否繼續刪除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
elseif(findmark!=0)
{
printf(" 沒有重名信息");
printf(" 沒有該用戶的紀錄");
printf(" 是否繼續刪除?(y/n)");
if(getch()=='y'||getch()=='Y')
deletebyphone();
return;
}
}
intdele()
{
charchoic;
printf(" 1-按電話號碼刪除2-按姓名刪除");
printf(" 請選擇:");
choic=getch();
switch(choic)
{
case'1':deletebyphone();break;
case'2':deletebyname();break;
}
return(0);
}
intsortbyname()//按姓名進行排序
{
inti,j;
structrecordtmp;
for(i=1;i<num;i++)
{
if(strcmp(student[i].name,student[i-1].name)<0)
{
tmp=student[i];
j=i-1;
do
{
student[j+1]=student[j];
j--;
}while((strcmp(tmp.name,student[j].name)<0&&j>=0));
student[j+1]=tmp;
}
}
printf(" 排序成功,是否顯示?(y/n)");
if(getch()=='y')
list();
return(0);
}
intmain()
{
printf(" ************************************************ ");
printf(" ********welcometoTONGXUNLU******************* ");
printf(" ###########codebyXXXXX################### ");
printf(" ************************************************* ");
printf("按任意鍵進入主菜單 ");
getch();
intselectnum;
while(1)
{
selectnum=menu_select();
switch(selectnum)
{
case0:
{
adser();
break;
}
case1:
{
list();
break;
}
case2:
{
searchbyname();
break;
}
case3:
{
searchbyphone();
break;
}
case4:
{
adser();//這里插入,應該能指定位置,不過意義不大,所以和添加記錄一樣了。
break;
}
case5:
{
sortbyname();
break;
}
case6:
{
dele();
break;
}
case7:
{
printf("BYEBYE! ");
system("pause");
getchar();
exit(0);
}
}
}
getchar();
return0;
}
㈣ C語言程序設計,通訊錄管理系統
/*【用C語言編寫具有數據插入、修改、刪除、顯示和查詢功能的電話薄管理程序】
1、數據包括:人名、工作單位、電話號碼和E-mail地址。
2、可對記錄中的姓名和電話號碼進行修改。
3、可增加或刪除記錄。
4、可顯示所有保存的記錄。
5、可按人名或電話號碼進行查詢。
另:採用結構體數組和文件系統實現。結構體包括人名、工作單位、電話號碼和E-mail
絕對原創,2010年7月12日晚剛寫完測試通過的,純C語言! */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define szSTR 64
#define fileDB "c:\\contact.txt"
struct CONTACT {
char name[szSTR];/*姓名*/
char unit[szSTR];/*工作單位*/
char tele[szSTR];/*電話*/
char mail[szSTR];/*mail*/
};
typedef struct CONTACT CON;
#define BNAME 1
#define BUNIT 2
#define BTELE 4
#define BMAIL 8
#define BFULL 0xFF
int OPER[5] = {BFULL, BNAME, BUNIT, BTELE, BMAIL};
void input(CON * s, int mask)
{
if(mask & BNAME) { printf("請輸入學生姓名:"); scanf("%s", s->name); }
if(mask & BUNIT) { printf("請輸入工作單位:"); scanf("%s", s->unit); }
if(mask & BTELE) { printf("請輸入聯系電話:"); scanf("%s", s->tele); }
if(mask & BMAIL) { printf("請輸入電子郵件:"); scanf("%s", s->mail); }
}/*end input*/
void show (int id, CON * s)
{
if(!s) return ;
if(id)
printf("%d\t%s\t%s\t%s\t%s\n",
id, s->name , s->unit , s->tele , s->mail );
else
printf("【姓名】%s【工作單位】%s【電話】%s【電郵】%s\n",
s->name , s->unit , s->tele , s->mail );
/*end if*/
}/*end show*/
void regist(void)
{
CON s; FILE * f = 0;
input(&s, BFULL);
f = fopen(fileDB, "ab");
fwrite(&s,sizeof(s),1,f);
fclose(f);
printf("資料已保存!\n");
}/*end regist*/
int browse(char * key, fpos_t * p, CON * s)
{
FILE * f =0; int i = 0, r = 0; CON t;
char * a = 0, * b = 0; fpos_t q;
if(!s) return 0;
f = fopen(fileDB, "rb");
if(!f) {
printf("資料庫暫無記錄\n");
return 0;
}/*end if*/
printf("編號\t姓名\t單位\t電話\t電郵\n");
printf("------------------------------------------------\n");
while(!feof(f)) {
fgetpos(f, &q);
r = fread(&t, sizeof(t), 1, f);
if(r<1)break;
if(t.name[0]==0) continue;
if(key) {
a = strstr(t.name , key);
b = strstr(t.tele , key);
if(a == 0 && b == 0) continue;
}/*end if*/
if(p) *p = q;
if(s) memcpy(s, &t, sizeof(t));
i++;
show(i, &t);
}/*end while*/
fclose(f);
printf("------------------------------------------------\n");
return i;
}/*end browse*/
fpos_t query(CON * s)
{
char key[szSTR]=""; fpos_t p = 0; int r = 0;
printf("請輸入姓名或者電話號碼:");
scanf("%s", key);
r = browse(key, &p, s);
return r?p:-1;
}/*end query*/
void clear(void)
{
FILE * f = 0; CON s;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
memset(&s, 0, sizeof(s));
fwrite(&s, sizeof(s),1, f);
fclose(f);
printf("記錄已刪除!\n");
}/*end clear*/
void modify(void)
{
FILE * f = 0; CON s; int cmd = 0;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
for(;;) {
show(0, &s);
printf("請選擇:1-姓名;2-工作單位;3-電話;4-電郵;5-保存並退出;6-放棄修改並退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 1:
case 2:
case 3:
case 4:
input(&s, OPER[cmd]);
break;
case 5:
fsetpos(f, &p);
fwrite(&s, sizeof(s), 1, f);
case 6:
fclose(f);
return;
default:
printf("錯誤的操作,請重新輸入。\n");
}/*end case*/
}/*mext*/
}/*end clear*/
int main(void)
{
int cmd = 0; CON s;
for(;;) {
printf("歡迎使用通信簿!請選擇:1-添加;2-查詢;3-刪除;4-修改;5-全部輸出;0-退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 0: return 0;
case 1: regist(); break;
case 2: query(&s); break;
case 3: clear(); break;
case 4: modify(); break;
case 5: browse(0, 0, &s); break;
}//end case
}
return 0;
}