C言語による簡易通信録の実現

20381 ワード

通信録における基本的な操作を実現する
提供方法:1.指定連絡先の手紙2を追加します.指定連絡先情報3を削除する.指定連絡先情報4を検索する.指定連絡先情報5を変更する.すべての連絡先情報6を表示する.すべての連絡先を空にする.すべての連絡先を名前または年齢でソート
ヘッダファイル
#include

#define MAX 1000
#define FIRST 3
#define EXT 2

typedef struct PeoInfo
{
    char name[20];
    char sex[5];
    int age;
    char phone[12];
    char addr[30];

}PeoInfo;

typedef struct Contact
{
    int sz;
    int capacity;
    PeoInfo Info[];
}Contact;


void init(Contact *pcon);
void add(Contact *pcon);
void show(Contact *pcon);
void modif(Contact *pcon);
void sub(Contact *pcon);
void search(Contact *pcon);
void empty(Contact *pcon);
void sort(Contact *pcon);


#endif

関連関数の実装
#include"contact.h"

void init(Contact *pcon)
{
    pcon->sz = 0;
    pcon->capacity = FIRST;
}


int find(Contact* pcon,const char *find_name)
{
    int i = 0;
    assert(pcon != NULL);
    for(i=0;isz;i++)
    {
        if(strcmp(pcon->Info[i].name,find_name) == 0)
        {
            return i;
        }
    }
    return -1;
}

void add(Contact* pcon)
{

    char name[20];
    char sex[5];
    int age;
    char phone[12];
    char addr[30];
    assert(pcon != NULL); 
    if(pcon->sz>=pcon->capacity)
    {
        PeoInfo *p = realloc(pcon,sizeof(PeoInfo)+EXT*sizeof(PeoInfo));
        if(p == NULL)
        {
            printf("    !
"
); exit(1); } else { printf(" !
"
); pcon->capacity += EXT; } } printf(" :>"); scanf("%s",&name); strcpy(pcon->Info[pcon->sz].name,name); printf("
:>"
); scanf("%s",&sex); strcpy(pcon->Info[pcon->sz].sex,sex); printf("
:>"
); scanf("%d",&age); pcon->Info[pcon->sz].age = age; printf("
:>"
); scanf("%s",&phone); strcpy(pcon->Info[pcon->sz].phone,phone); printf("
:>"
); scanf("%s",&addr); strcpy(pcon->Info[pcon->sz].addr,addr); pcon->sz++; printf(" !
"
); } void show(Contact *pcon) { int i = 0; assert(pcon != NULL); if(pcon->sz == 0) printf(" !
"
); else { printf("%-12s%-12s%-12s%-12s%-12s
"
,"name","sex","age","phone","addr"); for(i=0;isz;i++) { printf("%-12s%-12s%-12d%-12s%-12s
"
,pcon->Info[i].name,pcon->Info[i].sex,pcon->Info[i].age,pcon->Info[i].phone,pcon->Info[i].addr); } } } void modif(Contact *pcon) { int ret = 0; char find_name[20] = {0}; char sex[5] = {0}; int age = 0; char phone[12] = {0}; char addr[30] = {0}; char name[20] = {0}; int choose = 0; assert(pcon != NULL); printf(" :>
"
); scanf("%s",&find_name); ret = find(pcon,find_name); if(ret == -1) printf(" !
"
); else { printf("1.name 2.sex 3.age 4.phone 5.addr 0.save
"
); do { printf(" :>
"
); scanf("%d",&choose); switch(choose) { case 1: printf(" :>
"
); scanf("%s",name); strcpy(pcon->Info[ret].name,name); break; case 2: printf(" :>
"
); scanf("%s",sex); strcpy(pcon->Info[ret].sex,sex); break; case 3: printf(" :>
"
); scanf("%d",&age); pcon->Info[ret].age=age; break; case 4: printf(" :>
"
); scanf("%s",phone); strcpy(pcon->Info[ret].phone,phone); break; case 5: printf(" :>
"
); scanf("%s",addr); strcpy(pcon->Info[ret].name,name); break; case 0: printf("
"
); return; default: printf("
"
); } }while(1); } } void sub(Contact *pcon) { char find_name[20] = {0}; int i = 0; int ret = 0; assert(pcon != NULL); printf(" :>
"
); scanf("%s",find_name); ret = find(pcon,find_name); if(ret == -1) printf(" !
"
); else { for(i=0;isz-ret-1;i++) { pcon->Info[ret+i] = pcon->Info[ret+i+1]; } pcon->sz--; printf(" !
"
); } } void search(Contact *pcon) { int ret = 0; char find_name[20] = {0}; assert(pcon != NULL); printf(" :>
"
); scanf("%s",find_name); ret = find(pcon,find_name); if(ret == -1) printf(" !
"
); else { printf("name:%s sex:%s age:%d phone:%s addr:%s
"
,pcon->Info[ret].name,pcon->Info[ret].sex,pcon->Info[ret].age,pcon->Info[ret].phone,pcon->Info[ret].addr); } } void empty(Contact *pcon) { pcon->sz = 0; printf(" !
"
); } void sort_by_name(Contact *pcon) { PeoInfo tmp; int i = 0; int j = 0; for(i=0;isz-1;i++) { for(j=0;jsz-i-1;j++) { if(strcmp(pcon->Info[j].name,pcon->Info[j+1].name)>0) { tmp = pcon->Info[j]; pcon->Info[j] = pcon->Info[j+1]; pcon->Info[j+1] = tmp; } } } printf(" !
"
); } void sort_by_age(Contact *pcon) { int i = 0; int j = 0; PeoInfo tmp; for(i=0;isz-1;i++) { for(j=0;jsz-1-i;j++) { if(pcon->Info[j].age>pcon->Info[j+1].age) { tmp = pcon->Info[j]; pcon->Info[j] = pcon->Info[j+1]; pcon->Info[j+1] = tmp; } } } printf(" !
"
); } void sort(Contact *pcon) { int input = 0; printf(" :> 1. 2.
"
); scanf("%d",&input); switch(input) { case 1: sort_by_name(pcon); break; case 2: sort_by_age(pcon); break; default: printf(" , !
"
); break; } }

しゅかんすうぶ
#include
#include"contact.h"

enum op
{
    EXIT,
    ADD,
    SUB,
    SEARCH,
    MODIF,
    SHOW,
    EMPTY,
    SORT,
};

void meun()
{
    printf("*****************************
"
); printf("**** 1.Add 2.Sub ****
"
); printf("**** 3.Search 4.Modif ****
"
); printf("**** 5.Show 6.Empty ****
"
); printf("**** 7.Sort 0.exit ****
"
); printf("*****************************
"
); } // 、 、 、 、 int main() { int input = 0; Contact* pcon = (Contact *)malloc(sizeof(Contact)+FIRST*sizeof(PeoInfo)); if(pcon == NULL) { printf(" !"); exit(1); } init(pcon); meun(); do{ printf(" :>
"
); scanf("%d",&input); switch(input) { case EXIT: free(pcon); return; case ADD: add(pcon); break; case SUB: sub(pcon); break; case SEARCH: search(pcon); break; case MODIF: modif(pcon); break; case SHOW: show(pcon); break; case EMPTY: empty(pcon); break; case SORT: sort(pcon); break; default: printf(" , .
"
); } }while(1); return 0; }