来个直观的
/*******************************************************************************
* 有下面结构STRUC_a [ ] 这么一组数,现在要求把中间5个0删掉,
* 用后面3组数前移补充,也就是最后结果成了
* 1,2,3,0,0,4,5,6,0,0,7,8,9,1,0,1,3,5,0,0,2,4,6,0,0,2,4,6,0,0,3,5,7,0,0,
idata struct
{
char arr1[5];
}STRUC_a [ ]={{1,2,3},{4,5,6},{0,0,0,0},{7,8,9,1},{1,3,5},{2,4,6},{3,5,7}};
//有几种算法?哪个是最佳?也就是 code 最小,data 最少。
*******************************************************************************/
//------------------------------------------------------------------------------
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
typedef struct
{
char arr1[5];
} data_typedef;
data_typedef STRUC_a[ ]={{1,2,3},{4,5,6},{0,0,0,0},{7,8,9,1},{1,3,5},{2,4,6},{3,5,7}};
//------------------------------------------------------------------------------
void delstr(void);
//data_typedef *p_delstr(data_typedef *pDataStr,int size1,int n);
void delstr1(data_typedef *pDataStr,int size1,int n);
void Show(data_typedef *pDataStr,int size3);
//------------------------------------------------------------------------------
/*******************************************************************************/
int main(int argc, char *argv[])
{
int sizestr = sizeof(STRUC_a)/sizeof(STRUC_a[0]);
Show(STRUC_a,sizestr);
// SHOW0----------------------------------------
delstr();
Show(STRUC_a,sizestr);
// SHOW1----------------------------------------
// SHOW2----------------------------------------
delstr1(STRUC_a,sizestr,3);
Show(STRUC_a,sizestr);
return 0;
}
void Show(data_typedef *pDataStr,int size3)
{
int size4 = sizeof(pDataStr[0]);
//int size3 = sizeof(pDataStr);
cout <<">>-----------------------------"<<endl;
for (int i=0; i<size3;i++)
{
for(int j=0; j<size4; j++)
{
cout << (int)(pDataStr+i)->arr1[j]<<",";
}
cout << endl;
}
return;
}
//--------------------------------------------------------
void delstr(void)
{
char n;
char *p1, *p2;
p1=(char*)&STRUC_a[2];
p2=(char*)&STRUC_a[3];
for (n=20; n>0; n--)
*p1++ = *p2++;
return;
}
//------------------------------------------------------------
void delstr1(data_typedef *pDataStr,int size1,int n)
{
int size2 = sizeof(pDataStr[0]);
for (int i=n; i<size1;i++)
{
for (int j=0;j<size2;j++)
{
(pDataStr+i-1)->arr1[j] = (pDataStr+i)->arr1[j];
}
}
return;
}
//static data_typedef *pData=NULL;
//pData = new data_typedef[size1]; //申请内存
//delete [] pData; //释放内存
/****************************************************************************/ |