Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <fstream>
- #include<stdio.h>
- #include<string>
- using namespace std;
- string result1;
- string result2;
- string prefixConverter(string st);
- string postfixConverter(string st);
- void consoleInput();
- void fileInput();
- int setPrecedence(char ch);
- int main()
- {
- printf(" Name : MD. Abir Ahsan \n");
- printf(" ID : 011 172 077 \n");
- printf("\n$$~~~ Menu ~~~$$ ");
- int choice;
- for( ; ; )
- {
- printf("\n1. Console Input \n");
- printf("2. File Input \n");
- printf("3.Exit\n");
- printf("enter your choice :");
- cin >> choice;
- if (choice == 1)
- consoleInput();
- else if (choice == 2)
- fileInput();
- else if (choice == 3)
- exit(0);
- }
- return 0;
- }
- void consoleInput()
- {
- string st;
- int choice2;
- printf(" Enter infix expression: ");
- cin >> st;
- printf("\n1. Convert to Prefix \n");
- printf("2. Convert to Postfix \n");
- printf("Enter your choice :");
- cin >> choice2;
- if (choice2 == 1)
- {
- cout << "Prefix expression : " << prefixConverter(st) << endl;
- result2.clear();
- result1.clear();
- }
- else if (choice2 == 2)
- {
- cout << "Postfix expression : " << postfixConverter(st) << endl;
- result2.clear();
- result1.clear();
- }
- }
- void fileInput()
- {
- ifstream f_in;
- int choice3;
- string loc, line;
- printf("\nEnter File location: ");
- cin >> loc;
- f_in.open(loc.c_str());
- if (f_in.is_open())
- {
- while (getline(f_in,line))
- {
- printf("\n1. Convert to Prefix \n");
- printf("2. Convert to Postfix \n");
- printf("\nEnter your choice:");
- cin >> choice3;
- if (choice3 == 1)
- {
- prefixConverter(line);
- }
- else if (choice3 == 2)
- {
- postfixConverter(line);
- }
- }
- }
- else
- {
- std::cout << "Unable to open file" << std::endl << std::endl;
- }
- f_in.close();
- ofstream f_out;
- f_out.open(loc.c_str());
- if (choice3==1)
- {
- f_out << result1 << endl;
- cout << "Output written in File." << endl;
- result1.clear();
- result2.clear();
- }
- else if (choice3==2)
- {
- f_out << result2 << endl;
- cout << "Output written in File." << endl;
- result1.clear();
- result2.clear();
- }
- f_out.close();
- }
- string prefixConverter(string st)
- {
- reverse(st.begin(), st.end());
- for (int i=0;i<st.size();i++)
- {
- if (st[i] == '(')
- {
- st[i] = ')';
- i++;
- }
- else if (st[i] == ')')
- {
- st[i] = '(';
- i++;
- }
- }
- result1 = postfixConverter(st);
- reverse(result1.begin(), result1.end());
- return result1;
- }
- string postfixConverter(string st)
- {
- st = '(' + st + ')';
- stack<char> stk1;
- for (int i= 0; i < st.size(); i++)
- {
- if (isalpha(st[i]) || isdigit(st[i]))
- {
- result2 += st[i];
- }
- else if (st[i] == '(')
- {
- stk1.push('(');
- }
- else if (st[i] == ')')
- {
- while (stk1.top() != '(')
- {
- result2 += stk1.top();
- stk1.pop();
- }
- stk1.pop();
- }
- else
- {
- if (!isalpha(stk1.top()) && !isdigit(stk1.top()))
- {
- while ( setPrecedence(st[i]) <= setPrecedence((stk1.top())) )
- {
- result2+= stk1.top();
- stk1.pop();
- }
- stk1.push(st[i]);
- }
- }
- }
- return result2;
- }
- int setPrecedence(char ch)
- {
- if (ch == '+' || ch == '-')
- return 1;
- else if (ch == '*' || ch == '/')
- return 2;
- else if (ch == '^')
- return 3;
- else
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement