【殷人昆数据结构】第二章2.1 顺序表代码的调试

SeqList

调试代码调试有感,注释和文字原创,如果有什么错误欢迎喷我hhhhh
0v0


文件:
在这里插入图片描述
程序功能:输入

主函数

#include "SeqList.h"
#include <fstream>
#include <cassert>
using namespace std;

int main(){
   
	SeqList<int> list(5);        //实例化SeqList类为int型
	ifstream fin("list.txt");    //调用文件输入流ifstream,将txt输入缓存区fin
	assert(fin);                 //用于定位程序错误,如果fin为空或不存在则会终止程序并报错
	fin >> list;                 //调用了重载后的输入流运算符>>,将缓存区中的数据输入list
	cout << "The initial list in the file is:\n" << list << endl;
	list.Sort();
	cout << "After sorted:\n" << list << endl;

	cout << "========================================\n";
	int i, elem;
	cout << "Test the Insert, Remove and Search function:\n";
	cout << "Each test will terminate by an invaid input.";
	cout << "\n----------------------------------------\n";
	
	cout << "1. Test the Insert(int i, T &elem):\n";  
	while (1)	{
   
		cout << "Input the index i and data elem to insert: ";
		cin >> i >> elem;
		if (!cin)		{
   
			cin.clear();
			cin.ignore(100,'\n');
			break;
		}
		if (i < 0)	break;
		if (list.Insert(i, elem))	cout << "Insert successful!\n";
		else	cout << "Insert failed!\n";
	}
	cout << "\nAfter inserted\n" << list << endl;

	cout << "----------------------------------------\n";
	cout << "2. Test the Remove(int i, T &elem):\n";
	while (1)	{
   
		cout << "Input the index i in which you want to remove: ";
		cin >> i;
		if (!cin)		{
   
			cin.clear();
			cin.ignore(100,'\n');
			break;
		}
		if (i < 0)	break;
		if (list.Remove(i, elem))	cout << "The element " << elem << " has been removed!\n";
		else	cout << "Remove failed!\n";
	}
	cout << "\nAfter removed\n" << list << endl;

	cout << "----------------------------------------\n";
	cout << "3. Test the Search(T &elem):\n";
	while (1){
   
		cout << "Input the element you want to search: ";
		cin >> elem;
		if (!cin){
   
			cin.clear();
			cin.ignore(100,'\n');
			break;
		}
		if (elem < 0)	break;
		i = list.Search(elem);
		if (i != 0)	cout << "The index of element " << elem << " is " << i << ".\n";
		else	cout << "The element is not exist!\n";
	}
	cout << "\n----------------------------------------\n";
	cout << "End test!" << endl;
	return 0;
}

上面if(!cin)以下的语句是为了判断是否输入正确。如果输入错误的类型,比如规定输入 int类型却输入了char 型数据,则会出现cin输入错误。遇到输入类型不匹配,if(!cin)将报错,数据元素failbit被设定了为1。因此需要用clear()把被设定的failbit修改到原来的状态,然后跳出输入循环。
但是输入的错误数据仍然留在缓存区上,不清理则会赋值给下一个cin。
cin.ignore(100,'\n');的作用是清除输入流中换行标记前面的字符,将换行符前面的错误数据清除。

提问1:为什么最开始List容量设置为5?
SeqList<int> list(5); //实例化SeqList类为int型
后面>>运算符定义中,空间不够会变为原来空间的倍数,因此推荐写list.txt中元素数量的最大公约数,或者干脆分配所需要的足够大的空间。

这是头文件中,SeqList类的定义(后面会逐个函数逐条分析):

const int defaultSize = 100;
template <typename T>class SeqList{
   
protected:
	T *data;                          //指向每一个结点的指针
	int maxSize;                      //顺序表的容量
	int last;                         //有元素的结点的个数-1
public:
	SeqList(int sz = defaultSize);    //构造函数,默认大小为defaultSize
	SeqList(SeqList<T> &L);           //拷贝构造函数
	~SeqList(){
   
		delete []data;                //析构函数,清除数据成员data
	}
	void reSize(int newSize);         
	int Size() const{
   
		return maxSize;
	}
	int Length()const{
   
		return last+1;
	}
	int Search(T &x) const;
	int Locate(int i) const;
	bool getData(int i,T&x) const{
   
		if(i>0 && i<=last+1){
   
			x=data[i-1];
			return true;
		}
		else return false;
	}
	void setData(int i, T &x<
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值