#include <iostream>
#include <fstream>
#include <conio.h>
#include <iomanip>
#define getch(); _getch();
using namespace std;
int number = 1;
struct myclass
{
int index = 1;
int *mas_number;
void cout_String(int y)
{
ofstream file("F:\\file.txt",ios::app);
for (int i = 1; i <= y; i++)
{
cout << setw(4) << mas_number[i];
file << setw(4) << mas_number[i];
}
cout << endl;
file << "\n";
file.close();
}
void String()
{
mas_number[index++] = number;
number += 1;
}
void initialization(int y)
{
mas_number = new int[y];
}
};
void part1(myclass *obekt, int y, int x)
{
int c = 2,
i = 1,
k;
if (x >= y)
{
k = y;
if (y % 2 == 0)
k -= 2;
else
k -= 1;
}
else
k = x;
for (; c <= k; c += 2)
{
for (; i <= c; i++)
obekt[i].String();
i -= 2;
for (; i >= 1; i--)
obekt[i].String();
i = +1;
}
}
void part2(myclass *obekt, int y)
{
for (int i = 1; i <= y; i++)
obekt[i].String();
}
void part3(myclass *obekt, int y)
{
int c = 2,
i = y,
k = y;
k -= 1;
for (; c <= k; c += 2)
{
for (; i > c-1; i--)
obekt[i].String();
i += 2;
for (; i <= y; i++)
obekt[i].String();
i = y;
}
}
void part4(myclass *obekt, int y)
{
for (int i = 1; i < y; i++)
obekt[i].String();
for (int i = y; i > 1; i--)
obekt[i].String();
}
void part5(myclass *obekt, int y)
{
int i = 1;
for (int c = 1; c <= y; c += 2)
{
for (; i <= y; i++)
obekt[i].String();
i -= 1;
for (; i > c+2; i--)
obekt[i].String();
i = c + 2;
}
}
void part6(myclass *obekt, int x, int y)
{
bool vipadok = 0;
int i = y + 1,
c = 2;
if (y % 2 == 0)
obekt[1].String();
while (i <= x)
{
if (y % 2 != 0 || (y % 2 == 0 && vipadok == 1))
{
for (int j = i; j >= c; j--)
obekt[j].String();
c += 1;
i += 1;
}
if (i > x)
return;
else
for (int j = c; j <= i; j++)
obekt[j].String();
i += 1;
c += 1;
vipadok = 1;
}
}
void part7(myclass *obekt, int x, int y)
{
int i = y,
k = x;
bool vipadok = 0;
while (x >= i)
{
if (x % 2 == 0 || x % 2 != 0 && vipadok == 1)
for (int j = i; j <= x; j++)
obekt[j].String();
if (x % 2 == 0 || x % 2 != 0 && vipadok == 1)
i += 1;
for (int j = x; j >= i; j--)
obekt[j].String();
i += 1;
vipadok = 1;
}
}
void part8(myclass *obekt, int x, int y, bool napramok)
{
if (napramok == 1)
for (int j = x; j >= 1; j--)
obekt[j].String();
else
for (int j = 1; j <= x; j++)
obekt[j].String();
}
void part9(myclass *obekt, int x, int y, bool napramok)
{
if (napramok == 1)
for (int i = x; i >= y; i--)
obekt[i].String();
else
for (int i = y; i <= x; i++)
obekt[i].String();
}
void initialization(myclass *obekt, int x, int y)
{
for (int i = 1; i <= x; i++)
obekt[i].initialization(y);
}
void cout_String(myclass *obekt, int x, int y)
{
for (int i = 1; i <= x; i++)
obekt[i].cout_String(y);
}
int main()
{
double x, y;
cout << "Enter x , y ->";
cin >> x >> y;
if (x != (int)x || y != (int)y || x <= 0 || y <= 0)
{
cout << "Error!";
getch();
return -1;
}
myclass *obekt;
if (x >= y)
obekt = new myclass[(int)x + 1];
else
obekt = new myclass[(int)y + 1];
initialization(&obekt[0], x, y);
part1(&obekt[0], y, x);
if (y == x)
{
if ((int)y % 2 != 0)
{
part2(&obekt[0], y);
part3(&obekt[0], y);
}
else
{
part4(&obekt[0], y);
part5(&obekt[0], y);
}
}
else
{
if (x > y)
{
if ((int)x % 2 == 0)
{
if ((int)y % 2 != 0)
part2(&obekt[0], y);
else
part4(&obekt[0], y);
part6(&obekt[0], x, y);
if ((int)y % 2 != 0)
part7(&obekt[0], x, (x - y) + 2);
else
part7(&obekt[0], x, (x - y) + 2);
}
else
{
if ((int)y % 2 != 0)
part2(&obekt[0], y);
else
part4(&obekt[0], y);;
part6(&obekt[0], x, y);
if ((int)y % 2 != 0)
part7(&obekt[0], x, (x - y) + 2);
else
part7(&obekt[0], x, (x - y) + 2);
}
}
else
{
if ((int)x % 2 == 0)
{
bool napramok;
for (int i = x; i < y; i++)
{
if (i % 2 == 0)
napramok = 0;
else
napramok = 1;
part8(&obekt[0], x, y, napramok);
}
if ((int)y % 2 == 0)
for (int i = 2; i < y; i++)
{
if (i % 2 == 0)
napramok = 0;
else
napramok = 1;
part9(&obekt[0], x, i, napramok);
}
else
for (int i = 2; i < y; i++)
{
if (i % 2 != 0)
napramok = 0;
else
napramok = 1;
part9(&obekt[0], x, i, napramok);
}
}
else
{
bool napramok;
for (int i = x; i < y+1; i++)
{
if (i % 2 == 0)
napramok = 1;
else
napramok = 0;
part8(&obekt[0], x, y, napramok);
}
if ((int)y % 2 == 0)
for (int i = 2; i < y; i++)
{
if (i % 2 == 0)
napramok = 0;
else
napramok = 1;
part9(&obekt[0], x, i, napramok);
}
else
for (int i = 2; i < y; i++)
{
if (i % 2 != 0)
napramok = 0;
else
napramok = 1;
part9(&obekt[0], x, i, napramok);
}
}
}
}
cout_String(&obekt[0], x, y);
getch();
return 0;
}