用数组存放数据的增删改查操作
我们刚学习C++的时候,老师会叫我们去做一些小系统出来,比如说学生成绩管理系统,图书管理系统,通讯录之类的。现在我就用做一个简单的通讯录来举例吧
来来来,先上代码
/*
* 程序功能:实现学生通讯录的增、删、改、查。
* 作者:BossMao
*/
#include<iostream>
#include<string>
using namespace std;
#define STULEN 100 //数组长度
//学生信息
struct student_Info
{
double stu_Num; //学生学号
string stu_Name; //学生姓名
string stu_Parent_Name; //学生家长姓名
int stu_Age; //学生年龄
string stu_Sex; //学生性别
string stu_Address; //学生住址
char stu_PhoneNum[20]; //学生电话号码
char stu_Parent_PhoneNum[20]; //学生家长电话号码
};
//学生类
class Student{
private:
student_Info stu_Array[STULEN]; //学生数组
int current_stuNum; //当前学生人数
public:
Student();
~Student();
void menu(); //显示菜单
int get_current_stuNum(); //获取当前学生人数
void insert_student_Info(); //插入学生信息
void delete_student_Info(); //删除学生信息
void search_student_Info(); //查找学生信息
void update_student_Info(); //修改学生信息
void show_student_Info(); //显示学生信息
};
Student::Student(){
current_stuNum = 0;
}
Student::~Student(){
}
int Student::get_current_stuNum()
{
return current_stuNum;
}
void Student::menu()
{
int a;
cout << "1.插入学生信息" << endl;
cout << "2.修改学生信息" << endl;
cout << "3.删除学生信息" << endl;
cout << "4.查询学生信息" << endl;
cout << "5.显示学生信息" << endl;
cout << "0.退出" << endl;
cout << endl;
cout << "请输入你要执行功能对应的数字:" << endl;
cin >> a;
if (a >= 0 && a < 6)
{
switch (a)
{
case 1:
insert_student_Info();
break;
case 2:
update_student_Info();
break;
case 3:
delete_student_Info();
break;
case 4:
search_student_Info();
break;
case 5:
show_student_Info();
break;
case 0:
exit(0);
}
}
else{
cout << "输入有误!!请重新输入" << endl;
}
}
void Student::search_student_Info()
{
student_Info stu;
string name;
bool judgement = 0;
cout << "请输入你要查询的学生姓名:" << endl;
cin >> name;
cout << "查询结果:" << endl;
for (int i = 0; i < get_current_stuNum(); i++)
{
if (stu_Array[i].stu_Name == name)
{
judgement = 1;
cout << "======================================" << endl;
cout << "学号:";
cout << stu_Array[i].stu_Num << endl;
cout << "姓名:";
cout << stu_Array[i].stu_Name << endl;
cout << "家长姓名:";
cout << stu_Array[i].stu_Parent_Name << endl;
cout << "年龄:";
cout << stu_Array[i].stu_Age << endl;
cout << "性别:";
cout << stu_Array[i