Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // * * Bismillahir Rahmanir Rahim * *
- // ************************@Author*******************************
- /* Asik Ifthaker Hamim */
- #include <bits/stdc++.h>
- #include<stdlib.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long int ulli;
- typedef long long int lli;
- typedef unsigned long long ull;
- typedef vector<int> vi;
- typedef vector<ll> vl;
- typedef vector<string> vs;
- typedef pair<ll,ll> pr;
- #define pb push_back
- #define sz(n) n.size()
- #define vs(n) (int)n.size()
- #define pp pop_back
- #define sp(n) setprecision(n)
- #define big 1000000007
- #define ffor(a, b) for (int i = a; i <b; i++)
- #define frev(b, a) for (int i = b; i >= a; i--)
- #define PI (acos(-1.0))
- #define torad(x) ((x)*((2*acos(0))/180.0))
- #define todeg(x) ((x)*(180.0/(2*acos(0))))
- #define fixAngle(x) ((x)>1?1:(x)<-1?-1:(x))
- #define tan(a) tan(a)/(pi/180)
- #define sin(a) sin(a)/(pi/180)
- #define cos(a) cos(a)/(pi/180)
- #define inverse_sin(a) asin(a)/(pi/180)
- #define inverse_cos(a) acos(a)/(pi/180)
- #define inverse_tan(a) atan(a)/(pi/180)
- #define eps 1e-9
- const int dr4[] = {0, 1, 0, -1};
- const int dc4[] = {1, 0, -1, 0};
- const int dr8[] = {0, 1, 1, 1, 0, -1, -1, -1};
- const int dc8[] = {1, 1, 0, -1, -1, -1, 0, 1};
- using namespace std;
- double neg_infinity(-std::numeric_limits<double>::infinity());
- #define ran 1000000
- #define MAX_SIZE 100
- char Stack[MAX_SIZE];
- int top = -1;
- void push(char item)
- {
- Stack[++top] = item;
- }
- char pop()
- {
- return Stack[top--];
- }
- int priority(char symbol)
- {
- switch(symbol)
- {
- case '+':
- case '-':
- return 1;
- case '*':
- case '/':
- return 2;
- case '^':
- return 3;
- default:
- return 0;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- char infix[MAX_SIZE], postfix[MAX_SIZE];
- printf("Enter an infix expression: ");
- scanf("%s", infix);
- //infixToPostfix(infix, postfix);
- for(int k=00;k<1;k++)
- {
- int i, j;
- char item;
- push('(');
- strcat(infix, ")");
- i = j = 0;
- item = infix[i];
- while(item != '\0')
- {
- if(item == '(')
- {
- push(item);
- }
- else if(isalnum(item))
- {
- postfix[j] = item;
- j++;
- }
- else if(strchr("+-*/^", item) != NULL)
- {
- while(priority(Stack[top]) >= priority(item))
- {
- postfix[j] = pop();
- j++;
- }
- push(item);
- }
- else if(item == ')')
- {
- while(Stack[top] != '(')
- {
- postfix[j] = pop();
- j++;
- }
- pop();
- }
- else
- {
- //printf("Invalid expression");
- break;
- }
- i++;
- item = infix[i];
- postfix[j] = '\0';
- printf("Postfix expression: %s\n", postfix);
- }
- if(top > 0)
- {
- printf("Invalid expression");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement