用顺序表编程实现一个简易的商品管理系统

【实验题目内容】:

用顺序表编程实现一个简易的商品管理系统并完成报告。商品信息包括商品代码、商品名称、价格、库存量等。对商品库存表的管理包括从键盘提取1种商品的信息存入线性表中、对它进理行必要的处理、以及把处理后的结果存入线性表中。假设商品库存表的处理处理包括以下选项:
(1)从键盘提取1种商品的信息存入线性表中;
(2)打印(遍历)库存表;
(3)按商品代码更新记录的当前库存量。若查找到对应的记录,则从键盘上提取其修正量,把它累加到当前库存量域后,再把该记录存入线性表原来的位置;否则,报错。
(4)按商品代码删除指定记录。
(5)在主函数中测试各项功能。

【实验环境】(使用的软件):VS - 2010

【项目设计源代码】:

//所需的头文件
#include<iostream>
#include<string>
#include<cstdlib>
#include "Goods.h"
#include "SeqList.h"

using namespace std;
//Goods.h
class Goods
{
	string ID;
	string Name;
	double Price;
	int Inventory;
public:
	Goods(void);
	~Goods(void);
	void updateID(string id);
	void Print();
	bool operator == (Goods good);
	bool operator != (Goods good);
	friend istream & operator >> (istream & in, Goods & good);
    friend ostream & operator << (ostream & out,Goods & good);
};
//Goods.cpp
Goods::Goods(void)
{
}

Goods::~Goods(void)
{
}

void Goods::updateID(string id)
{
	ID = id;
}

bool Goods::operator == (Goods good)
{
    return ID == good.ID;
}

bool Goods::operator != (Goods good)
{
    return ID != good.ID;
}

istream & operator >> (istream & in, Goods & good)
{
	cout << "商品代码?";
    in >> good.ID;
    cout << "商品名称?";
    in >> good.Name;
    cout << "价格?";
    in >> good.Price;
	cout << "库存量?";
    in >> good.Inventory;
    return in;
}

ostream & operator << (ostream & out, Goods & good)
{
    out << "商品的代码为" << good.ID << ",商品名称为" << good.Name << ",价格为" << good.Price << ",库存量为" << good.Inventory;
    return out;
}
// SeqList.h顺序表类模板头文件
// 顺序表类模板
// T为顺序表中元素的模板类型
template <class T>
class SeqList
{
public:
	// 根据最大容量max(默认值为10000),
	// 构造新的顺序表
	SeqList(long max=10000);
	// 析构函数
	~SeqList(void);
protected:
	T* data;			// 顺序表
	long maxSize;		// 最大容量
	long currentSize;	// 当前元素个数
public:
	// 遍历输出顺序表
	void Print(void);
	// 原有的元素保留不变,扩展顺序表的最大容量,
	// 扩展成功,则返回true,否则,返回false
	bool Expand(void);
	// 根据数组Array中的Count个元素,
	// 构造新的顺序表
	SeqList(T* Array, long Count);
	// 取当前元素个数
	long getCurrentSize(void);
	// 取位置position中的元素,若取成功,
	// 则返回true,该元素为Data;否则,返回false
	bool getData(long position, T& Data);
	// 搜索元素Data,若找到,则返回其位置position,
	// 返回true,否则,返回false
	bool Search(T Data, long& position);
	// 将原来的元素oldData更新为新元素newData,
	// 更新成功则返回true,否则,返回false
	bool Update(T oldData, T newData);
	// 在顺序表中position位置插入新元素newData,插入成功,
	// 则返回true,否则,返回false
	bool Insert(long position, T newData);
	// 删除元素deleteData,删除成功,
	// 则返回true,否则,返回false
	bool Delete(T deleteData);
};

// 根据最大容量max(默认值为10000),
// 构造新的顺序表
template <class T>
SeqList<T>::SeqList(long max)
{
	if(max<1)
	{
		cerr<<"最大容量"<<max<<"不合理。"<<endl;
		// 最大容量不合理,退出
		exit(1);
	}
	// 以max的值初始化顺序表的最大容量
	maxSize = max;
	// 动态分配新数组data,容量为maxSize
	data = new T [maxSize];
	if(data==NULL)
	{
		cerr<<"内存分配失败!"<<endl;
		// 内存分配失败,退出
		exit(1);
	}
	// 当前元素个数初始化为0,即空表
	currentSize = 0;
}

// 析构函数
template <class T>
SeqList<T>::~SeqList(void)
{
	// 释放顺序表的内存空间
	delete [] data;
}

// 遍历输出顺序表
template <class T>
void SeqList<T>::Print(void)
{
	cout<<"顺序表的最大容量为"<<maxSize;
	if(currentSize==0)
	{
		cout<<",顺序表是空表,无任何元素。";
	}
	else
	{
		cout<<",共有"<<currentSize<<"个元素:";
	}
	for(long Index=0;Index<currentSize;Index++)
	{
		cout<<"位置"<<Index+1<<":元素["<<Index<<"]="<<data[Index]<<",";
	}
	cout<<endl;
}

// 原有的元素保留不变,扩展顺序表的最大容量,
// 扩展成功,则返回true,否则,返回false
template <class T>
bool SeqList<T>::Expand(void)
{
	// 动态分配新数组,容量为顺序表原最大容量的2倍
	T* newData = new T [2*maxSize];
	if(newData==NULL)
	{
		cerr<<"内存分配失败!"<<endl;
		// 内存分配失败,返回false
		return false;
	}
	for(long Index=0;Index<currentSize;Index++)
	{
		// 顺序表中原有的全部元素依次复制到新数组中
		newData[Index] = data[Index];
	}
	// 释放原顺序表的内存空间
	delete [] data;
	// 新数组成为新的顺序表
	data = newData;
	// 新顺序表的最大容量为原来的2倍
	maxSize += maxSize;
	// 顺序表容量扩展成功,返回true
	return true;
}

// 根据数组Array中的Count个元素,
// 构造新的顺序表
template <class T>
SeqList<T>::SeqList(T* Array, long Count)
{
	if(Count<1)
	{
		cerr<<"数组中的数据个数"<<Count<<"不合理。"<<endl;
		// 数组中的数据个数"不合理,退出
		exit(1);
	}
	maxSize = 2*Count;
	// 动态分配新数组data,容量为maxSize
	data = new T [maxSize];
	if(data==NULL)
	{
		cerr<<"内存分配失败!"<<endl;
		// 分配失败,退出
		exit(1);
	}
	if(Count>maxSize)
	{
		if(Expand()==false)
		{
			// 数组Array中的Count个元素超过顺序表的最大容量,
			// 则扩展顺序表,扩展失败,则退出程序
			exit(1);
		}
	}
	for(long Index=0;Index<Count;Index++)
	{
		// 数组Array中的Count个元素依次复制到顺序表中
		data[Index] = Array[Index];
	}
	// 当前元素个数初始化为Count的值
	currentSize = Count;
}

// 取当前元素个数
template <class T>
long SeqList<T>::getCurrentSize(void)
{
	// 返回当前元素个数
	return currentSize;
}

// 取位置position中的元素,若取成功,
// 则返回true,该元素为Data;否则,返回false
template <class T>
bool SeqList<T>::getData(long position, T& Data)
{
	if(position<1 || position>currentSize)
	{
		cerr<<"位置"<<position<<"非法。";
		// 位置position非法,返回false
		return false;
	}
	// 元素data[position-1]为Data
	Data = data[position-1];
	// 取位置position中的元素成功,返回true
	return true;
}

// 搜索元素Data,若找到,则返回其位置position,
// 返回true,否则,返回false
template <class T>
bool SeqList<T>::Search(T Data, long& position)
{
	for(long Index=0;Index<currentSize;Index++)
	{
		if(data[Index]==Data)
		{
			// 元素data[Index]等于Data,
			// 将Index+1存入位置position
			position=Index+1;
			// 查找成功,返回true
			return true;
		}
	}
	// 未找到,返回false
	return false;
}

// 将原来的元素oldData更新为新元素newData,
// 更新成功则返回true,否则,返回false
template <class T>
bool  SeqList<T>::Update(T oldData, T newData)
{
	// 元素的位置
	long position;
	if(Search(oldData,position))
	{
		// 找到原来的元素oldData,返回其位置position,
		// 新元素newData存入顺序表中position位置
		data[position-1] = newData;
		// 更新位置为position的元素成功,返回true
		return true;
	}
	else
	{
		// 未消找到要更新的元素oldData,
		// 更新失败,返回false
		return false;
	}
}

// 在顺序表中position位置插入新元素newData,插入成功,
// 则返回true,否则,返回false
template <class T>
bool SeqList<T>::Insert(long position, T newData)
{
	if(position<1 || position>currentSize+1)
	{
		cerr<<"插入位置"<<position<<"非法。";
		// 位置position非法,返回false
		return false;
	}
	if(currentSize==maxSize)
	{
		if(Expand()==false)
		{
			// 当前元素个数等于顺序表的最大容量(表满),
			// 则扩展顺序表,扩展失败,则退出程序
			exit(1);
		}
	}
	for(long Index=currentSize;Index>=position;Index--)
	{
		// 顺序表中位置position以后的元素依次后移1个位置
		data[Index] = data[Index-1];
	}
	// 新元素newData存入顺序表中position位置
	data[position-1] = newData;
	// 当前元素个数增1
	currentSize++;
	// 在位置position插入新元素成功,返回true
	return true;
}

// 删除元素deleteData,删除成功,
// 则返回true,否则,返回false
template <class T>
bool SeqList<T>::Delete(T deleteData)
{
	// 元素的位置
	long position;
	if(Search(deleteData,position))
	{
		do
		{
			// 找到元素deleteData,删除元素,
			// deleteData以后的元素依次前移1个位置
			data[position-1] = data[position];
			// 下一元素
			position++;
		}while(position<currentSize);
		// 当前元素个数减1
		currentSize--;
		// 删除成功,返回true
		return true;
	}
	// 未找到元素deleteData,
	// 未删除任何元素,返回false
	return false;
}
//主函数测试
int _tmain(int argc, _TCHAR* argv[])
{
	//测试
	system("color F0");
	cout << "//测试操作" << endl;
    cout << "测试建立一个简易的商品管理系统goodsList。";
    SeqList<Goods> goodsList;
    cout << "测试遍历输出商品管理系统goodsList:";
    goodsList.Print();
	cout << "测试在商品管理系统goodsList中增加1个商品。";
	cout << endl;
    Goods good, oldGood, newGood, deleteGood;
    cout << "输入新商品的信息:";
	cout << endl;
    cin >> good;
    if(goodsList.Insert(goodsList.getCurrentSize()+1, good))
    {
		cout << "已在商品管理系统goodsList中增加1个新商品。" << good << "。新的商品管理系统goodsList:";
    }
	 else
    {
		cout << "在商品管理系统goodsList中无法插入新商品。" << good <<"。原来的商品管理系统goodsList:";
    }
    goodsList.Print();

	//查询
	cout << endl;
	cout << "//查询操作" << endl;
	string id;
	long position;
    cout << "测试在商品管理系统goodsList中查询商品。" << endl;
	cout << "找什么商品代码的商品?";
    cin >> id;
    good.updateID(id);
    if(goodsList.Search(good, position))
    {
		cout << "找到商品代码为" << id << "的商品。" << good << "。" << endl;
    }
	else
    {
		cout << "未找到商品代码为" << id << "的商品。" << endl;
    }

	//更新
	cout << endl;
	cout << "//更新操作" << endl;
	cout << "测试更新指定商品代码的商品。" << endl;
	cout << "要更新什么商品代码的商品?";
    cin >> id;
    cout << "输入新商品的信息:" << endl;
    cin >> newGood;
    oldGood.updateID(id);
    if(goodsList.Update(oldGood,newGood))
    {
		cout << "商品代码为" << id << "的商品已更新为新商品。" << newGood << "。新的商品管理系统goodsList:";
    }
	    else
    {
		cout << "未找商品代码为" << id << "的商品,未更新任何商品。原来的商品管理系统goodsList:";
    }
    goodsList.Print();

	//删除
	cout << endl;
	cout << "//删除操作" << endl;
	cout << "测试删除指定商品代码的商品。" << endl;
	cout << "要删除什么商品代码的商品?";
    cin >> id;
    deleteGood.updateID(id);
    if(goodsList.Delete(deleteGood))
    {
		cout << "已删除商品代码为" << id << "的商品。" << "新的商品管理系统goodsList:";
    }
	else
	{
		cout << "未找到商品代码为" << id << "的商品。未删除任何商品。原来的商品管理系统goodsList:";
	}   
    goodsList.Print();

    system("PAUSE");
	return 0;
}

【项目测试运行结果截图】:

运行结果截图

备注:博客小白第一次上传东西,目的是想通过写博客来提升自己的代码水平,因此会有十分多的不足,所以请多多指教鸭。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值