Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <stdio.h>
- #include <mm_malloc.h>
- #include <stdbool.h>
- #include <memory.h>
- #include <ctype.h>
- typedef enum {
- ADD,
- SUBTRACT,
- MULTIPLY,
- DIVIDE,
- OPEN_BRACKET,
- CLOSE_BRACKET,
- NUMBER
- } Token;
- void checkExceptions(char* expression, bool *flagOfWrongInput, bool *flagOfUndefinedResult, size_t length);
- void tokenizingExpression(const char* expression, size_t length, Token* arrayOfTokens, size_t *quantityOfTokens);
- Token getNextToken(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken);
- int unary(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken);
- int mulAndDiv(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken);
- int addAndSub(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken;
- int parse(const char *expression, const Token *arrayOfTokens, size_t quantityOfTokens, size_t length);
- void checkExceptions(char* expression, bool *flagOfWrongInput, bool *flagOfUndefinedResult, size_t length) {
- int counterOfBrackets = 0;
- if (length == 0) {
- (*flagOfWrongInput)++;
- return;
- }
- for (int i = 0; i < length; i++) {
- char currentSymbol = expression[i];
- switch (currentSymbol) {
- case '(':
- if ((i > 0 && isdigit(expression[i - 1])) || expression[i + 1] == ')') {
- (*flagOfWrongInput)++;
- return;
- }
- counterOfBrackets++;
- break;
- case ')':
- if (i < length && isdigit(expression[i + 1])) {
- (*flagOfWrongInput)++;
- return;
- }
- counterOfBrackets--;
- if (counterOfBrackets < 0) {
- (*flagOfWrongInput)++;
- return;
- }
- break;
- case '+':
- if (i == 0 || i == length) {
- (*flagOfWrongInput)++;
- } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
- (*flagOfWrongInput)++;
- }
- break;
- case '-':
- if (i == 0 || i == length) {
- (*flagOfWrongInput)++;
- } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
- (*flagOfWrongInput)++;
- }
- break;
- case '*':
- if (i == 0 || i == length) {
- (*flagOfWrongInput)++;
- } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
- (*flagOfWrongInput)++;
- }
- break;
- case '/':
- if (i == 0 || i == length) {
- (*flagOfWrongInput)++;
- } else if ((!isdigit(expression[i - 1]) && expression[i - 1] != ')') || (!isdigit(expression[i + 1]) && expression[i + 1] != '(')) {
- (*flagOfWrongInput)++;
- }
- if (expression[i + 1] == '0') {
- (*flagOfUndefinedResult)++;
- return;
- }
- break;
- default:
- if (!isdigit(expression[i])) {
- (*flagOfWrongInput)++;
- return;
- }
- }
- }
- if (counterOfBrackets > 0 || getchar() == ' ') {
- (*flagOfWrongInput)++;
- return;
- }
- }
- void tokenizingExpression(const char* expression, size_t length, Token* arrayOfTokens, size_t *quantityOfTokens) {
- size_t currentQuantityOfTokens = 0;
- for (size_t i = 0; i < length; i++) {
- char currentSymbol = expression[i];
- switch (currentSymbol) {
- case '(':
- arrayOfTokens[currentQuantityOfTokens] = OPEN_BRACKET;
- break;
- case ')':
- arrayOfTokens[currentQuantityOfTokens] = CLOSE_BRACKET;
- break;
- case '+':
- arrayOfTokens[currentQuantityOfTokens] = ADD;
- break;
- case '-':
- arrayOfTokens[currentQuantityOfTokens] = SUBTRACT;
- break;
- case '*':
- arrayOfTokens[currentQuantityOfTokens] = MULTIPLY;
- break;
- case '/':
- arrayOfTokens[currentQuantityOfTokens] = DIVIDE;
- break;
- default:
- while (isdigit(expression[i])) {
- i++;
- }
- i--;
- arrayOfTokens[currentQuantityOfTokens] = NUMBER;
- }
- currentQuantityOfTokens++;
- }
- (*quantityOfTokens) = currentQuantityOfTokens;
- }
- Token getNextToken(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken)
- {
- }
- int unary(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken)
- {
- }
- int mulAndDiv(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken)
- {
- }
- int addAndSub(const char* expression, int *currentIndex, int *currentIndexOfToken, const Token* arrayOfTokens,
- size_t *quantityOfTokens, Token *currentToken)
- {
- }
- int parse(const char *expression, const Token *arrayOfTokens, size_t quantityOfTokens, size_t length) {
- int currentIndex = 0,
- currentIndexOfToken = 0;
- Token currentToken;
- return addAndSub(expression, ¤tIndex, ¤tIndexOfToken, arrayOfTokens, &quantityOfTokens, ¤tToken);
- }
- int main() {
- bool flagOfWrongInput = false,
- flagOfUndefinedResult = false;
- char* expression = (char*)calloc(1001, sizeof(char));
- if (scanf("%1000s", expression) == 0) {
- printf("bad input");
- free(expression);
- return 0;
- }
- size_t lengthOfExpression = strlen(expression);
- checkExceptions(expression, &flagOfWrongInput, &flagOfUndefinedResult, lengthOfExpression);
- // Checking before tokenizing
- if (flagOfWrongInput) {
- printf("bad input");
- free(expression);
- return 0;
- }
- if (flagOfUndefinedResult) {
- printf("division by zero");
- free(expression);
- return 0;
- }
- //Start tokenizing
- Token* arrayOfTokens = (Token*)calloc(lengthOfExpression, sizeof(Token));
- size_t quantityOfTokens;
- tokenizingExpression(expression, lengthOfExpression, arrayOfTokens, &quantityOfTokens);
- for (size_t i = 0; i < quantityOfTokens; i++) {
- printf("%d ", arrayOfTokens[i]);
- }
- //Start parsing
- free(expression);
- free(arrayOfTokens);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement