Advertisement
apl-mhd

assignment 2 update

Jul 30th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #include<cstdio>
  2. #include <ctype.h>
  3. #include <map>
  4. #include <string>
  5. #include <iostream>
  6. #include <stack>
  7. #include <fstream>
  8.  
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14.  
  15. map<char, int> charPriorty;
  16. stack<char> s;
  17.  
  18. int infixLength(string infix){
  19.  
  20.     int i=0;
  21.     int j=0;
  22.  
  23.     while(infix[i] != '\0'){
  24.  
  25.         if(infix[i]== '(' || infix[i] == ')'){
  26.  
  27.             i++;
  28.             continue;
  29.         }
  30.  
  31.  
  32.         i++;
  33.  
  34.         j++;
  35.     }
  36.  
  37.  
  38.     return  j;
  39. }
  40.  
  41. void postfix() {
  42.  
  43.     charPriorty['+'] = 1;
  44.     charPriorty['-'] = 1;
  45.     charPriorty['*'] = 2;
  46.     charPriorty['/'] = 2;
  47.  
  48.     string infix;
  49.  
  50.     cin >> infix;
  51.  
  52.     char  postfixString[100];
  53.     int j=0;
  54.  
  55.     int i = 0;
  56.  
  57.  
  58.     while (infix[i] != '\0') {
  59.  
  60.         if (infix[i] >= 'a' && infix[i] <= 'z') {
  61.  
  62.             cout << infix[i] << " ";
  63.             postfixString[j] = infix[i];
  64.             j++;
  65.         } else if (infix[i] == '(') {
  66.  
  67.             s.push(infix[i]);
  68.         } else if (infix[i] == ')') {
  69.  
  70.             while (s.top() != '(') {
  71.  
  72.                 cout << s.top() << " ";
  73.                 postfixString[j] = s.top();
  74.                 j++;
  75.                 s.pop();
  76.             }
  77.             s.pop();
  78.  
  79.         } else if (s.size() == 0) {
  80.             s.push(infix[i]);
  81.         } else if ((infix[i] == '+' || infix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
  82.             while (s.size() != 0) {
  83.                 cout << s.top() << " ";
  84.                 postfixString[j] = s.top();
  85.                 j++;
  86.                 s.pop();
  87.             }
  88.  
  89.             s.push(infix[i]);
  90.  
  91.         } else if ( charPriorty[infix[i]] ==charPriorty[s.top()]) {
  92.  
  93.             cout << s.top() << " ";
  94.             postfixString[j] = s.top();
  95.             j++;
  96.             s.pop();
  97.             s.push(infix[i]);
  98.  
  99.         } else{
  100.  
  101.             s.push(infix[i]);
  102.         }
  103.  
  104.         i++;
  105.     }
  106.  
  107.     while(!s.empty()){
  108.  
  109.         cout<<s.top()<<" ";
  110.         postfixString[j] = s.top();
  111.         j++;
  112.         s.pop();
  113.  
  114.     }
  115.  
  116.     postfixString[j] = '\0';
  117.  
  118.     cout<<"\n"<<postfixString<<endl;
  119.  
  120. }
  121.  
  122.  
  123.  
  124. void prefix() {
  125.  
  126.     charPriorty['+'] = 1;
  127.     charPriorty['-'] = 1;
  128.     charPriorty['*'] = 2;
  129.     charPriorty['/'] = 2;
  130.  
  131.     string prefix;
  132.  
  133.     cin >> prefix;
  134.  
  135.  
  136.     int len = prefix.size();
  137.  
  138.     int i=0;
  139.     int j = len-1;
  140.     while(i<j){
  141.  
  142.         char temp = prefix[i];
  143.         prefix[i] = prefix[j];
  144.         prefix[j] = temp;
  145.         i++;
  146.         j--;
  147.  
  148.     }
  149.  
  150.  
  151.      i = 0;
  152.  
  153.     j=0;
  154.  
  155.     char  prefixString[100];
  156.  
  157.     while (prefix[i] != '\0') {
  158.  
  159.         if (prefix[i] >= 'a' && prefix[i] <= 'z') {
  160.  
  161.             //cout << prefix[i] << " ";
  162.             prefixString[j] =prefix[i];
  163.             j++;
  164.         } else if (prefix[i] == '(') {
  165.  
  166.             s.push(prefix[i]);
  167.         } else if (prefix[i] == ')') {
  168.  
  169.             while (s.top() != '(') {
  170.  
  171.                 //cout << s.top() << " ";
  172.                 prefixString[j] = s.top();
  173.                 j++;
  174.                 s.pop();
  175.             }
  176.             s.pop();
  177.  
  178.         } else if (s.size() == 0) {
  179.             s.push(prefix[i]);
  180.         } else if ((prefix[i] == '+' || prefix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
  181.             while (s.size() != 0) {
  182.                 //cout << s.top() << " ";
  183.  
  184.                 prefixString[j] = s.top();
  185.                 j++;
  186.  
  187.                 s.pop();
  188.  
  189.  
  190.             }
  191.  
  192.             s.push(prefix[i]);
  193.  
  194.         } else if ( charPriorty[prefix[i]] ==charPriorty[s.top()]) {
  195.  
  196.             //cout << s.top() << " ";
  197.  
  198.             prefixString[j] = s.top();
  199.             j++;
  200.  
  201.             s.pop();
  202.             s.push(prefix[i]);
  203.  
  204.         } else{
  205.  
  206.             s.push(prefix[i]);
  207.         }
  208.  
  209.         i++;
  210.     }
  211.  
  212.     while(!s.empty()){
  213.  
  214.        // cout<<s.top()<<" ";
  215.         prefixString[j] = s.top();
  216.         j++;
  217.  
  218.         s.pop();
  219.  
  220.     }
  221.  
  222.     prefixString[j] = '\0';
  223.  
  224.      i=0;
  225.  
  226.      j = j-1;
  227.  
  228.     while(i<j){
  229.  
  230.         char temp = prefixString[i];
  231.         prefixString[i] = prefixString[j];
  232.         prefixString[j] = temp;
  233.         i++;
  234.         j--;
  235.  
  236.     }
  237.    
  238.     cout<<prefixString<<endl;
  239.  
  240.  
  241. }
  242.  
  243.  
  244.  
  245.  
  246. int main()
  247. {
  248.  
  249.  
  250.     postfix();
  251.  
  252.  
  253.    //prefix();
  254.  
  255.  
  256.  
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement