通讯录管理系统升级版,用结构体式,要求先完成注册,再登录,进入该用户的系统进行添加、删除、修改。写出以上的c语言代码
时间: 2024-05-03 14:18:28 浏览: 59
以下是一个简单的通讯录管理系统升级版的C语言代码实现,使用了结构体来存储联系人信息:
```c
#include <stdio.h>
#include <string.h>
struct Contact {
char name[20];
char phone[12];
char email[30];
};
int main() {
struct Contact contacts[100];
int num_contacts = 0;
int choice;
while (1) {
printf("\nMenu:\n");
printf("1. Register\n");
printf("2. Login\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("\nEnter your name: ");
scanf("%s", contacts[num_contacts].name);
printf("Enter your phone number: ");
scanf("%s", contacts[num_contacts].phone);
printf("Enter your email address: ");
scanf("%s", contacts[num_contacts].email);
printf("\nRegistration successful!\n");
num_contacts++;
}
else if (choice == 2) {
char name[20];
char phone[12];
char email[30];
int found = 0;
printf("\nEnter your name: ");
scanf("%s", name);
printf("Enter your phone number: ");
scanf("%s", phone);
printf("Enter your email address: ");
scanf("%s", email);
for (int i = 0; i < num_contacts; i++) {
if (strcmp(name, contacts[i].name) == 0 &&
strcmp(phone, contacts[i].phone) == 0 &&
strcmp(email, contacts[i].email) == 0) {
found = 1;
printf("\nWelcome, %s!\n", name);
while (1) {
int action;
printf("\nMenu:\n");
printf("1. Add contact\n");
printf("2. Delete contact\n");
printf("3. Edit contact\n");
printf("4. View all contacts\n");
printf("5. Logout\n");
printf("Enter your choice: ");
scanf("%d", &action);
if (action == 1) {
printf("\nEnter contact name: ");
scanf("%s", contacts[num_contacts].name);
printf("Enter contact phone number: ");
scanf("%s", contacts[num_contacts].phone);
printf("Enter contact email address: ");
scanf("%s", contacts[num_contacts].email);
num_contacts++;
printf("\nContact added successfully!\n");
}
else if (action == 2) {
char name[20];
printf("\nEnter contact name to delete: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_contacts; i++) {
if (strcmp(name, contacts[i].name) == 0) {
found = 1;
for (int j = i; j < num_contacts - 1; j++) {
contacts[j] = contacts[j + 1];
}
num_contacts--;
printf("\nContact deleted successfully!\n");
break;
}
}
if (!found) {
printf("\nContact not found!\n");
}
}
else if (action == 3) {
char name[20];
printf("\nEnter contact name to edit: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_contacts; i++) {
if (strcmp(name, contacts[i].name) == 0) {
found = 1;
printf("\nEnter new phone number: ");
scanf("%s", contacts[i].phone);
printf("Enter new email address: ");
scanf("%s", contacts[i].email);
printf("\nContact updated successfully!\n");
break;
}
}
if (!found) {
printf("\nContact not found!\n");
}
}
else if (action == 4) {
if (num_contacts == 0) {
printf("\nNo contacts found!\n");
}
else {
printf("\nContacts:\n");
for (int i = 0; i < num_contacts; i++) {
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("Email: %s\n\n", contacts[i].email);
}
}
}
else if (action == 5) {
printf("\nLogged out successfully!\n");
break;
}
else {
printf("\nInvalid choice! Please try again.\n");
}
}
break;
}
}
if (!found) {
printf("\nInvalid credentials! Please try again.\n");
}
}
else if (choice == 3) {
printf("\nGoodbye!\n");
break;
}
else {
printf("\nInvalid choice! Please try again.\n");
}
}
return 0;
}
```
这个程序使用了一个结构体数组来存储联系人信息,每个联系人包含名字、电话号码和电子邮件地址。程序的主要逻辑是在一个无限循环中,不断显示菜单并等待用户输入选项。如果用户选择注册,程序会要求他们输入他们的姓名、电话号码和电子邮件地址,并将这些信息添加到结构体数组中。如果用户选择登录,程序会要求他们输入他们的姓名、电话号码和电子邮件地址,并搜索结构体数组以查找匹配的联系人。如果找到一个匹配的联系人,程序会显示一个新的菜单,允许用户添加、删除、编辑和查看他们的联系人。最后,如果用户选择退出,程序会结束。
阅读全文