您好,登錄后才能下訂單哦!
//基于順序表實現的簡易通訊錄-----1 #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct stu //定義一個學生信息的結構體類型 { char name[20]; char sex[6]; int age; char tele[20]; char address[40]; }stu; typedef struct //創建一個順序表 { stu data[1000]; int i; }suquence; int find(suquence *book, char *names) { int n = 0; while (n < book->i) { if (strcmp(book->data[n].name, names) == 0) break; n++; } if (n != book->i) return n; //如果找到則返回這個位置 return -1; //沒找到返回-1 } void Add(suquence *book) { if (book->i >= 1000) //最多存儲1000條信息 { printf("通訊錄已滿\n"); } else { printf("請輸入名字:"); scanf("%s", book->data[book->i].name); printf("請輸入性別:"); scanf("%s", book->data[book->i].sex); printf("請輸入年齡:"); scanf("%d",&book->data[book->i].age); printf("請輸入電話:"); scanf("%s", book->data[book->i].tele); printf("請輸入地址:"); scanf("%s", book->data[book->i].address); ++book->i; printf("添加成功\n"); } } void delete(suquence *book,char *names) { if (book->i <= 0) { printf("通訊錄已空\n"); } else { int n = 0; n = find(book, names); //根據名字查找 if (n==0) { printf("沒有此人\n"); } else { while (n < book->i - 1) //如果找到,則進行刪除,依次向前覆蓋 { book->data[n] = book->data[n + 1]; } book->i--; //刪除一個,則記錄位置的book->i也要減一 printf("刪除成功\n"); } } } void change(suquence *book , char *names) { int ret = 0; ret = find(book,names); //先找到這個人的位置 if (ret == -1) { printf("沒有此人\n"); } else { printf("請輸入名字:"); scanf("%s", book->data[ret].name); printf("請輸入性別:"); scanf("%s", book->data[ret].sex); printf("請輸入年齡:"); scanf("%d", &book->data[ret].age); printf("請輸入電話:"); scanf("%s", book->data[ret].tele); printf("請輸入地址:"); scanf("%s", book->data[ret].address); printf("修改成功\n"); } } void output(suquence *book) { if (book->i <=0) { printf("通訊錄為空\n"); } else { int n =0; while (n < book->i) { printf("名字:%s 性別: %s 年齡: %d 電話: %s 地址: %s\n", book->data[n].name, book->data[n].sex, book->data[n].age, book->data[n].tele, book->data[n].address); n++; } } } void init(suquence *book) { book->i = 0; //只要將記錄位置的book->i置0 printf("初始化成功\n"); } void sort(suquence *book) { if (book->i >1) //當通訊錄中有1個以上記錄再進行排序 { int i= 0; int j = 0; int flag = 0; for (i = 0; i < book->i-1; i++) { flag = 1; for (j = 0; j < book->i - i - 1; j++) { if (strcmp(book->data[j].name, book->data[j + 1].name) < 0) //根據名字進行排序 { char arr[50]; strcpy(arr, book->data[j].name); strcpy(book->data[j].name, book->data[j + 1].name); strcpy(book->data[j + 1].name, arr); strcpy(arr, book->data[j].sex); strcpy(book->data[j].sex, book->data[j + 1].sex); strcpy(book->data[j + 1].sex, arr); int tmp = book->data[j].age; book->data[j].age = book->data[j + 1].age; book->data[j + 1].age = tmp; strcpy(arr, book->data[j].tele); strcpy(book->data[j].tele, book->data[j + 1].tele); strcpy(book->data[j + 1].tele, arr); strcpy(arr, book->data[j].address); strcpy(book->data[j].address, book->data[j + 1].address); strcpy(book->data[j + 1].address, arr); flag = 0; } //if 結束 } if (flag) //如果已經有序,則flag為真 break; } } printf("排序成功\n"); } void execute(suquence book) { int n = 0; char name[20]; while (1) { printf("請選擇:"); scanf("%d", &n); switch (n) { case 0: exit(1); break; case 1: Add(&book); break; case 2: printf("請輸入姓名:"); scanf("%s", name); delete(&book, name); break; case 3: printf("請輸入姓名:"); scanf("%s", name); change(&book, name); break; case 4: printf("請輸入姓名:"); scanf("%s", name); int ret = find(&book, name); if (ret ==-1) printf("查詢無果\n"); else printf("名字:%s 性別: %s 年齡: %d 電話: %s 地址: %s\n", book.data[ret].name, book.data[ret].sex, book.data[ret].age, book.data[ret].tele, book.data[ret].address); break; case 5: init(&book); break; case 6: output(&book); break; case 7: sort(&book); break; default: printf("選擇無效\n"); break; } } } int main() { printf("*******************************\n"); printf("*0.exit 1.Add *\n"); printf("*2.deletel 3.change *\n"); printf("*4.find 5.init *\n"); printf("*6.output 7.sort *\n\n"); suquence book; book.i = 0; execute(book); system("pause"); return 0; }
//后續增加更多版本
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。