根据你的要求写的。
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _student
{
char id[10]; //学号
char name[20];//姓名
char sex[4];//性别 男、女
char major[20];//专业
int cla; //班级
int romeid;//宿舍号
char addr[20];//籍贯
char phone[12];//手机号
char qq[10]; //qq
char vx[18]; //vx
}Student;
const char* filename = "student.txt"; //数据文件
//8.从文件读取结构体数据
int initFromFile(Student stu[])
{
FILE* fp = 0;
int n = 0;
fp = fopen(filename, "r");
if (fp == 0)
{
printf("文件读取失败\n");
return 0;
}
while (!feof(fp))
{
if (fscanf(fp, "%s %s %s %s %d %d %s %s %s %s", stu[n].id,stu[n].name,stu[n].sex,stu[n].major,&stu[n].cla,&stu[n].romeid,stu[n].addr,stu[n].phone,stu[n].qq,stu[n].vx)==10)
n++;
}
fclose(fp);
return n;
}
//7.写入文件
void write2file(Student stu[], int n)
{
int i = 0;
FILE* fp = fopen(filename, "w");
for (i = 0; i < n; i++)
fprintf(fp, "%s %s %s %s %d %d %s %s %s %s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].major, stu[i].cla, stu[i].romeid, stu[i].addr, stu[i].phone, stu[i].qq, stu[i].vx);
fclose(fp);
printf("数据写入文件完成!\n");
}
//1.添加联系人
int add(Student stu[], int n)
{
int op = 1;
while (op)
{
printf("请输入学号:"); scanf("%s", stu[n].id);
printf("请输入姓名:"); scanf("%s", stu[n].name);
printf("请输入性别:"); scanf("%s", stu[n].sex);
printf("请输入专业:"); scanf("%s", stu[n].major);
printf("请输入班级:"); scanf("%d", &stu[n].cla);
printf("请输入宿舍号:"); scanf("%d", &stu[n].romeid);
printf("请输入籍贯:"); scanf("%s", stu[n].addr);
printf("请输入手机号:"); scanf("%s", stu[n].phone);
printf("请输入QQ号:"); scanf("%s", stu[n].qq);
printf("请输入微信号:"); scanf("%s", stu[n].vx);
n++;
printf("是否继续添加:(1.是 0.否) ");
scanf("%d", &op);
}
return n;
}
//2.查询
void search(Student stu[], int n)
{
char tmp[20];
int i;
int flag = 0;
printf("请输入姓名或者学号或者手机号或者微信号或者qq号:"); //根据其中的任何一个查询
scanf("%s", tmp);
for (i = 0; i < n; i++)
{
if (strcmp(stu[i].id, tmp) == 0 || strcmp(stu[i].name, tmp) == 0 || strcmp(stu[i].phone, tmp) == 0 || strcmp(stu[i].qq, tmp) == 0 || strcmp(stu[i].vx, tmp) == 0)
{
flag = 1;
printf("%s %s %s %s %d %d %s %s %s %s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].major, stu[i].cla, stu[i].romeid, stu[i].addr, stu[i].phone, stu[i].qq, stu[i].vx);
}
}
if (flag == 0)
printf("查无此人\n");
return ;
}
//3.删除联系人
int del(Student stu[], int n)
{
char tmp[20];
int i, j;
int flag = 0;
int op;
printf("请输入学号或者手机号或者微信号或者qq号:"); //要求学号、手机号、微信号和QQ必须唯一
scanf("%s", tmp);
for (i = 0; i < n; i++)
{
if (strcmp(stu[i].id, tmp) == 0 || strcmp(stu[i].phone, tmp) == 0 || strcmp(stu[i].qq, tmp) == 0 || strcmp(stu[i].vx, tmp) == 0)
{
flag = 1;
printf("查询到的人员信息为:\n");
printf("%s %s %s %s %d %d %s %s %s %s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].major, stu[i].cla, stu[i].romeid, stu[i].addr, stu[i].phone, stu[i].qq, stu[i].vx);
printf("是否确认删除:(1.是 0.否) ");
scanf("%d", &op);
if (op == 1)
{
for (j = i; j < n - 1; j++)
stu[j] = stu[j + 1];
n -= 1;
return n;
}
}
}
if (flag == 0)
printf("查无此人\n");
return n;
}
//4.修改联系人信息
void mod(Student stu[], int n)
{
char tmp[20];
int i, j;
int flag = 0;
int op;
printf("请输入学号或者手机号或者微信号或者qq号:"); //要求学号、手机号、微信号和QQ必须唯一
scanf("%s", tmp);
for (i = 0; i < n; i++)
{
if (strcmp(stu[i].id, tmp) == 0 || strcmp(stu[i].phone, tmp) == 0 || strcmp(stu[i].qq, tmp) == 0 || strcmp(stu[i].vx, tmp) == 0)
{
flag = 1;
printf("查询到的人员信息为:\n");
printf("%s %s %s %s %d %d %s %s %s %s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].major, stu[i].cla, stu[i].romeid, stu[i].addr, stu[i].phone, stu[i].qq, stu[i].vx);
printf("是否确认修改:(1.是 0.否) ");
scanf("%d", &op);
if (op == 1)
{
printf("请输入学号:"); scanf("%s", stu[i].id);
printf("请输入姓名:"); scanf("%s", stu[i].name);
printf("请输入性别:"); scanf("%s", stu[i].sex);
printf("请输入专业:"); scanf("%s", stu[i].major);
printf("请输入班级:"); scanf("%d", &stu[i].cla);
printf("请输入宿舍号:"); scanf("%d", &stu[i].romeid);
printf("请输入籍贯:"); scanf("%s", stu[i].addr);
printf("请输入手机号:"); scanf("%s", stu[i].phone);
printf("请输入QQ号:"); scanf("%s", stu[i].qq);
printf("请输入微信号:"); scanf("%s", stu[i].vx);
printf("修改成功\n");
return ;
}
}
}
if (flag == 0)
printf("查无此人\n");
}
//5.输出列表
void show(Student stu[], int n)
{
int i = 0;
printf("联系人信息列表:\n");
for(;i<n;i++)
printf("%s %s %s %s %d %d %s %s %s %s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].major, stu[i].cla, stu[i].romeid, stu[i].addr, stu[i].phone, stu[i].qq, stu[i].vx);
}
//6.按学号排序
void sortById(Student stu[], int n)
{
int i, j;
Student t;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (strcmp(stu[j].id, stu[j + 1].id) > 0) //根据学号从小到大排序
{
t = stu[j];
stu[j] = stu[j+1];
stu[j + 1] = t;
}
}
}
printf("排序完成,排序后:\n");
show(stu, n);
}
int main()
{
int op = 1;
Student stu[100];
int n = 0;
n = initFromFile(stu); //初始化,将外存数据文件读入程序
while (op != 7)
{
system("cls"); //清屏
printf("------------------------------------------\n");
printf(" 个人通讯录管理系统 \n");
printf("------------------------------------------\n");
printf(" 1.添加联系人\n");
printf(" 2.查询联系人\n");
printf(" 3.删除联系人\n");
printf(" 4.修改联系人信息\n");
printf(" 5.输出联系人列表\n");
printf(" 6.按学号排序\n");
printf(" 7.退出程序(将数据写入文件)\n");
//printf(" 8.初始化\n"); //程序启动后自动执行
printf("------------------------------------------\n");
printf("请选择:");
scanf("%d", &op);
switch (op)
{
case 1:n = add(stu, n); break;
case 2:search(stu, n); break;
case 3:n = del(stu, n); break;
case 4:mod(stu, n); break;
case 5:show(stu, n); break;
case 6:sortById(stu, n); break;
case 7:write2file(stu, n); break;
}
system("pause");
}
}
#include <stdio.h>
#include <string.h>
#define N 108
typedef struct telephone
{
char name[25];
char num[25];
char tel[25];
} TEL;
void DisplayMenu(); //显示主菜单
void save(TEL a[], int n);
void CreateList(TEL a[], int n); //1.创建通讯录
void DisplayList(TEL a[], int n); //2.显示通讯录
void InquireListName(TEL a[], int n); //3.按姓名查询通讯录
void InquireListNum(TEL a[], int n); //4.按学号查询通讯录
void ChangeListName(TEL a[], int n); //5.按姓名修改通讯录
void ChangeListNum(TEL a[], int n); //6.按学号修改通讯录
int DelListName(TEL a[], int n); //7.按姓名删除通讯录
int DelListNum(TEL a[], int n); //8.按学号删除通讯录
void UpSortListName(TEL a[], int n); //9.姓名升序排序通讯录
void DownSortListNum(TEL a[], int n); //10.学号逆序排序通讯录
int AddList(TEL a[], int n); //11.添加学生信息
int main()
{
struct telephone a[N];
int i, n;
FILE *fp;
if ((fp = fopen("stu.dat", "r")) != NULL) //如果文件stu.dat已存在
{ // 则把数据从文件加载到数组a(读入记录个数n和n个姓名、学号、电话号码)
fscanf(fp, "%d", &n);
for (i = 0; i < n; i++)
fscanf(fp, "%s%s%s", a[i].name, a[i].num, a[i].tel);
}
else
{
printf("第一次使用该系统,请输入n和n个姓名、学号、电话号码:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%s%s%s", a[i].name, a[i].num, a[i].tel);
save(a, n);
}
DisplayMenu(); //显示主菜单
while (1)
{
char select;
char name[25];
scanf(" %c", &select);
if (select < 'a' || select > 'l')
{
printf("输入错误!\n");
continue;
}
//if( select=='l' )
// break;
switch (select)
{
case 'a':
DisplayMenu(); //显示主菜单
break;
case 'b':
DisplayList(a, n); //2.显示通讯录
break;
case 'c':
InquireListName(a, n); //3.按姓名查询通讯录
break;
case 'd':
InquireListNum(a, n); //4.按学号查询通讯录
break;
case 'e':
ChangeListName(a, n); //5.按姓名修改通讯录
break;
case 'f':
ChangeListNum(a, n); //6.按学号修改通讯录
break;
case 'g':
if (DelListName(a, n)) //7.按姓名删除通讯录
n--;
break;
case 'h':
if (DelListNum(a, n)) //8.按学号删除通讯录
n--;
break;
case 'i':
UpSortListName(a, n); //9.姓名升序排序通讯录
break;
case 'j':
DownSortListNum(a, n); //10.学号逆序排序通讯录
break;
case 'k':
if (AddList(a, n)) //11.添加学生信息
n++;
save(a, n);
break;
case 'l':
exit(0); //12.退出系统
break;
}
}
return 0;
}
void save(TEL a[], int n)
{
int i;
FILE *fp;
fp = fopen("stu.dat", "w");
fprintf(fp, "%d\n", n);
for (i = 0; i < n; i++)
fprintf(fp, "%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
}
void DisplayMenu() //1.显示主菜单
{
printf(" 学生通讯录管理系统\n\n");
printf("*************************系统功能菜单**************************\n");
printf(" -------------------------------------------------------------\n");
printf(" a.显示主菜单 b.显示通讯录\n");
printf("************************************************************\n");
printf(" c.按姓名查询通讯录 d.按学号查询通讯录\n");
printf("************************************************************\n");
printf(" e.按姓名修改通讯录 f.按学号修改通讯录\n");
printf("************************************************************\n");
printf(" g.按姓名删除通讯录 h.按学号删除通讯录\n");
printf("************************************************************\n");
printf(" i.姓名升序排序通讯录 j.学号逆序排序通讯录\n");
printf("************************************************************\n");
printf(" k.添加学生信息 l.退出系统\n");
printf(" -------------------------------------------------------------\n");
printf("***************************************************************\n\n");
printf("请选择您需要的操作:\n");
}
void DisplayList(TEL a[], int n) //2.显示通讯录
{
int i;
printf("显示通讯录:\n");
for (i = 0; i < n; i++)
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
printf("\n");
}
void InquireListName(TEL a[], int n) //3.按姓名查询通讯录
{
int i, flag;
char SearchName[25];
printf("按姓名查询通讯录:\n");
scanf("%s", SearchName);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchName, a[i].name) == 0)
{
printf("%s\t", a[i].name);
printf("%s\t", a[i].num);
printf("%s\n", a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
{
printf("未查询到该姓名信息!\n");
}
printf("\n");
}
void InquireListNum(TEL a[], int n) //4.按学号查询通讯录
{
int i, flag;
char SearchID[25];
printf("按学号查询通讯录:\n");
scanf("%s", SearchID);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchID, a[i].num) == 0)
{
printf("%s\t", a[i].name);
printf("%s\t", a[i].num);
printf("%s\n", a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
{
printf("未查询到该学号信息!\n");
}
printf("\n");
}
void ChangeListName(TEL a[], int n) //5.按姓名修改通讯录
{
int i, flag;
char SearchName[25];
printf("按姓名修改通讯录:\n");
scanf("%s", SearchName);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchName, a[i].name) == 0)
{
strcpy(a[i].name, SearchName);
printf("请输入要修改的学号:");
scanf("%s", a[i].num);
printf("请输入要修改的电话号码:");
scanf("%s", a[i].tel);
//strcpy(p[i].name,ChangeName);
//strcpy(p[i].name,ChangeTel);
save(a, n);
printf("信息修改完毕!\n");
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
printf("未找到该姓名信息!");
printf("\n");
}
void ChangeListNum(TEL a[], int n) //6.按学号修改通讯录
{
int i, flag;
char SearchNum[25];
printf("按学号修改通讯录:\n");
scanf("%s", SearchNum);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchNum, a[i].num) == 0)
{
strcpy(a[i].num, SearchNum);
printf("请输入要修改的名字:");
scanf("%s", a[i].name);
printf("请输入要修改的电话号码:");
scanf("%s", a[i].tel);
//strcpy(p[i].name,ChangeName);
//strcpy(p[i].name,ChangeTel);
save(a, n);
printf("信息修改完毕!\n");
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
printf("未找到该学号信息!");
printf("\n");
}
int DelListName(TEL a[], int n) //7.按姓名删除通讯录
{
int i, j;
char SearchName[25];
printf("按姓名删除通讯录:\n");
scanf("%s", SearchName);
for (i = 0; i < n; i++)
{
if (strcmp(a[i].name, SearchName) == 0)
break;
}
if (i >= n)
{
printf("未查询到该姓名信息!按姓名删除通讯录失败!\n");
return 0;
}
else
{
for (j = i; j < n - 1; j++)
a[j] = a[j + 1];
save(a, n);
printf("按姓名删除通讯录成功!\n");
return 1;
}
}
int DelListNum(TEL a[], int n) //8.按学号删除通讯录
{
int i, j, flag;
char SearchNum[25];
printf("按学号删除通讯录:\n");
scanf("%s", SearchNum);
for (i = 0; i < n; i++)
{
if (strcmp(a[i].num, SearchNum) == 0)
break;
}
if (i >= n)
{
printf("未查询到该学号信息!按学号删除通讯录失败!\n");
return 0;
}
else
{
for (j = i; j < n - 1; j++)
a[j] = a[j + 1];
printf("按学号删除通讯录成功!\n");
save(a, n);
return 1;
}
printf("\n");
}
void UpSortListName(TEL a[], int n) //9.姓名升序排序通讯录
{
int i, j;
TEL temp;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(a[i].name, a[j].name) > 0)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("姓名升序排序通讯录:\n");
for (i = 0; i < n; i++)
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
printf("\n");
}
void DownSortListNum(TEL a[], int n) //10.学号逆序排序通讯录
{
int i, j;
TEL temp;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(a[i].num, a[j].num) > 0)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("学号逆序排序通讯录:\n");
for (i = n - 1; i >= 0; i--)
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
printf("\n");
}
int AddList(TEL a[], int n) //11.添加学生信息
{
int i, flag;
char AddNum[25];
printf("添加通讯录:\n");
printf("请输入要添加的学号:");
scanf("%s", AddNum);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(AddNum, a[i].num) == 0)
{
flag = 1;
printf("学号重复!\n");
return 0;
}
}
if (flag == 0)
{
//n++;
printf("学号不重复!请添加信息:\n");
printf("请输入要添加的名字:");
scanf("%s", a[n].name);
strcpy(a[n].num, AddNum);
printf("请输入要添加的电话号码:");
scanf("%s", a[n].tel);
//save(a,n);
printf("添加信息成功!\n");
return 1;
//printf("%d",n);
}
printf("\n");
}
望采纳
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100 // 最大联系人数量
#define NAME_LEN 20 // 姓名最大长度
#define MAJOR_LEN 20 // 专业最大长度
#define LOCATION_LEN 20 // 籍贯最大长度
#define NUMBER_LEN 20 // 电话号码最大长度
// 班级通讯录管理系统中的联系人结构体
typedef struct Contact {
char name[NAME_LEN]; // 姓名
char gender; // 性别('M'或'F')
char major[MAJOR_LEN]; // 专业
char class[NAME_LEN]; // 班级
char dorm[NAME_LEN]; // 宿舍
char location[LOCATION_LEN]; // 籍贯
char phone[NUMBER_LEN]; // 手机号
char qq[NUMBER_LEN]; // QQ号
char wechat[NUMBER_LEN]; // 微信号
} Contact;
// 班级通讯录管理系统的数据结构
typedef struct AddressBook {
Contact contacts[MAX_CONTACTS]; // 联系人数组
int size; // 当前联系人数量
} AddressBook;
// 初始化通讯录
void initAddressBook(AddressBook *book) {
book->size = 0;
}
// 添加联系人
void addContact(AddressBook *book, Contact contact) {
if (book->size < MAX_CONTACTS) {
book->contacts[book->size] = contact;
book->size++;
}
}
// 查询联系人
Contact *findContact(AddressBook *book, char *name) {
for (int i = 0; i < book->size; i++) {
if (strcmp(book->contacts[i].name, name) == 0) {
return &book->contacts[i];
}
}
return NULL;
}
// 删除联系人
void deleteContact(AddressBook *book, char*name) {
int index = -1;
for (int i = 0; i < book->size; i++) {
if (strcmp(book->contacts[i].name, name) == 0) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < book->size - 1; i++) {
book->contacts[i] = book->contacts[i + 1];
}
book->size--;
}
}
// 修改联系人信息
void updateContact(Contact *contact, char *name, char gender, char *major, char *class, char *dorm, char *location, char *phone, char *qq, char *wechat) {
strcpy(contact->name, name);
contact->gender = gender;
strcpy(contact->major, major);
strcpy(contact->class, class);
strcpy(contact->dorm, dorm);
strcpy(contact->location, location);
strcpy(contact->phone, phone);
strcpy(contact->qq, qq);
strcpy(contact->wechat, wechat);
}
// 输出联系人列表
void printContacts(AddressBook *book) {
for (int i = 0; i < book->size; i++) {
Contact contact = book->contacts[i];
printf("姓名: %s\n", contact.name);
printf("性别: %c\n", contact.gender);
printf("专业: %s\n", contact.major);
printf("班级: %s\n", contact.class);
printf("宿舍: %s\n", contact.dorm);
printf("籍贯: %s\n", contact.location);
printf("手机号: %s\n", contact.phone);
printf("QQ号: %s\n", contact.qq);
printf("微信号: %s\n", contact.wechat);
printf("\n");
}
}
// 按学号排序
void sortContactsByName(AddressBook *book) {
for (int i = 0; i < book->size; i++) {
for (int j = i + 1; j < book->size; j++) {
if (strcmp(book->contacts[i].name, book->contacts[j].name) > 0) {
Contact temp = book->contacts[i];
book->contacts[i] = book->contacts[j];
book->contacts[j] = temp;
}
}
}
}
// 将通讯录写入文件
void saveAddressBook(AddressBook *book, char **filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("打开文件失败\n");
return;
}
fwrite(book, sizeof(AddressBook), 1, file);
fclose(file);
}
// 从文件读取通讯录
void loadAddressBook(AddressBook *book, char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("打开文件失败\n");
return;
}
fread(book, sizeof(AddressBook), 1, file);
fclose(file);
}
int main() {
AddressBook book;
initAddressBook(&book);
while (1) {
printf("1. 添加联系人\n");
printf("2. 查询联系人\n");
printf("3. 删除联系人\n");
printf("4. 修改联系人信息\n");
printf("5. 输出联系人列表\n");
printf("6. 按学号排序\n");
printf("7. 退出程序\n");
printf("8. 初始化\n");
int input;
scanf("%d", &input);
if (input == 1) {
// 添加联系人
Contact contact;
printf("请输入姓名: ");
scanf("%s", contact.name);
printf("请输入性别: ");
scanf(" %c", &contact.gender);
printf("请输入专业: ");
scanf("%s", contact.major);
printf("请输入班级: ");
scanf("%s", contact.class);
printf("请输入宿舍: ");
scanf("%s", contact.dorm);
printf("请输入籍贯: ");
scanf("%s", contact.location);
printf("请输入手机号: ");
scanf("%s", contact.phone);
printf("请输入QQ号: ");
scanf("%s", contact.qq);
printf("请输入微信号: ");
scanf("%s", contact.wechat);
addContact(&book, contact);
} else if (input == 2) {
// 查询联系人
char name[NAME_LEN];
printf("请输入姓名: ");
scanf("%s", name);
Contact *contact = findContact(&book, name);
if (contact != NULL) {
printf("姓名: %s\n", contact->name);
printf("性别: %c\n", contact->gender);
printf("专业: %s\n", contact->major);
printf("班级: %s\n", contact->class);
printf("宿舍: %s\n", contact->dorm);
printf("籍贯: %s\n", contact->location);
printf("手机号: %s\n", contact->phone);
printf("QQ号: %s\n", contact->qq);
printf("微信号: %s\n", contact->wechat);
} else {
printf("未找到联系人\n");
}
} else if (input == 3) {
// 删除联系人
char name[NAME_LEN];
printf("请输入姓名: ");
scanf("%s", name);
deleteContact(&book, name);
} else if (input == 4) {
// 修改联系人信息
char name[NAME_LEN];
printf("请输入姓名: ");
scanf("%s", name);
Contact *contact = findContact(&book, name);
if (contact != NULL) {
printf("请输入姓名: ");
scanf("%s", name);
char gender;
printf("请输入性别: ");
scanf(" %c", &gender);
char major[MAJOR_LEN];
printf("请输入专业: ");
scanf("%s", major);
char class[NAME_LEN];
printf("请输入班级: ");
scanf("%s", class);
char dorm[NAME_LEN];
printf("请输入宿舍: ");
scanf("%s", dorm);
char location[LOCATION_LEN];
printf("请输入籍贯: ");
scanf("%s", location);
char phone[NUMBER_LEN];
printf("请输入手机号: ");
scanf("%s", phone);
char qq[NUMBER_LEN];
printf("请输入QQ号: ");
scanf("%s", qq);
char wechat[NUMBER_LEN];
printf("请输入微信号: ");
scanf("%s", wechat);
updateContact(contact, name, gender, major, class, dorm, location, phone, qq, wechat);
} else {
printf("未找到联系人\n");
}
} else if (input == 5) {
// 输出联系人列表
printContacts(&book);
} else if (input == 6) {
// 按学号排序
sortContactsByName(&book);
} else if (input == 7) {
// 退出程序
char filename[NAME_LEN];
printf("请输入文件名: ");
scanf("%s", filename);
saveAddressBook(&book, filename);
break;
} else if (input == 8) {
// 初始化
char filename[NAME_LEN];
printf("请输入文件名: ");
scanf("%s", filename);
loadAddressBook(&book, filename);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct workers
{
char jobNo[20]; //职工号
char name[20]; //姓名
char sex[4]; //性别
char birthday[15]; //出生年月
char degree[15]; //学历
char position[30]; //职务
char salary[10]; //工资
char addr[100]; //住址
char tel[20]; //电话
};
//类型定义语句
typedef struct node
{
struct workers data;
struct node *next;
}Node;
//添加信息函数 "尾插法建表"
void Add(Node *worker)
{
Node *p, *q;
char n[10];
q = worker;
while (q->next != NULL)
{
q = q->next;
}
while (1)
{
printf("提示:输入0返回主菜单!\n");
printf("请输入职工号:");
scanf("%s", n);
if (strcmp(n, "0") == 0)
{
break;
}
p = (Node *)malloc(sizeof(Node));
strcpy(p->data.jobNo, n);
printf("请输入姓名:");
scanf("%s", p->data.name);
printf("请输入性别:");
scanf("%s", p->data.sex);
printf("请输入出生年月:");
scanf("%s", p->data.birthday);
printf("请输入学历:");
scanf("%s", p->data.degree);
printf("请输入职务:");
scanf("%s", p->data.position);
printf("请输入工资:");
scanf("%s", p->data.salary);
printf("请输入住址:");
scanf("%s", p->data.addr);
printf("请输入电话:");
scanf("%s", p->data.tel);
printf("提示:已经完成一条记录的添加!");
p->next = NULL;
q->next = p;
q = p;
}
}
//修改职工信息函数
int Modify(Node *worker)
{
Node *p;
char find[20];
p = worker->next;
if (p == NULL)
{
printf("\n提示:没有资料可以修改!\n");
return 0;
}
printf("请输入要修改的职工号");
scanf("%s", find);
while (p != NULL)
{
if (strcmp(p->data.jobNo, find) == 0)
{
break;
}
p = p->next;
}
if (p!=NULL)
{
int x;
while (1)
{
printf("完成修改请输入0,否则输入非0数字进行修改!");
scanf("%d", &x);
if (x == 0)
{
break;
}
printf(" 请输入新职工号: ");
scanf("%s", p->data.jobNo);
printf(" 请输入新职工姓名: ");
scanf("%s", p->data.name);
printf(" 请输入新职工性别: ");
scanf("%s", p->data.sex);
printf(" 请输入新职工出生年月: ");
scanf("%s", p->data.birthday);
printf(" 请输入新职工学历: ");
scanf("%s", p->data.degree);
printf(" 请输入新职工职务: ");
scanf("%s", p->data.position);
printf(" 请输入新职工工资: ");
scanf("%s", p->data.salary);
printf(" 请输入新职工住址: ");
scanf("%s", p->data.addr);
printf(" 请输入新职工电话: ");
scanf("%s", p->data.tel);
printf(" \n提示:该职工资料已经修改!\n ");
}
}
else
printf("\n提示:没有你要修改的资料!\n ");
}
//输出职工信息函数
int Display(Node *worker)
{
Node *p;
p = worker->next;
if (p == NULL)
{
printf(" \n提示:没有信息可以显示!\n ");
return 0;
}
printf("\t\t\t\t显示结果\n");
printf(" \n职工号 姓名 性别 出生年月 学历 职务 工资 住址 电话 \n ");
while (p!=NULL)
{
printf("\n %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t\n ", p->data.jobNo, p->data.name,
p->data.sex, p->data.birthday, p->data.degree, p->data.position,
p->data.salary, p->data.addr, p->data.tel);
p = p->next;
}
}
//查询职工信息函数: 按职工号查询 按职工姓名查询
int Search(Node *worker)
{
Node *p;
int sel;
char find[20];
p = worker->next;
if (p==NULL)
{
printf(" \n提示:没有资料可以查询!\n ");
return 0;
}
printf("\n提示:\n 输入 0---- 退出\t 输入 1----按职工号查询\t 输入 2-----按职工姓名查询\n");
printf("请选择:");
scanf("%d", &sel);
if (sel == 1)
{
printf("\n请输入你要查询分类的职工号:");
scanf("%s", find);
while (p!=NULL)
{
if (strcmp(p->data.jobNo, find) == 0)
{
printf(" 职工号 姓名 性别 出生年月 学历 职务 工资 住址 电话 \n ");
printf(" \n %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t\n ", p->data.jobNo, p->data.name,
p->data.sex, p->data.birthday, p->data.degree, p->data.position,
p->data.salary, p->data.addr, p->data.tel);
}
p = p->next;
}
}
else if (sel == 2)
{
printf(" \n输入你要查询分类的职工姓名:");
scanf("%s", find);
while (p!=NULL)
{
if (strcmp(p->data.name, find) == 0)
{
printf(" 职工号 姓名 性别 出生年月 学历 职务 工资 住址 电话 \n ");
printf(" \n %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t \n ", p->data.jobNo, p->data.name,
p->data.sex, p->data.birthday, p->data.degree, p->data.position,
p->data.salary, p->data.addr, p->data.tel);
}
p = p->next;
}
}
else if (sel == 0)
return 0;
}
//删除职工信息函数
void Del(Node *worker)
{
Node *p, *r;
char find[10];
p = worker->next;
if (p == NULL)
{
printf("提示:没有职工信息可以删除!\n ");
return 0;
}
printf(" \n提示:请输入你要删除的职工号!\n ");
scanf("%s", find);
while (p != NULL)
{
if (strcmp(p->data.jobNo, find) == 0)
{
break;
}
p = p->next;
}
if (p!=NULL)
{
r = worker;
while (r->next != p)
{
r = r->next;
}
r->next = r->next->next;
}
}
//将职工信息保存到文件中
void save(Node *worker)
{
int n;
Node *p = NULL;
FILE *fp;
printf("是否保存到文件?( 1-----保存!,0-----不保存! )");
scanf("%d", &n);
if (n == 1)
{
if (worker->next == NULL)
{
printf(" 没有记录!");
}
else
{
p = worker->next;
}
if ((fp = fopen("职工信息管理系统.txt", " wb ")) == NULL)
{
printf(" 文件不能打开!\n");
}
while (p != NULL)
{
fprintf(fp, "%s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t", p->data.jobNo, p->data.name, p->data.sex,
p->data.birthday, p->data.degree, p->data.position, p->data.salary,
p->data.addr, p->data.tel);
p = p->next;
}
fclose(fp);
}
}
//读取职工信息
void Read(Node *worker)
{
Node *p;
FILE *fp;
if ((fp = fopen("职工信息管理系统.txt", "rb")) == NULL)
{
printf("文件不能打开!\n ");
}
p = worker->next;
while (p!=NULL)
{
fscanf(fp, " %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t %s\t ", p->data.jobNo, p->data.name, p->data.sex,
p->data.birthday, p->data.degree, p->data.position, p->data.salary,
p->data.addr, p->data.tel);
p=p->next;
}
fclose(fp);
}
//主函数实现
int main()
{
Node *worker;
int flag;
worker = (Node*)malloc(sizeof(Node));
worker->next = NULL;
while(1)
{
printf("\t职 工 信 息 管 理 系 统\n");
printf("==================菜 单==================\n ");
printf("1.输入职工信息\n ");
printf("2.修改职工信息\n ");
printf("3.浏览职工信息\n ");
printf("4.查询职工信息\n ");
printf("5.删除职工信息\n ");
printf("6.读取职工信息\n ");
printf("0.退出\n ");
printf("输入你选择的序号:");
scanf("%d", &flag);
switch (flag)
{
case 0: printf(" \n提示:退出系统!\n "); break;
case 1: Add(worker); save(worker); break; //添加职工信息,保存到文件中
case 2: Modify(worker); break; //修改职工信息
case 3: Display(worker); break; //显示职工信息
case 4: Search(worker); break; //查询职工信息
case 5: Del(worker); break; //删除职工信息
case 6: Read(worker); break; //读取职工信息
default: printf("提示:输入错误!\n ");
}
}
}