c++数组描述线性表实现通讯录功能

#include <iostream>
#include <algorithm>
using namespace std;

//结构体
//功能:实现通讯录
struct Address
{
	string name;   //用户的名字
    string telephone;  //用户的手机
	int clas = 0;
	int dorm = 0;
};


//函数功能:改变结构体数组的长度大小
//2019/10/4/16:18 函数已通过检测 fsy
void changeLength1D(Address*& a, int oldLength, int newLength)
{
	if (newLength < 1)
	{
		cout << "new length must be >= 0 ";
		cout << "请重新输入newLength" << endl;
	}
	else
	{
		Address *temp = new Address[newLength];
		int number = min(oldLength, newLength);
		for (int i = 0; i < number; i++)
		{
			temp[i].name = a[i].name;
			temp[i].telephone = a[i].telephone;
			temp[i].dorm = a[i].dorm;
			temp[i].clas = a[i].clas;
		}
		a = temp;
	}
}

/*
类:数组实现的线性表
功能:提供操作: 插入、删除、 查找
*/
template<class T>
class arrayList
{
	// 保护成员变量
protected:
	Address *element;
	int arrayLength;    //这时线性表的空间大小
	int listSize;       //这是已经保存了多少数据的总长
public:
	//构造函数,复制构造函数,析构函数
	arrayList(int initialCapacity = 10);  //构造函数
	arrayList(const arrayList<T>&);   //复制构造函数
	~arrayList() { delete[]element; }
	//ADT方法
	bool checkIndex(int theIndex);//检查索引
	bool empty() const;//当且仅当线性表为空时返回true
	int size() const;  //返回线性表的元素个数
	T& get(int theIndex) const; //返回索引为theIndex的元素
	void erase(int theIndex);  //删除某个元素
	void insert(int theIndex, const Address& theElement);  //插入某个元素
	void print();  //打印某个元素

	//以下四个函数是og中需要实现的功能
	int checkNameNumb(string name);  //查找某个用户的名字所在的序号
	bool checkName(string name);//查找某个用户的名字是否存在
	int xorClas(int clas); //查找某个班级的人
	void changeInformation(string name,int choice,string newValue);//修改某一条信息
	void changeInformation(string name, int choice, int newValue);//重载函数:修改某一条信息
};

//函数功能:如果线性表为空的话就返回真,否则返回假
template<class T>
bool arrayList<T>::empty() const //当且仅当线性表为空时返回true
{
	if (listSize == 0)
	{
		return true;
	}

	else
	{
		return false;
	}
}

//函数功能:返回线性表的元素个数
template<class T>
int arrayList<T>::size() const
{
	return listSize;
}

//检查索引、查找元素、查找索引
template<class T>
bool arrayList<T>::checkIndex(int theIndex)
{
	if (theIndex < 0 || theIndex >= listSize)
	{
		cout << "index=" << theIndex << " listSize=" << listSize << endl;
		cout << "索引大小不合适,请重新输入一个索引" << endl;
		return false;
	}
	else
	{
		return true;
	}
}

//构造函数的定义
//2019/10/4/16:18 函数已通过检测 fsy
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{
	if (initialCapacity < 1)
	{
		cout << "initial capacity" << initialCapacity << "Must be >= 0";
	}
	else
	{
		arrayLength = initialCapacity;
		element = new Address[arrayLength];
		listSize = 0;
	}
}

//复制构造函数的定义
template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
	arrayLength = theList.arrayLength;
	listSize = theList.listSize;
	element = new Address[arrayLength];
	for (int i = 0;i < theList.arrayLength;i++)
	{
		element[i].clas = theList.element[i].clas;
		element[i].dorm = theList.element[i].dorm;
		element[i].name = theList.element[i].name;
		element[i].telephone = theList.element[i].telephone;
	}
}

//功能:取出该索引下的元素
template<class T>
T& arrayList<T>::get(int theIndex) const
{
	if (checkIndex(theIndex))//如果该索引下有元素
	{
		cout << element[theIndex].name << " "
			<< element[theIndex].telephone << " "
			<< element[theIndex].clas << " "
			<< element[theIndex].dorm << endl;
	}	
}

//功能:删除某个索引下的元素
//2019/10/4/16:16 函数已通过调试 fsy
template<class T>
void arrayList<T>::erase(int theIndex)
{
	//
	if (checkIndex(theIndex))//如果该索引下有元素
	{
		for (int i = theIndex;i < listSize - 1;i++)
		{
			element[i].name = element[i + 1].name;
			element[i].telephone = element[i + 1].telephone;
			element[i].clas = element[i+1].clas;
			element[i].dorm = element[i+1].dorm;
		}
	}
	--listSize; //把元素的数量减一
}

//函数功能:插入一个用户的信息
//
template<class T>
void arrayList<T>::insert(int theIndex, const Address & theElement)
{
	if (listSize == arrayLength)
	{
		changeLength1D(element, arrayLength, 2 * arrayLength);
		arrayLength *= 2;
	}
	//把所有元素向后移动一位
	//比如要插入的位置是5,就是a[4],所以从a[4]开始向后移动
	//比如listSize是7,那最后一个元素就是a[6],让a[7]=a[6],依此类推
	for (int i = listSize;i > theIndex; i--)
	{
		element[i].name = element[i - 1].name;
		element[i].telephone = element[i - 1].telephone;
		element[i].dorm = element[i-1].dorm;
		element[i].clas = element[i-1].clas;
	}
	
	element[theIndex].name = theElement.name;
	element[theIndex].telephone = theElement.telephone;
	element[theIndex].clas = theElement.clas;
	element[theIndex].dorm = theElement.dorm;

	listSize++;	
}

//打印整个线性表
//2019/10/4/16:13 函数已通过调试 fsy
template<class T>
void arrayList<T>::print()
{
	for (int i = 0;i < listSize; i++)
	{
		cout << element[i].name << " "
			<< element[i].telephone << " "
			<< element[i].clas << " "
			<< element[i].dorm << endl;
	}
}


//功能:查找某个用户的名字是否存在,并返回索引
//2019/10/4/16:15 函数已通过检测 fsy
template<class T>
int arrayList<T>::checkNameNumb(string name)
{
	for (int i = 0;i < listSize;i++)
	{
		if (element[i].name == name)
		{
			return i; 
		}
	}
}



//功能:查找某个用户的名字是否存在
//2019/10/4/16:25 函数通过调试 fsy
template<class T>
bool arrayList<T>::checkName(string name)
{
	for (int i = 0;i < listSize;i++)
	{
		if (element[i].name == name)
		{
			//cout << 1 << endl;
			return true; 
		}
	}
	//cout << 0 << endl;
	return false;
}

//功能:输出一个班级的人员信息时输出所有成员的宿舍号的异或值
//2019/10/4/21:43 函数通过调试 fsy
template<class T>
int arrayList<T>::xorClas(int clas)
{
	int ans = 0;  //0异或任何数是那个数本身
	for (int i = 0;i < listSize;i++)
	{
		if (element[i].clas == clas)  
		{
			ans = ans ^ element[i].dorm;
		}
	}
	return ans;
}

//功能:根据姓名编辑一条记录
//重载函数:修改电话号
//2019/10/4 函数已通过调试 fsy
template<class T>
void arrayList<T>::changeInformation(string name,int choice,string newValue)
{
	int h = checkNameNumb(name);  //找到改名字所对应的序号
	element[h].telephone = newValue;
	
}

//功能:根据姓名编辑一条记录
//重载函数:修改班级号和宿舍号某一条信息
//2019/10/4 已通过调试
template<class T>
void arrayList<T>::changeInformation(string name, int choice, int newValue)
{
	int h = checkNameNumb(name); //找到改名字所对应的序号
	if (choice == 2)
	{
		element[h].clas = newValue;
	}
	if (choice == 3)
	{
		element[h].dorm = newValue;
	}
}

int main()
{
	arrayList<int> a(100);
	Address b;
	int func = 0;  //最开始的功能选择
	int choice = 0;//func = 2时函数里面的选择功能
	string name; //临时的名字
	int clas = 0; //临时用到的班级
	int ans = 0;  //临时用到的答案
	string newValue1;//string改电话号
	int newValue2;//int型改宿舍号和班级号
	int informal = 0;
	int numb1 = 0;//insert函数插入的计数器
	cin>>informal;//接受一下他读入数据的行数
	while (informal--)
	{
		cin >> func;
		//* 0 姓名 电话 班级 宿舍 插入一条记录
		if (func == 0)   
		{
			cin >> b.name;
			cin >> b.telephone;
			cin >> b.clas;
			cin >> b.dorm;
			//成功
			a.insert(0,b);  //永远插在第一个,这样不会出错
			numb1 = a.size();
		}
		//*1 姓名 根据姓名删除一条记录
		if (func == 1)
		{
			cin >> name;
			a.erase(a.checkNameNumb(name));   //删除名字那条的索引
		}
		//* 2 姓名 编辑项目 项目新值 根据姓名编辑一条记录
		//编辑项目为1到3的整数,1代表编辑电话,2代表编辑班级,3代表编辑宿舍
		if (func == 2)
		{
			cin >> name;
			cin>> choice;
			if (choice == 1)
			{
				cin >> newValue1;
				if (a.checkName(name))//有可能要修改的人不存在
				    a.changeInformation(name, choice, newValue1);
			}
			if (choice == 2||choice==3)
			{
				cin>>newValue2;
				if (a.checkName(name))//有可能要修改的人不存在
				    a.changeInformation(name, choice, newValue2);
			}
		}
		//* 3 姓名 根据姓名查找,找到输出1,未找到输出0
		if (func == 3)
		{
			cin >> name;
			if (a.checkName(name))
			{
				cout<<1<<endl;
			}
			else
				cout<<0<<endl;
		}
		//* 4 班级 输出该班级的所有成员的宿舍号的异或值
		if (func == 4)
		{
			cin>>clas;
			ans = a.xorClas(clas);   //异或这里可能还是有问题
			cout<<ans<<endl;
		}
		
		if (func == 5)
		{
			a.print();
		}
	}
	return 0;
}

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值