【C语言】大一程序设计,学生通讯录系统

爱必应

作者闲说:本人新手,大佬勿喷,发布源码,纯属交流。请勿抄袭,代码仅供参考。
C语言——VS2019
能存储(读取)文件,可显示,查询,添加,修改,删除。
显示:
【C语言】大一程序设计,学生通讯录系统-图1

查询:
【C语言】大一程序设计,学生通讯录系统-图2

添加:
【C语言】大一程序设计,学生通讯录系统-图3

修改:
【C语言】大一程序设计,学生通讯录系统-图4

【C语言】大一程序设计,学生通讯录系统-图5

删除:
【C语言】大一程序设计,学生通讯录系统-图6

[C] 纯文本查看 复制代码

#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<string.h>#pragma warning(disable:4996)int num_of_peo=0;struct Student_date* head;struct Student_date* p1, * p2;struct Student_date { int num; //学号 char name[20]; //姓名 char phone_num[20]; //手机号 struct Student_date *next;};void scan_message() //这个函数用于读取文件{ int n = 0; FILE* fp; if ((fp = fopen("message.txt", "r")) == NULL) { printf("无法找到通讯录文件"); exit(0); } p1 = p2 = (struct Student_date*)malloc(sizeof(Student_date)); head = NULL; do { n = n + 1; if (n == 1) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (struct Student_date*)malloc(sizeof(Student_date)); fscanf(fp, "%d\t\t%s\t\t%s\n", &p2->num, p2->name, p2->phone_num); } while (!feof(fp)); p2->next = NULL; fclose(fp);}void Menu()/*显示主菜单子函数*/{ printf("\t\t\t| 我是一级主菜单 |\n"); printf("\t\t\t|----------------------|\n"); printf("\t\t\t|1.显示通讯录信息 |\n"); printf("\t\t\t|2.查询通讯录信息 |\n"); printf("\t\t\t|3.添加通讯录 |\n"); printf("\t\t\t|4.修改通讯录 |\n"); printf("\t\t\t|5.删除通讯录 |\n"); printf("\t\t\t|6.按学号查找通讯录信息|\n"); printf("\t\t\t|7.按姓名查找通讯录信息|\n"); printf("\t\t\t|----------------------|\n"); printf("\t\t\t请输入选项:");}void Print_message()/*显示通讯录信息*/{ int n=0; FILE* fp; scan_message(); struct Student_date* p3; num_of_peo = 0; if ((fp = fopen("message.txt", "r")) == NULL) { printf("无法找到通讯录文件"); exit(0); } fclose(fp); printf("学号\t\t姓名\t\t手机号\n"); p3 = head; if (head != NULL) { do { printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; num_of_peo = num_of_peo + 1; } while (p3 != NULL); } printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); fclose(fp);}struct Student_date * Add_message()/*添加通讯录*/{ FILE* fp; struct Student_date* p3; int n=0; if ((fp = fopen("message.txt", "r")) == NULL) { printf("\n无法找到通讯录文件,已创建新的通讯录文件\n"); fp = fopen("message.txt", "w"); } fclose(fp); fp = fopen("message.txt", "ab"); p1 = p2 = (struct Student_date*)malloc(sizeof(Student_date)); printf("在学号、姓名、手机均输入0终止输入\n"); printf("请输入学号:"); scanf("%d", &p1->num); printf("请输入姓名:"); scanf("%s", p1->name); printf("请输入手机号:"); scanf("%s", p1->phone_num); head = NULL; while (p1->num != 0) { n = n + 1; if (n == 1) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (struct Student_date*)malloc(sizeof(Student_date)); printf("---------------\n"); printf("---------------\n"); printf("请输入学号:"); scanf("%d", &p1->num); printf("请输入姓名:"); scanf("%s", p1->name); printf("请输入手机号:"); scanf("%s", p1->phone_num); } p2->next = NULL; p3 = head; if (head != NULL) { do { fprintf(fp, "%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; } while (p3 != NULL); } fclose(fp); free(p1); free(p2); free(p3); return (head);}void Fix_message()/*修改通讯录*/{ FILE* fp; struct Student_date* p3,*record[1000]; int i; int choice; int your_number; char your_name[20]; char your_phone[20]; int loca = 0; //loca用于检测是否找到输入的学号 printf("\t\t\t请输入您要修改的内容:\n"); printf("\t\t\t1.修改学号\n"); printf("\t\t\t2.修改姓名\n"); printf("\t\t\t3.修改手机号\n"); scanf("%d", &choice); scan_message(); p3 = head; if (head != NULL) { do { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; } while (p3 != NULL); } p3 = head; if (choice == 1) { i = 0; printf("请输入您将修改的学号:"); scanf("%d", &your_number); num_of_peo = 0; if (head != NULL) { do { if (your_number==p3->num) { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); loca = 1; record[i] = p3; i++; num_of_peo = num_of_peo + 1; } p3 = p3->next; } while (p3 != NULL); if (loca == 0) { printf("无法找到该学号,请检查是否输入错误,即将返回主菜单\n"); return; } printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); if (loca == 1) { printf("请输入编号,此编号用于确定修改的学生\n"); for (i = 0; i < num_of_peo; i++) { printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t%s\n", i + 1, record[i]->num, record[i]->name, record[i]->phone_num); } printf("您输入的编号:"); scanf("%d", &i); printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t%s", i, record[i - 1]->num, record[i - 1]->name, record[i-1]->phone_num); printf("\n请输入新的学号:"); scanf("%d", &record[i - 1]->num); printf("修改后的总数据:\n"); printf("学号\t\t姓名\t\t手机号\n"); } } else { printf("查找失败请检查是否存在通讯录文件"); } } if (choice == 2) { int i = 0; printf("请输入您将修改的姓名:"); scanf("%s", your_name); num_of_peo = 0; if (head != NULL) { do { if (strcmp(your_name, p3->name) == 0) { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); loca = 1; record[i] = p3; i++; num_of_peo = num_of_peo + 1; } p3 = p3->next; } while (p3 != NULL); if (loca == 0) { printf("无法找到该姓名,请检查是否输入错误,即将返回主菜单\n"); return; } printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); if (loca == 1) { printf("请输入编号,此编号用于确定修改的学生\n"); for (i = 0; i < num_of_peo; i++) { printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t%s\n", i + 1, record[i]->num, record[i]->name, record[i]->phone_num); } printf("您输入的编号:"); scanf("%d", &i); printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t%s", i, record[i - 1]->num, record[i - 1]->name, record[i - 1]->phone_num); printf("\n请输入新的姓名:"); scanf("%s", record[i - 1]->name); printf("修改后的总数据:\n"); printf("学号\t\t姓名\t\t手机号\n"); } } else { printf("查找失败请检查是否存在通讯录文件"); } } if (choice == 3) { int i = 0; printf("请输入您将修改的手机号:"); scanf("%s",your_phone); num_of_peo = 0; if (head != NULL) { do { if (strcmp(your_phone, p3->phone_num) == 0) { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); loca = 1; record[i] = p3; i++; num_of_peo = num_of_peo + 1; } p3 = p3->next; } while (p3 != NULL); if (loca == 0) { printf("无法找到该手机号,请检查是否输入错误,即将返回主菜单\n"); return; } printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); if (loca == 1) { printf("请输入编号,此编号用于确定修改的学生\n"); for (i = 0; i < num_of_peo; i++) { printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t%s\n", i + 1, record[i]->num, record[i]->name, record[i]->phone_num); } printf("您输入的编号:"); scanf("%d", &i); printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t", i , record[i-1]->num, record[i-1]->name); scanf("%s", record[i-1]->phone_num); printf("修改后的总数据:\n"); printf("学号\t\t姓名\t\t手机号\n"); } } else { printf("查找失败请检查是否存在通讯录文件"); } } if (loca == 1) { p3 = head; if (head != NULL) { fp = fopen("message.txt","wb+"); do { fprintf(fp, "%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; } while (p3 != NULL); } p3 = head; if (head != NULL) { do { printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; } while (p3 != NULL); } printf("修改成功!\n"); free(p1); free(p2); free(p3); } fclose(fp);} void Del_message()/*删除通讯录*/{ FILE* fp; scan_message(); int your_num,n,i; struct Student_date*p3,* fristp=head, * secondp=head; p3 = head; num_of_peo = 0; if (head != NULL) { i = 1; do { printf("编号\t\t学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%d\t\t%s\t\t%s\n",i, p3->num, p3->name, p3->phone_num); p3 = p3->next; i++; num_of_peo = num_of_peo + 1; } while (p3 != NULL); } printf("请输入您要删除项所对应的编号:"); scanf("%d", &your_num); n = num_of_peo - your_num + 1; if (your_num == 1 && num_of_peo > 1) { head = head->next; } else if (your_num == 1 && num_of_peo == 1) { head = NULL; } else { while (fristp->next != NULL) { if (n > 0) { fristp = fristp->next; n--; } else { fristp = fristp->next; secondp = secondp->next; } } secondp->next = secondp->next->next; } printf("学号\t\t姓名\t\t手机号\n"); p3 = head; if (head != NULL) { do { printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; } while (p3 != NULL); } p3 = head; if (head != NULL) { fp = fopen("message.txt", "wb+"); do { fprintf(fp, "%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); p3 = p3->next; } while (p3 != NULL); } fclose(fp);}void Sc_num()/*按学号查找通讯录信息*/{ struct Student_date* p3; int your_number; int loca=0; //loca用于检测是否找到输入的学号 printf("请输入您要查找的学号:"); scanf("%d", &your_number); scan_message(); p3 = head; num_of_peo = 0; if (head != NULL) { do { if (your_number == p3->num) { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); loca = 1; num_of_peo = num_of_peo + 1; } p3 = p3->next; } while (p3 != NULL); printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); if (loca == 0) { printf("无法找到该学号,请检查是否输入错误\n"); } } else { printf("查找失败请检查是否存在通讯录文件"); } free(p1); free(p2); free(p3);}void Sc_name()/*按姓名查找通讯录信息*/{ struct Student_date* p3; char your_name[20]; int loca = 0; //loca用于检测是否找到输入的手机号 printf("请输入您要查找的姓名:"); scanf("%s", your_name); scan_message(); p3 = head; num_of_peo = 0; if (head != NULL) { do { if (strcmp(your_name, p3->name) == 0) { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); loca = 1; num_of_peo = num_of_peo + 1; } p3 = p3->next; } while (p3 != NULL); printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); if (loca == 0) { printf("无法找到该姓名,请检查是否输入错误\n"); } } free(p1); free(p2); free(p3);}void Scan_message()/*查询通讯录信息*/{ int choice, loca = 1, sign = 1; char YorN; struct Student_date* p3; printf("\t\t\t---------------------\n"); printf("\t\t\t| 查询通讯录信息 |\n"); printf("\t\t\t---------------------\n"); printf("\t\t\t1.按 学号 查找通讯录信息\n"); printf("\t\t\t2.按 姓名 查找通讯录信息\n"); printf("\t\t\t3.按 手机号 查找通讯录信息\n"); printf("\t\t\t请输入选项:"); while (loca) { scanf("%d", &choice); printf("您输入的选项是:%d\n", choice); if (choice == 1) { Sc_num(); } else if (choice == 2) { Sc_name(); } else if (choice == 3) { char your_phone[20]; int loca = 0; //loca用于检测是否找到输入的手机号 printf("请输入您要查找的手机号:"); scanf("%s", your_phone); scan_message(); p3 = head; num_of_peo = 0; if (head != NULL) { do { if (strcmp(your_phone,p3->phone_num)==0) { printf("学号\t\t姓名\t\t手机号\n"); printf("%d\t\t%s\t\t%s\n", p3->num, p3->name, p3->phone_num); loca = 1; num_of_peo = num_of_peo + 1; } p3 = p3->next; } while (p3 != NULL); printf("-----------------\n"); printf("|当前找到%d名学生|\n", num_of_peo); printf("-----------------\n"); if (loca == 0) { printf("无法找到该手机号,请检查是否输入错误\n"); } } else { printf("查找失败请检查是否存在通讯录文件"); } } else { printf("选项错误\n"); } getchar(); do { printf("\n输入Y返回查询菜单,输入N结束查询\n"); YorN = getchar(); if (YorN == 'Y' || YorN == 'y') { printf("您选择了继续。\n"); Sleep(1500); system("cls"); sign = 0; printf("\t\t\t---------------------\n"); printf("\t\t\t| 查询通讯录信息 |\n"); printf("\t\t\t---------------------\n"); printf("\t\t\t1.按 学号 查找通讯录信息\n"); printf("\t\t\t2.按 姓名 查找通讯录信息\n"); printf("\t\t\t3.按 手机号 查找通讯录信息\n"); printf("\t\t\t请输入选项:"); } else if (YorN == 'N' || YorN == 'n') { loca = 0; printf("您选择了结束查询"); sign = 0; return; } else { printf("输入错误"); sign = 1; } } while (sign); sign = 1; } free(p1); free(p2); free(p3);}int main(){ int choice,loca=1,sign=1; char YorN; while(loca) { Menu(); scanf("%d", &choice); printf("您输入的选项是:%d\n", choice); if (choice == 1) { Print_message(); } else if (choice == 2) { Scan_message(); } else if (choice == 3) { Add_message(); } else if (choice == 4) { Fix_message(); } else if (choice == 5) { Del_message(); } else if (choice == 6) { Sc_num(); } else if (choice == 7) { Sc_name(); } else { printf("选项错误\n"); } choice = NULL; getchar(); do { printf("\n输入Y返回一级主菜单,输入N结束程序\n"); YorN=getchar(); if (YorN == 'Y'|| YorN == 'y') { printf("您选择了继续。\n"); Sleep(1500); system("cls"); sign = 0; } else if (YorN == 'N'|| YorN == 'n') { loca = 0; printf("您选择了结束"); sign = 0; } else { printf("输入错误"); sign = 1; } } while (sign); sign = 1; } return 0;}

【C语言】大一程序设计,学生通讯录系统-图7

声明:本站所有资源均由网友分享,如有侵权内容,请在文章下方留言,本站会立即处理。

原文链接:,转发请注明来源!

发表评论