请用C语言写一个包含头文件和源文件的通讯录管理系统的项目范例,约300行
时间: 2024-03-10 15:50:04 浏览: 75
c语言写的通讯录管理系统
4星 · 用户满意度95%
由于300行的范例可能比较长,这里简单给出一个包含头文件和源文件的通讯录管理系统的框架,供您参考:
头文件 contacts.h:
```c
#ifndef CONTACTS_H
#define CONTACTS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 15
#define MAX_EMAIL_LEN 30
#define MAX_CONTACTS 100
typedef struct {
char name[MAX_NAME_LEN+1];
char phone[MAX_PHONE_LEN+1];
char email[MAX_EMAIL_LEN+1];
} Contact;
Contact contacts[MAX_CONTACTS];
int num_contacts;
void load_contacts();
void save_contacts();
void print_contact(Contact c);
void print_contacts();
void add_contact(char *name, char *phone, char *email);
void delete_contact(int index);
void search_contact(char *keyword);
#endif
```
源文件 contacts.c:
```c
#include "contacts.h"
void load_contacts() {
FILE *fp = fopen("contacts.dat", "rb");
if (fp == NULL) {
num_contacts = 0;
return;
}
fread(&num_contacts, sizeof(int), 1, fp);
fread(contacts, sizeof(Contact), num_contacts, fp);
fclose(fp);
}
void save_contacts() {
FILE *fp = fopen("contacts.dat", "wb");
fwrite(&num_contacts, sizeof(int), 1, fp);
fwrite(contacts, sizeof(Contact), num_contacts, fp);
fclose(fp);
}
void print_contact(Contact c) {
printf("Name: %s\n", c.name);
printf("Phone: %s\n", c.phone);
printf("Email: %s\n", c.email);
}
void print_contacts() {
int i;
for (i = 0; i < num_contacts; i++) {
printf("Contact %d:\n", i+1);
print_contact(contacts[i]);
printf("\n");
}
}
void add_contact(char *name, char *phone, char *email) {
if (num_contacts >= MAX_CONTACTS) {
printf("Error: maximum number of contacts reached!\n");
return;
}
Contact c;
strcpy(c.name, name);
strcpy(c.phone, phone);
strcpy(c.email, email);
contacts[num_contacts++] = c;
printf("Contact added successfully!\n");
}
void delete_contact(int index) {
if (index < 1 || index > num_contacts) {
printf("Error: invalid index!\n");
return;
}
int i;
for (i = index-1; i < num_contacts-1; i++) {
contacts[i] = contacts[i+1];
}
num_contacts--;
printf("Contact deleted successfully!\n");
}
void search_contact(char *keyword) {
int i;
printf("Search results:\n");
for (i = 0; i < num_contacts; i++) {
if (strstr(contacts[i].name, keyword) != NULL ||
strstr(contacts[i].phone, keyword) != NULL ||
strstr(contacts[i].email, keyword) != NULL) {
printf("Contact %d:\n", i+1);
print_contact(contacts[i]);
printf("\n");
}
}
}
```
主程序 main.c:
```c
#include "contacts.h"
int main() {
load_contacts();
int choice;
char name[MAX_NAME_LEN+1];
char phone[MAX_PHONE_LEN+1];
char email[MAX_EMAIL_LEN+1];
int index;
char keyword[MAX_NAME_LEN+1];
do {
printf("1. Add contact\n");
printf("2. Delete contact\n");
printf("3. Search contact\n");
printf("4. Print all contacts\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter name: ");
scanf("%s", name);
printf("Enter phone: ");
scanf("%s", phone);
printf("Enter email: ");
scanf("%s", email);
add_contact(name, phone, email);
break;
case 2:
printf("Enter index: ");
scanf("%d", &index);
delete_contact(index);
break;
case 3:
printf("Enter keyword: ");
scanf("%s", keyword);
search_contact(keyword);
break;
case 4:
print_contacts();
break;
case 5:
save_contacts();
printf("Goodbye!\n");
break;
default:
printf("Error: invalid choice!\n");
break;
}
} while (choice != 5);
return 0;
}
```
该通讯录管理系统主要功能包括添加联系人、删除联系人、查找联系人和打印所有联系人。通讯录数据存储在 contacts.dat 文件中,每次程序启动时会从文件中读取数据并加载到内存中;程序结束时会将内存中的数据保存到文件中。
阅读全文