Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <Windows.h>
- #define swap(x,y) do { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
- memcpy(swap_temp, &y, sizeof(x)); \
- memcpy(&y, &x, sizeof(x)); \
- memcpy(&x, swap_temp, sizeof(x)); \
- } while (0)
- using namespace std;
- struct Num {
- int num;
- int color;
- };
- char* replaceComma(char* a, int length) {
- for (int i = 0; i < length; i++) {
- if (a[i] == ',') a[i] = '.';
- }
- return a;
- }
- int findStringLength(char* a, int limit) {
- int i = 0;
- while (a[i] != '\0' && ++i != limit);
- return i;
- }
- char* moveToRight(char* a, int length) {
- int i = length + 1;
- while (i-- != 1) a[i] = a[i - 1];
- a[0] = ' ';
- return a;
- }
- char* formatString(const char* a, int desiredLength) {
- char* sa = new char[desiredLength + 1];
- sa[desiredLength] = '\0';
- snprintf(sa, desiredLength, "%s", a);
- int numLength = findStringLength(sa, desiredLength);
- int d = desiredLength - numLength;
- for (int i = 0; i < d; i++) {
- sa[numLength + i] = ' ';
- }
- int offset = d / 2;
- for (int i = 0; i < offset; i++) sa = moveToRight(sa, numLength + i);
- return sa;
- }
- int intLength(int a) {
- return (a == 0 ? 1 : ceil(log10(a) + 1e-6)) + 2;
- }
- int pickColor(int fg, int bg) {
- return bg * 18 + fg;
- }
- void printArray(Num** a, int n, int m, int MAXNUM) {
- int l;
- char* c;
- int maxL = intLength(MAXNUM);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- l = intLength(a[i][j].num);
- c = new char[l];
- snprintf(c, l, "%d", a[i][j].num);
- c = formatString(c, maxL);
- for (int k = 0; k < maxL; k++) {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a[i][j].color);
- //if (c[k] != ' ') {
- // SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a[i][j].color);
- //}
- //else {
- // SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- //}
- cout << c[k];
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- }
- }
- cout << endl;
- }
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- }
- void remakeRows(Num*** array, int n) {
- Num** a = *array;
- int k;
- if (n < 4) {
- swap(a[0], a[n-1]);
- return;
- }
- for (int i = 0; i < n / 2 - 1; i++) {
- k = n / 2 - 1 - i;
- if (k <= i) break;
- swap(a[i], a[k]);
- }
- for (int i = (n + 1) / 2; i < n - 1; i++) {
- k = (n - 1) - (i - (n + 1) / 2);
- if (k <= i) break;
- swap(a[i], a[k]);
- }
- }
- void remakeCols(Num*** array, int n, int m) {
- Num** a = *array;
- int k;
- if (m < 4) {
- for (int i = 0; i < n; i++) {
- swap(a[i][0], a[i][m-1]);
- }
- return;
- }
- for (int j = 0; j < m / 2 - 1; j++) {
- k = m / 2 - 1 - j;
- if (k <= j) break;
- for (int i = 0; i < n; i++) {
- swap(a[i][j], a[i][k]);
- }
- }
- for (int j = (m + 1) / 2; j < m - 1; j++) {
- k = (m - 1) - (j - (m + 1) / 2);
- if (k <= j) break;
- for (int i = 0; i < n; i++) {
- swap(a[i][j], a[i][k]);
- }
- }
- }
- bool doubleToIntIfInteger(double a) {
- return (int)a == a;
- }
- void requireGoodInputInt(int* a, int minVal, int maxVal, const char* message, bool getLine = false) {
- double numd = 0;
- int maxL = intLength(maxVal);
- char* c;
- char* eptr;
- c = new char[100];
- cout << message << endl;
- getLine ? cin.getline(c, 100) : cin >> c;
- numd = strtod(c, &eptr);
- if (minVal <= numd && maxVal >= numd && *eptr == '\0' && doubleToIntIfInteger(numd)) {
- *a = (int)numd;
- }
- while ((*eptr != '\0' || minVal > numd || maxVal < numd || !doubleToIntIfInteger(numd)) && !(getLine && c[0] == '\0')) {
- c = new char[100];
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
- cout << "Bad number. ";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- cout << message << endl;
- getLine ? cin.getline(c, 100) : cin >> c;
- numd = strtod(c, &eptr);
- if (minVal <= numd && maxVal >= numd && *eptr == '\0' && doubleToIntIfInteger(numd)) {
- *a = (int)numd;
- }
- }
- }
- int main() {
- srand(time(NULL));
- CONSOLE_SCREEN_BUFFER_INFO start_attribute;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &start_attribute);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- int n = 5, m = 6;
- int randMax = 10;
- bool useColorsB = 0;
- cout << "Array NxM" << endl;
- cout << "1 <= N,M <= 20" << endl;
- cout << "1 <= RAND_MAX <= 1e10" << endl;
- cout << endl;
- requireGoodInputInt(&n, 1, 20, "Enter N");
- requireGoodInputInt(&m, 1, 20, "Enter M");
- char* temp = new char[100];
- cin.getline(temp, 100);
- requireGoodInputInt(&randMax, 1, 1e10, "Enter Rand max (optionally) default=10", true);
- if (n < 18 && m < 18) {
- cout << "Use colors? (y/n) default=n" << endl;
- char* useColors = new char[100];
- cin.getline(useColors, 100);
- if (useColors[0] != '\0' && useColors[1] == '\0') {
- useColorsB = useColors[0] == 'y';
- }
- }
- else {
- cout << "Can't use colors for array with one side greater than 17" << endl;
- }
- cout << endl;
- Num** a = new Num*[n];
- int MAX = 0;
- for (int i = 0; i < n; i++) {
- a[i] = new Num[m];
- for (int j = 0; j < m; j++) {
- a[i][j].num = round((double)rand() / RAND_MAX * randMax);
- a[i][j].color = useColorsB ? pickColor(0, 18-min(i+1,min(j+1,min(n-i,m-j)))) : pickColor(7, 0);
- MAX = max(MAX, a[i][j].num);
- }
- }
- printArray(a, n, m, MAX);
- cout << endl;
- remakeRows(&a, n);
- remakeCols(&a, n, m);
- printArray(a, n, m, MAX);
- cout << endl;
- cout << "Base console color: " << start_attribute.wAttributes << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), start_attribute.wAttributes);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment