text.c數組
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include "contact.h" void menu(){ printf("==================================\n"); printf("== 1.add增長 2.del刪除 ==\n"); printf("== 3.search查找 4.modify修改==\n"); printf("== 5.show顯示 6.sort排序 ==\n"); printf("== 0.exit退出 ==\n"); printf("==================================\n"); } int main(){ int input=0; struct Contact con; //con就是通信錄,裏面包含1000個元素的數組和size InitContact(&con); //初始化通信錄 do { menu(); printf("請選擇:>"); scanf("%d", &input); switch (input) { case ADD: AddContact(&con); //添加信息 break; case DEL: DelContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModifyContact(&con); break; case SHOW: ShowContact(&con); //打印信息 break; case SORT: SortContact(&con); break; case EXIT: printf("退出通信錄\n"); break; default: printf("選擇錯誤\n"); break; } } while (input); return 0; }
contact.hide
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h> #define MAX 1000 #define MAX_NAME 20 #define MAX_SEX 5 #define MAX_TELE 12 #define MAX_ADDR 30 enum Option{ EXIT,//0 ADD,//1 DEL,//2 SEARCH,//3 MODIFY,//4 SHOW,//5 SORT//6 }; struct Peoinfo { char name[MAX_NAME]; //名字 int age; //年齡 char sex[MAX_SEX]; //性別 char tele[MAX_TELE]; //電話 char addr[MAX_ADDR]; //住址 }; //通信錄類型 struct Contact{ struct Peoinfo data[MAX]; //存放一個類型 int size; //記錄當前已有元素個數 }; //聲明函數 //初始化通信錄 void InitContact(struct Contact* ps); //添加一個信息到通信錄 void AddContact(struct Contact* ps); //打印通信錄的內容 void ShowContact(const struct Contact* ps); //刪除通信錄的一個信息 void DelContact(struct Contact* ps); //查找 void SearchContact(const struct Contact* ps); //修改 void ModifyContact(struct Contact* ps); //排序 void SortContact(struct Contact* ps);
contact.c函數
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h> #include "contact.h" //初始化通信錄 void InitContact(struct Contact* ps){ //memset 內存設置,把整個數組設置爲一個值 memset(ps->data, 0, sizeof(ps->data)); ps->size = 0; //設置通信錄最初只有0 } //增長一個元素到通信錄裏面 void AddContact(struct Contact* ps){ if (ps->size == MAX){ printf("通信錄已滿,沒法增長"); } else { printf("請輸入名字:>"); scanf("%s", ps->data[ps->size].name); printf("請輸入年齡:>"); scanf("%d", &(ps->data[ps->size].age)); printf("請輸入性別:>"); scanf("%s", ps->data[ps->size].sex); printf("請輸入電話:>"); scanf("%s", ps->data[ps->size].tele); printf("請輸入地址:>"); scanf("%s", ps->data[ps->size].addr); ps->size++; printf("添加成功\n"); } } void ShowContact(const struct Contact* ps){ if (ps->size == 0){ printf("通信錄爲空\n"); } else { int i = 0; printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年齡", "性別", "電話", "地址"); //%後面加數字是佔多少各 \t是tab -號是左對齊 for ( i = 0; i < ps->size; i++) { printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].addr ); } } } //static靜態變量 只能在這個文件內部使用 static int FindByname(const struct Contact* ps,char name[MAX_NAME]){ int i = 0; for (i = 0; i < ps->size; i++) { if (0 == strcmp(ps->data[i].name, name)){ return i; } } return -1; } void DelContact(struct Contact* ps){ char name[MAX_NAME]; printf("請輸入要刪除人的名字:>"); scanf("%s", name); //1.查找要刪除的人的位置 //咱們但願這個函數找到返回元素的下標,找不到返回-1 int pos=FindByname(ps,name); //2.刪除 if (pos==-1) { printf("你要刪除的人不存在\n"); } else { int j = 0; for ( j = pos; j < ps->size-1; j++) { ps->data[j] = ps->data[j + 1]; } ps->size--; printf("刪除成功\n"); } } void SearchContact(const struct Contact* ps){ char name[MAX_NAME]; printf("請輸入要查找人的名字:>"); scanf("%s", name); int pos = FindByname(ps, name); if (pos == -1){ printf("要查找的人不存在"); } else { printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年齡", "性別", "電話", "地址"); //%後面加數字是佔多少各 \t是tab -號是左對齊 printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[pos].name, ps->data[pos].age, ps->data[pos].sex, ps->data[pos].tele, ps->data[pos].addr ); } } void ModifyContact(struct Contact* ps){ char name[MAX_NAME]; printf("請輸入要修改人的名字:>"); scanf("%s", name); int pos = FindByname(ps, name); if (pos == -1){ printf("要修改的信息不存在"); } else{ printf("請輸入名字:>"); scanf("%s", ps->data[pos].name); printf("請輸入年齡:>"); scanf("%d", &(ps->data[pos].age)); printf("請輸入性別:>"); scanf("%s", ps->data[pos].sex); printf("請輸入電話:>"); scanf("%s", ps->data[pos].tele); printf("請輸入地址:>"); scanf("%s", ps->data[pos].addr); printf("修改完成\n"); } } void SortContact(struct Contact* ps){ if (ps->size == 0){ printf("通信錄爲空\n"); } else{ int i = 0; for (i = 0; i < ps->size-1; i++) { printf("%d\n",strcmp(ps->data[i].name, ps->data[i + 1].name)); if (1 > strcmp(ps->data[i].name, ps->data[i + 1].name)){ } else{ struct Peoinfo count = ps->data[i + 1]; ps->data[i + 1] = ps->data[i]; ps->data[i] = count; } } } }