第一次写博客,海涵!
这次是第一章的1.1-1.14
这里是头文件模板类的实现,没有主程序,主程序测试写的太乱了,就不上传了。
<头文件 hanshu.h>
#ifndef HANSHU_H
#define HANSHU_H
void swap(int &x, int &y);
//1.2
//模板类,必须把全部实现写入头文件,不可分离
template<class T,int max>
int count(const T (&a)[max] , int b)
{
if (max < 1)
throw "GG";
int i;
int ccount = 1;
for (i = 0; i < max ; i++)
{
if (a[i] == b) ++ccount;
}
return ccount;
}
//1.3
template<class T,int max>
void fill(T (&a)[max], int value)
{
int i;
for (i = 0; i < max ; i++)
{
a[i] = value;
}
}
//1.4
template<class T,int max1 ,int max2>
int inner_product(const T(&a)[max1] ,const T(&b)[max2] )
{
if (max1 != max2)
{
std::cout << "error" << std::endl;
return 0;
}
else
{
int count = 0 ;
for ( int i = 0; i < max1; i++)
{
count = count + a[i] * b[i];
}
return count;
}
}
//1.5
template<class T1 , class T2 ,int max >
void iota(T1(&a)[max], const T2& value)
{
for (int i = 0; i < max; i++)
{
a[i] = a[i] + value;
}
}
//1.6
template<class T, int max >
bool is_sorted(T(&a)[max])
{
for (int i = 0; i < max-1; i++)
{
if (a[i] >= a[i + 1])
return false;
}
return true;
}
//1.7
template<class T1,class T2,int max1 ,int max2>
int mismatch(T1(&a)[max1],T2(&b)[max2])
{
if (max1 != max2)
{
std::cout << "error" << std::endl;
return 0;
}
else
{
for (int i = 0; i < max1; i++)
{
if (a[i] != b[i])
return i+1;
}
}
std::cout << "there is nothing" << std::endl;
return 0;
}
//1.10
template<class T1, class T2, class T3>
int abc1(const T1& a, const T2& b, const T3& c) //加const 的原因是为了接收常量
{
if (a < 0 && b < 0 && c < 0)
throw 1;
if (a == 0 && b == 0 && c == 0)
throw 2;
return a + b * c;
}
//1.12
template<class T >
void make2dArray(T** &x, int numberOfRows, int* &y )
{
//创建行指针
x = new T*[numberOfRows];
//创建相应的
y = new int[numberOfRows];
for (int i = 0; i < numberOfRows; i++)
y[i] = i;
//为每一行分配空间
for (int i = 0; i < numberOfRows; i++)
x[i] = new T[y[i]];
}
//1.13
template<class T>
void changeLength1D(T* &x ,int oldLength, int newLength)
{
//分配一个新的空间
T* y = new T[newLength];
//给旧的分配空间
x = new T[oldLength];
for (int i = 0; i < oldLength; i++)
{
x[i] = i;
}
//找到一个小的
int minL;
if (newLength > oldLength)
minL = oldLength;
else
minL = newLength;
//赋值
for (int i = 0; i < minL; i++)
{
y[i] = x[i];
std::cout << y[i];
}
//释放老数组
delete[] x;
}
//1.14
template<class T>
void changeLength2D(T** &x, int oldhang,int oldlie, int newhang, int newlie)
{
//给新的分配一个空间(行)
T** y = new T*[newhang];
//给每一列分配新的空间
for (int i = 0; i < newhang; i++)
y[i] = new T[newlie];
//给旧的分配空间
x = new T*[oldhang];
for (int i = 0; i < oldhang; i++)
x[i] = new T[oldlie];
for (int i = 0; i < oldhang; i++)
{
for (int j = 0; j < oldlie; j++)
{
x[i][j] = i * j;
}
}
//找到一个小的
int minhang,minlie;
if (newhang > oldhang)
minhang = oldhang;
else
minhang = newhang;
if (newlie > oldlie)
minlie = oldlie;
else
minlie = newlie;
//赋值
for (int i = 0; i < minhang; i++)
{
for (int j = 0; j < minlie; j++)
{
y[i][j] = x[i][j];
std::cout <<i <<' '<<j<<' '<<y[i][j]<<std::endl;
}
}
//清除空间
//先删除行空间,再删除行指针
for (int i = 0; i < oldhang; i++)
{
delete[] x[i];
}
delete[]x;
x = NULL;
}
#endif // !1
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}