Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int calculate(const char* expr, char **result)
- {
- if(expr == NULL || result == NULL)
- return 1;
- if(validate_expression(expr) != 0)
- return 2;
- *result = malloc(2000);
- if(*result == NULL)
- {
- return 3;
- }
- int i = 0;
- char *ex = (char*)expr;
- int flag = 0;
- if(*ex == '-')
- {
- **result = '-';
- ex++;
- flag = 1;
- }
- if(flag == 1)
- i = 1;
- int k = 0;
- while(*ex != '-' && *ex != '*' && *ex != '+' && *ex != '\0')
- {
- *(*result + i) = *ex;
- i++;
- k++;
- ex++;
- }
- *(*result + i) = '\0';
- if(*ex == '\0')
- {
- return 0;
- }
- char *temp;
- char *res;
- temp = malloc(500);
- if(temp == NULL)
- {
- free(result);
- return 3;
- }
- char znak = *ex;
- k = 0;
- while(*(ex + k) != '\0')
- {
- k++;
- }
- for(i = 1; i < k; i++)
- {
- int j = 0;
- while(*(ex + i) != '+' && *(ex + i) != '*' && *(ex + i) != '\0')
- {
- if(*(ex + i) == '-')
- {
- if(j == 0)
- {
- *(temp + j) = *(ex + i);
- i++;
- j++;
- }
- else
- break;
- }
- *(temp + j) = *(ex + i);
- j++;
- i++;
- }
- *(temp + j) = '\0';
- if(znak == '+')
- {
- add(*result, temp, &res);
- strcpy(*result, res);
- free(res);
- }
- else if(znak == '-')
- {
- subtract(*result, temp, &res);
- strcpy(*result, res);
- free(res);
- }
- else
- {
- multiply(*result, temp, &res);
- strcpy(*result, res);
- free(res);
- }
- znak = *(ex + i);
- }
- free(temp);
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement