Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include <ctype.h>
- #include <map>
- #include <string>
- #include <iostream>
- #include <stack>
- #include <fstream>
- using namespace std;
- map<char, int> charPriorty;
- stack<char> s;
- int infixLength(string infix){
- int i=0;
- int j=0;
- while(infix[i] != '\0'){
- if(infix[i]== '(' || infix[i] == ')'){
- i++;
- continue;
- }
- i++;
- j++;
- }
- return j;
- }
- void postfix() {
- charPriorty['+'] = 1;
- charPriorty['-'] = 1;
- charPriorty['*'] = 2;
- charPriorty['/'] = 2;
- string infix;
- cin >> infix;
- char postfixString[100];
- int j=0;
- int i = 0;
- while (infix[i] != '\0') {
- if (infix[i] >= 'a' && infix[i] <= 'z') {
- cout << infix[i] << " ";
- postfixString[j] = infix[i];
- j++;
- } else if (infix[i] == '(') {
- s.push(infix[i]);
- } else if (infix[i] == ')') {
- while (s.top() != '(') {
- cout << s.top() << " ";
- postfixString[j] = s.top();
- j++;
- s.pop();
- }
- s.pop();
- } else if (s.size() == 0) {
- s.push(infix[i]);
- } else if ((infix[i] == '+' || infix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
- while (s.size() != 0) {
- cout << s.top() << " ";
- postfixString[j] = s.top();
- j++;
- s.pop();
- }
- s.push(infix[i]);
- } else if ( charPriorty[infix[i]] ==charPriorty[s.top()]) {
- cout << s.top() << " ";
- postfixString[j] = s.top();
- j++;
- s.pop();
- s.push(infix[i]);
- } else{
- s.push(infix[i]);
- }
- i++;
- }
- while(!s.empty()){
- cout<<s.top()<<" ";
- postfixString[j] = s.top();
- j++;
- s.pop();
- }
- postfixString[j] = '\0';
- cout<<"\n"<<postfixString<<endl;
- }
- void prefix() {
- charPriorty['+'] = 1;
- charPriorty['-'] = 1;
- charPriorty['*'] = 2;
- charPriorty['/'] = 2;
- string prefix;
- cin >> prefix;
- int len = prefix.size();
- int i=0;
- int j = len-1;
- while(i<j){
- char temp = prefix[i];
- prefix[i] = prefix[j];
- prefix[j] = temp;
- i++;
- j--;
- }
- i = 0;
- j=0;
- char prefixString[100];
- while (prefix[i] != '\0') {
- if (prefix[i] >= 'a' && prefix[i] <= 'z') {
- //cout << prefix[i] << " ";
- prefixString[j] =prefix[i];
- j++;
- } else if (prefix[i] == '(') {
- s.push(prefix[i]);
- } else if (prefix[i] == ')') {
- while (s.top() != '(') {
- //cout << s.top() << " ";
- prefixString[j] = s.top();
- j++;
- s.pop();
- }
- s.pop();
- } else if (s.size() == 0) {
- s.push(prefix[i]);
- } else if ((prefix[i] == '+' || prefix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
- while (s.size() != 0) {
- //cout << s.top() << " ";
- prefixString[j] = s.top();
- j++;
- s.pop();
- }
- s.push(prefix[i]);
- } else if ( charPriorty[prefix[i]] ==charPriorty[s.top()]) {
- //cout << s.top() << " ";
- prefixString[j] = s.top();
- j++;
- s.pop();
- s.push(prefix[i]);
- } else{
- s.push(prefix[i]);
- }
- i++;
- }
- while(!s.empty()){
- // cout<<s.top()<<" ";
- prefixString[j] = s.top();
- j++;
- s.pop();
- }
- prefixString[j] = '\0';
- i=0;
- j = j-1;
- while(i<j){
- char temp = prefixString[i];
- prefixString[i] = prefixString[j];
- prefixString[j] = temp;
- i++;
- j--;
- }
- cout<<prefixString<<endl;
- }
- int main()
- {
- postfix();
- //prefix();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement