Skip to content

Instantly share code, notes, and snippets.

@ReRandom
Last active December 24, 2016 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReRandom/493b7ef1fe58b5e706ef07ce0b8478b2 to your computer and use it in GitHub Desktop.
Save ReRandom/493b7ef1fe58b5e706ef07ce0b8478b2 to your computer and use it in GitHub Desktop.
dick understand how much the dimension of the matrix
//вычисляет длинну числа, при записи в 10й сс
unsigned int numLen10(long a)
{
if(a == 0) return 1;
unsigned int r = 0;
if(a < 0) { ++r;a *= -1; }
while (a) { ++r; a/=10; }
return r;
}
//вставление числа в строку (без перераспределения памяти)
void addChar(char* str, unsigned long pos, long n, long max = 0)
{
max -= numLen10(n);
if(n == 0)
{
str[pos++] = '0';
while(max-- > 0) str[pos++] = ' ';
return;
}
if(n < 0)
{
str[pos++] = '-';
n *= -1;
}
pos += numLen10(n);
while(max > 0) str[pos+--max] = ' ';
while(n)
{
str[--pos] = static_cast<char>((n % 10) + '0');
n /= 10;
}
}
//вывод прямоугольной матрицы
char** print2(int** mass, unsigned int x, unsigned int y, unsigned int max)
{
char** result = new char*[y+1];
for(decltype(y) i = 0; i < y; ++i)
{
unsigned long pos = 0;
result[i] = new char[x*max + x + 1];
for(decltype(x) j = 0; j < x; ++j)
{
addChar(result[i], pos,mass[i][j], max);
pos += max + 1;
result[i][pos-1] = ' ';
}
result[i][pos] = '\0';
}
result[y] = new char('\0');
return result;
}
char** add_right(char** dest, char** sour)
{
unsigned long ld = 0, ls = 0, h = 0;
if(dest)
while(dest[0][ld++]);
while(sour[0][ls++]);
while(sour[h++][0]);
char** result = new char*[h];
for(decltype(h) i = 0; i < h-1; ++i)
{
result[i] = new char[ls + ld + 1];
if(dest)
{
for(decltype(ld) j = 0; j < ld-1; ++j)
result[i][j] = dest[i][j];
delete[] dest[i];
result[i][ld-1] = '|';
}
for(decltype(ls) j = 0; j < ls; ++j)
result[i][j+ld] = sour[i][j];
delete[] sour[i];
}
result[h-1] = new char('\0');
delete[] sour;
delete[] dest;
return result;
}
char** add_down(char** dest, char** sour)
{
unsigned long ld = 0, ls = 0, hd = 0, hs = 0;
if(dest)
{
while(dest[0][ld++]);
while(dest[hd++][0]);
}
while(sour[0][ls++]);
while(sour[hs++][0]);
char** result = new char*[((dest)?hd : 0) + hs];
if(dest)
{
for(decltype(hd) i = 0; i < hd - 1; ++i)
{
result[i] = new char[(ld > ls) ? ld : ls];
for(decltype(ld) j = 0; j < ld; ++j)
result[i][j] = dest[i][j];
delete[] dest[i];
}
result[hd - ((dest) ? 1 : 0)] = new char[(ld > ls) ? ld : ls];
for(decltype(ls) i = 0; i < ((ls > ld) ? ls : ld); ++i)
{
result[hd - ((dest) ? 1 : 0)][i] = '-';
}
result[hd - ((dest) ? 1 : 0)][((ls > ld) ? ls : ld) - 1] = '\0';
}
for(decltype(hs) i = 0; i < hs-1; ++i)
{
result[i+((dest)?hd:0)] = new char[(ld > ls)? ld : ls];
for(decltype(ls) j = 0; j < ls; ++j)
result[i+((dest)?hd:0)][j] = sour[i][j];
delete[] sour[i];
}
result[((dest)?hd:0) + hs - 1] = new char('\0');
delete[] sour;
delete[] dest;
return result;
}
//рекурсивыный вывод сколькоУгодноМерной матрицы
char** printN(void* mass, unsigned int d, unsigned int* s, unsigned int max)
{
if(d == 2)
return print2(reinterpret_cast<int**>(mass), s[0], s[1], max);
else
{
char** r = nullptr;
if(d % 2)
for(int i = 0; i < s[d-1]; ++i)
r = add_right(r, printN(reinterpret_cast<int**>(mass)[i], d-1, s, max));
else
for(int i = 0; i < s[d-1]; ++i)
r = add_down(r, printN(reinterpret_cast<int**>(mass)[i], d-1, s, max));
return r;
}
}
int main()
{
int***** a = new int****[2];
for(int w = 0; w < 2; ++w)
{
a[w] = new int***[4];
for(int q = 0; q < 4; ++q)
{
a[w][q] = new int **[3];
for(int i = 0; i < 3; ++i)
{
a[w][q][i] = new int *[2];
for(int j = 0; j < 2; ++j)
a[w][q][i][j] = new int[5];
}
}
}
for(int e = 0; e < 2; ++e)
for(int w = 0; w < 4; ++w)
for(int q = 0; q < 3; ++q)
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 5; ++j)
a[e][w][q][i][j] = e*4*3*2*5+ w*3*2*5+5*q*2+i*5+j+1;
unsigned int d[] = {5, 2, 3, 4, 2};
char** r = printN(a, 5, d, 4);
for(int i = 0; *r[i]; ++i)
cout << r[i] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment