當前位置:首頁 » 編程語言 » 通訊錄C語言程序設計

通訊錄C語言程序設計

發布時間: 2022-05-14 20:41:35

A. c語言程序設計——編一個通訊錄管理程序:序號 姓名 電話 地址

簡單通訊錄管理程序,代碼如下:#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
typedef
struct
student
*ST;
struct
student
{
char
stN[20];
char
stVal[128];
char
stNo[10];
char
stTel[12];
ST
next;
};
ST
head
=
NULL;
int
ShowNode()
{
char
no[20]
=
{0};
ST
t;
printf("輸入姓名:");
scanf("%s",
no);
for
(t
=
head;
t;
t
=
t->next)
{
if
(!memcmp(t->stN,
no,
strlen(no)))
{
printf("姓名:%s,
地址:%s
郵編:%s
電話:%s\n",
t->stN,
t->stVal,
t->stNo,
t->stTel);
return
0;
}
}
printf("無此人信息\n");
return
0;
}
int
ChangeNode()
{
char
no[20];
ST
t;
printf("輸入姓名:");
scanf("%s",
no);
for
(t
=
head;
t;
t
=
t->next)
{
if
(!memcmp(t->stN,
no,
strlen(no)))
{
printf("輸入新的記錄:\n");
printf("地址:");
scanf("%s",
t->stVal);
printf("郵編:");
scanf("%s",
t->stNo);
printf("英語成績:");
scanf("%s",
t->stTel);
return
0;
}
}
printf("無此人信息\n");
return
0;
}
int
InsertNode()
{
ST
t;
t
=
(ST)malloc(sizeof
*t);
printf("輸入姓名:");
scanf("%s",
t->stN);
printf("輸入地址:");
scanf("%s",
t->stVal);
printf("輸入郵編:");
scanf("%s",
t->stNo);
printf("輸入電話:");
scanf("%s",
t->stTel);
t->next
=
head;
head
=
t;
return
0;
}
int
DelNode()
{
ST
x,
y;
char
no[20]
=
{0};
printf("輸入姓名:");
scanf("%s",
no);
for
(x
=
y
=
head;
x;
y
=
x,
x
=
x->next)
{
if
(!memcmp(x->stN,
no,
strlen(no)))
{
if
(x
==y)
{
x
=
head
=
head->next;
y->next
=
NULL;
free(y);
return
0;
}
else
{
y->next
=
x->next;
x->next
=
NULL;
free(x);
return
0;
}
}
}
printf("無此學生\n");
return
0;
}
int
main(void)
{
int
i;
while(1)
{
printf("查詢記錄:1\n"
"更改記錄:2\n"
"插入記錄:3\n"
"刪除記錄:4\n"
"退出:5\n");
scanf("%d",
&i);
if
(1==i)
{
ShowNode();
}
else
if(2==i)
{
ChangeNode();
}
else
if(3==i)
{
InsertNode();
}
else
if(4==i)
{
DelNode();
}
else
if(5==i)
{
break;
}
else
{
printf("輸入錯誤!請重新輸入!\n");
continue;
}
}
return
0;
}

B. C語言程序設計 建立通信錄

#include "stdio.h" /*I/O函數*/
#include "stdlib.h" /*其它說明*/
#include "string.h" /*字元串函數*/
#include "conio.h" /*屏幕操作函數*/
#include "mem.h" /*內存操作函數*/
#include "ctype.h" /*字元操作函數*/
#include "alloc.h" /*動態地址分配函數*/
#define N 3 /*定義常數*/
typedef struct z1 /*定義數據結構*/
{
char no[11];
char name[15];
int score[N];
float sum;
float average;
int order;
struct z1 *next;
}STUDENT;
/*以下是函數原型*/
STUDENT *init(); /*初始化函數*/
STUDENT *create(); /*創建鏈表*/
STUDENT *delete(STUDENT *h); /*刪除記錄*/
void print(STUDENT *h); /* 顯示所有記錄*/
void search(STUDENT *h); /*查找*/
void save(STUDENT *h); /*保存*/
STUDENT *load(); /*讀入記錄*/
void computer(STUDENT *h); /*計算總分和均分*/
STUDENT *insert(STUDENT *h); /*插入記錄*/
void append(); /*追加記錄*/
void (); /*復制文件*/
STUDENT *sort(STUDENT *h); /*排序*/
STUDENT *index(STUDENT *h); /*索引*/
void total(STUDENT *h); /*分類合計*/
int menu_select(); /*菜單函數*/
/******主函數開始*******/
main()
{
int i;
STUDENT *head; /*鏈表定義頭指針*/
head=init(); /*初始化鏈表*/
clrscr(); /*清屏*/
for(;;) /*無限循環*/
{
switch(menu_select()) /*調用主菜單函數,返回值整數作開關語句的條件*/
{ /*值不同,執行的函數不同,break 不能省略*/
case 0:head=init();break; /*執行初始化*/
case 1:head=create();break; /*創建鏈表*/
case 2:head=delete(head);break; /*刪除記錄*/
case 3:print(head);break; /*顯示全部記錄*/
case 4:search(head);break; /*查找記錄*/
case 5:save(head);break; /*保存文件*/
case 6:head=load(); break; /*讀文件*/
case 7:computer(head);break; /*計算總分和均分*/
case 8:head=insert(head); break; /*插入記錄*/
case 9:();break; /*復制文件*/
case 10:head=sort(head);break; /*排序*/
case 11:append();break; /*追加記錄*/
case 12:head=index(head);break; /*索引*/
case 13:total(head);break; /*分類合計*/
case 14:exit(0); /*如菜單返回值為14程序結束*/
}
}
}
/*菜單函數,返回值為整數*/
menu_select()
{
char *menu[]={"***************MENU***************", /*定義菜單字元串數組*/
" 0. init list", /*初始化*/
" 1. Enter list", /*輸入記錄*/
" 2. Delete a record from list", /*從表中刪除記錄*/
" 3. print list ", /*顯示單鏈表中所有記錄*/
" 4. Search record on name", /*按照姓名查找記錄*/
" 5. Save the file", /*將單鏈表中記錄保存到文件中*/
" 6. Load the file", /*從文件中讀入記錄*/
" 7. compute the score", /*計算所有學生的總分和均分*/
" 8. insert record to list ", /*插入記錄到表中*/
" 9. the file to new file", /*復制文件*/
" 10. sort to make new file", /*排序*/
" 11. append record to file", /*追加記錄到文件中*/
" 12. index on nomber", /*索引*/
" 13. total on nomber", /*分類合計*/
" 14. Quit"}; /*退出*/
char s[3]; /*以字元形式保存選擇號*/
int c,i; /*定義整形變數*/
gotoxy(1,25); /*移動游標*/
printf("press any key enter menu......\n"); /*壓任一鍵進入主菜單*/
getch(); /*輸入任一鍵*/
clrscr(); /*清屏幕*/
gotoxy(1,1); /*移動游標*/
textcolor(YELLOW); /*設置文本顯示顏色為黃色*/
textbackground(BLUE); /*設置背景顏色為藍色*/
gotoxy(10,2); /*移動游標*/
putch(0xc9); /*輸出左上角邊框┏*/
for(i=1;i<44;i++)
putch(0xcd); /*輸出上邊框水平線*/
putch(0xbb); /*輸出右上角邊框 ┓*/
for(i=3;i<20;i++)
{
gotoxy(10,i);putch(0xba); /*輸出左垂直線*/
gotoxy(54,i);putch(0xba);
} /*輸出右垂直線*/
gotoxy(10,20);putch(0xc8); /*輸出左上角邊框┗*/
for(i=1;i<44;i++)
putch(0xcd); /*輸出下邊框水平線*/
putch(0xbc); /*輸出右下角邊框┛*/
window(11,3,53,19); /* 製作顯示菜單的窗口,大小根據菜單條數設計*/
clrscr(); /*清屏*/
for(i=0;i<16;i++) /*輸出主菜單數組*/
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK); /*設置背景顏色為黑色*/
window(1,1,80,25); /*恢復原窗口大小*/
gotoxy(10,21); /*移動游標*/
do{
printf("\n Enter you choice(0~14):"); /*在菜單窗口外顯示提示信息*/
scanf("%s",s); /*輸入選擇項*/
c=atoi(s); /*將輸入的字元串轉化為整形數*/
}while(c<0||c>14); /*選擇項不在0~14之間重輸*/
return c; /*返回選擇項,主程序根據該數調用相應的函數*/
}
STUDENT *init()
{
return NULL;
}

/*創建鏈表*/
STUDENT *create()
{
int i; int s;
STUDENT *h=NULL,*info; /* STUDENT指向結構體的指針*/
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info) /*如果指針info為空*/
{
printf("\nout of memory"); /*輸出內存溢出*/
return NULL; /*返回空指針*/
}
inputs("enter no:",info->no,11); /*輸入學號並校驗*/
if(info->no[0]=='@') break; /*如果學號首字元為@則結束輸入*/
inputs("enter name:",info->name,15); /*輸入姓名,並進行校驗*/
printf("please input %d score \n",N); /*提示開始輸入成績*/
s=0; /*計算每個學生的總分,初值為0*/
for(i=0;i<N;i++) /*N門課程循環N次*/
{
do{
printf("score%d:",i+1); /*提示輸入第幾門課程*/
scanf("%d",&info->score[i]); /*輸入成績*/
if(info->score[i]>100||info->score[i]<0) /*確保成績在0~100之間*/
printf("bad data,repeat input\n"); /*出錯提示信息*/
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i]; /*累加各門課程成績*/
}
info->sum=s; /*將總分保存*/
info->average=(float)s/N; /*求出平均值*/
info->order=0; /*未排序前此值為0*/
info->next=h; /*將頭結點做為新輸入結點的後繼結點*/
h=info; /*新輸入結點為新的頭結點*/
}
return(h); /*返回頭指針*/
}
/*輸入字元串,並進行長度驗證*/
inputs(char *prompt, char *s, int count)
{
char p[255];
do{
printf(prompt); /*允鞠提示信息*/
scanf("%s",p); /*輸入字元串*/
if(strlen(p)>count)printf("\n too long! \n"); /*進行長度校驗,超過count值重輸入*/
}while(strlen(p)>count);
strcpy(s,p); /*將輸入的字元串拷貝到字元串s中*/
}
/*輸出鏈表中結點信息*/
void print(STUDENT *h)
{
int i=0; /* 統計記錄條數*/
STUDENT *p; /*移動指針*/
clrscr(); /*清屏*/
p=h; /*初值為頭指針*/
printf("\n\n\n****************************STUDENT********************************\n");
printf("|rec|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|---|----------|---------------|----|----|----|--------|-------|-----|\n");
while(p!=NULL)
{
i++;
printf("|%3d |%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", i, p->no,p->name,p->score[0],p->score[1],
p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("**********************************end*********************************\n");
}

/*刪除記錄*/
STUDENT *delete(STUDENT *h)
{
STUDENT *p,*q; /*p為查找到要刪除的結點指針,q為其前驅指針*/
char s[11]; /*存放學號*/
clrscr(); /*清屏*/
printf("please deleted no\n"); /*顯示提示信息*/
scanf("%s",s); /*輸入要刪除記錄的學號*/
q=p=h; /*給q和p賦初值頭指針*/
while(strcmp(p->no,s)&&p!=NULL) /*當記錄的學號不是要找的,或指針不為空時*/
{
q=p; /*將p指針值賦給q作為p的前驅指針*/
p=p->next; /*將p指針指向下一條記錄*/
}
if(p==NULL) /*如果p為空,說明鏈表中沒有該結點*/
printf("\nlist no %s student\n",s);
else /*p不為空,顯示找到的記錄信息*/
{
printf("*****************************have found***************************\n");
printf("|no | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,
p->average,p->order);
printf("********************************end*******************************\n");
getch(); /*壓任一鍵後,開始刪除*/
if(p==h) /*如果p==h,說明被刪結點是頭結點*/
h=p->next; /*修改頭指針指向下一條記錄*/
else
q->next=p->next; /*不是頭指針,將p的後繼結點作為q的後繼結點*/
free(p); /*釋放p所指結點空間*/
printf("\n have deleted No %s student\n",s);
printf("Don't forget save\n");/*提示刪除後不要忘記保存文件*/
}
return(h); /*返回頭指針*/
}
/*查找記錄*/
void search(STUDENT/*當記錄的姓名不是要找的,或指針不為空時*/
{ p=p->next; /*移動指針,指向下一結點*/
if(p==NULL) /*如果指針為空*/
printf("\nlist no %s student\n",s); /*顯示沒有該學生*/
else /*顯示找到的記錄信息*/
{
printf (*h)
{
STUDENT *p; /* 移動指針*/
char s[15]; /*存放姓名的字元數組*/
clrscr(); /*清屏幕*/
printf("please enter name for search\n");
scanf("%s",s); /*輸入姓名*/
p=h; /*將頭指針賦給p*/
while(strcmp(p->name,s)&&p!=NULL) ("\n\n*****************************havefound***************************\n");
printf("|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("********************************end*******************************\n");
}
}
}
/*插入記錄*/
STUDENT *insert(STUDENT *h)
{
STUDENT *p,*q,*info; /*p指向插入位置,q是其前驅,info指新插入記錄*/
char s[11]; /*保存插入點位置的學號*/
int s1,i;
printf("please enter location before the no\n");
scanf("%s",s); /*輸入插入點學號*/
printf("\nplease new record\n"); /*提示輸入記錄信息*/
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info)
{
printf("\nout of memory"); /*如沒有申請到,內存溢出*/
return NULL; /*返回空指針*/
}
inputs("enter no:",info->no,11); /*輸入學號*/
inputs("enter name:",info->name,15); /*輸入姓名*/
printf("please input %d score \n",N); /*提示輸入分數*/
s1=0; /*保存新記錄的總分,初值為0*/
for(i=0;i<N;i++) /*N門課程循環N次輸入成績*/
{
do{ /*對數據進行驗證,保證在0~100之間*/

printf("score%d:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)
printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0);
s1=s1+info->score[i]; /*計算總分*/
}
info->sum=s1; /*將總分存入新記錄中*/
info->average=(float)s1/N; /*計算均分*/
info->order=0; /*名次賦值0*/
info->next=NULL; /*設後繼指針為空*/
p=h; /*將指針賦值給p*/
q=h; /*將指針賦值給q*/
while(strcmp(p->no,s)&&p!=NULL) /*查找插入位置*/
{
q=p; /*保存指針p,作為下一個p的前驅*/
p=p->next; /*將指針p後移*/
}
if(p==NULL) /*如果p指針為空,說明沒有指定結點*/
if(p==h) /*同時p等於h,說明鏈表為空*/
h=info; /*新記錄則為頭結點*/
else
q->next=info; /*p為空,但p不等於h,將新結點插在表尾*/
else
if(p==h) /*p不為空,則找到了指定結點*/
{
info->next=p; /*如果p等於h,則新結點插入在第一個結點之前*/
h=info; /*新結點為新的頭結點*/
}
else
{
info->next=p; /*不是頭結點,則是中間某個位置,新結點的後繼為p*/
q->next=info; /*新結點作為q的後繼結點*/
}
printf("\n ----have inserted %s student----\n",info->name); printf("---Don't forget save---\n"); /*提示存檔*/
return(h); /*返回頭指針*/
}
/*保存數據到文件*/
void save(STUDENT *h)
{
FILE *fp; /*定義指向文件的指針*/
STUDENT *p; /* 定義移動指針*/
char outfile[10]; /*保存輸出文件名*/
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示文件名格式信息*/
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*為輸出打開一個二進制文件,如沒有則建立*/
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file......\n"); /*打開文件,提示正在保存*/
p=h; /*移動指針從頭指針開始*/
while(p!=NULL) /*如p不為空*/
{
fwrite(p,sizeof(STUDENT),1,fp);/*寫入一條記錄*/
p=p->next; /*指針後移*/
}
fclose(fp); /*關閉文件*/
printf("-----save success!!-----\n"); /*顯示保存成功*/
}
/* 從文件讀數據*/
STUDENT *load()
{
STUDENT *p,*q,*h=NULL; /*定義記錄指針變數*/
FILE *fp; /* 定義指向文件的指針*/
char infile[10]; /*保存文件名*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*輸入文件名*/
if((fp=fopen(infile,"rb"))==NULL) /*打開一個二進制文件,為讀方式*/
{
printf("can not open file\n"); /*如不能打開,則結束程序*/
exit(1);
}
printf("\n -----Loading file!-----\n");
p=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!p)
{
printf("out of memory!\n"); /*如沒有申請到,則內存溢出*/
return h; /*返回空頭指針*/
}
h=p; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環讀數據直到文件尾結束*/
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break; /*如果沒讀到數據,跳出循環*/
p->next=(STUDENT *)malloc(sizeof(STUDENT)); /*為下一個結點申請空間*/
if(!p->next)
{
printf("out of memory!\n"); /*如沒有申請到,則內存溢出*/
return h;
}
q=p; /*保存當前結點的指針,作為下一結點的前驅*/
p=p->next; /*指針後移,新讀入數據鏈到當前表尾*/
}
q->next=NULL; /*最後一個結點的後繼指針為空*/
fclose(fp); /*關閉文件*/
printf("---You have success read data from file!!!---\n");
return h; /*返回頭指針*/
}
/*追加記錄到文件*/
void append()

{
FILE *fp; /*定義指向文件的指針*/
STUDENT *info; /*新記錄指針*/
int s1,i;
char infile[10]; /*保存文件名*/
printf("\nplease new record\n");
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info)
{
printf("\nout of memory"); /*沒有申請到,內存溢出本函數結束*/
return ;
}
inputs("enter no:",info->no,11); /*調用inputs輸入學號*/
inputs("enter name:",info->name,15); /*調用inputs輸入姓名*/
printf("please input %d score \n",N); /*提示輸入成績*/
s1=0;
for(i=0;i<N;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&info->score[i]); /*輸入成績*/
if(info->score[i]>100||info->score[i]<0)printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0); /*成績數據驗證*/
s1=s1+info->score[i]; /*求總分*/
}
info->sum=s1; /*保存總分*/
info->average=(float)s1/N; /*求均分*/
info->order=0; /*名次初始值為0*/
info->next=NULL; /*將新記錄後繼指針賦值為空*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*輸入文件名*/
if((fp=fopen(infile,"ab"))==NULL) /*向二進制文件尾增加數據方式打開文件*/
{
printf("can not open file\n"); /*顯示不能打開*/
exit(1); /*退出程序*/
}
printf("\n -----Appending record!-----\n");
if(1!=fwrite(info,sizeof(STUDENT),1,fp)) /*寫文件操作*/
{
printf("-----file write error!-----\n");
return; /*返回*/
}
printf("-----append sucess!!----\n");
fclose(fp); /*關閉文件*/
}
/*文件拷貝*/
void ()
{
char outfile[10],infile[10];
FILE *sfp,*tfp; /*源和目標文件指針*/
STUDENT *p=NULL; /*移動指針*/
clrscr(); /*清屏*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n");
scanf("%s",infile); /*輸入源文件名*/
if((sfp=fopen(infile,"rb"))==NULL) /*二進制讀方式打開源文件*/
{
printf("can not open input file\n");
exit(0);
}
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示輸入目標文件名*/
scanf("%s",outfile); /*輸入目標文件名*/
if((tfp=fopen(outfile,"wb"))==NULL) /*二進制寫方式打開目標文件*/
{
printf("can not open output file \n");
exit(0);
}
while(!feof(sfp)) /*讀文件直到文件尾*/
{
if(1!=fread(p,sizeof(STUDENT),1,sfp))
break; /*塊讀*/
fwrite(p,sizeof(STUDENT),1,tfp); /*塊寫*/
}
fclose(sfp); /*關閉源文件*/
fclose(tfp); /*關閉目標文件*/
printf("you have success file!!!\n"); /*顯示成功拷貝*/
}
/*排序*/
STUDENT *sort(STUDENT *h)
{
int i=0; /*保存名次*/
STUDENT *p,*q,*t,*h1; /*定義臨時指針*/
h1=h->next; /*將原表的頭指針所指的下一個結點作頭指針*/
h->next=NULL; /*第一個結點為新表的頭結點*/
while(h1!=NULL) /*當原表不為空時,進行排序*/
{
t=h1; /*取原表的頭結點*/
h1=h1->next; /*原表頭結點指針後移*/
p=h; /*設定移動指針p,從頭指針開始*/
q=h; /*設定移動指針q做為p的前驅,初值為頭指針*/
while(t->sum<p->sum&&p!=NULL) /*作總分比較*/
{
q=p; /*待排序點值小,則新表指針後移*/
p=p->next;
}
if(p==q) /*p==q,說明待排序點值大,應排在首位*/
{
t->next=p; /*待排序點的後繼為p*/
h=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置q和p之間,如p為空則是尾部*/
{
t->next=p; /*t的後繼是p*/
q->next=t; /*q的後繼是t*/

}
}
p=h; /*已排好序的頭指針賦給p,准備填寫名次*/
while(p!=NULL) /*當p不為空時,進行下列操作*/
{
i++; /*結點序號*/
p->order=i; /*將名次賦值*/
p=p->next; /*指針後移*/
}
printf("sort sucess!!!\n"); /*排序成功*/
return h; /*返回頭指針*/
}
/*計算總分和均值*/
void computer(STUDENT *h)
{
STUDENT *p; /*定義移動指針*/
int i=0; /*保存記錄條數初值為0*/
long s=0; /*總分初值為0*/
float average=0; /*均分初值為0*/
p=h; /*從頭指針開始*/
while(p!=NULL) /*當p不為空時處理*/
{
s+=p->sum; /*累加總分*/
i++; /*統計記錄條數*/
p=p->next; /*指針後移*/
}
average=(float)s/i;/* 求均分,均分為浮點數,總分為整數,所以做類型轉換*/
printf("\n--All students sum score is:%ld average is %5.2f\n",s,average);
}
/*索引*/
STUDENT *index(STUDENT *h)
{
STUDENT *p,*q,*t,*h1; /*定義臨時指針*/
h1=h->next; /*將原表的頭指針所指的下一個結點作頭指針*/
h->next=NULL; /*第一個結點為新表的頭結點*/
while(h1!=NULL) /*當原表不為空時,進行排序*/
{
t=h1; /*取原表的頭結點*/
h1=h1->next; /*原表頭結點指針後移*/
p=h; /*設定移動指針p,從頭指針開始*/
q=h; /*設定移動指針q做為p的前驅,初值為頭指針*/
while(strcmp(t->no,p->no)>0&&p!=NULL) /*作學號比較*/
{
q=p; /*待排序點值大,應往後插,所以新表指針後移*/
p=p->next;
}
if(p==q) /*p==q,說明待排序點值小,應排在首位*/
{
t->next=p; /*待排序點的後繼為p*/
h=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置q和p之間,如p為空則是尾部*/
{
t->next=p; /*t的後繼是p*/
q->next=t; /*q的後繼是t*/
}
}
printf("index sucess!!!\n"); /*索引排序成功*/
return h; /*返回頭指針*/
}
/*分類合計*/
void total(STUDENT *h)
{
STUDENT *p,*q; /*定義臨時指針變數*/
char sno[9],qno[9],*ptr; /*保存班級號的*/
float s1,ave; /*保存總分和均分*/
int i; /*保存班級人數*/
clrscr(); /*清屏*/
printf("\n\n *******************Total*****************\n");
printf("---class---------sum--------------average----\n");
p=h; /*從頭指針開始*/
while(p!=NULL) /*當p不為空時做下面的處理*/
{
memcpy(sno,p->no,8); /*從學號中取出班級號*/
sno[8]='\0'; /*做字元串結束標記*/
q=p->next; /*將指針指向待比較的記錄*/
s1=p->sum; /*當前班級的總分初值為該班級的第一條記錄總分*/
ave=p->average; /*當前班級的均分初值為該班級的第一條記錄均分*/
i=1; /*統計當前班級人數*/
while(q!=NULL) /*內循環開始*/
{
memcpy(qno,q->no,8); /*讀取班級號*/
qno[8]='\0'; /*做字元串結束標記*/
if(strcmp(qno,sno)==0) /*比較班級號*/
{
s1+=q->sum; /*累加總分*/
ave+=q->average; /*累加均分*/
i++; /*累加班級人數*/
q=q->next; /*指針指向下一條記錄*/
}
else
break; /*不是一個班級的結束本次內循環*/
}
printf("%s %10.2f %5.2f\n",sno,s1,ave/i);
if(q==NULL)
break; /*如果當前指針為空,外循環結束,程序結束*/
else
p=q; /*否則,將當前記錄作為新的班級的第一條記錄開始新的比較*/
}
printf("---------------------------------------------\n");
}

C. 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;
}

D. 急求c語言 個人通訊錄 程序設計

給題主一個簡單的通訊錄程序吧

#include<stdio.h>
#include<string.h>

#defineARRAY_LEN30

/*聯系人結構體*/
/*註:本程序忽略重名現象。若重名則查找時以最後輸入的聯系人為准。*/
typedefstruct{
charname[10];
intage;
charsex[10];
charmp[13];
chartel[13];
}friend_list;

/*列印所有聯系人信息*/
voidprintAll(friend_listfriList[],int*friNum){
inti;

if(*friNum){
printf("姓名 年齡 性別 手機 家庭 ");
for(i=0;i<*friNum;i++)
printf("%s %d %s %s %s ",friList[i].name,friList[i].age,friList[i].sex,friList[i].mp,friList[i].tel);
printf(" ");
}
else{
printf("無任何聯系人信息! ");
printf(" ");
}

}

/*列印聯系人信息*/
voidprintFri(friend_listfriList[],intindex){

if(index==-1){
printf("查無此人! ");
printf(" ");
}
else{
printf("姓名 年齡 性別 手機 家庭 ");
printf("%s %d %s %s %s ",friList[index].name,friList[index].age,friList[index].sex,friList[index].mp,friList[index].tel);
printf(" ");
}
}

/*按姓名定位聯系人所在數組位置*/
intfindIndex(friend_listfriList[],int*friNum,char*tarName){
inti,index;
index=-1;

for(i=0;i<*friNum;i++)
if(strcmp(tarName,friList[i].name)==0)/*兩字元串相等strcmp函數返回0*/
index=i;/*定位聯系人在結構體數組中的下標*/

returnindex;
}

/*錄入聯系人信息*/
voidaddFri(friend_listfriList[],int*friNum){
inti=*friNum;

printf("請輸入聯系人信息: ");
printf("姓名:");
scanf("%s",&friList[i].name);
printf("年齡:");
scanf("%d",&friList[i].age);
printf("性別:");
scanf("%s",&friList[i].sex);
printf("手機:");
scanf("%s",&friList[i].mp);
printf("家庭:");
scanf("%s",&friList[i].tel);

*friNum+=1;/*聯系人數加1*/

printf(" ");
}

/*按姓名查找聯系人信息*/
voidfindFri(friend_listfriList[],int*friNum){
inti,index;
chartarName[10];

printf("請輸入欲查找的聯系人姓名:");
scanf("%s",&tarName);
printf(" ");

index=findIndex(friList,friNum,tarName);
printFri(friList,index);/*列印聯系人信息*/
}

/*刪除聯系人信息*/
voiddelFri(friend_listfriList[],int*friNum){
inti,index;
chartarName[10];

printf("請輸入欲刪除的聯系人姓名:");
scanf("%s",&tarName);
printf(" ");

index=findIndex(friList,friNum,tarName);

if(index==-1){
printf("查無此人! ");
printf(" ");
}
else{
for(i=index;i<*friNum;i++)
friList[i]=friList[i+1];
*friNum-=1;
printf("聯系人%s已刪除! ",tarName);
printf(" ");
}
}

/*修改聯系人信息*/
voidaltFri(friend_listfriList[],int*friNum){
intindex;
chartarName[10];

printf("請輸入欲修改的聯系人姓名:");
scanf("%s",&tarName);
printf(" ");

index=findIndex(friList,friNum,tarName);

if(index==-1){
printf("查無此人! ");
printf(" ");
}
else{
printf("請重新輸入聯系人信息: ");
printf("姓名:%s ",friList[index].name);
printf("年齡:");
scanf("%d",&friList[index].age);
printf("性別:");
scanf("%s",&friList[index].sex);
printf("手機:");
scanf("%s",&friList[index].mp);
printf("家庭:");
scanf("%s",&friList[index].tel);

printf("聯系人%s信息已修改! ",tarName);
printf(" ");
}
}

/*列印菜單*/
voidprintMenu(void){
printf("====================================================== ");
printf("序號 功能詳情 ");
printf("1 新增聯系人 ");
printf("2 按姓名查找聯系人 ");
printf("3 按姓名刪除聯系人 ");
printf("4 按姓名修改聯系人 ");
printf("5 列出所有聯系人信息 ");
printf("6 退出 ");
printf(" ");
printf("====================================================== ");
}

/*輸入命令*/
intinstructions(void){
intkey;

printf("請輸入功能序號以開啟操作:");
scanf("%d",&key);
printf(" ");
printf("====================================================== ");

returnkey;
}

intmain(void){
intkey;

intfriNum=0;/*聯系人數量*/
friend_listfriList[ARRAY_LEN];/*聯系人結構體數組*/

do{
printMenu();

key=instructions();

switch(key){
case1:addFri(friList,&friNum);break;
case2:findFri(friList,&friNum);break;
case3:delFri(friList,&friNum);break;
case4:altFri(friList,&friNum);break;
case5:printAll(friList,&friNum);break;
case6:printf("程序結束! ");break;
default:printf("輸入錯誤,請重新輸入! ");break;
}
if(key!=6){
printf("按回車鍵繼續 ");getchar();getchar();
}
}while(key!=6);
return0;
}

運行結果

主界面

E. c語言通訊錄程序

以前寫了一個簡單的:
#include <stdio.h>
#include <stdlib.h> /*與malloc.h差不多*/
#include <string.h>
#include <iostream>
using namespace std;
#define maxlen 15
struct persons
{int num; /*定義結構體數組用於緩存數據*/
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
}persons[maxlen];

typedef struct lnode
{ /*通訊錄結構中結點的定義*/
int num;
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
struct lnode *next;
}listnode,*linklist;

linklist head=NULL,r=NULL; /*定義頭指針和尾指針*/
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int i;
char name1[10],ch;
char tel_no1[15];
char arch1;
char sim_no1;
char e_addr1[20];
char s1[20];
FILE *fp; /*定義文件指針*/

void creat() /*將文件的信息讀入結構體數組在轉存入鏈表中*/
{ int j;
long k;
fp=fopen("數據文件.txt","r t"); /*打開文件*/
if(fp!=NULL)
{for(i=0;i<=maxlen;i++ )
{ j=fgetc(fp);
if(j==EOF)
return;
k=i;
fseek(fp,k*sizeof(struct persons),0); /*讀取一個人的信息*/
fread(&persons[i],sizeof(struct persons),1,fp);

s=(linklist)malloc(sizeof(listnode)); /*裝存入鏈表中*/
s->num=persons[i].num;
strcpy(s->name,persons[i].name);
strcpy(s->e_addr,persons[i].e_addr);
strcpy(s->tel_no,persons[i].tel_no);
s->sim_no=persons[i].sim_no;
s->arch=persons[i].arch;
if(head==NULL) /*用尾插法將其插入鏈表中*/
{head=s;r=head;head->next=NULL;}
else
{r->next=s;
r=s;r->next=NULL;
}
}fclose(fp);
}
else
{ fp=fopen("數據文件.txt","w"); /*不能打開另開辟一個文件*/
i=1;
}
}

void Show()
{printf("成功調用該函數\n");}
void Delete()
{printf("成功調用該函數\n");}

void Input() /*向.通訊錄中輸入一個人的信息*/
{ s=(linklist)malloc(sizeof(listnode));
printf("\n\n\t請輸入該用戶的信息:");
printf("姓名:");
scanf("%s",&s->name);
printf("電話號碼:");
scanf("%s",&s->tel_no);
printf("單鍵撥號:");
scanf("%s",&s->sim_no);
printf("E-mail地址:");
scanf("%s",&s->e_addr);
printf("類別:");
scanf("%s",&s->arch);

if(head==NULL)printf("\n\n");
else
{p8=head;
while(p8!=NULL&&strcmp(s->name,p8->name)!=0&&strcmp(s->tel_no,p8->tel_no)!=0)
p8=p8->next;
if(p8!=NULL)
{printf("您添加的用戶已存在!");
free(s);}}
if(head==NULL)
{
s->next = 0;
head=s;
r = s;
}
else
{
s->next = 0;
r->next = s;
r = s;

}
}
void Alter()
{ printf("success!\n");}
int main()
{
system("color a");
creat();
do
{
printf("\n\n\t\t請選擇操作:");
printf("\n\t\t1.顯示通訊錄");
printf("\n\t\t2.刪除通訊錄");
printf("\n\t\t3.添加通訊錄");
printf("\n\t\t4.編輯通訊錄");
printf("\n\n\n");
printf("\t請選擇:");
cin>>ch;
switch(ch)
{ case '1': Show(); /*用單條件多選擇語句實現調用與循環*/
break;
case '2': Delete();
break;
case '3': Input();
break;
case '4': Alter();
break;
fclose(fp);
exit(0);
break;
default:
printf("\n\t The num should 1-6!!! \n");
break;
}
}
while(1);
}

F. C語言程序設計:設計一個程序來創建一個通訊錄

如果你平時有許多同學和朋友的信息,試試在電腦上編寫一個我這樣的通訊錄。很方便的喲。 先新建一個工程,在「工具箱->右鍵->部件」添加「Microsoft windows common control 6.0」,然後在窗體上添加五個標簽,「標題」分別為 「姓名、電話、Oicq、E-Mail、地址」;添加一個文本框(text1), 然後[復制/粘貼4次]使其成為控制項數組;添加一個Listview控制項(Listview1),在其屬性框中,添加五個「列首」,「文本」值分別為「姓名、電話、Oicq、E-Mail、地址」,「查看屬性」為 「3-lvwReport」, 選中「整行選擇」,取消「隱藏選擇」;添加兩個按鈕,其標題屬性分別為「添加」、「刪除」。添加如下代碼:
Option Explicit
Dim fileName As String
Private Sub Command1_Click()
'將文本框中的信息添加到列表框中
Dim i As Integer
With ListView1.ListItems.Add()
.Text = Text1(0)
For i = 1 To 4
.SubItems(i) = Text1(i)
Next i
End With
End Sub

Private Sub Command2_Click()
'刪除列表框中信息
If ListView1.ListItems.Count > 0 Then
If MsgBox("真的要刪除嗎?", vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
ListView1.ListItems.Remove ListView1.SelectedItem.Index
End If
End If
End Sub

Private Sub Form_Load()
Dim tpStr As String, i As Integer
fileName = App.Path & "\data.txt"
If Dir(fileName) <> "" Then
'載入數據
Open fileName For Input As #1
Do While Not EOF(1)
With ListView1.ListItems.Add()
For i = 0 To 4
Line Input #1, tpStr
If i = 0 Then
.Text = tpStr
Else
.SubItems(i) = tpStr
End If
Next i
End With
Loop
Close #1
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Dim tpList As ListItem
'保存數據
Open fileName For Output As #1
For Each tpList In ListView1.ListItems
Print #1, tpList.Text
For i = 1 To 4
Print #1, tpList.SubItems(i)
Next i
Next tpList
Close #1
End Sub

本程序已在windows98+VB6.0環境下調試通過。

G. 用C語言設計一個通訊錄系統

/*
* main_tongxunlu.c
*
* Created on: 2011-6-21
* Author: zhanglujin
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct record
{
char name[20]; //姓名
char phone[12]; //電話
char adress[50]; //地址
char postcode[8]; //郵政編碼
char e_mail[20]; //電子郵件。
}student[100]; //假設最大數為100.
//定義全局變數num,表示已經輸入的人數 。
int num; //這里使用數組解決通訊錄的問題,實際上使用鏈表更好。
int menu_select()
{
char s[80];
int a;/*定義整形變數*/
system("cls");
printf("\t\t***********歡迎進入通訊管理界面********\n\n");
printf("\t\t\t0. 輸入記錄\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. Quit\n");
printf("\t\t***********************************************\n\n");
do{
printf("Enter you choice(0~7):");
scanf("%s",s);
a=atoi(s);
}
while (a<0 || a>7);
return a;
}
int adser()
{
printf("\t\t\t**************** 請輸入用戶信息 ****************\n");
printf("\t\t\t輸入姓名:\n");
scanf("%s",student[num].name);
printf("\t\t\t輸入電話號碼:\n");
scanf("%s",student[num].phone);
printf("\t\t\t輸入地址:\n");
scanf("%s",student[num].adress);
printf("\t\t\t輸入郵編:\n");
scanf("%s",student[num].postcode);
printf("\t\t\t輸入e-mail:\n");
scanf("%s",student[num].e_mail);
num++;
printf("\t\t\t是否繼續添加?(Y/N):\n");
if(getch()=='y' || getch()=='Y')
adser();
return(0);
}
void list()
{
int i;
system("cls");
if(num!=0)
{
printf("\t\t\t*************** 以下為通訊錄所有信息************\n");
for (i=0;i<num;i++)
{
printf("\t\t\t姓名:%s\n",student[i].name);
printf("\t\t\t電話:%s\n",student[i].phone);
printf("\t\t\t地址:%s\n",student[i].adress);
printf("\t\t\t郵編:%s\n",student[i].postcode);
printf("\t\t\te-mail:%s\n",student[i].e_mail);
if(i+1<num)
{
system("pause");
}
}
printf("\t\t\t************************************************\n");
}
else
printf("\t\t\t通訊錄中無任何紀錄\n");
printf("\t\t\t按任意鍵返回主菜單:\n");
getch(); //這里是無回顯的輸入字元,你輸入的字元不會顯示在屏幕上。
return;
}
int searchbyname()
{
int mark=0;
int i;
printf("\t\t\t***************** 按姓名查找 *******************\n");
char name[20];
printf("\t\t\t請輸入姓名:\n");
scanf("%s",name);
for(i=0;i<num;i++)
{
if (strcmp(student[i].name,name)==0)
{
printf("\t\t\t************* 以下是您查找的用戶信息 ***********\n");
printf("\t\t\t姓名: %s",student[i].name);
printf("\t\t\t電話: %s",student[i].phone);
printf("\t\t\t地址: %s",student[i].adress);
printf("\t\t\te-mail:%s",student[i].e_mail);
printf("\t\t\t************************************************\n");
mark++;
if((i+1)<num)
{
printf("\t\t\t是否繼續查找相同名字的用戶信息:(y/n)\n");
if(getch()=='y' || getch()=='Y')
{

H. c語言程序設計通信錄

給,已經編譯運行確認了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct record
{
char id[10];
char name[20];
char phone[20];
char adress[40];
char postcode[10];
char e_mail[30];
}student[500];
int num=0;//外部變數num為文件中的紀錄數
FILE *fp;
void mainmenu();//主菜單
void newrecord();//新添紀錄
void searchmenu();//查詢菜單
void searchbyid();//按學號查詢
void searchbyname();//按姓名查詢
void searchbyphone();//按電話查詢
void deletemenu();//刪除菜單
void deleteall();//刪除所有
void deleteone();//刪除單個
void showall();//顯示所有
void readfromfile();//讀取文件
void writetofile();//寫入文件
void deletebyid();//按學號刪除
void deletebyname();//按姓名刪除
void listbyid();//按學號排序
void listbyname();//按姓名排序
void listmenu();//排序菜單
void main()
{
readfromfile();
while (1)
{
mainmenu();
}
}
void readfromfile()//從文件導入
{
if((fp=fopen("student.bin","rb"))==NULL)
{
printf("\n\t\t通訊錄文件不存在");
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t建立失敗");
exit(0);
}
else
{
printf("\n\t\t通訊錄文件已建立");
printf("\n\t\t按任意鍵進入主菜單");
getch();
return;
}
exit(0);
}
fseek(fp,0,2); /*文件位置指針移動到文件末尾*/
if (ftell(fp)>0) /*文件不為空*/
{
rewind(fp); /*文件位置指針移動到文件開始位置*/
for (num=0;!feof(fp) && fread(&student[num],sizeof(struct record),1,fp);num++);
printf("\n\t\t文件導入成功");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
printf("\n\t\t文件導入成功");
printf("\n\t\t通訊錄文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
void mainmenu()//主菜單
{
char choic;
system("cls");
printf("\n\t\t******************** 主菜單 ********************");
printf("\n\t\t*********** 1-新添紀錄 2-查詢菜單 ************");
printf("\n\t\t*********** 3-刪除菜單 4-記錄排序 ************");
printf("\n\t\t*********** 5-保存退出 6-不保存退出***********");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':newrecord();break;
case '2':searchmenu();break;
case '3':deletemenu();break;
case '4':listmenu();break;
case '5':writetofile();break;
case '6':exit(0);
default:mainmenu();
}
}
void searchmenu()//查詢菜單
{
char choic;
system("cls");
printf("\n\t\t******************* 查詢菜單 *******************");
printf("\n\t\t********** 1-顯示所有 2-按學號查詢 ************");
printf("\n\t\t********** 3-按姓名查詢4-按電話查詢 ************");
printf("\n\t\t********** 5-返回主菜單 ************");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':showall();break;
case '2':searchbyid();break;
case '3':searchbyname();break;
case '4':searchbyphone();break;
case '5':mainmenu();break;
}
}
void deletemenu()//刪除菜單
{
char choic;
if(num==0)
{
printf("\n\t\t對不起,文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
system("cls");
printf("\n\t\t******************* 刪除菜單 *******************");
printf("\n\t\t*********** 1-刪除所有 2-刪除單個 ***********");
printf("\n\t\t*********** 3-返回主菜單 ***********");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':deleteall();break;
case '2':deleteone();break;
case '3':mainmenu();break;
default:mainmenu();break;
}
}
void deleteall()//刪除所有
{
printf("\n\t\t確認刪除?(y/n)");
if (getch()=='y')
{
fclose(fp);
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t不能打開文件,刪除失敗");
readfromfile();
}
num=0;
printf("\n\t\t紀錄已刪除,按任意鍵返回主菜單");
getch();
return;
}
else
return;
}
void deletebyname()//按姓名刪除
{
int a=0;
int findmark=0;
int j;
int deletemark=0;
int i;
char name[20];
printf("\n\t\t請輸入要刪除學生姓名:");
scanf("%s",name);
for (i=a;i<num;i++)
{
if (strcmp(student[i].name,name)==NULL)
{
printf("\n\t\t以下是您要刪除的學生紀錄:");
findmark++;
printf("\n\t\t________________________________");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t________________________________");
printf("\n\t\t是否刪除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++) /*紀錄移動,從stud數組中刪除之*/
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t刪除成功");
if((i+1)<num)
{
printf("\n\t\t是否繼續刪除相同姓名的同學信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf("\n\t\t是否繼續刪除相同姓名的同學信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
}
else
continue;
}
if ((deletemark==0)&&(findmark==0))
{
printf("\n\t\t沒有該同學的紀錄");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
return;
}
else if (findmark!=0)
{
printf("\n\t\t沒有重名信息");
printf("\n\t\t沒有該同學的紀錄");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
return;
}
}
void deletebyid()//按學號刪除
{
int i,j;
int deletemark=0;
char id[20];
printf("\n\t\t請輸入要刪除學生學號:");
scanf("%s",id);
if(num==0)
{
printf("\n\t\t對不起,文件中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單");
getch();
return;
}
for (i=0;i<num;i++)
{
if (strcmp(student[i].id,id)==NULL)
{
printf("\n\t\t以下是您要刪除的學生紀錄:");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t是否刪除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++) /*紀錄移動,從stud數組中刪除之*/
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t刪除成功");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
}
else
return;
}
continue;

}
if (deletemark==0)
{
printf("\n\t\t沒有該同學的紀錄");
printf("\n\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyid();
return;
}
}
void newrecord()//添加紀錄
{
printf("\n\t\t**************** 請輸入學生信息 ****************\n");
printf("\n\t\t輸入學號:");
scanf("%s",&student[num].id);
printf("\n\t\t輸入姓名:");
scanf("%s",&student[num].name);
printf("\n\t\t輸入電話號碼:");

scanf("%s",&student[num].phone);
printf("\n\t\t輸入地址:");
scanf("%s",&student[num].adress);
printf("\n\t\t輸入郵編:");
scanf("%s",&student[num].postcode);
printf("\n\t\t輸入e-mail:");
scanf("%s",&student[num].e_mail);
num++;
printf("\n\t\t是否繼續添加?(Y/N):");
if (getch()=='y')
newrecord();
return;
}
void showall()//顯示所有
{
int i;
system("cls");
if(num!=0)
{
printf("\n\t\t*************** 以下為通訊錄所有信息************");
for (i=0;i<num;i++)
{
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\t郵編: %s",student[i].postcode);
printf("\n\t\te-mail:%s",student[i].e_mail);

printf("\t\t");
if (i+1<num)
{
printf("\n\t\t__________________________");
system("pause");
}
}
printf("\n\t\t************************************************");
}
else
printf("\n\t\t通訊錄中無任何紀錄");
printf("\n\t\t按任意鍵返回主菜單:");
getch();
return;
}

//按電話查詢
void searchbyphone()
{
int mark=0;
int i;
int a=0;
printf("\n\t\t****************** 按電話查找 ******************");
char phone[10];
printf("\n\t\t請輸入學號:");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if (strcmp(student[i].phone,phone)==0)
{
printf("\n\t\t************** 以下是您查找的學生信息 **********");
printf("\n\t\t學號: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t電話: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t************************************************");
printf("\n\t\t按任意鍵返回主菜單:");
mark++;
getch();
return;
}
}

熱點內容
如何才能使郵件伺服器高效穩定地工作 發布:2024-04-24 04:30:55 瀏覽:460
sql數字開頭的 發布:2024-04-24 04:29:17 瀏覽:60
c電梯調度演算法 發布:2024-04-24 04:15:34 瀏覽:785
郵件對稱加密 發布:2024-04-24 04:10:26 瀏覽:666
c語言讀一行 發布:2024-04-24 04:10:25 瀏覽:149
如何破解博客密碼 發布:2024-04-24 04:05:22 瀏覽:962
我的世界伺服器點不動 發布:2024-04-24 04:04:42 瀏覽:384
安卓小說怎麼導出 發布:2024-04-24 03:51:23 瀏覽:348
不用編譯安裝linux 發布:2024-04-24 03:50:00 瀏覽:630
希望之村體驗服如何進入伺服器 發布:2024-04-24 03:31:11 瀏覽:210