C++用数组存放数据

                                                                    用数组存放数据的增删改查操作

  我们刚学习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].stu_Sex << endl;
			cout << "地址:";
			cout << stu_Array[i].stu_Address << endl;
			cout << "学生电话号码:";
			cout << stu_Array[i].stu_PhoneNum << endl;
			cout << "家长电话号码:";
			cout << stu_Array[i].stu_Parent_PhoneNum << endl;
		}
		
	}
	cout << "======================================" << endl;

	if (judgement==0)
	{
		cout << "查无此人!!!" << endl;
		cout << "======================================" << endl;
	}

}

void Student::delete_student_Info()
{
	bool judgement = 0;
	string name;
	char select;
	cout << "请输入你要删除的学生姓名:" << endl;
	cin >> name;
	for (int i = 0; i < get_current_stuNum(); i++)
	{
		if (stu_Array[i].stu_Name == name)
		{
			judgement = 1;
			cout << "======================================" << endl;
			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].stu_Sex << endl;
			cout << "地址:";
			cout << stu_Array[i].stu_Address << endl;
			cout << "学生电话号码:";
			cout << stu_Array[i].stu_PhoneNum << endl;
			cout << "家长电话号码:";
			cout << stu_Array[i].stu_Parent_PhoneNum << endl;
			cout << "======================================" << endl;

			cout << "是否删除此学生的信息?(Y/N)" << endl;
			while (1){
				cin >> select;
				if (select == 'Y' || select == 'y' || select == 'N' || select == 'n')
				{
					break;
				}
				else{
					cout << "输入有误,请重新输入!!" << endl;
				}
			}

			if (select == 'Y' || select == 'y')
			{
				for (int j = i; j<get_current_stuNum(); j++)
				{
					stu_Array[j].stu_Num = stu_Array[j + 1].stu_Num;
					stu_Array[j].stu_Name = stu_Array[j + 1].stu_Name;
					stu_Array[j].stu_Parent_Name = stu_Array[j + 1].stu_Parent_Name;
					stu_Array[j].stu_Age = stu_Array[j + 1].stu_Age;
					stu_Array[j].stu_Sex = stu_Array[j + 1].stu_Sex;
					stu_Array[j].stu_Address = stu_Array[j + 1].stu_Address;
					for (int k = 0; k < 20; k++)
					{
						stu_Array[j].stu_PhoneNum[k] = stu_Array[j + 1].stu_PhoneNum[k];
					}

					for (int k = 0; k < 20; k++)
					{
						stu_Array[j].stu_Parent_PhoneNum[k] = stu_Array[j + 1].stu_Parent_PhoneNum[k];

					}
				}
			}

			if (select == 'N' || select == 'n')
			{
				menu();
			}
		}
	}
	if (judgement == 1)
	{
		cout << "======================================" << endl;
		cout << "删除成功。。" << endl;
		cout << "======================================" << endl;
		current_stuNum--;
	}

	if (judgement == 0)
	{
		cout << "======================================" << endl;
		cout << "查无此人!!!" << endl;
		cout << "======================================" << endl;
	}
}

void Student::update_student_Info()
{
	bool judgement = 0;
	string name;
	char select;
	cout << "请输入你要修改的学生姓名:" << endl;
	cin >> name;
	for (int i = 0; i < get_current_stuNum(); i++)
	{
		if (stu_Array[i].stu_Name == name)
		{
			judgement = 1;
			cout << "======================================" << endl;
			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].stu_Sex << endl;
			cout << "地址:";
			cout << stu_Array[i].stu_Address << endl;
			cout << "学生电话号码:";
			cout << stu_Array[i].stu_PhoneNum << endl;
			cout << "家长电话号码:";
			cout << stu_Array[i].stu_Parent_PhoneNum << endl;
			cout << "======================================" << endl;

			cout << "是否修改此学生的信息?(Y/N)" << endl;
			while (1){
				cin >> select;
				if (select == 'Y' || select == 'y' || select == 'N' || select == 'n')
				{
					break;
				}
				else{
					cout << "输入有误,请重新输入!!" << endl;
				}
			}

			if (select == 'Y' || select == 'y')
			{
				judgement = 1;
				cout << "======================================" << endl;
				cout << "学号:";
				cin >> stu_Array[i].stu_Num;
				cout << "姓名:";
				cin >> stu_Array[i].stu_Name;
				cout << "家长姓名:";
				cin >> stu_Array[i].stu_Parent_Name;
				cout << "年龄:";
				cin >> stu_Array[i].stu_Age;
				cout << "性别: ";
				cin >> stu_Array[i].stu_Sex;
				cout << "地址:";
				cin >> stu_Array[i].stu_Address;
				cout << "学生电话号码:";
				cin >> stu_Array[i].stu_PhoneNum;
				cout << "家长电话号码:";
				cin >> stu_Array[i].stu_Parent_PhoneNum;
				cout << "======================================" << endl;
				cout << "修改成功。。" << endl;
				cout << "======================================" << endl;
			}

		}
	}
	if (judgement == 0)
	{
		cout << "======================================" << endl;
		cout << "查无此人!!!" << endl;
		cout << "======================================" << endl;
	}
}

void Student::insert_student_Info(){
	student_Info s1;
	int i;
	char s;

	while (1)
	{
		cout << "需要插入学生的信息(Y/N)?" << endl;
		while (1){
			cin >> s;
			if (s == 'Y' || s == 'y' || s == 'N' || s == 'n')
			{
				break;
			}
			else{
				cout << "输入有误,请重新输入!!" << endl;
				cout << "需要插入学生的信息(Y/N)?" << endl;
			}
		}
		if (s == 'N' || s == 'n')
		{
			break;
		}

		cout << "需要插入学生信息的位置为?" << endl;
		while (1){
			cin >> i;
			if (i > 0 && i <= get_current_stuNum()+1)
			{
				break;
			}
			else{
				cout << "输入有误,请重新输入!!" << endl;
				cout << "需要插入学生信息的位置为?" << endl;
			}
		}
		cout << "======================================" << endl;
		cout << "第" << i << "个学生信息:" << endl;
		cout << "学号:";
		cin >> s1.stu_Num;
		cout << "姓名:";
		cin >> s1.stu_Name;
		cout << "家长姓名:";
		cin >> s1.stu_Parent_Name;
		cout << "年龄:";
		cin >> s1.stu_Age;
		cout << "性别: ";
		cin >> s1.stu_Sex;
		cout << "地址:";
		cin >> s1.stu_Address;
		cout << "学生电话号码:";
		cin >> s1.stu_PhoneNum;
		cout << "家长电话号码:";
		cin >> s1.stu_Parent_PhoneNum;
		cout << "======================================" << endl;
	//判断插入位置是否合法
	if (i<0||i>current_stuNum+1 )
	{
		cout << "插入的位置不正确,位置值不能是负数或超过顺序表最大容量!" << endl;
		return;
	}

	//判断顺序表是否已满
	if (current_stuNum == STULEN)
	{
		cout << "顺序表已满,无法继续插入一个元素!" << endl;
		return;
	}

	//移动其他学生信息,将要插入的位置的学生信息空出来
	int j;
	for (j = current_stuNum; j>i-1; j--)
	{
		stu_Array[j].stu_Num = stu_Array[j - 1].stu_Num;
		stu_Array[j].stu_Name = stu_Array[j - 1].stu_Name;
		stu_Array[j].stu_Parent_Name = stu_Array[j - 1].stu_Parent_Name;
		stu_Array[j].stu_Age = stu_Array[j - 1].stu_Age;
		stu_Array[j].stu_Sex = stu_Array[j - 1].stu_Sex;
		stu_Array[j].stu_Address = stu_Array[j - 1].stu_Address;
		for (int k = 0; k<20; k++)
		{
			stu_Array[j].stu_PhoneNum[k] = stu_Array[j - 1].stu_PhoneNum[k];
		}

		for (int k = 0; k<20; k++)
		{
			stu_Array[j].stu_Parent_PhoneNum[k] = stu_Array[j - 1].stu_Parent_PhoneNum[k];

		}
		
	}
	//插入合适位置
	stu_Array[j].stu_Num = s1.stu_Num;
	stu_Array[j].stu_Name = s1.stu_Name;
	stu_Array[j].stu_Parent_Name = s1.stu_Parent_Name;
	
	stu_Array[j].stu_Age = s1.stu_Age;
	stu_Array[j].stu_Sex = s1.stu_Sex;
	stu_Array[j].stu_Address = s1.stu_Address;

	for (int k = 0; k<20; k++)
	{
		stu_Array[j].stu_PhoneNum[k] = s1.stu_PhoneNum[k];
	}

	for (int k = 0; k<20; k++)
	{
		stu_Array[j].stu_Parent_PhoneNum[k] = s1.stu_Parent_PhoneNum[k];
	}
	
	//修改当前学生数
	current_stuNum++;

	cout << "插入成功。。" << endl;
	cout << "======================================" << endl;
	}
}

void Student::show_student_Info()
{

	cout << "\n输出学生信息:" << endl;
	for (int i = 0; i < current_stuNum; i++)
	{
		cout << "======================================" << endl;
		cout << "第" << i + 1 << "个学生信息:" << 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].stu_Sex << endl;
		cout << "地址:";
		cout << stu_Array[i].stu_Address << endl;
		cout << "学生电话号码:";
		cout << stu_Array[i].stu_PhoneNum << endl;
		cout << "家长电话号码:";
		cout << stu_Array[i].stu_Parent_PhoneNum << endl;
	}
	cout << "======================================" << endl;
}

void main()
{
	Student stu1;
	while (1)
	{
		stu1.menu();
	}
	
}


代码中的函数都没有传参数,这是一个很不好的,虽然不传参数也能实现功能。代码非常简单,一般都能看懂
先来讲一下函数的调用吧,代码的所有函数(除main函数外)都是Student类的成员。在main函数里建立一个stu1对象,并用stu1去调用菜单函数,进入菜单函数后,根据用户的功能选择再去调用对应的功能函数,调用完后都返回到菜单函数

现在来讲讲用线性表存放数据的一些操作
增操作:如果增加一个人的信息之前,数组中还没有数据,那么我们插入第一个人的数据就存放在数组的前面,这里不用解释太多。当数组里面已经有n个人的数据后,我们再插入新的数据时,我们就可以插入到那n个人之间的任何一个位置。例如:我想插入到第二个位置,那么我就需要将第二个和第二个以后的每一个人往后移动一个位置,将第二个位置空出来,这样我们就可以向第二个位置添加数据了。
移动位置的代码(特别注意:移动位置时,一定要从最后面开始移动,否则第二个位置以后的数据都将会被第二个位置的数据覆盖)
//移动其他学生信息,将要插入的位置的学生信息空出来
	int j;
	for (j = current_stuNum; j>i-1; j--)
	{
		stu_Array[j].stu_Num = stu_Array[j - 1].stu_Num;
		stu_Array[j].stu_Name = stu_Array[j - 1].stu_Name;
		stu_Array[j].stu_Parent_Name = stu_Array[j - 1].stu_Parent_Name;
		stu_Array[j].stu_Age = stu_Array[j - 1].stu_Age;
		stu_Array[j].stu_Sex = stu_Array[j - 1].stu_Sex;
		stu_Array[j].stu_Address = stu_Array[j - 1].stu_Address;
		for (int k = 0; k<20; k++)
		{
			stu_Array[j].stu_PhoneNum[k] = stu_Array[j - 1].stu_PhoneNum[k];
		}

		for (int k = 0; k<20; k++)
		{
			stu_Array[j].stu_Parent_PhoneNum[k] = stu_Array[j - 1].stu_Parent_PhoneNum[k];

		}
		
	}
插入对应位置的代码
//插入合适位置
	stu_Array[j].stu_Num = s1.stu_Num;
	stu_Array[j].stu_Name = s1.stu_Name;
	stu_Array[j].stu_Parent_Name = s1.stu_Parent_Name;
	
	stu_Array[j].stu_Age = s1.stu_Age;
	stu_Array[j].stu_Sex = s1.stu_Sex;
	stu_Array[j].stu_Address = s1.stu_Address;

	for (int k = 0; k<20; k++)
	{
		stu_Array[j].stu_PhoneNum[k] = s1.stu_PhoneNum[k];
	}

	for (int k = 0; k<20; k++)
	{
		stu_Array[j].stu_Parent_PhoneNum[k] = s1.stu_Parent_PhoneNum[k];
	}
	
	//修改当前学生数
	current_stuNum++;

删操作:说到删操作,这也是很好玩的。但是很多不会用数组存放数据的人都可能会无从下手,或者认为要删除数组中的元素是直接delete掉,那要怎么delete呢?
 其实说白了,删除数组的元素就是覆盖数组的元素。比如说我要删除第二个位置的数据,那么我就可以将第三个位置的数据直接移动到第二个位置,第四个位置的移动到第三个位置,以此类推。有一个要特别注意的,移动数据时一定要从前面开始移动,不能从后面开始移动,否则第二个位置后的数据都会被第n个位置的数据所覆盖。移动完成后有些同学就会想到,第n个位置的数据已经移动到第n-1个位置去了,那么第n个位置存放什么呢?其实第n个位置移动前存的是什么移动后存放的也是什么。有些同学就会想,第n-1个位置与第n个位置存一样的数据不就不对了吗?其实我们删除一个位置的数据后我们要将位置总数减一,这样你就不用去访问第n个位置的数据啦。这样的话,第n个位置的内存不就被无用的数据占用了吗?其实我们不用管它,当我们再次插入数据到第n个位置时,自然就将那些无用的数据覆盖了,所以并没什么影响。

改操作:改操作没什么好说的,直接查找到需要修改的数据,然后将新的数据覆盖老的数据就行。

查操作:查操作也没什么好说的,查找到对应的数据直接数出来就行了。


这是用线性表来存放数据的一些操作,弄懂数组后就会觉得很简单,并没有什么难的。




  • 10
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值