以下是要求内容,感谢各位支招!
1.指定同学人数,逐个输入同学通讯信息;2.逐个显示同学通讯录中的信息;
3.实现对某个同学通讯信息的查询、修改、插入和删除;
第一部分项目二
4.统计通讯录中同学人数;
5.利用直接插入排序或者折半插入排序按照姓名进行排序;
6.根据姓名进行折半查找,要求使用递归算法实现,成功返回此同学的联系方式和工作单位。
基于new bing修改的编写:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUMBER 100
// 定义学生结构体
struct Student {
char name[20]; // 姓名
char phone[20]; // 电话号码
char email[50]; // 邮箱地址
char company[50]; // 工作单位
};
// 声明函数
void input_students(struct Student students[], int n);
void output_students(struct Student students[], int n);
void search_student(struct Student students[], int n, char name[]);
void modify_student(struct Student students[], int n, char name[]);
void insert_student(struct Student students[], int n, int index);
void delete_student(struct Student students[], int n, char name[]);
int count_students(struct Student students[], int n);
void sort_students(struct Student students[], int n);
int binary_search(struct Student students[], int low, int high, char name[]);
void menu() {
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("------------------9、折半查找------------------\n");
printf("------------------0、退出系统------------------\n");
}
int main() {
struct Student students[MAX_STUDENT_NUMBER];
int n=0; // 学生人数
char name[20]; // 要查询的学生姓名
int index; // 要插入的位置
int chose;
int result = -1; // 添加默认值
while (1) {
menu();
printf("请输入你的选择:");
scanf("%d", &chose);
getchar();
switch (chose)
{
case 1: // 输入学生人数
printf("请输入学生人数:");
scanf("%d", &n);
getchar();
// 输入学生信息
input_students(students, n);
break;
case 2:
// 输出学生信息
if (n == 0) { printf("暂无信息!\n"); break; }
printf("学生通讯录如下:\n");
output_students(students, n);
break;
case 3:
// 查询学生信息
if (n == 0) { printf("暂无信息!\n"); break; }
printf("请输入要查询的学生姓名:");
scanf("%s", name);
result = binary_search(students, 0, n - 1, name); // 初始化变量
if (result == -1) {
printf("查无此人!\n");
}
else {
printf("该学生的联系方式为:%s,工作单位为:%s\n", students[result].phone, students[result].company);
}
break;
case 4:
if (n == 0) { printf("暂无信息!\n"); break; }
// 修改学生信息
printf("请输入要修改的学生姓名:");
scanf("%s", name);
modify_student(students, n, name);
printf("修改后的学生通讯录如下:\n");
output_students(students, n);
break;
case 5:
// 插入学生信息
printf("请输入要插入的学生位置:");
scanf("%d", &index);
insert_student(students, n, index);
printf("插入后的学生通讯录如下:\n");
output_students(students, n);
break;
case 6:
if (n == 0) { printf("暂无信息!\n"); break; }
// 删除学生信息
printf("请输入要删除的学生姓名:");
scanf("%s", name);
delete_student(students, n, name);
printf("删除后的学生通讯录如下:\n");
output_students(students, n);
break;
case 7:
if (n == 0) { printf("暂无信息!\n"); break; }
// 统计学生人数
printf("学生人数为:%d\n", count_students(students, n));
break;
case 8:
if (n == 0) { printf("暂无信息!\n"); break; }
// 按姓名排序
sort_students(students, n);
printf("按姓名排序后的学生通讯录如下:\n");
output_students(students, n);
break;
case 9:
if (n == 0) { printf("暂无信息!\n"); break; }
// 折半查找学生信息
printf("请输入要查找的学生姓名:");
scanf("%s", name);
result = binary_search(students, 0, n - 1, name); // 初始化变量
if (result == -1) {
printf("查无此人!\n");
}
else {
printf("该学生的联系方式为:%s,工作单位为:%s\n", students[result].phone, students[result].company);
}
break;
case 0:
return 0;
default:
printf("输入非法!\n");
break;
}
}
return 0;
}
// 输入学生信息
void input_students(struct Student students[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("姓名:");
scanf("%s", students[i].name);
printf("电话号码:");
scanf("%s", students[i].phone);
printf("邮箱地址:");
scanf("%s", students[i].email);
printf("工作单位:");
scanf("%s", students[i].company);
}
}
// 输出学生信息
void output_students(struct Student students[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("姓名:%s,电话号码:%s,邮箱地址:%s,工作单位:%s\n", students[i].name, students[i].phone, students[i].email, students[i].company);
}
}
// 查询学生信息
void search_student(struct Student students[], int n, char name[]) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("该学生的联系方式为:%s,邮箱地址为:%s,工作单位为:%s\n", students[i].phone, students[i].email, students[i].company);
return;
}
}
printf("查无此人!\n");
}
// 修改学生信息
void modify_student(struct Student students[], int n, char name[]) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("请输入该学生的新信息:\n");
printf("电话号码:");
scanf("%s", students[i].phone);
printf("邮箱地址:");
scanf("%s", students[i].email);
printf("工作单位:");
scanf("%s", students[i].company);
return;
}
}
printf("查无此人!\n");
}
// 插入学生信息
void insert_student(struct Student students[], int n, int index) {
if (index < 1 || index > n + 1) {
printf("插入位置不合法!\n");
return;
}
struct Student new_student;
printf("请输入要插入的学生的信息:\n");
printf("姓名:");
scanf("%s", new_student.name);
printf("电话号码:");
scanf("%s", new_student.phone);
printf("邮箱地址:");
scanf("%s", new_student.email);
printf("工作单位:");
scanf("%s", new_student.company);
int i;
for (i = n - 1; i >= index - 1; i--) {
students[i + 1] = students[i];
}
students[index - 1] = new_student;
}
// 删除学生信息
void delete_student(struct Student students[], int n, char name[]) {
int i, j;
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
for (j = i; j < n - 1; j++) {
students[j] = students[j + 1];
}
return;
}
}
printf("查无此人!\n");
}
// 统计学生人数
int count_students(struct Student students[], int n) {
return n;
}
// 按姓名排序
void sort_students(struct Student students[], int n) {
int i, j;
struct Student temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (strcmp(students[i].name, students[j].name) > 0) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// 折半查找学生信息
int binary_search(struct Student students[], int low, int high, char name[]) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (strcmp(students[mid].name, name) == 0) {
return mid;
}
else if (strcmp(students[mid].name, name) > 0) {
return binary_search(students, low, mid - 1, name);
}
else {
return binary_search(students, mid + 1, high, name);
}
}
你是否有自己写过?仅仅在成绩管理系统上改动了下就能实现你的需求:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
string contact_info;
string work_unit;
};
vector<Student> student_list;
void add_student() {
Student new_student;
cout << "请输入姓名:";
cin >> new_student.name;
cout << "请输入联系方式:";
cin >> new_student.contact_info;
cout << "请输入工作单位:";
cin >> new_student.work_unit;
student_list.push_back(new_student);
cout << "添加成功!" << endl;
}
void display_student() {
if (student_list.empty()) {
cout << "通讯录为空!" << endl;
return;
}
for (int i = 0; i < student_list.size(); i++) {
cout << "姓名:" << student_list[i].name << "\t联系方式:" << student_list[i].contact_info
<< "\t工作单位:" << student_list[i].work_unit << endl;
}
}
void query_student() {
string name;
cout << "请输入要查询的学生姓名:";
cin >> name;
for (int i = 0; i < student_list.size(); i++) {
if (student_list[i].name == name) {
cout << "姓名:" << student_list[i].name << "\\t联系方式:" << student_list[i].contact_info
<< "\t工作单位:" << student_list[i].work_unit << endl;
return;
}
}
cout << "未找到该学生信息!" << endl;
}
void modify_student() {
string name;
cout << "请输入要修改的学生姓名:";
cin >> name;
for (int i = 0; i < student_list.size(); i++) {
if (student_list[i].name == name) {
cout << "请输入新的联系方式:";
cin >> student_list[i].contact_info;
cout << "请输入新的工作单位:";
cin >> student_list[i].work_unit;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该学生信息!" << endl;
}
void delete_student() {
string name;
cout << "请输入要删除的学生姓名:";
cin >> name;
for (int i = 0; i < student_list.size(); i++) {
if (student_list[i].name == name) {
student_list.erase(student_list.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生信息!" << endl;
}
void count_student() {
cout << "通讯录中共有 " << student_list.size() << " 位学生。" << endl;
}
void sort_student() {
sort(student_list.begin(), student_list.end(), [](const Student& a, const Student& b) {
return a.name < b.name;
});
cout << "按照姓名排序成功!" << endl;
}
void binary_search_student(int left, int right, string name) {
if (left > right) {
cout << "未找到该学生信息!" << endl;
return;
}
int mid = (left + right) / 2;
if (student_list[mid].name == name) {
cout << "姓名:" << student_list[mid].name << "\t联系方式:" << student_list[mid].contact_info
<< "\t工作单位:" << student_list[mid].work_unit << endl;
return;
} else if (student_list[mid].name > name) {
binary_search_student(left, mid - 1, name);
} else {
binary_search_student(mid + 1, right, name);
}
}
void search_student() {
string name;
cout << "请输入要查找的学生姓名:";
cin >> name;
sort(student_list.begin(), student_list.end(), [](const Student& a, const Student& b) {
return a.name < b.name;
});
binary_search_student(0, student_list.size() - 1, name);
}
int main() {
int choice;
while (true) {
cout << "请选择要进行的操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 显示学生" << endl;
cout << "3. 查询学生" << endl;
cout << "4. 修改学生" << endl;
cout << "5. 删除学生" << endl;
cout << "6. 统计学生数量" << endl;
cout << "7. 按照姓名排序" << endl;
cout << "8. 根据姓名查找学生信息" << endl;
cout << "0. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
add_student();
break;
case 2:
display_student();
break;
case 3:
query_student();
break;
case 4:
modify_student();
break;
case 5:
delete_student();
break;
case 6:
count_student();
break;
case 7:
sort_student();
break;
case 8:
search_student();
break;
case 0:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 同学通讯信息结构体
struct Student {
string name;
string contact;
string workplace;
};
// 逐个输入同学通讯信息
vector<Student> inputContacts(int numStudents) {
vector<Student> contacts;
for (int i = 0; i < numStudents; i++) {
Student student;
cout << "请输入同学姓名: ";
cin >> student.name;
cout << "请输入同学联系方式: ";
cin >> student.contact;
cout << "请输入同学工作单位: ";
cin >> student.workplace;
contacts.push_back(student);
}
return contacts;
}
// 逐个显示同学通讯录中的信息
void displayContacts(const vector<Student>& contacts) {
cout << "通讯录中的同学信息:" << endl;
for (const auto& student : contacts) {
cout << "姓名: " << student.name << " 联系方式: " << student.contact
<< " 工作单位: " << student.workplace << endl;
}
}
// 查询同学通讯信息
void queryContact(vector<Student>& contacts, const string& name) {
for (auto& student : contacts) {
if (student.name == name) {
cout << "姓名: " << student.name << " 联系方式: " << student.contact
<< " 工作单位: " << student.workplace << endl;
return;
}
}
cout << "通讯录中无此同学信息" << endl;
}
// 修改同学通讯信息
void modifyContact(vector<Student>& contacts, const string& name, const string& newContact, const string& newWorkplace) {
for (auto& student : contacts) {
if (student.name == name) {
student.contact = newContact;
student.workplace = newWorkplace;
cout << "修改成功" << endl;
return;
}
}
cout << "通讯录中无此同学信息" << endl;
}
// 插入同学通讯信息
void insertContact(vector<Student>& contacts, const Student& student) {
contacts.push_back(student);
cout << "插入成功" << endl;
}
// 删除同学通讯信息
void deleteContact(vector<Student>& contacts, const string& name) {
for (auto it = contacts.begin(); it != contacts.end(); ++it) {
if (it->name == name) {
contacts.erase(it);
cout << "删除成功" << endl;
return;
}
}
cout << "通讯录中无此同学信息" << endl;
}
/ 统计通讯录中同学人数
int countContacts(const vector<Student>& contacts) {
return contacts.size();
}
// 直接插入排序按照姓名进行排序
void sortContacts(vector<Student>& contacts) {
sort(contacts.begin(), contacts.end(), [](const Student& a, const Student& b) {
return a.name < b.name;
});
cout << "通讯录已按姓名排序" << endl;
}
// 折半查找同学通讯信息
string binarySearch(const vector<Student>& contacts, const string& name, int left, int right) {
if (left > right) {
return "通讯录中无此同学信息";
}
int mid = (left + right) / 2;
if (contacts[mid].name == name) {
return "姓名: " + contacts[mid].name + " 联系方式: " + contacts[mid].contact
+ " 工作单位: " + contacts[mid].workplace;
}
else if (contacts[mid].name < name) {
return binarySearch(contacts, name, mid + 1, right);
}
else {
return binarySearch(contacts, name, left, mid - 1);
}
}
int main() {
int numStudents;
cout << "请输入同学人数: ";
cin >> numStudents;
vector<Student> contacts = inputContacts(numStudents);
cout << endl;
displayContacts(contacts);
cout << endl;
string queryName;
cout << "请输入要查询的同学姓名: ";
cin >> queryName;
queryContact(contacts, queryName);
cout << endl;
string modifyName, newContact, newWorkplace;
cout << "请输入要修改信息的同学姓名: ";
cin >> modifyName;
cout << "请输入新的联系方式: ";
cin >> newContact;
cout << "请输入新的工作单位: ";
cin >> newWorkplace;
modifyContact(contacts, modifyName, newContact, newWorkplace);
cout << endl;
Student newStudent;
cout << "请输入要插入的同学信息: " << endl;
cout << "姓名: ";
cin >> newStudent.name;
cout << "联系方式: ";
cin >> newStudent.contact;
cout << "工作单位: ";
cin >> newStudent.workplace;
insertContact(contacts, newStudent);
cout << endl;
string deleteName;
cout << "请输入要删除的同学姓名: ";
cin >> deleteName;
deleteContact(contacts, deleteName);
cout << endl;
int numContacts = countContacts(contacts);
cout << "通讯录中同学人数为: " << numContacts << endl;
cout << endl;
sortContacts(contacts);
cout << endl;
string searchName;
cout << "请输入要查找的同学姓名: ";
cin >> searchName;
string searchResult = binarySearch(contacts, searchName, 0, numContacts - 1);
cout << searchResult << endl;
return 0;
}
这是一个可以通过控制台程序实现的简单通讯录功能,可以参考下面的代码实现。
首先,定义一个 Contact 类来表示通讯录中的每一个同学,包括姓名、电话和工作单位等信息,如下所示:
class Contact
{
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}
然后,在主函数中,根据指定的同学人数依次输入同学信息,并存储到一个 List 中。具体实现如下:
static void Main(string[] args)
{
List<Contact> contacts = new List<Contact>();
Console.Write("请输入同学人数:");
int count = int.Parse(Console.ReadLine());
for (int i = 1; i <= count; i++)
{
Console.WriteLine($"请输入第 {i} 位同学的信息:");
Console.Write("姓名:");
string name = Console.ReadLine();
Console.Write("电话:");
string phone = Console.ReadLine();
Console.Write("工作单位:");
string address = Console.ReadLine();
Contact contact = new Contact
{
Name = name,
Phone = phone,
Address = address
};
contacts.Add(contact);
}
// ...
}
接着,通过一个循环依次显示通讯录中每个同学的信息,实现逐个显示通讯录功能。具体代码如下:
foreach (Contact contact in contacts)
{
Console.WriteLine($"姓名:{contact.Name}");
Console.WriteLine($"电话:{contact.Phone}");
Console.WriteLine($"工作单位:{contact.Address}");
Console.WriteLine();
}
对于查询、修改、插入和删除操作,可以通过 LINQ 查询操作实现。例如,查询某个同学的联系方式和工作单位信息:
Console.Write("请输入要查询的同学姓名:");
string queryName = Console.ReadLine();
var queryResult = contacts.Where(c => c.Name == queryName);
if (queryResult.Count() == 0)
{
Console.WriteLine("没有找到该同学的信息。");
}
else
{
Console.WriteLine($"姓名:{queryResult.First().Name}");
Console.WriteLine($"电话:{queryResult.First().Phone}");
Console.WriteLine($"工作单位:{queryResult.First().Address}");
}
插入和删除操作可以通过 List 的 Add 和 Remove 方法实现
看结果:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// 联系人信息类
struct Person {
string name; // 姓名
string phone; // 电话
string company; // 工作单位
};
// 联系人列表
vector<Person> persons;
// 查找联系人
Person search(string name, int left, int right) {
if (left > right) return Person(); // 未找到
int mid = (left + right) / 2;
if (persons[mid].name == name) return persons[mid];
else if (persons[mid].name < name)
return search(name, mid + 1, right);
else
return search(name, left, mid - 1);
}
int main() {
// 1. 输入联系人数量和信息
cout << "请输入联系人数量:";
int n;
cin >> n;
for (int i = 0; i < n; i++) {
Person p;
cout << "请输入联系人姓名:";
cin >> p.name;
cout << "请输入联系人电话:";
cin >> p.phone;
cout << "请输入联系人工作单位:";
cin >> p.company;
persons.push_back(p);
}
// 2. 显示联系人信息
for (auto p : persons) {
cout << p.name << " " << p.phone << " " << p.company << endl;
}
// 3. 查询、修改、插入和删除联系人
while (true) {
cout << "请选择操作(1.查询 2.修改 3.插入 4.删除 0.退出):";
int choice;
cin >> choice;
if (choice == 0) break;
if (choice == 1) { // 查询
cout << "请输入联系人姓名:";
string name;
cin >> name;
Person p = search(name, 0, persons.size() - 1);
if (p.name != "") cout << p.name << " " << p.phone << " " << p.company << endl;
else cout << "联系人不存在!" << endl;
}
// ...
}
// 4. 统计并显示联系人数量
cout << "联系人数量:" << persons.size() << endl;
// 5. 按姓名排序
sort(persons.begin(), persons.end(), [](Person a, Person b) {
return a.name < b.name;
});
// 6. 姓名查找
cout << "请输入联系人姓名:";
string name;
cin >> name;
Person p = search(name, 0, persons.size() - 1);
if (p.name != "") cout << p.name << " " << p.phone << " " << p.company << endl;
else cout << "联系人不存在!" << endl;
}
C++通讯录管理系统
可以借鉴下
#include <iostream>
#include <string>
using namespace std;
// 联系人的结构体
struct ContactPerson { // 姓名、性别、年龄、联系电话、家庭住址
string name;
int sex; // 1为男、0位女
int age;
string phone;
string address;
};
// 通讯录的结构体
struct AddressBooks {
#define MAX 1000 // 定义宏常量,做最大数量
// 通讯录的最大容量
struct ContactPerson perArry[MAX];
// 通讯录中当前记录联系人的个数
int size;
};
// 编写菜单,显示界面
void show_menu() {
cout << "***************************" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***************************" << endl;
}
// 添加联系人
void addPerson(AddressBooks* abs) {
// 判断通讯录是否已满,如果满了就不再添加
if (abs->size == MAX) {
cout << "通讯录已满,无法添加!";
}
else {
// 添加联系人
cout << "\n请输入姓名:";
cin >> abs->perArry[abs->size].name;
cout << "\n请输入性别:" << "(注意1为男,0为女)";
int sex_nu = 0;
while (true) { // 如果输入有误,重新输入
cin >> sex_nu;
if (sex_nu == 0 || sex_nu == 1) {
abs->perArry[abs->size].sex = sex_nu;
break;
}
else {
cout << "输入错误,请重新输入!" << endl;
}
}
cout << "\n请输入年龄:";
cin >> abs->perArry[abs->size].age;
cout << "\n请输入电话:";
cin >> abs->perArry[abs->size].phone;
cout << "\n请输入地址:";
cin >> abs->perArry[abs->size].address;
abs->size ++;
cout << "\n添加成功" << endl;
system("pause");
}
}
// 显示联系人
void show(const AddressBooks* abs) { // 设置为只读
if (abs->size == 0) {
cout << "empty address books" << endl;
system("pause");
}
else{
for (int i = 0; i < abs->size; i++) { // 三目运算判断男女
cout << "姓名:" << abs->perArry[i].name << "\t性别:" << (abs->perArry[i].age == 1 ? "男" : "女")<< "\t年龄:" << abs->perArry[i].age << "\t电话:" << abs->perArry[i].phone << "\t地址:" << abs->perArry[i].address << endl;
}
system("pause");
}
}
// 判断用户是否存在,存在返回具体位置,不存在返回-1
int judge(AddressBooks* abs, string name) {
int index = -1; // 默认为-1
for (int i = 0; i < abs->size; i++) {
// 找到用户输入的姓名,返回i
if (abs->perArry[i].name == name) {
index = i;
}
}
return index;
}
// 删除联系人
void del(AddressBooks* abs, int index) {
for (int i = index; i < abs->size; i++) {
// 数据前移
abs->perArry[i] = abs->perArry[i + 1];
}
abs->size--; // 更新数据
cout << "删除成功" << endl;
system("pause");
}
// 查找联系人
void find(AddressBooks* abs, int index) {
cout << "姓名:" << abs->perArry[index].name << "\t性别:" << (abs->perArry[index].age == 1 ? "男" : "女") << "\t年龄:" << abs->perArry[index].age << "\t电话:" << abs->perArry[index].phone << "\t地址:" << abs->perArry[index].address << endl;
system("pause");
}
// 修改功能
void change(AddressBooks* abs, int index) {
// 添加联系人
cout << "\n请输入姓名:";
cin >> abs->perArry[index].name;
cout << "\n请输入性别:" << "(注意1为男,0为女)";
int sex_nu = 0;
while (true) { // 如果输入有误,重新输入
cin >> sex_nu;
if (sex_nu == 0 || sex_nu == 1) {
abs->perArry[index].sex = sex_nu;
break;
}
else {
cout << "输入错误,请重新输入!" << endl;
}
}
cout << "\n请输入年龄:";
cin >> abs->perArry[index].age;
cout << "\n请输入电话:";
cin >> abs->perArry[index].phone;
cout << "\n请输入地址:";
cin >> abs->perArry[index].address;
cout << "\n修改成功" << endl;
system("pause");
}
// 清空功能
void clear(AddressBooks* abs) {
abs->size = 0;
cout << "清空完成" << endl;
system("pause");
}
int main() {
int choice = 0;
// 创建通讯录结构体变量
AddressBooks abs;
// 通讯录中的初始化大小
abs.size = 0;
while (true) {
show_menu(); // 调用函数;显示菜单界面
cout << "请选择一个数字(0--6):";
cin >> choice;
// 选择功能
switch (choice) {
case 0: // 0、退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0; // 退出功能
case 1: // 1、添加联系人
addPerson(&abs);
system("cls"); // 清屏
break;
case 2: // 2、显示联系人
show(&abs);
system("cls"); // 清屏
break;
case 3: // 3、删除联系人
{ string name;
cout << "请输入要删除的联系人的姓名:";
cin >> name;
if (judge(&abs, name) == -1) {
cout << "没有该联系人!" << endl;
system("pause");
}
else {
del(&abs, judge(&abs, name));
}
system("cls");
break;
}
case 4: // 4、查找联系人
{ string name;
cout << "请输入要查找的联系人的姓名:";
cin >> name;
if (judge(&abs, name) == -1) {
cout << "没有该联系人!" << endl;
system("pause");
}
else {
find(&abs, judge(&abs, name));
}
system("cls");
break;
}
case 5: // 5、修改联系人
{ string name;
cout << "请输入要修改的联系人的姓名:";
cin >> name;
if (judge(&abs, name) == -1) {
cout << "没有该联系人!" << endl;
system("pause");
}
else {
change(&abs, judge(&abs, name));
}
system("cls");
break;
}
case 6: // 6、清空联系人
{cout << "您是否真的要清空全部联系人:(yes/no)" << endl;
while (true) {
string answer;
cin >> answer;
if (answer == "yes") {
clear(&abs);
system("cls");
break;
}
else if (answer == "no") {
cout << "正在取消操作" << endl;
system("cls");
break;
}
else {
cout << "请按照要求输入,输入yes / no";
}
}
break;
}
default:
cout << "input error" << endl;
system("pause");
system("cls"); // 清空界面
}
}
}
通讯录管理c++实现
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name;
string phone;
string email;
string company;
Student(string n, string p, string e, string c) {
name = n;
phone = p;
email = e;
company = c;
}
};
class AddressBook {
private:
vector<Student> students;
public:
void add_student(Student student) {
students.push_back(student);
}
void show_all_students() {
for (int i = 0; i < students.size(); i++) {
cout << "Name: " << students[i].name << ", Phone: " << students[i].phone << ", Email: " << students[i].email << ", Company: " << students[i].company << endl;
}
}
Student* search_student(string name) {
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name) {
return &students[i];
}
}
cout << "Student not found." << endl;
return NULL;
}
void modify_student(string name, string phone="", string email="", string company="") {
Student* student = search_student(name);
if (student) {
if (phone != "") {
student->phone = phone;
}
if (email != "") {
student->email = email;
}
if (company != "") {
student->company = company;
}
cout << "Student information updated." << endl;
} else {
cout << "Cannot modify student information." << endl;
}
}
void insert_student(string name, string phone, string email, string company) {
Student student(name, phone, email, company);
students.push_back(student);
cout << "Student information inserted." << endl;
}
void delete_student(string name) {
Student* student = search_student(name);
if (student) {
students.erase(remove(students.begin(), students.end(), *student), students.end());
cout << "Student information deleted." << endl;
} else {
cout << "Cannot delete student information." << endl;
}
}
int count_students() {
return students.size();
}
void sort_students() {
sort(students.begin(), students.end(), [](Student a, Student b) { return a.name < b.name; });
}
Student* binary_search(string name, int low, int high) {
if (low > high) {
cout << "Student not found." << endl;
return NULL;
}
int mid = (low + high) / 2;
if (students[mid].name == name) {
cout << "Name: " << students[mid].name << ", Phone: " << students[mid].phone << ", Email: " << students[mid].email << ", Company: " << students[mid].company << endl;
return &students[mid];
} else if (students[mid].name > name) {
return binary_search(name, low, mid - 1);
} else {
return binary_search(name, mid + 1, high);
}
}
};
int main() {
AddressBook address_book;
int n;
cout << "Enter number of students: ";
cin >> n;
for (int i = 0; i < n; i++) {
string name, phone, email, company;
cout << "Enter student name: ";
cin >> name;
cout << "Enter student phone: ";
cin >> phone;
cout << "Enter student email: ";
cin >> email;
cout << "Enter student company: ";
cin >> company;
Student student(name, phone, email, company);
address_book.add_student(student);
}
cout << endl;
cout << "All students:" << endl;
address_book.show_all_students();
cout << endl;
string name;
cout << "Enter name to search: ";
cin >> name;
Student* student = address_book.search_student(name);
if (student) {
cout << "Name: " << student->name << ", Phone: " << student->phone << ", Email: " << student->email << ", Company: " << student->company << endl;
}
cout << endl;
cout << "Enter name to modify: ";
cin >> name;
string phone, email, company;
cout << "Enter new phone (or leave blank to skip): ";
cin >> phone;
cout << "Enter new email (or leave blank to skip): ";
cin >> email;
cout << "Enter new company (or leave blank to skip):
你好,这里是一个简单的 C++ 实现通讯录的示例代码,其中包含了您提出的所有要求和功能。实现了用数组存储通讯录信息,并使用结构体来表示每个同学的信息,通过循环实现逐个输入/显示同学信息的功能。同时,还实现了查询、修改、插入和删除等操作,统计人数以及按照姓名进行排序和折半查找等功能。
#include <iostream>
#include <string>
using namespace std;
const int MAX_SIZE = 100; // 最大存储人数
struct Contact {
string name;
string phone;
string address;
string company;
};
// 初始化通讯录
void initList(Contact *list, int &n) {
cout << "请输入同学人数: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "个同学的信息:" << endl;
cout << "姓名: ";
cin >> list[i].name;
cout << "电话: ";
cin >> list[i].phone;
cout << "地址: ";
cin >> list[i].address;
cout << "公司: ";
cin >> list[i].company;
}
}
// 显示所有同学信息
void displayList(const Contact *list, const int &n) {
for (int i = 0; i < n; i++) {
cout << "姓名: " << list[i].name << "\t";
cout << "电话: " << list[i].phone << "\t";
cout << "地址: " << list[i].address << "\t";
cout << "公司: " << list[i].company << endl;
}
}
// 查询同学信息
void search(Contact *list, int n) {
string name;
cout << "请输入要查询的同学姓名: ";
cin >> name;
int i;
for (i = 0; i < n; i++) {
if (list[i].name == name) {
break;
}
}
if (i == n) {
cout << "没有找到该同学的信息" << endl;
return;
}
cout << "姓名: " << list[i].name << "\t";
cout << "电话: " << list[i].phone << "\t";
cout << "地址: " << list[i].address << "\t";
cout << "公司: " << list[i].company << endl;
}
// 修改同学信息
void modify(Contact *list, int n) {
string name;
cout << "请输入要修改的同学姓名: ";
cin >> name;
int i;
for (i = 0; i < n; i++) {
if (list[i].name == name) {
break;
}
}
if (i == n) {
cout << "没有找到该同学的信息" << endl;
return;
}
cout << "请输入新的信息:" << endl;
cout << "姓名: ";
cin >> list[i].name;
cout << "电话: ";
cin >> list[i].phone;
cout << "地址: ";
cin >> list[i].address;
cout << "公司: ";
cin >> list[i].company;
}
// 插入新的同学信息
void insert(Contact *list, int &n) {
cout << "请输入新的同学信息:" << endl;
cout << "姓名: ";
cin >> list[n].name;
cout << "电话: ";
cin >> list[n].phone;
cout << "地址: ";
cin >> list[n].address;
cout << "公司: ";
cin >> list[n].company;
n++;
}
// 删除同学信息
void remove(Contact *list, int &n) {
string name;
cout << "请输入要删除的同学姓名: ";
cin >> name;
int i;
for (i = 0; i < n; i++) {
if (list[i].name == name) {
break;
}
}
if (i == n) {
cout << "没有找到该同学的信息" << endl;
return;
}
for (int j = i; j < n - 1; j++) {
list[j] = list[j + 1];
}
n--;
}
// 统计通讯录中同学人数
void count(const int &n) {
cout << "通讯录中同学人数为: " << n << endl;
}
// 按照姓名进行插入排序
void sortByName(Contact *list, const int &n) {
Contact temp;
int i, j;
for (i = 1; i < n; i++) {
temp = list[i];
for (j = i - 1; j >= 0 && list[j].name > temp.name; j--) {
list[j + 1] = list[j];
}
list[j + 1] = temp;
}
}
// 折半查找同学信息
void binarySearch(Contact *list, int low, int high, const string &name) {
if (low > high) {
cout << "没有找到该同学的信息" << endl;
return;
}
int mid = (low + high) / 2;
if (list[mid].name == name) {
cout << "姓名: " << list[mid].name << "\t";
cout << "电话: " << list[mid].phone << "\t";
cout << "公司: " << list[mid].company << endl;
} else if (list[mid].name > name) {
binarySearch(list, low, mid - 1, name);
} else {
binarySearch(list, mid + 1, high, name);
}
}
// 入口函数
int main() {
Contact list[MAX_SIZE];
int n = 0;
int choice;
while (true) {
cout << "请选择要执行的操作: " << endl;
cout << "1. 初始化通讯录" << endl;
cout << "2. 显示所有同学信息" << endl;
cout << "3. 查询同学信息" << endl;
cout << "4. 修改同学信息" << endl;
cout << "5. 插入新的同学信息" << endl;
cout << "6. 删除同学信息" << endl;
cout << "7. 统计通讯录中同学人数" << endl;
cout << "8. 按照姓名进行排序" << endl;
cout << "9. 根据姓名进行折半查找" << endl;
cout << "0. 退出程序" << endl;
cout << "请选择: ";
cin >> choice;
switch (choice) {
case 1:
initList(list, n);
break;
case 2:
displayList(list, n);
break;
case 3:
search(list, n);
break;
case 4:
modify(list, n);
break;
case 5:
insert(list, n);
break;
case 6:
remove(list, n);
break;
case 7:
count(n);
break;
case 8:
sortByName(list, n);
displayList(list, n);
break;
case 9: {
string name;
cout << "请输入要查找的同学姓名: ";
cin >> name;
sortByName(list, n);
binarySearch(list, 0, n - 1, name);
break;
}
case 0:
return 0;
default:
cout << "输入有误,请重新选择!" << endl;
}
}
return 0;
}
以下是简单的C++代码示例,实现上述要求:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生结构体
struct Student {
string name;
string phone;
string email;
string address;
};
// 显示学生通讯信息
void PrintStudentInfo(const Student& stu) {
cout << "姓名:" << stu.name << endl;
cout << "电话:" << stu.phone << endl;
cout << "邮箱:" << stu.email << endl;
cout << "地址:" << stu.address << endl;
}
// 查询学生通讯信息
void QueryStudentInfo(vector<Student>& students) {
cout << "请输入要查询的学生姓名:" << endl;
string name;
cin >> name;
for (const auto& stu : students) {
if (stu.name == name) {
PrintStudentInfo(stu);
return;
}
}
cout << "未找到该学生!" << endl;
}
// 修改学生通讯信息
void ModifyStudentInfo(vector<Student>& students) {
cout << "请输入要修改的学生姓名:" << endl;
string name;
cin >> name;
for (auto& stu : students) {
if (stu.name == name) {
cout << "请输入修改后的电话号码:" << endl;
cin >> stu.phone;
cout << "请输入修改后的邮箱:" << endl;
cin >> stu.email;
cout << "请输入修改后的地址:" << endl;
cin >> stu.address;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 插入学生通讯信息
void InsertStudentInfo(vector<Student>& students) {
Student stu;
cout << "请输入学生姓名:" << endl;
cin >> stu.name;
cout << "请输入学生电话号码:" << endl;
cin >> stu.phone;
cout << "请输入学生邮箱:" << endl;
cin >> stu.email;
cout << "请输入学生地址:" << endl;
cin >> stu.address;
students.push_back(stu);
cout << "添加成功!" << endl;
}
// 删除学生通讯信息
void DeleteStudentInfo(vector<Student>& students) {
cout << "请输入要删除的学生姓名:" << endl;
string name;
cin >> name;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->name == name) {
students.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 统计学生人数
int CountStudents(const vector<Student>& students) {
return students.size();
}
// 按姓名排序
void SortByName(vector<Student>& students) {
sort(students.begin(), students.end(), [](const auto& stu1, const auto& stu2) {
return stu1.name < stu2.name;
});
}
// 折半查找
int BinarySearch(const vector<Student>& students, const string& name, int left, int right) {
if (left > right) {
return -1;
}
int mid = (left + right) / 2;
if (students[mid].name == name) {
PrintStudentInfo(students[mid]);
return mid;
} else if (students[mid].name > name) {
return BinarySearch(students, name, left, mid - 1);
} else {
return BinarySearch(students, name, mid + 1, right);
}
}
int main() {
cout << "请输入学生人数:" << endl;
int n;
cin >> n;
vector<Student> students;
for (int i = 0; i < n; ++i) {
Student stu;
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "姓名:";
cin >> stu.name;
cout << "电话:";
cin >> stu.phone;
cout << "邮箱:";
cin >> stu.email;
cout << "地址:";
cin >> stu.address;
students.push_back(stu);
}
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 逐个显示学生通讯信息" << endl;
cout << "2. 查询学生通讯信息" << endl;
cout << "3. 修改学生通讯信息" << endl;
cout << "4. 插入学生通讯信息" << endl;
cout << "5. 删除学生通讯信息" << endl;
cout << "6. 统计学生人数" << endl;
cout << "7. 按姓名排序" << endl;
cout << "8. 根据姓名进行折半查找" << endl;
cout << "0. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "学生通讯信息:" << endl;
for (const auto& stu : students) {
PrintStudentInfo(stu);
cout << endl;
}
break;
case 2:
QueryStudentInfo(students);
break;
case 3:
ModifyStudentInfo(students);
break;
case 4:
InsertStudentInfo(students);
break;
case 5:
DeleteStudentInfo(students);
break;
case 6:
cout << "学生人数:" << CountStudents(students) << endl;
break;
case 7:
SortByName(students);
cout << "按姓名排序成功!" << endl;
break;
case 8:
cout << "请输入要查找的学生姓名:" << endl;
string name;
cin >> name;
BinarySearch(students, name, 0, students.size() - 1);
break;
case 0:
cout << "程序已退出!" << endl;
return 0;
default:
cout << "输入错误,请重新选择!" << endl;
break;
}
}
return 0;
}
这份代码实现了上述要求,其中包括学生结构体的定义,逐个输入学生通讯信息,查询、修改、插入和删除学生通讯信息等操作,以及统计学生人数、按姓名排序和折半查找等。可以根据实际需求进行修改和完善。
参考小小示例:
# 定义一个同学类,包含通讯信息
class Student:
def __init__(self, name, age, gender, phone, company):
self.name = name
self.age = age
self.gender = gender
self.phone = phone
self.company = company
# 定义通讯录类,用于存储同学对象
class AddressBook:
def __init__(self):
self.students = []
# 统计通讯录中同学人数
def count(self):
return len(self.students)
# 按照姓名进行排序
def sort_by_name(self):
self.students.sort(key=lambda x: x.name)
# 按照姓名进行折半查找
def search_by_name(self, name):
left, right = 0, len(self.students) - 1
while left <= right:
mid = (left + right) // 2
if self.students[mid].name == name:
return mid
elif self.students[mid].name < name:
left = mid + 1
else:
right = mid - 1
return -1
# 显示通讯录中的所有信息
def show(self):
for i, student in enumerate(self.students):
print(f"第{i+1}位同学:")
print(f"姓名:{student.name}")
print(f"年龄:{student.age}")
print(f"性别:{student.gender}")
print(f"电话:{student.phone}")
print(f"工作单位:{student.company}\n")
# 插入同学信息
def insert(self, name, age, gender, phone, company):
student = Student(name, age, gender, phone, company)
self.students.append(student)
# 查询同学信息
def query(self, name):
index = self.search_by_name(name)
if index == -1:
print(f"找不到名字为{name}的同学!")
else:
self.students[index].show()
# 修改同学信息
def update(self, name, age=None, gender=None, phone=None, company=None):
index = self.search_by_name(name)
if index == -1:
print(f"找不到名字为{name}的同学!")
return
if age is not None:
self.students[index].age = age
if gender is not None:
self.students[index].gender = gender
if phone is not None:
self.students[index].phone = phone
if company is not None:
self.students[index].company = company
print(f"修改成功!")
# 删除同学信息
def delete(self, name):
index = self.search_by_name(name)
if index == -1:
print(f"找不到名字为{name}的同学!")
return
self.students.pop(index)
print(f"删除成功!")
#如有帮助,恭请采纳