矩阵深层复制(将一个矩阵复制到另一个矩阵里)由于对象不可以直接赋值所以需要重载运算符,
delete p[];
n = r.n;
p=new int [n];
for(int i=0;i<r.n;i++)
{
this->p[i] = r.p[i];
}
两个矩阵相加
Matrix operator+(const Matrix &m1;consr Matrix &m2)
{
Matrix NewMatrix;
int n=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
{
n = m1.getElement(i,j)+m2.getElement(i,j);
NewMatrix.setElement(i,j,n);
}
return NewMatrix;
}
实现矩阵的行列转换
void Matirx::transpose()
{ for(int i=0;i<M;i++)
for(int j=i+1;j<N;j++)
{
int t1=getElement(i,j);
int t2 =getElement(j,i);
setElement(i,j,t2);
setElement(j,i,t1);
}
}
计数一串字符串中有几个单词
int count =0;
unsigned i=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]==" "')
count=count+1;
}
if(str[i-1]==" "&& str[i]==''\0')
count=count+1;
return count;
往字符串中插入另一串字符串
MinString & MinString::insert(int pos,char *p,int n)
{
MinString temp(str);
delete[] str;
int OldLen;
OldLen =strlen(temp.str);
str = new char [OldLen+n+1];
strncpy(str,temp.str,pos);
str[pos]='\0';
strncat(str,p,n);
str[pos+n]='\0';
strcat(str,temp.str+pos);
str[OldLen+n]='\0';
return *this
}
字符串合并排序
d = new double[len];
while(c < newlen && b < list.len)
{
if (newdata[c] < list.d[b])
{
d[m] = newdata[c];
m++;
c++;
}
else
{
d[m] = list.d[b];
m++;
b++;
}
}
if (c > newlen)
{
for (; b < list.len; b++)
{
d[m] = list.d[b];
m++;
}
}
else
{
for (; c < newlen; c++)
{
d[m] = newdata[c];
m++;
}
}
delete[]newdata;
}
向有序数列中插入单个数值
void IntegerSet::add(int element)
{
int j;
for (j = counter; j > 0; j--)
{
if (element >= elem[j - 1])
break;
if (j > 0)
if (element == = elem[j - 1])
return;
}
for (int k = counter; k > j; k--)
{
elem[k] = elem[k - 1];
elem[j] = element;
counter++;
}
}
有序数列中每次插入任意个数字
void SortedList::insert(double data)
{
for (int i = 0; i < len; i++)
{
if (d[i] == data)
return;
int newlen = len;
len++;
double* newdata;
newdata = new double[newlen];
for (intc = 0; c < newlen; c++)
{
newdata[c] = d[c];
}
int j = 0;
int i = 0;
delete[]d;
将原有的空间释放
d = new double[len];
while (j < len)
if (data > newdata[i])
{
d[j] = newdata[i];
j++;
i++;
}
else
{
d[j] = data;
j++;
for (int a = i; a < len - 1; a++)
{
d[j++] = newdata[a];
}
return;
}
}
}
字符串翻转
int len = strlen(str);
char ch = ' ';
for (int i = 0; i < len / 2; i++)
{
ch = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = ch;
}
str[len] = '\0';
统计字符串中字母出现的次数
void doc::count()
{
int i, j;
for (j = 0; j < length; j++)
{
int(i = 0; i < 26; i++)
{
if (str[j] == i + 0x41 || str[j] == i + 0x61)
{
counter[i]++;
break;
}
}
}
}
字符串完成重载+
MinString operator+(const MinString& AddStr)
{
MinString NewStr("");
if (AddStr.length != 0)
{
if (NewStr.sPtr != 0)
delete[]NewStr.sPtr;
NewStr.length = length + AddStr.length;
NewStr.sPtr = new char[NewStr.length + 1];
strcpy(NewStr.sPtr, AddStr.sPtr);
}
return NewStr;
}
数组中数字各个数字之和是偶数
void Sample::calValue(Sample& s)
{
int qw, bw, sw, gw;
long he = 0;
for (int i = 0; i < Max; i++)
{
qw = s.A[i] / 1000;
bw = (s.A[i] % 1000) / 100;
sw = (s.A[i] % 100) / 10;
gw = (s.a[i] % 10);
if ((qw + bw + sw + gw) % 2 == 0)
{
s.totCnt++;
he = he + s.A[i];
}
}
s.totPjz = (double)he / s.totCnt;
}
200个数字求前二十个数字平均数并输出
void Sample::funSort(Sample &s)
{ double value =0.0;
int i,j,cnt=0;
for(i;i<Max;i++)
{
cnt++;
val += s.A[i];
if(cnt%20==0)
{
s.B[j++] = val/20.;
val = 0;
}
}
for(i=0;i<MAX;i++)
for(j=i+1;j<Max;j++)
{
if(s.B[i]>s.A[j])
{
val = s.B[i];
s.B[i] = s.A[j];
s.A[j] = val;
}
}
}
对数列进行排序,然后将前十个输出
void Sample::funSort(Sample& s)
{
int i, j, k, temp;
for (i = 0; i < Max; i++)
{
k = i;
for (j = i + 1; j < Max; j++)
{
if (s.A[j] < s.A[k])
k = j;
temp = s.A[i];
s.A[i] = s.A[k];
s.A[k] = temp;
}
}
}
将数组从小到大进行排序
unsigned int i, j, temp;
for (i = 0; i < size - 1; i++)
{
for (j = i + 1; j < size; j++)
{
if (data[i] > data[j])
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
|