Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <math.h>
- /* ********** Stack handling routines *********/
- // Push element to top of stack
- template<class x>
- int stack_push(x *stk, int &top, x el)
- {
- stk[top] = el;
- return ++top;
- }
- // Get top most element
- template<class x>
- int stack_pop(x *stk, int &top, x &el)
- {
- if(top > 0) el = stk[--top];
- else return -1;
- return top;
- }
- // Get value of top most element
- template<class x>
- int stack_top(x *stk, int top, x &el)
- {
- return (top > 0) ? el = stk[top - 1], top : -1;
- }
- // Find out priority of operation given
- int get_op_priority(char op)
- {
- switch(op)
- {
- case '+':
- case '(':
- case ')':
- return 1;
- case '-':
- return 2;
- case '*':
- return 3;
- case '/':
- return 4;
- case 'sin':
- case 'cos':
- case 'tg':
- case 'ln':
- case 'lg':
- return 99;
- default:
- return 0;
- }
- }
- char atoop(char *str)
- {
- static char *table[] = {"sin", "cos", "tg", "ln", "lg", NULL};
- int i = 0;
- while(table[i] != NULL)
- {
- if(stricmp(str, table[i]) == 0) return i + 1;
- i++;
- }
- return 0;
- }
- // Perform calculation
- float calculate(float a, float b, char op)
- {
- switch(op)
- {
- case '+':
- return a + b;
- case '-':
- return a - b;
- case '*':
- return a * b;
- case '/':
- return a / b;
- case '1':
- return -1;
- case 2: // cos
- return -2;
- case 3: // tg
- return -3;
- case 4: // ln
- return -4;
- case 5: // lg
- return -5;
- default:
- return -1;
- }
- }
- // Main function
- int main(void)
- {
- // Opearands stack
- long operands[128] = {0};
- int opr_top = 0;
- // Opeartions stack
- char operations[128] = {0};
- int ops_top = 0;
- char expr[1024] = {0};
- int i = 0;
- long a = 0, b = 0, c = 0;
- // "345+765" = "345" + "+" + "765"
- // "34*(32+78)" "34*sin(32+78)"
- printf("Enter expression to calculate\n");
- gets(expr);
- stack_push<char>(operations, ops_top, '(');
- a = strlen(expr);
- expr[a] = ')';
- expr[a + 1] = '\0';
- while(expr[i] != '\0')
- {
- char tmpexpr[128];
- int n = 0;
- // If current character is numeric put it to buffer
- while(isdigit(expr[i])) tmpexpr[n++] = expr[i++];
- if(n > 0)
- {
- tmpexpr[n] = '\0';
- stack_push<long>(operands, opr_top, atoi(tmpexpr));
- n = 0;
- }
- else
- {
- char op = 0, op_next = 0, tmpop;
- while(isalpha(expr[i])) tmpexpr[n++] = expr[i++];
- if(n > 0)
- {
- tmpop = atoop(tmpexpr);
- if(tmpop == 0) tmpop = expr[i++];
- }
- // else if(((expr[i] != '(') && (expr[i] != ')')) &&((i > 0) && (!isdigit(expr[i - 1])) || (i < 1)))
- // {
- // stack_push<long>(operands, opr_top, 0);
- // i++;
- // }
- else tmpop = expr[i++];
- switch(tmpop)
- {
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- stack_push<long>(operands, opr_top, 0);
- case '+':
- case '-':
- case '*':
- case '/':
- case '(':
- stack_push<char>(operations, ops_top, tmpop);
- break;
- case ')':
- while(stack_pop<char>(operations, ops_top, op) > 0)
- {
- if(op == '(') break;
- n = stack_top<char>(operations, ops_top, op_next);
- if((n < 1) || (get_op_priority(op) >= get_op_priority(op_next)))
- {
- stack_pop<long>(operands, opr_top, b);
- stack_pop<long>(operands, opr_top, a);
- stack_push<long>(operands, opr_top, calculate(a, b, op));
- }
- else
- {
- stack_pop<char>(operations, ops_top, op_next);
- c = b;
- b = a;
- stack_pop<long>(operands, opr_top, a);
- stack_push<long>(operands, opr_top, calculate(a,b, op_next));
- stack_push<char>(operations, ops_top, op);
- }
- }
- break;
- default:
- printf("Operation is not supported or incorrect symbol \"%c\" at %d\n",
- expr[i], i);
- getch();
- return 0;
- }
- }
- }
- stack_pop<long>(operands, opr_top, a);
- printf("result: %d", a);
- getch();
- return 0;
- }
Add Comment
Please, Sign In to add comment