今天主要是写了一个项目——通讯录,然后做了一下学校的C语言的期末复习题,已经开始准备C的期末了。通讯录目前是我写过最长的代码,但其实实现不是最复杂的,是因为我为了让用户友好,所以说加了一些很多重复的代码(sleep和cls等),所以说很多冗杂的代码,但能够让用户的体验感很好,当看到这么长的代码跑起来,心里真的很爽!
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//int main()
//{
// int arr[][5] = { 1,2,3,4,5,6,7,8,9,10 };
// int i, j;
// for (i = 0; i < 2; i++)
// {
// for (j = 0; j < 5; j++)
// {
// printf("%d ", arr[i][j]);
// }
// printf("\n");
// }
// return 0;
//}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<errno.h>#define MAX 100
#define NAME_MAX 20
#define SEX_MAX 10
#define ADD_MAX 50
#define TELE_MAX 13typedef struct peo
{char name[NAME_MAX];char sex[SEX_MAX];int age;char add[ADD_MAX];char tele[TELE_MAX];
}peo;typedef struct con
{peo data[MAX];int count;
}con;void menu(void)
{printf("\t\t\t\t\t\t 欢迎使用通讯录\n");printf("\t\t\t\t\t\t 1.增加联系人\n");printf("\t\t\t\t\t\t 2.删除联系人\n");printf("\t\t\t\t\t 3.查看联系人\n");printf("\t\t\t\t\t\t 4.排序联系人\n");printf("\t\t\t\t\t\t 5.查找联系人\n");printf("\t\t\t\t\t\t 6.从本地读取\n");printf("\t\t\t\t\t\t 7.保存到本地\n");printf("\n");printf("\t\t\t\t\t\t 0.退出通讯录\n");printf("\n");
}void int_contact(con* contact)
{contact->count = 0;memset(contact->data, 0, sizeof(contact->data));
}void add(con* contact)
{if (contact->count == MAX){printf("通讯录已满,请扩容\n");return;}else{printf("请输入名字\n");scanf("%s", contact->data[contact->count].name);printf("请输入性别\n");scanf("%s", contact->data[contact->count].sex);printf("请输入年龄\n");scanf("%d", &(contact->data[contact->count].age));printf("请输入地址\n");scanf("%s", contact->data[contact->count].add);printf("请输入电话\n");scanf("%s", contact->data[contact->count].tele);Sleep(300);printf("输入成功\n");Sleep(300);printf("按任意键继续-->\n");getch();system("cls");contact->count++;}
}void print_contact(con* contact)
{Sleep(300);printf("\t\t\t%-5s\t%-3s\t%-3s\t%-25s\t%-11s\n", "姓名", "性别", "年龄", "地址", "电话");Sleep(300);int i = 0;for (i = 0; i < contact->count; i++){printf("\t\t\t%-5s\t%-3s\t%-3d\t%-25s\t%-11s\n", contact->data[i].name,contact->data[i].sex,contact->data[i].age,contact->data[i].add,contact->data[i].tele);Sleep(100);}Sleep(300);printf("输出完毕,按任意键继续-->\n");getch();system("cls");
}//进行查找联系人的方式选择
int choice(void)
{system("cls");Sleep(500);int choice;
flag:printf("\t\t\t\t\t\t 请选择你想要查找的方式?\n");Sleep(200);printf("\t\t\t\t\t\t 1.按照姓名查找\n");Sleep(200);printf("\t\t\t\t\t\t 2.按照电话查找\n");Sleep(200);printf("请选择--->\n");scanf("%d", &choice);switch (choice){case 1:{return 1;}case 2:{return 2;}default:{system("cls");printf("选择错误,请重新选择!\n");Sleep(300);printf("按任意键继续-->\n");getch();system("cls");goto flag;break;}}
}int find_name(con* contact)
{if (contact->count == 0){system("cls");Sleep(300);printf("联系人为空,请先输入!\n");return -1;}system("cls");Sleep(300);printf("请输入要查找的名字-->\n");char name[20] = { 0 };scanf("%s", name);int i;for (i = 0; i < contact->count; i++){if (strcmp(name, contact->data[i].name) == 0){return i + 1;}}return 0;
}int find_tele(con* contact)
{if (contact->count == 0){system("cls");Sleep(300);printf("联系人为空,请先输入!\n");return -1;}system("cls");Sleep(300);printf("请输入要查找的电话号码-->\n");char telenum[20] = { 0 };scanf("%s", telenum);int i;for (i = 0; i < contact->count; i++){if (strcmp(telenum, contact->data[i].tele) == 0){return i + 1;}}return 0;
}int dele_choice(void)
{system("cls");Sleep(500);int choice;
flag:printf("\t\t\t\t\t\t 请选择你想要删除的方式?\n");Sleep(200);printf("\t\t\t\t\t\t 1.按照姓名删除\n");Sleep(200);printf("\t\t\t\t\t\t 2.按照电话删除\n");Sleep(200);printf("请选择--->\n");scanf("%d", &choice);switch (choice){case 1:{return 1;}case 2:{return 2;}default:{system("cls");printf("选择错误,请重新选择!\n");Sleep(300);printf("按任意键继续-->\n");getch();system("cls");goto flag;break;}}
}void dele_name(con* contact)
{if (contact->count == 0){system("cls");Sleep(300);printf("联系人为空,请先输入!\n");}else{int place = find_name(contact);if (place > 0){int i = place;for (i = place - 1; i < contact->count - 1; i++){contact->data[i] = contact->data[i + 1];}contact->count--;Sleep(300);}}
}void dele_tele(con* contact)
{if (contact->count == 0){system("cls");Sleep(300);printf("联系人为空,请先输入!\n");}else{int place = find_tele(contact);if (place > 0){int i = place;for (i = place - 1; i < contact->count - 1; i++){contact->data[i] = contact->data[i + 1];}contact->count--;Sleep(300);}}
}void file_save(con* contact)
{if (contact->count == 0){system("cls");Sleep(300);printf("联系人为空,请先输入!\n");}else{FILE* pf = fopen("通讯录.txt", "w");if (pf == NULL){printf("FILE:%s", strerror(errno));}else{int i = 0;fprintf(pf, "\t\t\t%-5s\t%-3s\t%-3s\t%-25s\t%-11s\n", "姓名", "性别", "年龄", "地址", "电话");for (i = 0; i < contact->count; i++){fprintf(pf, "\t\t\t%-5s\t%-3s\t%-3d\t%-25s\t%-11s\n", contact->data[i].name,contact->data[i].sex,contact->data[i].age,contact->data[i].add,contact->data[i].tele);}}fclose(pf);pf = NULL;}
}int main()
{int input = 0;con contact;int_contact(&contact);do{menu();Sleep(500);printf("请选择-->\n");scanf("%d", &input);switch (input){case 1:{system("cls");Sleep(500);add(&contact);break;}case 2:{int ret = dele_choice();if (ret == 1){dele_name(&contact);printf("删除成功,现在有的联系人:\n");print_contact(&contact);}else{dele_tele(&contact);printf("删除成功,现在有的联系人:\n");print_contact(&contact);}break;}case 3:{system("cls");print_contact(&contact);break;}case 4:{system("cls");printf("开发中\n");printf("按任意键继续-->\n");getch();system("cls");break;}case 5:{int ret = choice();if (ret == 1){int temp = find_name(&contact);if (temp == 0){printf("找不到,请检查是否输入错误?\n");Sleep(300);printf("按任意键继续-->\n");getch();system("cls");}else if (temp > 0){Sleep(300);printf("\n找到了,是第%d个联系人\n\n", temp);Sleep(300);printf("\t\t\t%-5s\t%-3s\t%-3s\t%-25s\t%-11s\n", "姓名", "性别", "年龄", "地址", "电话");printf("\t\t\t%-5s\t%-3s\t%-3d\t%-25s\t%-11s\n", contact.data[temp - 1].name,contact.data[temp - 1].sex,contact.data[temp - 1].age,contact.data[temp - 1].add,contact.data[temp - 1].tele);printf("按任意键继续-->\n");getch();system("cls");}else{Sleep(300);printf("按任意键继续-->\n");getch();system("cls");}}else{int mid = find_tele(&contact);if (mid == 0){printf("找不到,请检查是否输入错误?\n");Sleep(300);printf("按任意键继续-->\n");getch();system("cls");}else if (mid > 0){Sleep(300);printf("找到了,是第%d个联系人\n\n", mid);Sleep(300);printf("\t\t\t%-5s\t%-3s\t%-3s\t%-25s\t%-11s\n", "姓名", "性别", "年龄", "地址", "电话");printf("\t\t\t%-5s\t%-3s\t%-3d\t%-25s\t%-11s\n", contact.data[mid - 1].name,contact.data[mid - 1].sex,contact.data[mid - 1].age,contact.data[mid - 1].add,contact.data[mid - 1].tele);printf("按任意键继续-->\n");getch();system("cls");}else{Sleep(300);printf("按任意键继续-->\n");getch();system("cls");}}break;}case 6:{system("cls");printf("开发中\n");printf("按任意键继续-->\n");getch();system("cls");break;}case 7:{system("cls");printf("正在保存中\n");file_save(&contact);printf(".");Sleep(500);printf(".");Sleep(500);printf(".");Sleep(500);printf(".");Sleep(500);printf(".");Sleep(500);printf(".\n");Sleep(500);printf("save successfully\n");Sleep(500);printf("按任意键继续-->\n");getch();system("cls");break;}case 0:{system("cls");printf("程序退出\n");Sleep(500);printf("按任意键继续-->\n");getch();break;}default:{system("cls");printf("选择错误,请重新选择-->\n");Sleep(500);printf("按任意键继续-->\n");getch();Sleep(500);system("cls");}}} while (input);return 0;
}
写代码的成就感,也许就像这样,明天继续努力!