作业实现
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_STR_LEN (260)
class HadesArray
{
private:
int* _iData;
size_t _iLen;
size_t _index;
public:
HadesArray(size_t iLen = 5)
{
_iLen = iLen;
_index = 0;
_iData = new int[_iLen];
}
~HadesArray()
{
if (_iData)
delete[] _iData;
}
// 后边课程会使用运算符的重载
int GetData(const size_t index) const
{
if (index < _iLen)
return _iData[index];
return 0;
}
// 后边课程会使用运算符的重载
void SetData(const size_t index, const int iData)
{
if (index < _iLen)
_iData[index] = iData;
}
size_t GetLen() const
{
return _index;
}
void AddData(const int iData)
{
if (_index >= _iLen)
{
_iLen = _index * 2;
// 每次分配2倍INDEX的空间
// 5 * 2 * 2 * 2 效率最高~
int* tmp = new int[_iLen];
memset(tmp, 0x00, sizeof(int) * _iLen);
memcpy(tmp, _iData, _index * sizeof(int));
delete[] _iData;
_iData = tmp;
}
_iData[_index++] = iData;
}
void AddData(const HadesArray& data)
{
for (size_t i = 0; i < data._index; ++i)
{
// 在这里 可以使用private的_index
// 因为是在同一个类中
AddData(data.GetData(i));
}
}
void AddData(const int* data, size_t len)
{
for (size_t i = 0; i < len; ++i)
{
AddData(data[i]);
}
}
void PrintData(const size_t len = 0) const
{
for (size_t i = 0; i < ((len == 0 || len > _index) ? _index : len); ++i)
{
cout << _iData[i] << " ";
if ((i % 20 == 0) && (i != 0))
cout << endl;
}
cout << endl;
}
void PushData(const float fVal)
{
PushData(static_cast<double>(fVal));
}
void PushData(const double dVal)
{
AddData(static_cast<int>(dVal));
}
void PushData(const char cVal)
{
AddData(static_cast<int>(cVal));
}
void PushData(const char* szVal)
{
for (size_t i = 0; i < strlen(szVal); ++i)
{
if (i > MAX_STR_LEN)
{
cout << "字符串超长!已强制退出方法!" << endl;
break;
}
PushData(szVal[i]);
}
}
// Index 从0开始
void InsertData(const int iData, const size_t index)
{
if (index < _index)
{
// 需要对数组进行拷贝
_iLen += 1;
int* tmp = new int[_iLen];
memset(tmp, 0x00, sizeof(int) * _iLen);
memcpy(tmp, _iData, index * sizeof(int));
tmp[index] = iData;
memcpy(tmp + index + 1, _iData + index, (_index - index) * sizeof(int));
delete[] _iData;
_iData = tmp;
}
else if (index == _index)
{
AddData(iData);
}
else // index > _index
{
if (index < _iLen)
{
_index = index;
AddData(iData);
}
else
{
// 重新分配空间
_iLen = index + 1;
int* tmp = new int[_iLen];
memset(tmp, 0x00, sizeof(int) * _iLen);
memcpy(tmp, _iData, _index * sizeof(int));
delete[] _iData;
_iData = tmp;
_iData[(_index = index)] = iData;
}
}
}
void InsertData(const float fData, const size_t index)
{
InsertData(static_cast<double>(fData), index);
}
void InsertData(const double dData, const size_t index)
{
InsertData(static_cast<int>(dData), index);
}
void InsertData(const char cData, const size_t index)
{
InsertData(static_cast<int>(cData), index);
}
void InsertData(const char* szData, const size_t index)
{
for (size_t i = 0; i < strlen(szData); ++i)
{
if (i > MAX_STR_LEN)
{
cout << "字符串超长!已强制退出方法!" << endl;
break;
}
InsertData(szData[i], index);
}
}
};
int main(int argc, char* argv[])
{
HadesArray arrayTest;
cout << "==============AddData 1--15==============" << endl;
arrayTest.AddData(1);
arrayTest.AddData(2);
arrayTest.AddData(3);
arrayTest.AddData(4);
arrayTest.AddData(5);
arrayTest.AddData(6);
arrayTest.AddData(7);
arrayTest.AddData(8);
arrayTest.AddData(9);
arrayTest.AddData(10);
arrayTest.AddData(11);
arrayTest.AddData(12);
arrayTest.AddData(13);
arrayTest.AddData(14);
arrayTest.AddData(15);
arrayTest.PrintData();
HadesArray arrayAdd;
arrayAdd.AddData(16);
arrayAdd.AddData(17);
arrayAdd.AddData(18);
arrayAdd.AddData(19);
arrayAdd.AddData(20);
arrayAdd.AddData(21);
cout << "-------------要添加的HadesArray为:-------------" << endl;
arrayAdd.PrintData();
cout << "==============AddHadesArray==============" << endl;
arrayTest.AddData(arrayAdd);
arrayTest.PrintData();
cout << "==============Add int[]==============" << endl;
int iVals[20] = { 0 };
for (size_t i = 0; i < 20; ++i)
{
iVals[i] = 666;
}
arrayTest.AddData(iVals, 20);
arrayTest.PrintData();
cout << "==============PUSH float 666.666==============" << endl;
arrayTest.PushData(666.666f);
arrayTest.PrintData();
cout << "==============PUSH double 66.66666==============" << endl;
arrayTest.PushData(666.66666);
arrayTest.PrintData();
cout << "==============PUSH char H==============" << endl;
arrayTest.PushData('H');
arrayTest.PrintData();
cout << "==============PUSH char* HXHLB==============" << endl;
arrayTest.PushData("HXHLB");
arrayTest.PrintData();
cout << "==============Insert by Idx 3 int 666==============" << endl;
arrayTest.InsertData(666, 3);
arrayTest.PrintData();
cout << "==============Insert by Idx 5 float 666.666F==============" << endl;
arrayTest.InsertData(666.666f, 5);
arrayTest.PrintData();
cout << "==============Insert by Idx 7 double 888.888==============" << endl;
arrayTest.InsertData(888.888, 7);
arrayTest.PrintData();
cout << "==============Insert by Idx 9 char H==============" << endl;
arrayTest.InsertData('H', 9);
arrayTest.PrintData();
cout << "==============Insert by Idx 11 char* Hades==============" << endl;
arrayTest.InsertData("Hades", 11);
arrayTest.PrintData();
return 0;
}
本文由 花心胡萝卜 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2017-01-05 at 02:47 pm