信管117118李志荣数据结构实验三---间接寻址实现

本文介绍了一个名为Indirection的C++类的实现细节,该类使用动态数组来管理分数数据。主要内容包括设置、删除、查找、打印和插入操作的具体实现,并提供了一个简单的用户交互界面以演示这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//head.h
const int Maxsize = 5;
class Indirection
{
public:
	Indirection() { count = 0; }
	void Set();
	void Delete();
	void Seek();
	void Print();
	void Insert();
private:
	double* score[Maxsize];
	int count;
};

#include<iostream>
#include"head.h"
using namespace std;
int main()
{
	Indirection Student;
	cout << "MENU";
	int use = 0;
	while (use != 6)
	{
		cout << "\n1.SET\n2.Delete\n3.Seek\n4.Print\n5.Insert\n6.Exit\nNow input a number to use function:";
		cin >> use;
		switch (use)
		{
		case 1:Student.Set(); break;
		case 2:Student.Delete(); break;
		case 3:Student.Seek(); break;
		case 4:Student.Print(); break;
		case 5:Student.Insert(); break;
		default:
			break;
		}
	}
	return 0;
}

//function.cpp
#include<iostream>
#include"head.h"
using namespace std;

void Indirection::Set()
{
	if (count == Maxsize) throw "overflow";
	cout << "enter a score:";
	double *s = new double;
	cin >> *s;
	score[count++] = s;
}
void Indirection::Delete()
{
	if (count == 0) throw "no data";
	cout << "enter the location you want to delete";
	int location;
	cin >> location;
	if (location > Maxsize || location < 0) throw "wrong location";
	if (score[location - 1] != NULL)
	{
		cout << "the score:" << *score[location - 1] << "is destroied";
		score[location - 1] = NULL;
		count--;
	}
	else cout << "no score you can delete";
}
void Indirection::Seek()
{
	if (count == 0) throw "no data";
	cout << "enter the location you want to find";
	int location;
	cin >> location;
	if (location > Maxsize || location < 0) throw "wrong location";
	if (score[location - 1] != NULL)
		cout << "the score:" << *score[location - 1];
}
void Indirection::Print()
{
	if (count == 0) throw "no data";
	cout << "all score:";
	for (int i = 0; i<count;)
	{
		if (score[i] != NULL)
		{
			cout << *score[i] << "    ";
			i++;
		}
	}
}
void Indirection::Insert()
{
	if (count == Maxsize) throw "overflow";
	cout << "enter the location you want to insert";
	int location;
	cin >> location;
	if (location > Maxsize || location < 0) throw "wrong location";
	double *s = new double;
	cout << "the insert score are:";
	cin >> *s;
	if (score[location - 1] == NULL)
	{
		score[location - 1] = s;
		count++;
	}
	else
	{
		for (int i = Maxsize - 1; i >= location - 1; i--)
		{
		score[i] = score[i - 1];
		}
		score[location-1] = s;
		count++;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值