?? 101.c
字號(hào):
#include "stdio.h" /*I/O函數(shù)*/
#include "stdlib.h" /*標(biāo)準(zhǔn)庫(kù)函數(shù)*/
#include "string.h" /*字符串函數(shù)*/
#include "ctype.h" /*字符操作函數(shù)*/
#define MAX 50 /*定義常數(shù)表示記錄數(shù)*/
struct ADDRESS /*定義數(shù)據(jù)結(jié)構(gòu)*/
{
char name[15]; /*姓名*/
char units[20]; /*單位*/
char phone[15]; /*電話*/
};
int InputRecord(struct ADDRESS r[]); /*輸入記錄*/
void ListRecord(struct ADDRESS r[],int n); /*顯示記錄*/
int DeleteRecord(struct ADDRESS r[],int n); /*刪除記錄*/
int InsertRecord(struct ADDRESS r[],int n); /*插入記錄*/
void SaveRecord(struct ADDRESS r[],int n); /*記錄保存為文件*/
int LoadRecord(struct ADDRESS r[]); /*從文件中讀記錄*/
int FindRecord(struct ADDRESS r[],int n,char *s) ; /*查找函數(shù)*/
void ShowRecord(struct ADDRESS temp); /*顯示單條記錄*/
void main()
{
int i;
char s[128];
struct ADDRESS address[MAX];/*定義結(jié)構(gòu)體數(shù)組*/
int num;/*保存記錄數(shù)*/
clrscr();
while(1)
{
clrscr();
printf("********************MENU*******************\n\n");
printf("| 0: Input a record |\n");
printf("| 1: List records in the file |\n");
printf("| 2: Delete a record |\n");
printf("| 3: Insert a record to the list |\n");
printf("| 4: Save records to the file |\n");
printf("| 5: Load records from the file |\n");
printf("| 6: Quit |\n\n");
printf("*******************************************\n");
do{
printf("\n Input your choice(0~6):"); /*提示輸入選項(xiàng)*/
scanf("%s",s); /*輸入選擇項(xiàng)*/
i=atoi(s); /*將輸入的字符串轉(zhuǎn)化為整型數(shù)*/
}while(i<0||i>6); /*選擇項(xiàng)不在0~11之間重輸*/
switch(i) /*調(diào)用主菜單函數(shù),返回值整數(shù)作開(kāi)關(guān)語(yǔ)句的條件*/
{
case 0:num=InputRecord(address);break;/*輸入記錄*/
case 1:ListRecord(address,num);break; /*顯示全部記錄*/
case 2:num=DeleteRecord(address,num);break; /*刪除記錄*/
case 3:num=InsertRecord(address,num); break; /*插入記錄*/
case 4:SaveRecord(address,num);break; /*保存文件*/
case 5:num=LoadRecord(address); break; /*讀文件*/
case 6:exit(0); /*如返回值為11則程序結(jié)束*/
}
}
}
/***輸入記錄,形參為結(jié)構(gòu)體數(shù)組,函數(shù)值返回類(lèi)型為整型表示記錄長(zhǎng)度*/
int InputRecord(struct ADDRESS t[])
{
int i,n;
char *s;
clrscr(); /*清屏*/
printf("\n please input record num you want to input. \n"); /*提示信息*/
scanf("%d",&n); /*輸入記錄數(shù)*/
printf("please input the %d records \n",n); /*提示輸入記錄*/
printf("name units telephone\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
{
scanf("%s%s%s",t[i].name,t[i].units,t[i].phone); /*輸入記錄*/
printf("----------------------------------------------\n");
}
getch();
return n; /*返回記錄條數(shù)*/
}
/*顯示記錄,參數(shù)為記錄數(shù)組和記錄條數(shù)*/
void ListRecord(struct ADDRESS t[],int n)
{
int i;
clrscr();
printf("\n\n*******************ADDRESS LIST*****************\n");
printf("name units telephone\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%-13s%-20s%-15s\n",t[i].name,t[i].units,t[i].phone);
printf("************************end*********************\n");
getch();
}
/*顯示指定的一條記錄*/
void ShowRecord(struct ADDRESS temp)
{
printf("\n\n************************************************\n");
printf("name units telephone\n");
printf("------------------------------------------------\n");
printf("%-13s%-20s%-15s\n",temp.name,temp.units,temp.phone);
printf("**********************end***********************\n");
getch();
}
/*刪除函數(shù),參數(shù)為記錄數(shù)組和記錄條數(shù)*/
int DeleteRecord(struct ADDRESS t[],int n)
{
char s[20]; /*要?jiǎng)h除記錄的姓名*/
int i,j;
clrscr();
printf("please record name:\n"); /*提示信息*/
scanf("%s",s);/*輸入姓名*/
i=FindRecord(t,n,s); /*調(diào)用FindRecord函數(shù)*/
if(i>n-1) /*如果i>n-1超過(guò)了數(shù)組的長(zhǎng)度*/
printf("Can't found the record\n"); /*顯示沒(méi)找到要?jiǎng)h除的記錄*/
else
{
ShowRecord(t[i]); /*調(diào)用輸出函數(shù)顯示該條記錄信息*/
for(j=i+1;j<n;j++) /*刪除該記錄,實(shí)際后續(xù)記錄前移*/
{
strcpy(t[j-1].name,t[j].name); /*將后一條記錄的姓名拷貝到前一條*/
strcpy(t[j-1].units,t[j].units); /*將后一條記錄的單位拷貝到前一條*/
strcpy(t[j-1].phone,t[j].phone); /*將后一條記錄的電話拷貝到前一條*/
}
n--; /*記錄數(shù)減1*/
printf("Delete a record successfully!\n");
}
getch();
return n; /*返回記錄數(shù)*/
}
/*插入記錄函數(shù),參數(shù)為結(jié)構(gòu)體數(shù)組和記錄數(shù)*/
int InsertRecord(struct ADDRESS t[],int n)/*插入函數(shù),參數(shù)為結(jié)構(gòu)體數(shù)組和記錄數(shù)*/
{
struct ADDRESS temp; /*新插入記錄信息*/
int i,j;
char s[20]; /*確定插入在哪個(gè)記錄之前*/
clrscr();
printf("please input record\n");
printf("************************************************\n");
printf("name units telephone\n");
printf("------------------------------------------------\n");
scanf("%s%s%s",temp.name,temp.units,temp.phone); /*輸入插入信息*/
printf("------------------------------------------------\n");
printf("please input locate name \n");
scanf("%s",s); /*輸入插入位置的姓名*/
i=FindRecord(t,n,s); /*調(diào)用FindRecord,確定插入位置*/
for(j=n-1;j>=i;j--) /*從最后一個(gè)結(jié)點(diǎn)開(kāi)始向后移動(dòng)一條*/
{
strcpy(t[j+1].name,t[j].name); /*當(dāng)前記錄的姓名拷貝到后一條*/
strcpy(t[j+1].units,t[j].units); /*當(dāng)前記錄的單位拷貝到后一條*/
strcpy(t[j+1].phone,t[j].phone); /*當(dāng)前記錄的電話拷貝到后一條*/
}
strcpy(t[i].name,temp.name); /*將新插入記錄的姓名拷貝到第i個(gè)位置*/
strcpy(t[i].units,temp.units); /*將新插入記錄的單位拷貝到第i個(gè)位置*/
strcpy(t[i].phone,temp.phone); /*將新插入記錄的電話拷貝到第i個(gè)位置*/
n++; /*記錄數(shù)加1*/
getch();
return n; /*返回記錄數(shù)*/
}
/*保存函數(shù),參數(shù)為結(jié)構(gòu)體數(shù)組和記錄數(shù)*/
void SaveRecord(struct ADDRESS t[],int n)
{
int i;
FILE *fp; /*指向文件的指針*/
clrscr();
printf("Saving the records to the file address.txt\n.............\n");
if((fp=fopen("address.txt","wb"))==NULL) /*打開(kāi)文件,并判斷打開(kāi)是否正常*/
{
printf("can not open file\n");/*沒(méi)打開(kāi)*/
exit(1); /*退出*/
}
printf("\nSaving the file\n"); /*輸出提示信息*/
fprintf(fp,"%d",n); /*將記錄數(shù)寫(xiě)入文件*/
fprintf(fp,"\r\n"); /*將換行符號(hào)寫(xiě)入文件*/
for(i=0;i<n;i++)
{
fprintf(fp,"%-20s%-30s%-10s",t[i].name,t[i].units,t[i].phone);/*格式寫(xiě)入記錄*/
fprintf(fp,"\r\n"); /*將換行符號(hào)寫(xiě)入文件*/
}
fclose(fp);/*關(guān)閉文件*/
printf("Save the records successfully!\n"); /*顯示保存成功*/
getch();
}
/*讀入函數(shù),參數(shù)為結(jié)構(gòu)體數(shù)組*/
int LoadRecord(struct ADDRESS t[])
{
int i,n;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("address.txt","rb"))==NULL)/*打開(kāi)文件*/
{
printf("can not open file\n"); /*不能打開(kāi)*/
exit(1); /*退出*/
}
fscanf(fp,"%d",&n); /*讀入記錄數(shù)*/
for(i=0;i<n;i++)
fscanf(fp,"%20s%30s%10s",t[i].name,t[i].units,t[i].phone); /*按格式讀入記錄*/
fclose(fp); /*關(guān)閉文件*/
printf("You have successfully load files from file!\n"); /*顯示保存成功*/
getch();
return n; /*返回記錄數(shù)*/
}
/*查找函數(shù),參數(shù)為記錄數(shù)組和記錄條數(shù)以及姓名s */
int FindRecord(struct ADDRESS t[],int n,char *s)
{
int i;
for(i=0;i<n;i++)/*從第一條記錄開(kāi)始,直到最后一條*/
{
if(strcmp(s,t[i].name)==0) /*記錄中的姓名和待比較的姓名是否相等*/
return i; /*相等,則返回該記錄的下標(biāo)號(hào),程序提前結(jié)結(jié)束*/
}
return i; /*返回i值*/
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -