Advertisement
apl-mhd

infix,postfix

Aug 2nd, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 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) {
  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.         writeFile = fopen("file.txt", "a");
  113.         fprintf(writeFile, "%s", postfixString);
  114.  
  115.         fclose(writeFile);
  116.  
  117.  
  118.     } else
  119.  
  120.         cout<<"Postfix : "<<postfixString<<endl;
  121.  
  122.  
  123. }
  124.  
  125.  
  126.  
  127. void prefix(string prefix, int f){
  128.  
  129.  
  130.  
  131.     charPriorty['+'] = 1;
  132.     charPriorty['-'] = 1;
  133.     charPriorty['*'] = 2;
  134.     charPriorty['/'] = 2;
  135.  
  136.  
  137.     int len = prefix.size();
  138.  
  139.     int i=0;
  140.     int j = len-1;
  141.     while(i<j){
  142.  
  143.         char temp = prefix[i];
  144.         prefix[i] = prefix[j];
  145.         prefix[j] = temp;
  146.         i++;
  147.         j--;
  148.  
  149.     }
  150.  
  151.     //cout<<"Prefix : "<<prefix<<endl;
  152.  
  153.  
  154.  
  155.  
  156.      i = 0;
  157.  
  158.     j=0;
  159.  
  160.     char  prefixString[100];
  161.  
  162.     while (prefix[i] != '\0') {
  163.  
  164.  
  165.  
  166.         if ((prefix[i] >= 'a' && prefix[i] <= 'z') || (prefix[i] >= '0' && prefix[i] <= '9')){
  167.  
  168.             //cout << prefix[i] << " ";
  169.             prefixString[j] =prefix[i];
  170.             j++;
  171.         } else if (prefix[i] == ')') {
  172.  
  173.             s.push(prefix[i]);
  174.         } else if (prefix[i] == '(') {
  175.  
  176.             while (s.top() != ')') {
  177.  
  178.                 //cout << s.top() << " ";
  179.                 prefixString[j] = s.top();
  180.                 j++;
  181.                 s.pop();
  182.             }
  183.             s.pop();
  184.  
  185.         } else if (s.size() == 0) {
  186.             s.push(prefix[i]);
  187.         } else if ((prefix[i] == '+' || prefix[i] == '-') and (s.top() == '*' || s.top() == '/')) {
  188.             while ( s.size() != 0 ) {
  189.  
  190.                 if(s.top() ==')')
  191.                     break;
  192.  
  193.                 prefixString[j] = s.top();
  194.                 j++;
  195.                 s.pop();
  196.  
  197.             }
  198.  
  199.             s.push(prefix[i]);
  200.  
  201.         } else if ( charPriorty[prefix[i]] ==charPriorty[s.top()]) {
  202.  
  203.             //cout << s.top() << " ";
  204.  
  205.             //prefixString[j] = s.top();
  206.             //j++;
  207.             //s.pop();
  208.  
  209.             s.push(prefix[i]);
  210.  
  211.         } else{
  212.  
  213.             s.push(prefix[i]);
  214.         }
  215.  
  216.         i++;
  217.     }
  218.  
  219.     while(!s.empty()){
  220.  
  221.        // cout<<s.top()<<" ";
  222.         prefixString[j] = s.top();
  223.         j++;
  224.  
  225.         s.pop();
  226.  
  227.     }
  228.  
  229.     prefixString[j] = '\0';
  230.  
  231.      i=0;
  232.  
  233.      j = j-1;
  234.  
  235.     while(i<j){
  236.  
  237.         char temp = prefixString[i];
  238.         prefixString[i] = prefixString[j];
  239.         prefixString[j] = temp;
  240.         i++;
  241.         j--;
  242.  
  243.     }
  244.  
  245.  
  246.     if(f == 2){
  247.  
  248.  
  249.         cout<<"Please Check file.txt For Infix  Expression....\n";
  250.         writeFile = fopen("file.txt", "a");
  251.         fprintf(writeFile, "%s", prefixString);
  252.  
  253.         fclose(writeFile);
  254.  
  255.  
  256.     } else
  257.  
  258.         cout<<"Infix : "<<prefixString<<endl;
  259.  
  260.  
  261.  
  262.  
  263. }
  264.  
  265.  
  266.  
  267.  
  268. int main()
  269. {
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.     bool decision = true;
  278.  
  279.     while(decision){
  280.  
  281.         cout<<endl<<"1. Console Input :\n";
  282.         cout<<"2. File Input :\n";
  283.         cout<<"3. Exit() :\n\n";
  284.  
  285.  
  286.         int n;
  287.         cin>>n;
  288.  
  289.         if(n == 1 ){
  290.  
  291.  
  292.             cout<<"\nPlease Enter Infix Expression :  ";
  293.             string infixInput;
  294.  
  295.             cin>>infixInput;
  296.  
  297.             cout<<"1 For Prefix Conversion"<<endl;
  298.             cout<<"2 For Postfix Conversion"<<endl;
  299.  
  300.  
  301.  
  302.             int x;
  303.             cin>>x;
  304.  
  305.  
  306.  
  307.             if(x==1){
  308.  
  309.                 prefix(infixInput,1);
  310.             }
  311.             if (x==2){
  312.  
  313.                 postfix(infixInput, 1);
  314.             }
  315.  
  316.            
  317.         } else if(n == 2 ){
  318.  
  319.  
  320.             cout<<"\nInfix Expression has read from file............\n\n";
  321.  
  322.             char name[100];
  323.  
  324.             readFile = fopen("file.txt", "r");
  325.             fscanf(readFile, "%s", name);
  326.  
  327.             fclose(readFile);
  328.  
  329.  
  330.  
  331.  
  332.             cout<<"1 For Prefix Conversion"<<endl;
  333.             cout<<"2 For Postfix Conversion"<<endl;
  334.  
  335.  
  336.  
  337.             int x;
  338.             cin>>x;
  339.  
  340.  
  341.  
  342.             if(x==1){
  343.  
  344.                 prefix(name ,2 );
  345.             }
  346.             if (x==2){
  347.  
  348.                 postfix(name, 2);
  349.             }
  350.  
  351.  
  352.         }
  353.  
  354.  
  355.  
  356.         else if(n == 3){
  357.  
  358.             decision = false;
  359.  
  360.         }
  361.  
  362.  
  363.  
  364.  
  365. }
  366.  
  367.     return  0;
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement