Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void main()
- {
- int flag,matrixCount=0;
- flag = 1;
- char command[81], input[81];
- char matrixAname[81] = { 0 };
- char matrixBname[81] = { 0 };
- int* matrixA = 0;
- int matrixARowLenght = 0;
- int matrixAColLenght = 0;
- int* matrixB = 0;
- int matrixBRowLenght = 0;
- int matrixBColLenght = 0;
- while (flag == 1)
- {
- printf_s("@");
- memset(input, 0, sizeof(input)); // memset= meapes et hamarach
- memset(command, 0, sizeof(command));
- gets_s(input, sizeof(input));
- sscanf_s(input, "%s", command, sizeof(command));
- if (strcmp(command,"zeroes") == 0)
- {
- if (matrixCount < 2)
- {
- char tmpMatrixName[81] = { 0 };
- int numOfRows = 0;
- int numOfCols = 0;
- sscanf_s(input, "%s %s %d %d", command, sizeof(command), tmpMatrixName, sizeof(tmpMatrixName), &numOfRows, &numOfCols);
- if (strlen(tmpMatrixName)>10)
- {
- printf_s("Error: '%s' illegal variable name!\n", tmpMatrixName);
- continue;
- }
- for (int t = 0; t < strlen(tmpMatrixName); t++)
- {
- if (!(tmpMatrixName[t]>64 && tmpMatrixName[t]<91) && !(tmpMatrixName[t]>96 && tmpMatrixName[t]<123)) // name witheout numbers
- {
- printf_s("Error: '%s' illegal variable name!\n", tmpMatrixName);
- continue;
- }
- }
- if (numOfRows == 0 || numOfCols == 0)
- {
- printf_s("Error: invalid dimension!\n");
- continue;
- }
- if (matrixCount == 0) {
- // Set's the name
- for (int t = 0; t < sizeof(tmpMatrixName); t++)
- {
- matrixAname[t] = tmpMatrixName[t];
- }
- // Set's rows and cols sizes
- matrixARowLenght = numOfRows;
- matrixAColLenght = numOfCols;
- // Set's the matrix
- matrixA = (int*) malloc(numOfRows * numOfCols * sizeof(int));
- for (int i = 0; i < matrixARowLenght; i++)
- {
- for (int j = 0; j < matrixAColLenght; j++)
- {
- *(matrixA + i * matrixAColLenght + j) = 0;
- }
- }
- }
- else if (matrixCount == 1) {
- if (strcmp(tmpMatrixName, matrixAname) == 0) {
- printf_s("Error: variable name '%s' is already in use!\n", tmpMatrixName);
- continue;
- }
- for (int t = 0; t < sizeof(tmpMatrixName); t++)
- {
- matrixBname[t] = tmpMatrixName[t];
- }
- // Set's rows and cols sizes
- matrixBRowLenght = numOfRows;
- matrixBColLenght = numOfCols;
- matrixB = (int*) malloc(matrixBRowLenght * matrixBColLenght * sizeof(int));
- for (int i = 0; i < matrixBRowLenght; i++)
- {
- for (int j = 0; j < matrixBColLenght; j++)
- {
- *(matrixB + i * matrixBColLenght + j) = 0;
- }
- }
- }
- matrixCount++;
- }
- else
- {
- printf_s("Error: zeroes cannot save more than 2 variables!");
- }
- }
- else if (strcmp(command, "set") == 0)
- {
- char requestedMatrixName[81] = { 0 };
- sscanf_s(input, "%s %s", command, sizeof(command), requestedMatrixName, sizeof(requestedMatrixName));
- int strCmpA = strcmp(matrixAname, requestedMatrixName);
- int strCmpB = strcmp(matrixBname, requestedMatrixName);
- if (strCmpA == 0 || strCmpB == 0)
- {
- if (strCmpA == 0) {
- for (int i = 0; i < matrixARowLenght; i++)
- {
- for (int j = 0; j < matrixAColLenght; j++)
- {
- scanf_s("%d", matrixA + i * matrixAColLenght + j);
- }
- }
- }
- else {
- for (int i = 0; i < matrixBRowLenght; i++)
- {
- for (int j = 0; j < matrixBColLenght; j++)
- {
- scanf_s("%d", matrixB + i * matrixBColLenght + j);
- }
- }
- }
- // Clears the extra new line from the buffer to prevent @@
- gets_s(input, sizeof(input));
- }
- else
- {
- printf_s("Error: '%s' - unknown variable!\n", requestedMatrixName);
- }
- continue;
- }
- else if (strcmp(command, "echo") == 0)
- {
- char echoMatrixName[81] = { 0 };
- sscanf_s(input, "%s %s", command, sizeof(command), echoMatrixName, sizeof(echoMatrixName));
- int strCmpA = strcmp(matrixAname, echoMatrixName);
- int strCmpB = strcmp(matrixBname, echoMatrixName);
- if (strCmpA == 0 || strCmpB == 0)
- {
- if (strCmpA == 0) {
- for (int i = 0; i < matrixARowLenght; i++)
- {
- for (int j = 0; j < matrixAColLenght; j++)
- {
- printf_s("%d ", *(matrixA+ i* matrixAColLenght +j));
- }
- printf_s("\n");
- }
- }
- else {
- for (int i = 0; i < matrixBRowLenght; i++)
- {
- for (int j = 0; j < matrixBColLenght; j++)
- {
- printf_s("%d ", *(matrixB + i * matrixBColLenght + j));
- }
- printf_s("\n");
- }
- }
- }
- else
- {
- printf_s("Error: '%s' - unknown variable!\n", echoMatrixName);
- }
- }
- else if (strcmp(command, "frob") == 0)
- {
- char firstMatrixName[81] = { 0 };
- char secondMatrixName[81] = { 0 };
- sscanf_s(input, "%s %s %s", command, sizeof(command), firstMatrixName, sizeof(firstMatrixName), secondMatrixName, sizeof(secondMatrixName));
- int strCmpAA = strcmp(matrixAname, firstMatrixName);
- int strCmpAB = strcmp(matrixAname, secondMatrixName);
- int strCmpBB = strcmp(matrixBname, secondMatrixName);
- int strCmpBA = strcmp(matrixBname, firstMatrixName);
- if (strCmpAA == 0 && strCmpBB == 0)
- {
- int* matrixATransposed = (int*) malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
- for (int i = 0; i < matrixARowLenght; i++)
- {
- for (int j = 0; j < matrixAColLenght; j++)
- {
- *(matrixATransposed + i * matrixAColLenght + j) = *(matrixA + j * matrixAColLenght + i);
- }
- }
- int* res = (int*)malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
- for (int i = 0; i < matrixAColLenght; i++) {
- for (int j = 0; j < matrixBColLenght; j++) {
- *(res + i * matrixARowLenght + j) = 0;
- for (int k = 0; k < matrixARowLenght; k++)
- *(res + i * matrixAColLenght + j) += *(matrixATransposed + i * matrixAColLenght + k) * *(matrixB + k * matrixBColLenght + j);
- }
- }
- int sum = 0;
- for (int i = 0; i < matrixARowLenght; i++) {
- for (int j = 0; j < matrixAColLenght; j++) {
- if (i == j) {
- sum += *(res + i * matrixAColLenght + j);
- }
- }
- }
- printf_s("Sum: %d\n", sum);
- }
- else if (strCmpAB == 0 && strCmpBA == 0) {
- printf_s("Kapara Kapara\n");
- }
- else
- {
- if (strCmpAA != 0 && strCmpBA != 0) {
- printf_s("Error: '%s' - unknown variable!\n", firstMatrixName);
- }
- else if (strCmpAB != 0 && strCmpBB != 0) {
- printf_s("Error: '%s' - unknown variable!\n", secondMatrixName);
- }
- }
- // Valid matrix dimentions
- if (matrixARowLenght == matrixBRowLenght && matrixAColLenght == matrixBColLenght) {
- }
- else {
- printf_s("Error: matrix dimensions mismatch!\n");
- continue;
- }
- }
- else if (strcmp(command, "exit") == 0)
- {
- flag = 0;
- }
- else
- {
- if (command[0]!=0)
- {
- printf_s("Error: illegal command!\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement