您好,登錄后才能下訂單哦!
小編給大家分享一下編程開發(fā)中如何實(shí)現(xiàn)文件版本通訊錄,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
源碼:
"Contact.h"
#ifndef __CONTACT_H__ #define __CONTACT_H__ #include <stdio.h> #include <string.h> #include<stdlib.h> enum OP { EXIT, ADD, DELE, SER, MODIFY, DISPLAY, CLEAR, SORT }; #define NAME_MAX 20 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 25 #define MAX 1000 #define DEFAULT 2 //通訊錄首次的默認(rèn)大小 #define MAX_INIT 2 typedef struct PerInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }PeoInfo; typedef struct Contact { PeoInfo *data; int size; int i; int temp; int capacity; }Contact,*Pcon; void init(Pcon pcon); void _add(Pcon pcon); void _display(Pcon pcon); void _del(Pcon pcon); void _search(Pcon pcon); void _modify(Pcon pcon); void _sort(Pcon pcon); void _clear(Pcon pcon); void _destory(Pcon pcon); void _save(Pcon pcon); void _load(Pcon pcon); #endif //__CONTACT_H__
"Contact.c"
#include "Contact.h" void init(Pcon pcon) { pcon->size = 0; pcon->capacity = DEFAULT; pcon->data=(PeoInfo *)malloc(MAX_INIT*sizeof(PeoInfo)); if(pcon->data == NULL) { printf("out of menory"); exit(EXIT_FAILURE); } memset(pcon->data,0,pcon->capacity*sizeof(PeoInfo)); _load(pcon); } void _rise(Pcon pcon) //增大容量 { PeoInfo *temp = (PeoInfo *)realloc(pcon->data,(pcon->capacity+MAX_INIT)*sizeof(PeoInfo)); if(temp == NULL) { printf("out of memory!\n"); exit(0); } else pcon->data = temp; } void _dela(Pcon pcon) //減小容量 { PeoInfo *temp = (PeoInfo *)realloc(pcon->data,(pcon->capacity-MAX_INIT)*sizeof(PeoInfo)); if(temp == NULL) { printf("電話本已空!\n"); return ; } else pcon->data = temp; } void _destory(Pcon pcon) { if(pcon->data != NULL) { free(pcon->data); pcon->data = NULL; } } void _add(Pcon pcon) { PeoInfo *temp = NULL; if(pcon->size == pcon->capacity) { _rise(pcon); } printf("請(qǐng)輸入名字:>"); scanf("%s",pcon->data[pcon->size].name); printf("請(qǐng)輸入年齡:>"); scanf("%d",&(pcon->data[pcon->size].age)); printf("請(qǐng)輸入性別:>"); scanf("%s",pcon->data[pcon->size].sex); printf("請(qǐng)輸入電話:>"); scanf("%s",pcon->data[pcon->size].tele); printf("請(qǐng)輸入地址:>"); scanf("%s",pcon->data[pcon->size].addr); pcon->size++; printf("添加成功\n"); } void _display(Pcon pcon) { int i = 0; printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr"); for(i = 0;i<pcon->size;i++) { printf("%9s\t%3d\t%4s\t%11s\t%10s\n", pcon->data[i].name, pcon->data[i].age, pcon->data[i].sex, pcon->data[i].tele, pcon->data[i].addr); } } static int find(Pcon pcon, char *name) { int i = 0; for(i = 0;i<pcon->size; i++) { if(strcmp(pcon->data[i].name,name) == 0) { return i; } } return -1; } void _del(Pcon pcon) { int pos = 0; int index = 0; char name[NAME_MAX]; if(pcon->size == 0) { printf("電話本空\(chéng)n"); return; } printf("請(qǐng)輸入要?jiǎng)h除人的名字:>"); scanf("%s",name); pos = find(pcon, name); if(pos == -1) { printf("找不到要?jiǎng)h除的人\n"); return; } // for(index = pos; index < pcon->size; index++) { pcon->data[index] = pcon->data[index+1]; } pcon->size--; printf("刪除成功\n"); _dela(pcon); } void _search(Pcon pcon) { char name[NAME_MAX] = {0}; int pos = 0; printf("請(qǐng)輸入要查找人的名字:>"); scanf("%s",name); pos = find(pcon, name); if(pos == -1) { printf("指定聯(lián)系人不存在\n"); return; } else { printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr"); printf("%9s\t%3d\t%4s\t%11s\t%10s\n", pcon->data[pos].name, pcon->data[pos].age, pcon->data[pos].sex, pcon->data[pos].tele, pcon->data[pos].addr); } } void _modify(Pcon pcon) { char name[NAME_MAX] = {0}; int pos = 0; printf("請(qǐng)輸入要修改人的名字:>"); scanf("%s",name); pos = find(pcon, name); if(pos == -1) { printf("指定聯(lián)系人不存在\n"); return; } else { printf("請(qǐng)輸入名字:>"); scanf("%s",pcon->data[pos].name); printf("請(qǐng)輸入年齡:>"); scanf("%d",&(pcon->data[pos].age)); printf("請(qǐng)輸入性別:>"); scanf("%s",pcon->data[pos].sex); printf("請(qǐng)輸入電話:>"); scanf("%s",pcon->data[pos].tele); printf("請(qǐng)輸入地址:>"); scanf("%s",pcon->data[pos].addr); } } void _sort(Pcon pcon) { int i = 0; int j = 0; PeoInfo temp = {0}; for(i = 0;i<pcon->size-1; i++)//控制排序趟數(shù) { for(j = 0; j<pcon->size-1-i; j++) { if(strcmp(pcon->data[j].name,pcon->data[j+1].name) > 0) { temp = pcon->data[j]; pcon->data[j] = pcon->data[j+1]; pcon->data[j+1] = temp; } } } } void _clear(Pcon pcon) { pcon->size = 0; } void _save(Pcon pcon) //寫入文件 { int i = 0; FILE *pfWrite = fopen("contact.txt","w"); //打開一個(gè)文件 if(pfWrite == NULL) { perror("open file for write"); exit(0); } else { for(i=0; i<pcon->size; i++) { fwrite(&(pcon->data[i]),sizeof(PeoInfo),1,pfWrite); } fclose(pfWrite); } } void _load(Pcon pcon) //加載文件內(nèi)容 { int i = 0; PeoInfo temp = {0}; FILE *pfRead = fopen("contact.txt","r"); if(pfRead == NULL) { perror("open file for read"); exit(0); } while(fread(&temp,sizeof(PeoInfo),1,pfRead)) { _rise(pcon); pcon->data[i] = temp; i++; pcon->size++; } }
"test.c"
#include "Contact.h" void menu() { printf("***************************\n"); printf("********* 1.add ***********\n"); printf("********* 2.del **********\n"); printf("********* 3.search ********\n"); printf("********* 4.modify ********\n"); printf("********* 5.display *******\n"); printf("********* 6.clear *********\n"); printf("********* 7.sort **********\n"); printf("********* 0.exit **********\n"); printf("***************************\n"); } int main() { int input = 1; Contact pcon; init(&pcon); while(input) { menu(); printf("請(qǐng)選擇要操作的數(shù)字:"); scanf("%d",&input); printf("\n"); { switch(input) { case ADD: _add(&pcon); break; case DELE: _del(&pcon); break; case SER: _search(&pcon); break; case MODIFY: _modify(&pcon); break; case DISPLAY: _display(&pcon); break; case CLEAR: _clear(&pcon); break; case SORT: _sort(&pcon); break; case EXIT: _save(&pcon); _destory(&pcon); break; default: printf("無(wú)效選擇!"); break; } } } system("pause"); return 0; }
以上是“編程開發(fā)中如何實(shí)現(xiàn)文件版本通訊錄”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。