Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.41 KB | None | 0 0
  1. include <stdio.h>
  2. #include <mm_malloc.h>
  3. #include <stdbool.h>
  4. #include <memory.h>
  5. #include <ctype.h>
  6.  
  7. typedef enum {
  8.     ADD,
  9.     SUBTRACT,
  10.     MULTIPLY,
  11.     DIVIDE,
  12.     OPEN_BRACKET,
  13.     CLOSE_BRACKET,
  14.     NUMBER
  15. } Token;
  16.  
  17. void checkExceptions(char* expression, bool *flagOfWrongInput, bool *flagOfUndefinedResult, size_t length);
  18. void tokenizingExpression(const char* expression, size_t length, Token* arrayOfTokens, size_t *quantityOfTokens);
  19. Token getNextToken(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  20.                    size_t *quantityOfTokens, Token *currentToken);
  21. int unary(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  22.           size_t *quantityOfTokens, Token *currentToken);
  23. int mulAndDiv(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  24.               size_t *quantityOfTokens, Token *currentToken);
  25. int addAndSub(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  26.               size_t *quantityOfTokens, Token *currentToken;
  27. int parse(const char *expression, const Token *arrayOfTokens, size_t quantityOfTokens, size_t length);
  28.  
  29.  
  30. void checkExceptions(char* expression, bool *flagOfWrongInput, bool *flagOfUndefinedResult, size_t length) {
  31.     int counterOfBrackets = 0;
  32.     if (length == 0) {
  33.         (*flagOfWrongInput)++;
  34.         return;
  35.     }
  36.  
  37.  
  38.     for (int i = 0; i < length; i++) {
  39.         char currentSymbol = expression[i];
  40.         switch (currentSymbol) {
  41.             case '(':
  42.                 if ((i > 0 && isdigit(expression[i - 1])) || expression[i + 1] == ')') {
  43.                     (*flagOfWrongInput)++;
  44.                     return;
  45.                 }
  46.                 counterOfBrackets++;
  47.                 break;
  48.             case ')':
  49.                 if (i < length && isdigit(expression[i + 1])) {
  50.                     (*flagOfWrongInput)++;
  51.                     return;
  52.                 }
  53.                 counterOfBrackets--;
  54.                 if (counterOfBrackets < 0) {
  55.                     (*flagOfWrongInput)++;
  56.                     return;
  57.                 }
  58.                 break;
  59.             case '+':
  60.                 if (i == 0 || i == length) {
  61.                     (*flagOfWrongInput)++;
  62.                 } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
  63.                     (*flagOfWrongInput)++;
  64.                 }
  65.                 break;
  66.             case '-':
  67.                 if (i == 0 || i == length) {
  68.                     (*flagOfWrongInput)++;
  69.                 } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
  70.                     (*flagOfWrongInput)++;
  71.                 }
  72.                 break;
  73.             case '*':
  74.                 if (i == 0 || i == length) {
  75.                     (*flagOfWrongInput)++;
  76.                 } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
  77.                     (*flagOfWrongInput)++;
  78.                 }
  79.                 break;
  80.             case '/':
  81.                 if (i == 0 || i == length) {
  82.                     (*flagOfWrongInput)++;
  83.                 } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
  84.                     (*flagOfWrongInput)++;
  85.                 }
  86.                 if (expression[i + 1] == '0') {
  87.                     (*flagOfUndefinedResult)++;
  88.                     return;
  89.                 }
  90.                 break;
  91.             default:
  92.                 if (!isdigit(expression[i])) {
  93.                     (*flagOfWrongInput)++;
  94.                     return;
  95.                 }
  96.         }
  97.     }
  98.  
  99.  
  100.     if (counterOfBrackets > 0 || getchar() == ' ') {
  101.         (*flagOfWrongInput)++;
  102.         return;
  103.     }
  104. }
  105.  
  106. void tokenizingExpression(const char* expression, size_t length, Token* arrayOfTokens, size_t *quantityOfTokens) {
  107.     size_t currentQuantityOfTokens = 0;
  108.     for (size_t i = 0; i < length; i++) {
  109.         char currentSymbol = expression[i];
  110.         switch (currentSymbol) {
  111.             case '(':
  112.                 arrayOfTokens[currentQuantityOfTokens] = OPEN_BRACKET;
  113.                 break;
  114.             case ')':
  115.                 arrayOfTokens[currentQuantityOfTokens] = CLOSE_BRACKET;
  116.                 break;
  117.             case '+':
  118.                 arrayOfTokens[currentQuantityOfTokens] = ADD;
  119.                 break;
  120.             case '-':
  121.                 arrayOfTokens[currentQuantityOfTokens] = SUBTRACT;
  122.                 break;
  123.             case '*':
  124.                 arrayOfTokens[currentQuantityOfTokens] = MULTIPLY;
  125.                 break;
  126.             case '/':
  127.                 arrayOfTokens[currentQuantityOfTokens] = DIVIDE;
  128.                 break;
  129.             default:
  130.                 while (isdigit(expression[i])) {
  131.                     i++;
  132.                 }
  133.                 i--;
  134.                 arrayOfTokens[currentQuantityOfTokens] = NUMBER;
  135.         }
  136.         currentQuantityOfTokens++;
  137.     }
  138.     (*quantityOfTokens) = currentQuantityOfTokens;
  139. }
  140.  
  141. Token getNextToken(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  142.                    size_t *quantityOfTokens, Token *currentToken)
  143. {
  144.  
  145. }
  146.  
  147. int unary(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  148.           size_t *quantityOfTokens, Token *currentToken)
  149. {
  150.  
  151. }
  152.  
  153. int mulAndDiv(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  154.               size_t *quantityOfTokens, Token *currentToken)
  155. {
  156.  
  157. }
  158.  
  159. int addAndSub(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
  160.                 size_t *quantityOfTokens, Token *currentToken)
  161. {
  162.  
  163.  
  164. }
  165.  
  166. int parse(const char *expression, const Token *arrayOfTokens, size_t quantityOfTokens, size_t length) {
  167.     int currentIndex = 0,
  168.         currentIndexOfToken = 0;
  169.     Token currentToken;
  170.     return addAndSub(expression, &currentIndex, &currentIndexOfToken, arrayOfTokens, &quantityOfTokens, &currentToken);
  171. }
  172.  
  173. int main() {
  174.     bool flagOfWrongInput = false,
  175.          flagOfUndefinedResult = false;
  176.     char* expression = (char*)calloc(1001, sizeof(char));
  177.     if (scanf("%1000s", expression) == 0) {
  178.         printf("bad input");
  179.         free(expression);
  180.         return 0;
  181.     }
  182.  
  183.     size_t lengthOfExpression = strlen(expression);
  184.     checkExceptions(expression, &flagOfWrongInput, &flagOfUndefinedResult, lengthOfExpression);
  185.  
  186.     // Checking before tokenizing
  187.  
  188.     if (flagOfWrongInput) {
  189.         printf("bad input");
  190.         free(expression);
  191.         return 0;
  192.     }
  193.     if (flagOfUndefinedResult) {
  194.         printf("division by zero");
  195.         free(expression);
  196.         return 0;
  197.     }
  198.  
  199.     //Start tokenizing
  200.  
  201.     Token* arrayOfTokens = (Token*)calloc(lengthOfExpression, sizeof(Token));
  202.     size_t quantityOfTokens;
  203.     tokenizingExpression(expression, lengthOfExpression, arrayOfTokens, &quantityOfTokens);
  204.  
  205.     for (size_t i = 0; i < quantityOfTokens; i++) {
  206.         printf("%d ", arrayOfTokens[i]);
  207.     }
  208.  
  209.     //Start parsing
  210.  
  211.  
  212.     free(expression);
  213.     free(arrayOfTokens);
  214.     return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement