Advertisement
Guest User

Untitled

a guest
May 11th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. void main()
  7. {
  8.     int flag,matrixCount=0;
  9.     flag = 1;
  10.  
  11.     char command[81], input[81];
  12.     char matrixAname[81] = { 0 };
  13.     char matrixBname[81] = { 0 };
  14.     int* matrixA = 0;
  15.     int matrixARowLenght = 0;
  16.     int matrixAColLenght = 0;
  17.  
  18.     int* matrixB = 0;
  19.     int matrixBRowLenght = 0;
  20.     int matrixBColLenght = 0;
  21.    
  22.     while (flag == 1)
  23.     {
  24.         printf_s("@");
  25.         memset(input, 0, sizeof(input)); // memset= meapes et hamarach
  26.         memset(command, 0, sizeof(command));
  27.         gets_s(input, sizeof(input));
  28.  
  29.         sscanf_s(input, "%s", command, sizeof(command));
  30.  
  31.         if (strcmp(command,"zeroes") == 0)
  32.         {
  33.             if (matrixCount < 2)
  34.             {
  35.                 char tmpMatrixName[81] = { 0 };
  36.                 int numOfRows = 0;
  37.                 int numOfCols = 0;
  38.                 sscanf_s(input, "%s %s %d %d", command, sizeof(command), tmpMatrixName, sizeof(tmpMatrixName), &numOfRows, &numOfCols);
  39.                 if (strlen(tmpMatrixName)>10)
  40.                 {
  41.                     printf_s("Error: '%s' illegal variable name!\n", tmpMatrixName);
  42.                     continue;
  43.                 }
  44.                 for (int t = 0; t < strlen(tmpMatrixName); t++)
  45.                 {
  46.                     if (!(tmpMatrixName[t]>64 && tmpMatrixName[t]<91) && !(tmpMatrixName[t]>96 && tmpMatrixName[t]<123)) // name witheout numbers
  47.                     {
  48.                         printf_s("Error: '%s' illegal variable name!\n", tmpMatrixName);
  49.                         continue;
  50.                     }
  51.                 }
  52.                 if (numOfRows  == 0 || numOfCols == 0)
  53.                 {
  54.                     printf_s("Error: invalid dimension!\n");
  55.                     continue;
  56.                 }
  57.  
  58.                 if (matrixCount == 0) {
  59.                     // Set's the name
  60.                     for (int t = 0; t < sizeof(tmpMatrixName); t++)
  61.                     {
  62.                         matrixAname[t] = tmpMatrixName[t];
  63.                     }
  64.                     // Set's rows and cols sizes
  65.                     matrixARowLenght = numOfRows;
  66.                     matrixAColLenght = numOfCols;
  67.                     // Set's the matrix
  68.                     matrixA = (int*) malloc(numOfRows * numOfCols * sizeof(int));
  69.                     for (int i = 0; i < matrixARowLenght; i++)
  70.                     {
  71.                         for (int j = 0; j < matrixAColLenght; j++)
  72.                         {
  73.                             *(matrixA + i * matrixAColLenght + j) = 0;
  74.                         }
  75.                     }
  76.                 }
  77.                 else if (matrixCount == 1) {
  78.                     if (strcmp(tmpMatrixName, matrixAname) == 0) {
  79.                         printf_s("Error: variable name '%s' is already in use!\n", tmpMatrixName);
  80.                         continue;
  81.                     }
  82.                     for (int t = 0; t < sizeof(tmpMatrixName); t++)
  83.                     {
  84.                         matrixBname[t] = tmpMatrixName[t];
  85.                     }
  86.                     // Set's rows and cols sizes
  87.                     matrixBRowLenght = numOfRows;
  88.                     matrixBColLenght = numOfCols;
  89.                     matrixB = (int*) malloc(matrixBRowLenght * matrixBColLenght * sizeof(int));
  90.                     for (int i = 0; i < matrixBRowLenght; i++)
  91.                     {
  92.                         for (int j = 0; j < matrixBColLenght; j++)
  93.                         {
  94.                             *(matrixB + i * matrixBColLenght + j) = 0;
  95.                         }
  96.                     }
  97.  
  98.                 }
  99.                 matrixCount++;
  100.             }
  101.             else
  102.             {
  103.                 printf_s("Error: zeroes cannot save more than 2 variables!");
  104.             }
  105.  
  106.         }
  107.  
  108.         else if (strcmp(command, "set") == 0)
  109.         {
  110.             char requestedMatrixName[81] = { 0 };
  111.             sscanf_s(input, "%s %s", command, sizeof(command), requestedMatrixName, sizeof(requestedMatrixName));
  112.            
  113.             int strCmpA = strcmp(matrixAname, requestedMatrixName);
  114.             int strCmpB = strcmp(matrixBname, requestedMatrixName);
  115.  
  116.             if (strCmpA == 0 || strCmpB == 0)
  117.             {
  118.                 if (strCmpA == 0) {
  119.                     for (int i = 0; i < matrixARowLenght; i++)
  120.                     {
  121.                         for (int j = 0; j < matrixAColLenght; j++)
  122.                         {
  123.                             scanf_s("%d", matrixA + i * matrixAColLenght + j);
  124.                         }
  125.                     }
  126.                 }
  127.                 else {
  128.                     for (int i = 0; i < matrixBRowLenght; i++)
  129.                     {
  130.                         for (int j = 0; j < matrixBColLenght; j++)
  131.                         {
  132.                             scanf_s("%d", matrixB + i * matrixBColLenght + j);
  133.                         }
  134.                     }
  135.                 }
  136.                 // Clears the extra new line from the buffer to prevent @@
  137.                 gets_s(input, sizeof(input));
  138.             }
  139.             else
  140.             {
  141.                 printf_s("Error: '%s' - unknown variable!\n", requestedMatrixName);
  142.             }
  143.             continue;
  144.         }
  145.         else if (strcmp(command, "echo") == 0)
  146.         {
  147.             char echoMatrixName[81] = { 0 };
  148.             sscanf_s(input, "%s %s", command, sizeof(command), echoMatrixName, sizeof(echoMatrixName));
  149.             int strCmpA = strcmp(matrixAname, echoMatrixName);
  150.             int strCmpB = strcmp(matrixBname, echoMatrixName);
  151.  
  152.             if (strCmpA == 0 || strCmpB == 0)
  153.             {
  154.                 if (strCmpA == 0) {
  155.                     for (int i = 0; i < matrixARowLenght; i++)
  156.                     {
  157.                         for (int j = 0; j < matrixAColLenght; j++)
  158.                         {
  159.                             printf_s("%d ", *(matrixA+ i* matrixAColLenght +j));
  160.                         }
  161.                         printf_s("\n");
  162.                     }
  163.                 }
  164.                 else {
  165.                     for (int i = 0; i < matrixBRowLenght; i++)
  166.                     {
  167.                         for (int j = 0; j < matrixBColLenght; j++)
  168.                         {
  169.                             printf_s("%d ", *(matrixB + i * matrixBColLenght + j));
  170.                         }
  171.                         printf_s("\n");
  172.                     }
  173.                 }
  174.             }
  175.             else
  176.             {
  177.                 printf_s("Error: '%s' - unknown variable!\n", echoMatrixName);
  178.             }
  179.         }
  180.        
  181.         else if (strcmp(command, "frob") == 0)
  182.         {
  183.             char firstMatrixName[81] = { 0 };
  184.             char secondMatrixName[81] = { 0 };
  185.             sscanf_s(input, "%s %s %s", command, sizeof(command), firstMatrixName, sizeof(firstMatrixName), secondMatrixName, sizeof(secondMatrixName));
  186.             int strCmpAA = strcmp(matrixAname, firstMatrixName);
  187.             int strCmpAB = strcmp(matrixAname, secondMatrixName);
  188.             int strCmpBB = strcmp(matrixBname, secondMatrixName);
  189.             int strCmpBA = strcmp(matrixBname, firstMatrixName);
  190.  
  191.             if (strCmpAA == 0 && strCmpBB == 0)
  192.             {
  193.                 int* matrixATransposed = (int*) malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
  194.                 for (int i = 0; i < matrixARowLenght; i++)
  195.                 {
  196.                     for (int j = 0; j < matrixAColLenght; j++)
  197.                     {
  198.                         *(matrixATransposed + i * matrixAColLenght + j) = *(matrixA + j * matrixAColLenght + i);
  199.                     }
  200.                 }
  201.                 int* res = (int*)malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
  202.                 for (int i = 0; i < matrixAColLenght; i++) {
  203.                     for (int j = 0; j < matrixBColLenght; j++) {
  204.                         *(res + i * matrixARowLenght + j) = 0;
  205.                         for (int k = 0; k < matrixARowLenght; k++)
  206.                             *(res + i * matrixAColLenght + j) += *(matrixATransposed + i * matrixAColLenght + k) * *(matrixB + k * matrixBColLenght + j);
  207.                     }
  208.                 }
  209.                 int sum = 0;
  210.                 for (int i = 0; i < matrixARowLenght; i++) {
  211.                     for (int j = 0; j < matrixAColLenght; j++) {
  212.                         if (i == j) {
  213.                             sum += *(res + i * matrixAColLenght + j);
  214.                         }
  215.                     }
  216.                 }
  217.                 printf_s("Sum: %d\n", sum);
  218.             }
  219.             else if (strCmpAB == 0 && strCmpBA == 0) {
  220.                 printf_s("Kapara Kapara\n");
  221.             }
  222.             else
  223.             {
  224.                 if (strCmpAA != 0 && strCmpBA != 0) {
  225.                     printf_s("Error: '%s' - unknown variable!\n", firstMatrixName);
  226.                 }
  227.                 else if (strCmpAB != 0 && strCmpBB != 0) {
  228.                     printf_s("Error: '%s' - unknown variable!\n", secondMatrixName);
  229.                 }
  230.             }
  231.  
  232.             // Valid matrix dimentions
  233.             if (matrixARowLenght == matrixBRowLenght && matrixAColLenght == matrixBColLenght) {
  234.  
  235.             }
  236.             else {
  237.                 printf_s("Error: matrix dimensions mismatch!\n");
  238.                 continue;
  239.             }
  240.         }
  241.         else if (strcmp(command, "exit") == 0)
  242.         {
  243.             flag = 0;
  244.         }
  245.         else
  246.         {
  247.             if (command[0]!=0)
  248.             {
  249.                 printf_s("Error: illegal command!\n");
  250.             }
  251.         }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement