Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- bool varForExit = false;
- typedef struct _Queue {
- int* stringElem;
- int n;
- } Queue;
- typedef struct _Stak {
- char* stringElem;
- int n;
- } Stack;
- void createQueue(Queue* q) {
- q->stringElem = (int*)calloc(1000, sizeof(int));
- }
- void createStack(Stack* s) {
- s->stringElem = (char*)calloc(1000, sizeof(char));
- }
- void pushQueue(Queue* q, int value) {
- q->stringElem[q->n++] = value;
- }
- void popQueue(Queue* q) {
- if (q->n == 0) {
- varForExit = true;
- }
- q->n--;
- }
- void pushStack(Stack* s, char value) {
- s->stringElem[s->n++] = value;
- }
- void popStack(Stack* s) {
- if (s->n == 0) {
- varForExit = true;
- }
- s->n--;
- }
- int calcOperation(char op, int first, int second) {
- switch (op) {
- case '+':
- return (first + second);
- break;
- case '-':
- return (first - second);
- break;
- case '*':
- return (first * second);
- break;
- case '/':
- if (second == 0) {
- printf("division by zero");
- exit(0);
- }
- return (first / second);
- break;
- default: return 0;
- }
- }
- void checkString(char a) {
- if ((a < '(') || (a > '9') || (a == ',') || (a == '.')) {
- printf("syntax error");
- varForExit = true;
- return;
- }
- }
- int priority(char element) {
- int x;
- switch (element) {
- case '-':
- case '+':
- x = 2;
- break;
- case '/':
- case '*':
- x = 4;
- break;
- case '(':
- case ')':
- x = 1;
- break;
- default: return 0;
- }
- return(x);
- }
- void functionFotExit(Queue* queueForRPN, Queue* queueForAccount, Stack* stackForNumbers, char* string) {
- free(string);
- free(queueForRPN->stringElem);
- free(queueForAccount->stringElem);
- free(stackForNumbers->stringElem);
- free(queueForRPN);
- free(queueForAccount);
- free(stackForNumbers);
- }
- int main() {
- char* string = calloc(1001, sizeof(char));
- if (scanf("%1000s", string) == 0) {
- printf("syntax error");
- free(string);
- return 0;
- }
- if ((char)getchar() != '\n') {
- printf("syntax error");
- free(string);
- return 0;
- }
- int lengthString = strlen(string);
- for (int i = 0; i < lengthString; i++) {
- checkString(string[i]);
- if (varForExit) {
- free(string);
- return 0;
- }
- }
- Queue* queueForRPN = calloc(1, sizeof(Queue));
- Queue* queueForAccount = calloc(1, sizeof(Queue));
- Stack* stackForNumbers = calloc(1, sizeof(Stack));
- createQueue(queueForRPN);
- createQueue(queueForAccount);
- createStack(stackForNumbers);
- int variableForBracket = 0;
- int variableForOperation = 0;
- for (int i = 0; i < lengthString; i++) {
- switch (string[i]) {
- case '+':
- case '-': {
- if (((i > 0) && ((string[i - 1] == '/') || (string[i - 1] == '*') || (string[i - 1] == '+') || (string[i - 1] == '-'))) || (queueForRPN->n == 0)) {
- printf("syntax error");
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- if ((stackForNumbers->n != 0) && (priority(stackForNumbers->stringElem[stackForNumbers->n - 1]) >= priority(string[i]))) {
- pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
- popStack(stackForNumbers);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- }
- pushStack(stackForNumbers, string[i]);
- variableForOperation = 1;
- break;
- }
- case '*':
- case '/': {
- if (((i > 0) && ((string[i - 1] == '/') || (string[i - 1] == '*') || (string[i - 1] == '+') || (string[i - 1] == '-'))) || (queueForRPN->n == 0)) {
- printf("syntax error");
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- if (queueForRPN->n == 0) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- if ((stackForNumbers->n == 0) || (priority(stackForNumbers->stringElem[stackForNumbers->n - 1]) < priority(string[i]))) pushStack(stackForNumbers, string[i]); else {
- if (priority(stackForNumbers->stringElem[stackForNumbers->n - 1]) == priority(string[i])) {
- pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
- popStack(stackForNumbers);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- pushStack(stackForNumbers, string[i]);
- }
- }
- variableForOperation = 1;
- break;
- }
- case '(':
- pushStack(stackForNumbers, string[i]);
- variableForBracket = 1;
- break;
- case ')':
- if (((i > 0) && ((string[i - 1] == '(') || (variableForBracket == 0))) || (i == 0)) {
- printf("syntax error");
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- while ((stackForNumbers->stringElem[stackForNumbers->n - 1]) != '(') {
- pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
- popStack(stackForNumbers);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- }
- popStack(stackForNumbers);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- break;
- default:
- if ((i > 0) && (string[i - 1] >= '0') && (string[i - 1] <= '9')) {
- queueForRPN->stringElem[queueForRPN->n - 1] = (queueForRPN->stringElem[queueForRPN->n - 1]) * 10 + ((int)string[i] - '0');
- }
- else pushQueue(queueForRPN, (int)string[i] - '0');
- }
- }
- if (((queueForRPN->n == 1) && (variableForOperation == 1)) || (queueForRPN->n == 0)) {
- printf("syntax error");
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- if (variableForOperation == 0) {
- for (int i = 0; i < queueForRPN->n; i++) {
- printf("%d", queueForRPN->stringElem[i]);
- }
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- while (stackForNumbers->n > 0) {
- pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
- popStack(stackForNumbers);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- }
- int variable;
- for (int i = 0; i < queueForRPN->n; i++) {
- if (queueForRPN->stringElem[i] < 0) {
- if (queueForAccount->n < 2) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- variable = calcOperation((char)queueForRPN->stringElem[i] + '0', queueForAccount->stringElem[queueForAccount->n - 2], queueForAccount->stringElem[queueForAccount->n - 1]);
- popQueue(queueForAccount);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- popQueue(queueForAccount);
- if (varForExit) {
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
- pushQueue(queueForAccount, variable);
- }
- else {
- pushQueue(queueForAccount, queueForRPN->stringElem[i]);
- }
- }
- printf("%d", queueForAccount->stringElem[queueForAccount->n - 1]);
- functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement