Sc3ric

Untitled

Dec 7th, 2022
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <Windows.h>
  4.  
  5. #define swap(x,y) do { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
  6.        memcpy(swap_temp, &y, sizeof(x)); \
  7.        memcpy(&y, &x, sizeof(x)); \
  8.        memcpy(&x, swap_temp, sizeof(x)); \
  9.     } while (0)
  10.  
  11. using namespace std;
  12.  
  13. struct Num {
  14.     int num;
  15.     int color;
  16. };
  17.  
  18. char* replaceComma(char* a, int length) {
  19.     for (int i = 0; i < length; i++) {
  20.         if (a[i] == ',') a[i] = '.';
  21.     }
  22.     return a;
  23. }
  24.  
  25. int findStringLength(char* a, int limit) {
  26.     int i = 0;
  27.     while (a[i] != '\0' && ++i != limit);
  28.     return i;
  29. }
  30.  
  31. char* moveToRight(char* a, int length) {
  32.     int i = length + 1;
  33.  
  34.     while (i-- != 1) a[i] = a[i - 1];
  35.     a[0] = ' ';
  36.  
  37.     return a;
  38. }
  39.  
  40. char* formatString(const char* a, int desiredLength) {
  41.     char* sa = new char[desiredLength + 1];
  42.     sa[desiredLength] = '\0';
  43.     snprintf(sa, desiredLength, "%s", a);
  44.     int numLength = findStringLength(sa, desiredLength);
  45.     int d = desiredLength - numLength;
  46.     for (int i = 0; i < d; i++) {
  47.         sa[numLength + i] = ' ';
  48.     }
  49.  
  50.     int offset = d / 2;
  51.     for (int i = 0; i < offset; i++) sa = moveToRight(sa, numLength + i);
  52.     return sa;
  53. }
  54.  
  55. int intLength(int a) {
  56.     return (a == 0 ? 1 : ceil(log10(a) + 1e-6)) + 2;
  57. }
  58.  
  59. int pickColor(int fg, int bg) {
  60.     return bg * 18 + fg;
  61. }
  62.  
  63. void printArray(Num** a, int n, int m, int MAXNUM) {
  64.     int l;
  65.     char* c;
  66.     int maxL = intLength(MAXNUM);
  67.     for (int i = 0; i < n; i++) {
  68.         for (int j = 0; j < m; j++) {
  69.             l = intLength(a[i][j].num);
  70.             c = new char[l];
  71.             snprintf(c, l, "%d", a[i][j].num);
  72.             c = formatString(c, maxL);
  73.             for (int k = 0; k < maxL; k++) {
  74.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a[i][j].color);
  75.                 //if (c[k] != ' ') {
  76.                 //  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a[i][j].color);
  77.                 //}
  78.                 //else {
  79.                 //  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  80.                 //}
  81.                 cout << c[k];
  82.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  83.             }
  84.         }
  85.         cout << endl;
  86.     }
  87.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  88. }
  89.  
  90. void remakeRows(Num*** array, int n) {
  91.     Num** a = *array;
  92.     int k;
  93.  
  94.     if (n < 4) {
  95.         swap(a[0], a[n-1]);
  96.         return;
  97.     }
  98.  
  99.     for (int i = 0; i < n / 2 - 1; i++) {
  100.         k = n / 2 - 1 - i;
  101.         if (k <= i) break;
  102.         swap(a[i], a[k]);
  103.     }
  104.     for (int i = (n + 1) / 2; i < n - 1; i++) {
  105.         k = (n - 1) - (i - (n + 1) / 2);
  106.         if (k <= i) break;
  107.         swap(a[i], a[k]);
  108.     }
  109. }
  110. void remakeCols(Num*** array, int n, int m) {
  111.     Num** a = *array;
  112.     int k;
  113.  
  114.     if (m < 4) {
  115.         for (int i = 0; i < n; i++) {
  116.             swap(a[i][0], a[i][m-1]);
  117.         }
  118.         return;
  119.     }
  120.    
  121.     for (int j = 0; j < m / 2 - 1; j++) {
  122.         k = m / 2 - 1 - j;
  123.         if (k <= j) break;
  124.         for (int i = 0; i < n; i++) {
  125.             swap(a[i][j], a[i][k]);
  126.         }
  127.        
  128.     }
  129.     for (int j = (m + 1) / 2; j < m - 1; j++) {
  130.         k = (m - 1) - (j - (m + 1) / 2);
  131.         if (k <= j) break;
  132.         for (int i = 0; i < n; i++) {
  133.             swap(a[i][j], a[i][k]);
  134.         }
  135.     }
  136. }
  137.  
  138. bool doubleToIntIfInteger(double a) {
  139.     return (int)a == a;
  140. }
  141.  
  142. void requireGoodInputInt(int* a, int minVal, int maxVal, const char* message, bool getLine = false) {
  143.     double numd = 0;
  144.     int maxL = intLength(maxVal);
  145.     char* c;
  146.     char* eptr;
  147.  
  148.     c = new char[100];
  149.     cout << message << endl;
  150.     getLine ? cin.getline(c, 100) : cin >> c;
  151.     numd = strtod(c, &eptr);
  152.     if (minVal <= numd && maxVal >= numd && *eptr == '\0' && doubleToIntIfInteger(numd)) {
  153.         *a = (int)numd;
  154.     }
  155.  
  156.     while ((*eptr != '\0' || minVal > numd || maxVal < numd || !doubleToIntIfInteger(numd)) && !(getLine && c[0] == '\0')) {
  157.         c = new char[100];
  158.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
  159.         cout << "Bad number. ";
  160.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  161.         cout << message << endl;
  162.         getLine ? cin.getline(c, 100) : cin >> c;
  163.         numd = strtod(c, &eptr);
  164.         if (minVal <= numd && maxVal >= numd && *eptr == '\0' && doubleToIntIfInteger(numd)) {
  165.             *a = (int)numd;
  166.         }
  167.     }
  168. }
  169.  
  170. int main() {
  171.     srand(time(NULL));
  172.     CONSOLE_SCREEN_BUFFER_INFO start_attribute;
  173.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &start_attribute);
  174.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  175.  
  176.     int n = 5, m = 6;
  177.     int randMax = 10;
  178.     bool useColorsB = 0;
  179.     cout << "Array NxM" << endl;
  180.     cout << "1 <= N,M <= 20" << endl;
  181.     cout << "1 <= RAND_MAX <= 1e10" << endl;
  182.     cout << endl;
  183.  
  184.     requireGoodInputInt(&n, 1, 20, "Enter N");
  185.     requireGoodInputInt(&m, 1, 20, "Enter M");
  186.  
  187.     char* temp = new char[100];
  188.     cin.getline(temp, 100);
  189.  
  190.    
  191.     requireGoodInputInt(&randMax, 1, 1e10, "Enter Rand max (optionally) default=10", true);
  192.  
  193.     if (n < 18 && m < 18) {
  194.         cout << "Use colors? (y/n) default=n" << endl;
  195.         char* useColors = new char[100];
  196.         cin.getline(useColors, 100);
  197.         if (useColors[0] != '\0' && useColors[1] == '\0') {
  198.             useColorsB = useColors[0] == 'y';
  199.         }
  200.     }
  201.     else {
  202.         cout << "Can't use colors for array with one side greater than 17" << endl;
  203.     }
  204.  
  205.     cout << endl;
  206.     Num** a = new Num*[n];
  207.     int MAX = 0;
  208.  
  209.     for (int i = 0; i < n; i++) {
  210.         a[i] = new Num[m];
  211.         for (int j = 0; j < m; j++) {
  212.             a[i][j].num = round((double)rand() / RAND_MAX * randMax);
  213.             a[i][j].color = useColorsB ? pickColor(0, 18-min(i+1,min(j+1,min(n-i,m-j)))) : pickColor(7, 0);
  214.             MAX = max(MAX, a[i][j].num);
  215.         }
  216.     }
  217.     printArray(a, n, m, MAX);
  218.     cout << endl;
  219.     remakeRows(&a, n);
  220.     remakeCols(&a, n, m);
  221.     printArray(a, n, m, MAX);
  222.     cout << endl;
  223.  
  224.     cout << "Base console color: " << start_attribute.wAttributes << endl;
  225.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), start_attribute.wAttributes);
  226.  
  227.     return 0;
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment