Advertisement
apl-mhd

infix,postfix updated

Aug 2nd, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 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. FILE *writeFile, *readFile;
  18.  
  19.  
  20.  
  21.  
  22. void postfix(string infix, int f, string filePath) {
  23.  
  24.  
  25.     charPriorty['+'] = 1;
  26.     charPriorty['-'] = 1;
  27.     charPriorty['*'] = 2;
  28.     charPriorty['/'] = 2;
  29.  
  30.  
  31.  
  32.  
  33.  
  34.     char  postfixString[100];
  35.     int j=0;
  36.  
  37.     int i = 0;
  38.  
  39.  
  40.     while (infix[i] != '\0') {
  41.  
  42.         if ((infix[i] >= 'a' && infix[i] <= 'z') || (infix[i] >= '0' && infix[i] <= '9')) {
  43.  
  44.            // cout << infix[i] << " ";
  45.             postfixString[j] = infix[i];
  46.             j++;
  47.         } else if (infix[i] == '(') {
  48.  
  49.             s.push(infix[i]);
  50.         } else if (infix[i] == ')') {
  51.  
  52.             while (s.top() != '(') {
  53.  
  54.                 //cout << s.top() << " ";
  55.                 postfixString[j] = s.top();
  56.                 j++;
  57.                 s.pop();
  58.             }
  59.             s.pop();
  60.  
  61.         } else if (s.size() == 0) {
  62.             s.push(infix[i]);
  63.         } else if ((infix[i] == '+' || infix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
  64.             while (s.size() != 0) {
  65.  
  66.                 if (s.top()  == '(')
  67.                     break;
  68.                // cout << s.top() << " ";
  69.                 postfixString[j] = s.top();
  70.                 j++;
  71.                 s.pop();
  72.             }
  73.  
  74.             s.push(infix[i]);
  75.  
  76.         } else if ( charPriorty[infix[i]] == charPriorty[s.top()]) {
  77.  
  78.            // cout << s.top() << " ";
  79.             postfixString[j] = s.top();
  80.             j++;
  81.             s.pop();
  82.             s.push(infix[i]);
  83.  
  84.         } else{
  85.  
  86.             s.push(infix[i]);
  87.         }
  88.  
  89.         i++;
  90.     }
  91.  
  92.     while(!s.empty()){
  93.  
  94.        // cout<<s.top()<<" ";
  95.         postfixString[j] = s.top();
  96.         j++;
  97.         s.pop();
  98.  
  99.     }
  100.  
  101.     postfixString[j] = '\0';
  102.  
  103.     //cout<<"\n"<<postfixString<<endl;
  104.  
  105.  
  106.  
  107.     if(f == 2){
  108.  
  109.  
  110.         cout<<"Please Check file.txt For Postfix  Expression....\n";
  111.  
  112.  
  113.         ofstream myfile;
  114.         myfile.open (filePath);
  115.         myfile <<postfixString;
  116.         myfile.close();
  117.  
  118.  
  119.     } else
  120.  
  121.         cout<<"Postfix : "<<postfixString<<endl;
  122.  
  123.  
  124. }
  125.  
  126.  
  127.  
  128. void prefix(string prefix, int f,  string filePath){
  129.  
  130.  
  131.  
  132.     charPriorty['+'] = 1;
  133.     charPriorty['-'] = 1;
  134.     charPriorty['*'] = 2;
  135.     charPriorty['/'] = 2;
  136.  
  137.  
  138.     int len = prefix.size();
  139.  
  140.     int i=0;
  141.     int j = len-1;
  142.     while(i<j){
  143.  
  144.         char temp = prefix[i];
  145.         prefix[i] = prefix[j];
  146.         prefix[j] = temp;
  147.         i++;
  148.         j--;
  149.  
  150.     }
  151.  
  152.     //cout<<"Prefix : "<<prefix<<endl;
  153.  
  154.  
  155.  
  156.  
  157.      i = 0;
  158.  
  159.     j=0;
  160.  
  161.     char  prefixString[100];
  162.  
  163.     while (prefix[i] != '\0') {
  164.  
  165.  
  166.  
  167.         if ((prefix[i] >= 'a' && prefix[i] <= 'z') || (prefix[i] >= '0' && prefix[i] <= '9')){
  168.  
  169.             //cout << prefix[i] << " ";
  170.             prefixString[j] =prefix[i];
  171.             j++;
  172.         } else if (prefix[i] == ')') {
  173.  
  174.             s.push(prefix[i]);
  175.         } else if (prefix[i] == '(') {
  176.  
  177.             while (s.top() != ')') {
  178.  
  179.                 //cout << s.top() << " ";
  180.                 prefixString[j] = s.top();
  181.                 j++;
  182.                 s.pop();
  183.             }
  184.             s.pop();
  185.  
  186.         } else if (s.size() == 0) {
  187.             s.push(prefix[i]);
  188.         } else if ((prefix[i] == '+' || prefix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
  189.             while ( s.size() != 0 ) {
  190.  
  191.                 if(s.top() ==')')
  192.                     break;
  193.  
  194.                 prefixString[j] = s.top();
  195.                 j++;
  196.                 s.pop();
  197.  
  198.             }
  199.  
  200.             s.push(prefix[i]);
  201.  
  202.         } else if ( charPriorty[prefix[i]] ==charPriorty[s.top()]) {
  203.  
  204.             //cout << s.top() << " ";
  205.  
  206.             //prefixString[j] = s.top();
  207.             //j++;
  208.             //s.pop();
  209.  
  210.             s.push(prefix[i]);
  211.  
  212.         } else{
  213.  
  214.             s.push(prefix[i]);
  215.         }
  216.  
  217.         i++;
  218.     }
  219.  
  220.     while(!s.empty()){
  221.  
  222.        // cout<<s.top()<<" ";
  223.         prefixString[j] = s.top();
  224.         j++;
  225.  
  226.         s.pop();
  227.  
  228.     }
  229.  
  230.     prefixString[j] = '\0';
  231.  
  232.      i=0;
  233.  
  234.      j = j-1;
  235.  
  236.     while(i<j){
  237.  
  238.         char temp = prefixString[i];
  239.         prefixString[i] = prefixString[j];
  240.         prefixString[j] = temp;
  241.         i++;
  242.         j--;
  243.  
  244.     }
  245.  
  246.  
  247.     if(f == 2){
  248.  
  249.  
  250.         cout<<"Please Check file.txt For Infix  Expression....\n";
  251.         ofstream myfile;
  252.         myfile.open (filePath);
  253.         myfile <<prefixString;
  254.         myfile.close();
  255.  
  256.  
  257.     } else
  258.  
  259.         cout<<"Infix : "<<prefixString<<endl;
  260.  
  261. }
  262.  
  263.  
  264.  
  265.  
  266. int main()
  267. {
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.     bool decision = true;
  280.  
  281.     while(decision){
  282.  
  283.         cout<<endl<<"1. Console Input :\n";
  284.         cout<<"2. File Input :\n";
  285.         cout<<"3. Exit() :\n\n";
  286.  
  287.  
  288.         int n;
  289.         cin>>n;
  290.  
  291.         if(n == 1 ){
  292.  
  293.  
  294.             cout<<"\nPlease Enter Infix Expression :  ";
  295.             string infixInput;
  296.  
  297.             cin>>infixInput;
  298.  
  299.             cout<<"1 For Prefix Conversion"<<endl;
  300.             cout<<"2 For Postfix Conversion"<<endl;
  301.  
  302.  
  303.  
  304.             int x;
  305.             cin>>x;
  306.  
  307.  
  308.  
  309.             if(x==1){
  310.  
  311.                 prefix(infixInput,1,"");
  312.             }
  313.             if (x==2){
  314.  
  315.                 postfix(infixInput, 1,"");
  316.             }
  317.  
  318.            
  319.         } else if(n == 2 ){
  320.  
  321.  
  322.             cout<<"\nInfix Expression has read from file............\n\n";
  323.  
  324.             //string path =  "/Users/apelmahmud/Desktop/assign2/cmake-build-debug/file.txt";
  325.  
  326.             string infix;
  327.             char fileName[256];
  328.             cout << "Enter a file : ";
  329.             cin >> fileName;
  330.             ifstream myfile;
  331.             myfile.open (fileName);
  332.             myfile >>infix;
  333.             myfile.close();
  334.  
  335.  
  336.  
  337.             cout<<"1 For Prefix Conversion"<<endl;
  338.             cout<<"2 For Postfix Conversion"<<endl;
  339.  
  340.  
  341.  
  342.             int x;
  343.             cin>>x;
  344.  
  345.  
  346.  
  347.             if(x==1){
  348.  
  349.                 prefix(infix ,2, fileName);
  350.             }
  351.             if (x==2){
  352.  
  353.                 postfix(infix, 2, fileName);
  354.             }
  355.  
  356.  
  357.         }
  358.  
  359.  
  360.  
  361.         else if(n == 3){
  362.  
  363.             decision = false;
  364.  
  365.         }
  366.  
  367.  
  368.  
  369.  
  370. }
  371.  
  372.     return  0;
  373.  
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement