電話簿管理系統c語言
1. 用c語言或c++做一個通訊錄管理系統
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
class pbook
{
int cellphone,phone;
char name[20],first,sex[10],workplace[30];
char date[15];
public:
void addin();
char* gname(){return name;}
char getf(){ return first;}
void print()
{
cout<<setw(12)<<name<<setw(12)<<sex;
cout<<setw(12)<<workplace<<setw(12)<<cellphone<<setw(12)<<phone<<setw(12)<<date<<endl;}
static int dele(pbook *book,int n);
static void edit(pbook *book,int n,char *name);
static void findn(pbook *book,int n,char *name);
static void findf(pbook *book,int n,char f);
static void rang(pbook *book,int n);
};
void pbook::addin()
{
cout<<"姓名:";
cin>>name;
cout<<endl<<"性別(男|女): ";
cin>>sex;
cout<<endl<<"工作單位:";
cin>>workplace;
cout<<endl<<"手機號碼:";
cin>>cellphone;
cout<<endl<<"固定電話:";
cin>>phone;
cout<<endl<<"儲存日期:";
cin>>date;
cout<<endl<<"姓名首字母(大寫):";
cin>>first;
cout<<endl<<"電話簿添加成功!"<<endl;
}
int pbook::dele(pbook *book,int n){
int p=-2,c=n;
char name[20];cout<<"請輸入姓名"<<endl;
cin>>name;
for(int i=0;i<n;i++)
{
if(strcmp(book[i].gname(),name)==0)
{
p=i;break;
}
}
if(p!=-2)
{
for(i=p;i<n;i++)
book[i]=book[i+1];
return --c;
}
else
return c;
}
void pbook::edit(pbook *book,int n,char *name)
{ int p;
for(int i=0;i<n;i++)
{if(strcmp(book[i].gname(),name)==0)
p=i;break;
}
book[p].addin();
cout<<"修改成功"<<endl;
}
void pbook::findn(pbook *book,int n,char *sname)
{
int i;
for(i=0;i<n;i++)
if(strcmp(book[i].gname(),sname)==0)
{
book[i].print();break;
}
if(i==n)
{
cout<<"查無此人"<<endl;
}
}
void pbook::findf(pbook *book,int n,char f)
{
int p=-1;
cout<<setw(15)<<"姓名"<<setw(15)<<"性別"<<setw(15)<<"工作單位"<<setw(15)<<"手機號碼";
cout<<setw(15)<<"固定電話"<<setw(15)<<"存儲日期"<<endl;
for(int i=0;i<n;i++)
{
if(book[i].getf()==f)
{
p=i;
book[p].print();
}
}
if(p==-1)cout<<"查無此人"<<endl;
}
void pbook::rang(pbook *book,int n)
{
pbook m;int i,j;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
if(book[i].getf()>book[i+1].getf())
{
m=book[j];
book[j]=book[j+1];
book[j+1]=m;
}
}
cout<<setw(12)<<"姓名"<<setw(12)<<"性別"<<setw(12)<<"工作單位"<<setw(12)<<"手機號碼";
cout<<setw(12)<<"固定電話"<<setw(12)<<"存儲日期"<<endl;
for(i=0;i<n;i++)
book[i].print();
}
void display()
{
cout <<endl;
cout<<"********************電話簿********************"<<endl;
cout<<" ------- 1.添加聯系人 ------- "<<endl;
cout<<" ------- 2.刪除聯系人 ------- "<<endl;
cout<<" ------- 3.顯示所有聯系人 ------- "<<endl;
cout<<" ------- 4.修改聯系人信息 ------- "<<endl;
cout<<" ------- 5.按姓名查詢 ------- "<<endl;
cout<<" ------- 6.按首字母查詢 ------- "<<endl;
cout<<" ------- 7.退出系統請按 ------- "<<endl;
cout<<"**********************************************"<<endl;
}
void main()
{
pbook book[10];int n=0;char name[20];
int i;char j=1;char g;char a;
while(j!=0)
{
display();
cout<<"請選擇菜單號(1---7):";
cin>>i;
while(!(i==1||i==2||i==3||i==4||i==5||i==6||i==7))
{
cout<<"\a\a選擇錯誤! 請重新選擇! 請選擇菜單號(0---7):";
cin>>i;
}
while(i==1||i==2||i==3||i==4||i==5||i==6||i==7)
{
if(i==1)
{ n++;book[n-1].addin();}
else if(i==7)
{
cout<<"謝謝使用本系統!"<<endl;
j=0;
}
else
{
if(i==2)
n=pbook::dele(book,n);
else if(i==3)
pbook::rang(book,n);
else if(i==4)
{ cout<<"請輸入姓名"<<endl;
cin>>name;
pbook::edit(book,n,name);
}
else if(i==5)
{
cout<<"請輸入姓名"<<endl;
cin>>name;
cout<<setw(12)<<"姓名"<<setw(12)<<"性別"<<setw(12)<<"工作單位"<<setw(12)<<"手機號碼";
cout<<setw(12)<<"固定電話"<<setw(12)<<"存儲日期"<<endl;
pbook::findn(book,n,name);
}
else
{
cout<<"請輸入字元:";
cin>>g;
pbook::findf(book,n,g);
}
}
cout<<"繼續請按y,否則請按n."<<endl;
cin>>a;
if(a=='y'){};
if(a=='n')break;
}
cout<<"繼續菜單請按y,否則請按n."<<endl;
cin>>j;
}
}
2. 用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;
}
3. 求用C語言編寫一個學生通訊錄管理系統
/* HELLO.C -- Hello, world */
#include<stdio.h>
struct person
{
char name[8];
char tel[15];
char addr[50];
};
char filename[20]="E:\\txl.txt";
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void delete();
main( )
{
int m;
creat();
while(1)
{
printf("\n\t\t*********歡迎使用通訊錄信息管理系統*********\n\n");
printf("\n\t\t添加,請按1");
printf("\n\t\t查找,請按2");
printf("\n\t\t修改,請按3");
printf("\n\t\t刪除,請按4");
printf("\n\t\t輸出,請按5");
printf("\n\t\t退出,請按0\n");
printf("\n\t\t********************************************\n\n");
printf("Please select(0--5):");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delete();
break;
case 5: output();
break;
case 0: exit();
}
printf("\n\n操作完畢,請再次選擇!");
}
else
printf("\n\n選擇錯誤,請再次選擇!");
}
}
/*輸入模塊creat( ): 創建通訊錄的子函數。*/
void creat()
{
struct person one;
long s1;
/*printf("\n請輸入通訊錄文件名:");
scanf("%s",filename); */
if((fp=fopen(filename,"at+"))==NULL)
{
printf("\n不能建立通訊錄!");
exit();
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","電話號碼","住址");
printf("\n請輸入姓名、電話號碼及住址(以0結束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
/*輸出模塊output( ):輸出通訊錄中聯系人通訊信息的子函數*/
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
printf("\n\n%20s\n","通 訊 錄");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
/*添加模塊append( ):向通訊錄中添加某人通訊信息的子函數*/
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
printf("\n請輸入添加的姓名、電話號碼及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
}
/*查找模塊search( ):在通訊錄中查找某人通訊信息的子函數*/
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n對不起,通訊簿中沒有此人的記錄。");
fclose(fp);
}
/*修改模塊modify( ):在通訊錄中修改某人通訊信息的子函數*/
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,記錄為:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n請輸入新姓名、電話號碼及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
else
printf("\n對不起,通訊錄中沒有此人的記錄。");
fclose(fp);
}
/*刪除模塊delete( ):在通訊錄中刪除某人通訊信息的子函數*/
void delete()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("\n請輸入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打開通訊錄!");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
if(k)
{
printf("\n已查到,記錄為");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n確實要刪除,按1;不刪除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n","","","");
}
else
printf("\n對不起,通訊錄中沒有此人的記錄。");
fclose(fp);
}
}
}
}
<PIXTEL_MMI_EBOOK_2005>1###########################################################</PIXTEL_MMI_EBOOK_2005>
4. 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;
}
5. 求用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;
}
6. C語言課程設計---電話薄管理(C語言高手進)
/*
vc 6.0運行通過-原創
鏈表做的
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define ID struct id
struct id
{
char name[20];
char tele[20];
ID *next;
};
int pc=0;
ID *creat()
{
ID *p1,*p2,*head;
char str[20];
p1=p2=head=NULL;
printf("\t\t\t 開始輸入記錄(姓名 # 結束)!\n");
while(1)
{
printf("請輸入姓名:\n");scanf("%s",str);getchar();
if(strcmp(str,"#")==0) break;
p1=(ID*)malloc(sizeof(ID));
strcpy(p1->name,str);
printf("請輸入電話號碼:\n");scanf("%s",p1->tele);getchar();
if(head==NULL)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
pc++;
}
p2->next=NULL;
return(head);
}
/*輸入/添加記錄*/
ID *insert(ID *head)
{
ID *temp,*p1,*p2;
printf("插入操作開始!!!\n");
temp=(ID *)malloc(sizeof(ID));
printf("請輸入姓名:\n");scanf("%s",temp->name);getchar();
printf("請輸入電話號碼:\n");scanf("%s",temp->tele);getchar();
if (head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
p1=head;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
}
p2->next=temp;
temp->next=p1;
}
printf("插入成功");
pc++;
return (head);
}
/*刪除記錄*/
ID *delet(ID *head)
{
ID *p1,*p2;
char str[20];
printf("請輸入要刪除的電話號碼:");scanf("%s",str);getchar();
p1=head;
if (head==NULL)
{
printf("沒有記錄\n");
goto end;
}
while(p1!=NULL && strcmp(p1->tele,str))
{
p2=p1;p1=p1->next;
}
if(p1==NULL)
printf("未找到符合記錄!\n");
else if(strcmp(p1->tele,str)==0)
{
if (p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("刪除成功!!!\n");
pc--;
}
end:return head;
}
/*查找記錄*/
ID *search(ID *head)
{
ID *p1,*p2;
int flag=0;
char c;
printf("請輸入姓名的首字母:");scanf("%c",&c);getchar();
p1=head;
while( p1!=NULL)
{
if(p1->name[0]==c)
{
printf("姓名:%s\t電話號碼:%s\n",p1->name,p1->tele);
flag=1;
}
p2=p1;p1=p1->next;
}
if(flag==0) printf("未找到符合記錄!\n");
return head;
}
/*修改記錄*/
ID *modify(ID *head)
{
ID *p1,*p2;
int mode;
char str[20];
printf("請輸入要修改記錄的姓名:");scanf("%s",str);getchar();
p1=head;
while( p1!=NULL)
{
if(strcmp(p1->name,str)==0)
{
printf("1.姓名:%s\t2.電話號碼:%s\n",p1->name,p1->tele);
printf("請選擇要修改選項:\n");
scanf("%d",&mode);getchar();
if(mode==1)
{
printf("請輸入修改後的姓名\n");
scanf("%s",p1->name);getchar();
}
else if(mode==2)
{
printf("請輸入修改後的電話號碼\n");
scanf("%s",p1->tele);getchar();
}
else
printf("輸入有誤!\n");
break;
}
p2=p1;p1=p1->next;
}
if(p1==NULL) printf("未找到符合要求的記錄!\n");
return head;
}
/*顯示結果函數*/
void print(ID *head)
{
ID *p;
p=head;
printf("\t\t\t*****************\n");
printf("顯示結果是:\n");
printf("姓名\t電話號碼\n");
if(head!=NULL)
do
{
printf("%s\t%s\n",p->name,p->tele);
p=p->next;
} while(p!=NULL);
}
void main()
{
ID *head=NULL;
int choise;
printf("\t\t\t* * * * C語言課設* * * *\n");
while(1)
{
printf("\t\t 電話簿管理系統\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t 1.輸入\n");
printf("\t\t 2.顯示\n");
printf("\t\t 3.查找\n");
printf("\t\t 4.插入\n");
printf("\t\t 5.刪除\n");
printf("\t\t 0.退出\n");
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("請選擇(0-6):");
scanf("%d",&choise);getchar();
switch(choise)
{
case 1: head=creat();
break;
case 2: print(head);
break;
case 3: head=search(head);
break;
case 4: head=insert(head);
break;
case 5: head=delet(head);
break;
case 0:
exit(0);
break;
default :printf("輸入錯誤,請重新輸入!\n");
}
}
}