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