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;
- FILE *writeFile, *readFile;
- void postfix(string infix, int f, string filePath) {
- charPriorty['+'] = 1;
- charPriorty['-'] = 1;
- charPriorty['*'] = 2;
- charPriorty['/'] = 2;
- char postfixString[100];
- int j=0;
- int i = 0;
- while (infix[i] != '\0') {
- if ((infix[i] >= 'a' && infix[i] <= 'z') || (infix[i] >= '0' && infix[i] <= '9')) {
- // 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) {
- if (s.top() == '(')
- break;
- // 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;
- if(f == 2){
- cout<<"Please Check file.txt For Postfix Expression....\n";
- ofstream myfile;
- myfile.open (filePath);
- myfile <<postfixString;
- myfile.close();
- } else
- cout<<"Postfix : "<<postfixString<<endl;
- }
- void prefix(string prefix, int f, string filePath){
- charPriorty['+'] = 1;
- charPriorty['-'] = 1;
- charPriorty['*'] = 2;
- charPriorty['/'] = 2;
- 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--;
- }
- //cout<<"Prefix : "<<prefix<<endl;
- i = 0;
- j=0;
- char prefixString[100];
- while (prefix[i] != '\0') {
- if ((prefix[i] >= 'a' && prefix[i] <= 'z') || (prefix[i] >= '0' && prefix[i] <= '9')){
- //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 ) {
- if(s.top() ==')')
- break;
- 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--;
- }
- if(f == 2){
- cout<<"Please Check file.txt For Infix Expression....\n";
- ofstream myfile;
- myfile.open (filePath);
- myfile <<prefixString;
- myfile.close();
- } else
- cout<<"Infix : "<<prefixString<<endl;
- }
- int main()
- {
- bool decision = true;
- while(decision){
- cout<<endl<<"1. Console Input :\n";
- cout<<"2. File Input :\n";
- cout<<"3. Exit() :\n\n";
- int n;
- cin>>n;
- if(n == 1 ){
- cout<<"\nPlease Enter Infix Expression : ";
- string infixInput;
- cin>>infixInput;
- cout<<"1 For Prefix Conversion"<<endl;
- cout<<"2 For Postfix Conversion"<<endl;
- int x;
- cin>>x;
- if(x==1){
- prefix(infixInput,1,"");
- }
- if (x==2){
- postfix(infixInput, 1,"");
- }
- } else if(n == 2 ){
- cout<<"\nInfix Expression has read from file............\n\n";
- //string path = "/Users/apelmahmud/Desktop/assign2/cmake-build-debug/file.txt";
- string infix;
- char fileName[256];
- cout << "Enter a file : ";
- cin >> fileName;
- ifstream myfile;
- myfile.open (fileName);
- myfile >>infix;
- myfile.close();
- cout<<"1 For Prefix Conversion"<<endl;
- cout<<"2 For Postfix Conversion"<<endl;
- int x;
- cin>>x;
- if(x==1){
- prefix(infix ,2, fileName);
- }
- if (x==2){
- postfix(infix, 2, fileName);
- }
- }
- else if(n == 3){
- decision = false;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement