SeqList
调试代码调试有感,注释和文字原创,如果有什么错误欢迎喷我hhhhh
0v0
文章目录
文件:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/60a82650a6c0d3e377eab2f989247652.jpeg)
程序功能:输入
主函数
#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<