做一个c语言程序要求五、手机通沸录程序设计 总体需求 具有数据插入、修改、删涂、显示、查词和烧计功能的电话薄管理程序。基本功能: 人数不定,数据使用文件存放. 1.记录每位联系人的学号、姓名、性别、工作单位、电话号码和E-mail地址建立单独的条目,存入数据 文件。 2.可对讯录中的性名和电话号码等进行修改。 .可增加或删涂记录 4.习显示所有保存的录5.加入统计功能,可以统计男女或总人数。 6. 加入查间功能,通过性名、学号或电话号码查询到同学的条目. 在开始画面加入简单的菜单便于远择功能。文件格式1.储存数振文件格式 编号姓名性别工作单位电话号码E-mail I李四……,….
时间: 2024-02-01 21:15:47 浏览: 144
好的,这个程序看起来比较简单,需要用到C语言的文件操作和基本的数据结构。以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_WORKPLACE_LEN 100
#define MAX_EMAIL_LEN 100
#define MAX_PHONE_NUM_LEN 20
typedef struct {
int student_id;
char name[MAX_NAME_LEN];
char gender;
char workplace[MAX_WORKPLACE_LEN];
char phone_num[MAX_PHONE_NUM_LEN];
char email[MAX_EMAIL_LEN];
} Contact;
void print_menu();
void insert_contact(FILE *fp);
void modify_contact(FILE *fp);
void delete_contact(FILE *fp);
void display_contacts(FILE *fp);
void count_contacts(FILE *fp);
void search_contacts(FILE *fp);
int main() {
FILE *fp;
int choice;
fp = fopen("contacts.dat", "rb+");
if (fp == NULL) {
fp = fopen("contacts.dat", "wb+");
if (fp == NULL) {
printf("Failed to open file!\n");
return 1;
}
}
do {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
insert_contact(fp);
break;
case 2:
modify_contact(fp);
break;
case 3:
delete_contact(fp);
break;
case 4:
display_contacts(fp);
break;
case 5:
count_contacts(fp);
break;
case 6:
search_contacts(fp);
break;
case 7:
printf("Thanks for using the program!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 7);
fclose(fp);
return 0;
}
void print_menu() {
printf("--------------------\n");
printf("1. Insert contact\n");
printf("2. Modify contact\n");
printf("3. Delete contact\n");
printf("4. Display contacts\n");
printf("5. Count contacts\n");
printf("6. Search contacts\n");
printf("7. Exit\n");
printf("--------------------\n");
}
void insert_contact(FILE *fp) {
Contact c;
printf("Enter student ID: ");
scanf("%d", &c.student_id);
printf("Enter name: ");
scanf("%s", c.name);
printf("Enter gender (M/F): ");
scanf(" %c", &c.gender);
printf("Enter workplace: ");
scanf("%s", c.workplace);
printf("Enter phone number: ");
scanf("%s", c.phone_num);
printf("Enter email: ");
scanf("%s", c.email);
fseek(fp, 0, SEEK_END);
fwrite(&c, sizeof(Contact), 1, fp);
printf("Contact added!\n");
}
void modify_contact(FILE *fp) {
int id;
Contact c;
int found = 0;
printf("Enter student ID to modify: ");
scanf("%d", &id);
rewind(fp);
while (fread(&c, sizeof(Contact), 1, fp) == 1) {
if (c.student_id == id) {
printf("Enter new name (or press Enter to keep the old one): ");
scanf("\n%[^\n]", c.name);
printf("Enter new gender (M/F or press Enter to keep the old one): ");
scanf(" %c", &c.gender);
printf("Enter new workplace (or press Enter to keep the old one): ");
scanf("\n%[^\n]", c.workplace);
printf("Enter new phone number (or press Enter to keep the old one): ");
scanf("%s", c.phone_num);
printf("Enter new email (or press Enter to keep the old one): ");
scanf("%s", c.email);
fseek(fp, -sizeof(Contact), SEEK_CUR);
fwrite(&c, sizeof(Contact), 1, fp);
found = 1;
break;
}
}
if (!found) {
printf("Contact not found!\n");
} else {
printf("Contact modified!\n");
}
}
void delete_contact(FILE *fp) {
int id;
Contact c;
int found = 0;
printf("Enter student ID to delete: ");
scanf("%d", &id);
rewind(fp);
FILE *temp_fp = fopen("temp.dat", "wb");
while (fread(&c, sizeof(Contact), 1, fp) == 1) {
if (c.student_id != id) {
fwrite(&c, sizeof(Contact), 1, temp_fp);
} else {
found = 1;
}
}
fclose(temp_fp);
if (!found) {
printf("Contact not found!\n");
remove("temp.dat");
} else {
printf("Contact deleted!\n");
remove("contacts.dat");
rename("temp.dat", "contacts.dat");
}
}
void display_contacts(FILE *fp) {
Contact c;
rewind(fp);
printf("%-10s %-20s %-10s %-30s %-20s %-30s\n", "ID", "Name", "Gender", "Workplace", "Phone Number", "Email");
while (fread(&c, sizeof(Contact), 1, fp) == 1) {
printf("%-10d %-20s %-10c %-30s %-20s %-30s\n", c.student_id, c.name, c.gender, c.workplace, c.phone_num, c.email);
}
}
void count_contacts(FILE *fp) {
Contact c;
rewind(fp);
int male_count = 0, female_count = 0, total_count = 0;
while (fread(&c, sizeof(Contact), 1, fp) == 1) {
if (c.gender == 'M') {
male_count++;
} else if (c.gender == 'F') {
female_count++;
}
total_count++;
}
printf("Total contacts: %d\n", total_count);
printf("Male contacts: %d\n", male_count);
printf("Female contacts: %d\n", female_count);
}
void search_contacts(FILE *fp) {
int choice;
Contact c;
int found = 0;
printf("Search by:\n");
printf("1. Student ID\n");
printf("2. Name\n");
printf("3. Phone Number\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter student ID to search: ");
scanf("%d", &c.student_id);
break;
case 2:
printf("Enter name to search: ");
scanf("%s", c.name);
break;
case 3:
printf("Enter phone number to search: ");
scanf("%s", c.phone_num);
break;
default:
printf("Invalid choice!\n");
return;
}
rewind(fp);
printf("%-10s %-20s %-10s %-30s %-20s %-30s\n", "ID", "Name", "Gender", "Workplace", "Phone Number", "Email");
while (fread(&c, sizeof(Contact), 1, fp) == 1) {
if ((choice == 1 && c.student_id == c.student_id) ||
(choice == 2 && strcmp(c.name, c.name) == 0) ||
(choice == 3 && strcmp(c.phone_num, c.phone_num) == 0)) {
printf("%-10d %-20s %-10c %-30s %-20s %-30s\n", c.student_id, c.name, c.gender, c.workplace, c.phone_num, c.email);
found = 1;
}
}
if (!found) {
printf("No contacts found!\n");
}
}
```
程序使用了一个结构体 `Contact` 来保存每位联系人的信息,同时使用了文件来保存所有联系人的信息。在主函数中,打开文件并根据用户的选择执行不同的功能。以下是每个功能的具体实现:
1. `insert_contact`:让用户输入新联系人的信息,并将其添加到文件中。
2. `modify_contact`:让用户输入要修改的联系人的学号,然后查找文件中是否有该联系人。如果找到,让用户输入新信息,并将其更新到文件中。
3. `delete_contact`:让用户输入要删除的联系人的学号,然后查找文件中是否有该联系人。如果找到,将其从文件中删除。
4. `display_contacts`:在屏幕上显示所有联系人的信息。
5. `count_contacts`:统计所有联系人的数量以及男女联系人的数量,并在屏幕上显示。
6. `search_contacts`:让用户选择按照学号、姓名或电话号码查询联系人,然后在文件中查找并在屏幕上显示符合条件的联系人。
程序使用了文件操作函数 `fopen`、`fread`、`fwrite`、`fseek`、`fclose`、`remove` 和 `rename`,以及字符串操作函数 `scanf` 和 `printf`。其中,`scanf` 和 `printf` 的格式化字符串需要根据不同的数据类型进行调整,否则可能会导致输入输出错误。程序还使用了一些基本的流程控制语句和数据结构,需要熟悉这些基本知识才能理解和编写这个程序。
阅读全文