实验思路C++应用实验 利用线性表实现一个通讯录管理,通讯录的数据格式如下: struct DataType int ID; //编号 char name[10]; //姓名 char ch; //性别 char phone[13]; //电话 char addr[31]; //地址 要求: ·实现通讯录的建立、增加、删除、修改、查询等功能; ·能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作; ·能够保存每次更新的数据(选做); ·能够进行通讯录分类,比如班级类、好友类、黑名单等(选做); ·编写main()函数测试线性表的正确性。
时间: 2023-05-19 21:03:43 浏览: 120
这是一个很好的编程问题,可以使用线性表来实现通讯录管理。可以使用结构体来存储通讯录的数据,然后使用链表或数组来实现线性表。可以使用菜单交互来实现用户输入命令的选择,然后根据用户的选择来执行不同的操作。可以使用文件来保存每次更新的数据,也可以进行通讯录分类。最后,可以编写main()函数来测试线性表的正确性。如果您需要更具体的实现细节,可以参考相关的编程教程或者咨询相关的编程专家。
相关问题
C++应用实验 利用线性表实现一个通讯录管理,通讯录的数据格式如下: struct DataType int ID; //编号 char name[10]; //姓名 char ch; //性别 char phone[13]; //电话 char addr[31]; //地址 要求: ·实现通讯录的建立、增加、删除、修改、查询等功能; ·能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作; ·能够保存每次更新的数据(选做); ·能够进行通讯录分类,比如班级类、好友类、黑名单等(选做); ·编写main()函数测试线性表的正确性。
以下是一个简单的通讯录管理程序的实现,使用线性表来存储通讯录数据:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 10
#define MAX_PHONE_LEN 13
#define MAX_ADDR_LEN 31
struct DataType {
int ID;
char name[MAX_NAME_LEN];
char ch;
char phone[MAX_PHONE_LEN];
char addr[MAX_ADDR_LEN];
};
typedef struct Node {
struct DataType data;
struct Node *next;
} Node;
Node *createList() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = NULL;
return head;
}
void destroyList(Node *head) {
Node *p = head->next;
while (p != NULL) {
Node *q = p->next;
free(p);
p = q;
}
free(head);
}
void printList(Node *head) {
Node *p = head->next;
while (p != NULL) {
printf("%d %s %c %s %s\n", p->data.ID, p->data.name, p->data.ch, p->data.phone, p->data.addr);
p = p->next;
}
}
Node *findNode(Node *head, int ID) {
Node *p = head->next;
while (p != NULL) {
if (p->data.ID == ID) {
return p;
}
p = p->next;
}
return NULL;
}
void insertNode(Node *head, struct DataType data) {
Node *p = (Node *)malloc(sizeof(Node));
p->data = data;
p->next = head->next;
head->next = p;
}
void deleteNode(Node *head, int ID) {
Node *p = head->next;
Node *prev = head;
while (p != NULL) {
if (p->data.ID == ID) {
prev->next = p->next;
free(p);
return;
}
prev = p;
p = p->next;
}
}
void updateNode(Node *head, int ID, struct DataType data) {
Node *p = findNode(head, ID);
if (p != NULL) {
p->data = data;
}
}
void queryNode(Node *head, char *name) {
Node *p = head->next;
while (p != NULL) {
if (strcmp(p->data.name, name) == 0) {
printf("%d %s %c %s %s\n", p->data.ID, p->data.name, p->data.ch, p->data.phone, p->data.addr);
}
p = p->next;
}
}
int main() {
Node *head = createList();
while (1) {
printf("1. Add a contact\n");
printf("2. Delete a contact\n");
printf("3. Update a contact\n");
printf("4. Query a contact\n");
printf("5. Print all contacts\n");
printf("6. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
if (choice == 1) {
struct DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter gender (M/F): ");
scanf(" %c", &data.ch);
printf("Enter phone number: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
insertNode(head, data);
} else if (choice == 2) {
int ID;
printf("Enter ID: ");
scanf("%d", &ID);
deleteNode(head, ID);
} else if (choice == 3) {
int ID;
printf("Enter ID: ");
scanf("%d", &ID);
struct DataType data;
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter gender (M/F): ");
scanf(" %c", &data.ch);
printf("Enter phone number: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
updateNode(head, ID, data);
} else if (choice == 4) {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
queryNode(head, name);
} else if (choice == 5) {
printList(head);
} else if (choice == 6) {
break;
} else {
printf("Invalid choice\n");
}
}
destroyList(head);
return 0;
}
注意,这个程序只是一个简单的示例,还有很多需要改进的地方,比如输入的合法性检查、数据的持久化存储等。
应用实验 利用线性表实现一个通讯录管理,通讯录的数据格式如下: struct DataType int ID; //编号 char name[10]; //姓名 char ch; //性别 char phone[13]; //电话 char addr[31]; //地址 要求: ·实现通讯录的建立、增加、删除、修改、查询等功能; ·能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作; ·能够保存每次更新的数据(选做); ·能够进行通讯录分类,比如班级类、好友类、黑名单等(选做); ·编写main()函数测试线性表的正确性。
以下是一个简单的通讯录管理程序的实现,使用线性表来存储通讯录数据:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 10
#define MAX_PHONE_LEN 13
#define MAX_ADDR_LEN 31
typedef struct {
int ID;
char name[MAX_NAME_LEN];
char ch;
char phone[MAX_PHONE_LEN];
char addr[MAX_ADDR_LEN];
} DataType;
typedef struct {
DataType *data;
int length;
int capacity;
} List;
void initList(List *list, int capacity) {
list->data = (DataType *)malloc(sizeof(DataType) * capacity);
list->length = 0;
list->capacity = capacity;
}
void destroyList(List *list) {
free(list->data);
list->data = NULL;
list->length = 0;
list->capacity = 0;
}
int insertList(List *list, int pos, DataType data) {
if (pos < 0 || pos > list->length) {
return 0;
}
if (list->length >= list->capacity) {
return 0;
}
for (int i = list->length - 1; i >= pos; i--) {
list->data[i + 1] = list->data[i];
}
list->data[pos] = data;
list->length++;
return 1;
}
int deleteList(List *list, int pos) {
if (pos < 0 || pos >= list->length) {
return 0;
}
for (int i = pos; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
return 1;
}
int updateList(List *list, int pos, DataType data) {
if (pos < 0 || pos >= list->length) {
return 0;
}
list->data[pos] = data;
return 1;
}
int searchList(List *list, char *name) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->data[i].name, name) == 0) {
return i;
}
}
return -1;
}
void printList(List *list) {
printf("ID\tName\tSex\tPhone\t\tAddress\n");
for (int i = 0; i < list->length; i++) {
printf("%d\t%s\t%c\t%s\t%s\n", list->data[i].ID, list->data[i].name, list->data[i].ch, list->data[i].phone, list->data[i].addr);
}
}
void menu() {
printf("1. Add a contact\n");
printf("2. Delete a contact\n");
printf("3. Update a contact\n");
printf("4. Search a contact\n");
printf("5. Print all contacts\n");
printf("0. Exit\n");
}
int main() {
List list;
initList(&list, 100);
int choice = -1;
while (choice != 0) {
menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter sex: ");
scanf(" %c", &data.ch);
printf("Enter phone: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
if (insertList(&list, list.length, data)) {
printf("Contact added successfully.\n");
} else {
printf("Failed to add contact.\n");
}
break;
}
case 2: {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
int pos = searchList(&list, name);
if (pos == -1) {
printf("Contact not found.\n");
} else {
if (deleteList(&list, pos)) {
printf("Contact deleted successfully.\n");
} else {
printf("Failed to delete contact.\n");
}
}
break;
}
case 3: {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
int pos = searchList(&list, name);
if (pos == -1) {
printf("Contact not found.\n");
} else {
DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter sex: ");
scanf(" %c", &data.ch);
printf("Enter phone: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
if (updateList(&list, pos, data)) {
printf("Contact updated successfully.\n");
} else {
printf("Failed to update contact.\n");
}
}
break;
}
case 4: {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
int pos = searchList(&list, name);
if (pos == -1) {
printf("Contact not found.\n");
} else {
printf("ID\tName\tSex\tPhone\t\tAddress\n");
printf("%d\t%s\t%c\t%s\t%s\n", list.data[pos].ID, list.data[pos].name, list.data[pos].ch, list.data[pos].phone, list.data[pos].addr);
}
break;
}
case 5: {
printList(&list);
break;
}
case 0: {
break;
}
default: {
printf("Invalid choice.\n");
break;
}
}
}
destroyList(&list);
return 0;
}
阅读全文