Napišite program za pretvorbo izraza Infix v obrazec Postfix.
Infiksni izraz: Izraz v obliki operatorja b (a + b), tj. ko je operator vmes med vsakim parom operandov.
Postfiksni izraz: Izraz oblike a b operator (ab+), tj. ko vsakemu paru operandov sledi operator.
Primeri:
Vnos: A + B * C + D
Izhod: ABC*+D+Vnos: ((A + B) – C * (D / E)) + F
Izhod: AB+CDE/*-F+
Zakaj postfiksna predstavitev izraza?
Prevajalnik skenira izraz od leve proti desni ali od desne proti levi.
Razmislite o izrazu: a + b * c + d
- Prevajalnik najprej pregleda izraz, da ovrednoti izraz b * c, nato ponovno pregleda izraz, da mu doda a.
- Rezultat se nato doda k d po drugem skeniranju.
Zaradi ponavljajočega skeniranja je zelo neučinkovito. Infiksni izrazi so ljudje zlahka berljivi in rešljivi, medtem ko računalnik ne more zlahka razlikovati med operatorji in oklepaji, zato je bolje, da pred vrednotenjem pretvorite izraz v postfix (ali predpono) obliko.
Ustrezen izraz v postfiksni obliki je abc*+d+ . Postfiksne izraze je mogoče enostavno ovrednotiti z uporabo sklada.
Kako pretvoriti izraz Infix v izraz Postfix?
Če želite pretvoriti infiksni izraz v postfiksni izraz, uporabite Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Preglejte infiksni izraz od leve proti desni .
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Če je optično prebrani znak operand, ga vstavite v postfix izraz.
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- V nasprotnem primeru naredite naslednje
- Če sta prednost in asociativnost skeniranega operatorja večji od prednosti in asociativnosti operatorja v skladu [ali je sklad prazen ali sklad vsebuje ( ‘ ], nato pa ga potisnite v sklad. [' ^ 'operator je pravilno asociativen in drugi operatorji kot ' + ',' – ',' * ' in ' / ‘so levo-asociativni].
- Posebej preverite stanje, ko sta operater na vrhu sklada in optično prebrani operator ' ^ ‘. V tem stanju je prednost skeniranega operatorja večja zaradi njegove desne asociativnosti. Tako bo potisnjen v sklad operaterja.
- V vseh drugih primerih, ko je vrh operatorskega sklada enak skeniranemu operatorju, potem izloči operator iz sklada zaradi leve asociativnosti, zaradi katere ima skenirani operator manjšo prednost.
- V nasprotnem primeru iz sklada izloči vse operatorje, ki imajo prednost večjo ali enako kot pri skeniranem operatorju.
- Po tem Potisnite skenirani operater na sklad. (Če med pojavom naletite na oklepaj, se ustavite tam in potisnite optično prebrani operator v sklad.)
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Če je skenirani znak ' ( «, ga potisnite na sklad.
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Če je skenirani znak ' ) «, odstranite sklad in ga izpišite, dokler se ne prikaže » ( « in zavrzite oba oklepaja.
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Ponovite korake 2-5 dokler se infiksni izraz ne pregleda.
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
jfx java tutorial
- Ko je skeniranje končano, odstranite sklad in dodajte operatorje v postfix izraz, dokler ni prazen.
- Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Na koncu natisnite postfiksni izraz.
Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Ilustracija:
Spodaj so navedeni koraki za izvedbo zgornje zamisli:
- Za boljše razumevanje sledite spodnji sliki Spodaj so navedeni koraki za izvedbo zgornje zamisli:
Razmislite o infiksnem izrazu exp = a+b*c+d
in infiksni izraz se pregleda z uporabo iteratorja jaz , ki je inicializiran kot i = 0 .1. korak: Tukaj je i = 0 in exp[i] = 'a', tj. operand. Torej dodajte to v postfix izraz. zato postfiks = a .
Dodajte 'a' v postfix
2. korak: Tukaj je i = 1 in exp[i] = ‘+’, tj. operator. Potisnite to v sklad. postfiks = a in sklad = {+} .
Potisnite '+' v nizu
3. korak: Zdaj je i = 2 in exp[i] = 'b', tj. operand. Torej dodajte to v postfix izraz. postfiks = ab in sklad = {+} .
Dodajte 'b' v postfix
4. korak: Zdaj je i = 3 in exp[i] = '*', tj. operator. Potisnite to v sklad. postfiks = ab in sklad = {+, *} .
Potisnite '*' v sklad
5. korak: Zdaj je i = 4 in exp[i] = 'c', tj. operand. Dodajte to v postfix izraz. postfiks = abc in sklad = {+, *} .
Dodajte 'c' v postfix
6. korak: Zdaj je i = 5 in exp[i] = ‘+’, tj. operator. Najvišji element sklada ima višjo prednost. Tako izpirajte, dokler sklad ne postane prazen ali ima zgornji element manj prednosti. »*« je izstopen in dodan v postfix. torej postfiks = abc* in sklad = {+} .
Izpirajte '*' in dodajte postfix
Zdaj je zgornji element ' + ' tudi to nima nič manjše prednosti. Pop it. postfiks = abc*+ .
Popnite '+' in ga dodajte v postfix
Zdaj je sklad prazen. Torej potiskaj '+' v kupu. sklad = {+} .
Potisnite '+' v nizu
7. korak: Zdaj je i = 6 in exp[i] = 'd', tj. operand. Dodajte to v postfix izraz. postfiks = abc*+d .
Dodajte 'd' v postfix
Zadnji korak: Zdaj ni več nobenega elementa. Torej izpraznite sklad in ga dodajte v postfiksni izraz. postfiks = abc*+d+ .
Popnite '+' in ga dodajte v postfix
Spodaj je izvedba zgornjega algoritma:
CJava#include #include #include // Function to return precedence of operators int prec(char c) c == '-') return 1; else return -1; // Function to return associativity of operators char associativity(char c) { if (c == '^') return 'R'; return 'L'; // Default to left-associative } // The main function to convert infix expression to postfix expression void infixToPostfix(char s[]) { char result[1000]; int resultIndex = 0; int len = strlen(s); char stack[1000]; int stackIndex = -1; for (int i = 0; i < len; i++) { char c = s[i]; // If the scanned character is an operand, add it to the output string. if ((c>= 'a' && c<= 'z') || (c>= 'A' && c<= 'Z') || (c>= '0' && c<= '9')) { result[resultIndex++] = c; } // If the scanned character is an ‘(‘, push it to the stack. else if (c == '(') { stack[++stackIndex] = c; } // If the scanned character is an ‘)’, pop and add to the output string from the stack // until an ‘(‘ is encountered. else if (c == ')') { while (stackIndex>= 0 && stack[stackIndex] != '(') { rezultat[resultIndex++] = stack[stackIndex--]; } stackIndex--; // Pop '(' } // Če je operator pregledan else { while (stackIndex>= 0 && (prec(s[i])< prec(stack[stackIndex]) || prec(s[i]) == prec(stack[stackIndex]) && associativity(s[i]) == 'L')) { result[resultIndex++] = stack[stackIndex--]; } stack[++stackIndex] = c; } } // Pop all the remaining elements from the stack while (stackIndex>= 0) { rezultat[resultIndex++] = stack[stackIndex--]; } rezultat[rezultatIndeks] = ' '; printf('%s ', rezultat); } // Koda gonilnika int main() { char exp[] = 'a+b*(c^d-e)^(f+g*h)-i'; // Klic funkcije infixToPostfix(exp); vrni 0; }>Pythonimport java.util.Stack; public class InfixToPostfix { // Function to return precedence of operators static int prec(char c) // Function to return associativity of operators static char associativity(char c) { if (c == '^') return 'R'; return 'L'; // Default to left-associative } // The main function to convert infix expression to postfix expression static void infixToPostfix(String s) { StringBuilder result = new StringBuilder(); Stackstack = new Stack(); za (int i = 0; i< s.length(); i++) { char c = s.charAt(i); // If the scanned character is an operand, add it to the output string. if ((c>= 'a' && c<= 'z') || (c>= 'A' && c<= 'Z') || (c>= '0' && c<= '9')) { result.append(c); } // If the scanned character is an ‘(‘, push it to the stack. else if (c == '(') { stack.push(c); } // If the scanned character is an ‘)’, pop and add to the output string from the stack // until an ‘(‘ is encountered. else if (c == ')') { while (!stack.isEmpty() && stack.peek() != '(') { result.append(stack.pop()); } stack.pop(); // Pop '(' } // If an operator is scanned else { while (!stack.isEmpty() && (prec(s.charAt(i)) < prec(stack.peek()) || prec(s.charAt(i)) == prec(stack.peek()) && associativity(s.charAt(i)) == 'L')) { result.append(stack.pop()); } stack.push(c); } } // Pop all the remaining elements from the stack while (!stack.isEmpty()) { result.append(stack.pop()); } System.out.println(result); } // Driver code public static void main(String[] args) { String exp = 'a+b*(c^d-e)^(f+g*h)-i'; // Function call infixToPostfix(exp); } }> C#def prec(c): if c == '^': return 3 elif c == '/' or c == '*': return 2 elif c == '+' or c == '-': return 1 else: return -1 def associativity(c): if c == '^': return 'R' return 'L' # Default to left-associative def infix_to_postfix(s): result = [] stack = [] for i in range(len(s)): c = s[i] # If the scanned character is an operand, add it to the output string. if ('a' <= c <= 'z') or ('A' <= c <= 'Z') or ('0' <= c <= '9'): result.append(c) # If the scanned character is an ‘(‘, push it to the stack. elif c == '(': stack.append(c) # If the scanned character is an ‘)’, pop and add to the output string from the stack # until an ‘(‘ is encountered. elif c == ')': while stack and stack[-1] != '(': result.append(stack.pop()) stack.pop() # Pop '(' # If an operator is scanned else: while stack and (prec(s[i]) < prec(stack[-1]) or (prec(s[i]) == prec(stack[-1]) and associativity(s[i]) == 'L')): result.append(stack.pop()) stack.append(c) # Pop all the remaining elements from the stack while stack: result.append(stack.pop()) print(''.join(result)) # Driver code exp = 'a+b*(c^d-e)^(f+g*h)-i' # Function call infix_to_postfix(exp)>Javascriptusing System; using System.Collections.Generic; class Program { // Function to return precedence of operators static int Prec(char c) c == '*') return 2; else if (c == '+' // Function to return associativity of operators static char Associativity(char c) { if (c == '^') return 'R'; return 'L'; // Default to left-associative } // The main function to convert infix expression to postfix expression static void InfixToPostfix(string s) { Stacksklad = nov sklad (); Seznam rezultat = nov seznam (); za (int i = 0; i< s.Length; i++) { char c = s[i]; // If the scanned character is an operand, add it to the output string. if ((c>= 'a' && c<= 'z') || (c>= 'A' && c<= 'Z') || (c>= '0' && c<= '9')) { result.Add(c); } // If the scanned character is an ‘(‘, push it to the stack. else if (c == '(') { stack.Push(c); } // If the scanned character is an ‘)’, pop and add to the output string from the stack // until an ‘(‘ is encountered. else if (c == ')') { while (stack.Count>0 && stack.Peek() != '(') { result.Add(stack.Pop()); } stack.Pop(); // Pop '(' } // Če je operator pregledan else { while (stack.Count> 0 && (Prec(s[i])< Prec(stack.Peek()) || Prec(s[i]) == Prec(stack.Peek()) && Associativity(s[i]) == 'L')) { result.Add(stack.Pop()); } stack.Push(c); } } // Pop all the remaining elements from the stack while (stack.Count>0) {rezultat.Dodaj(stack.Pop()); } Console.WriteLine(string.Join('', rezultat)); } // Koda gonilnika static void Main() { string exp = 'a+b*(c^d-e)^(f+g*h)-i'; // Klic funkcije InfixToPostfix(exp); } }> C++14/* Javascript implementation to convert infix expression to postfix*/ //Function to return precedence of operators function prec(c) c == '-') return 1; else return -1; // The main function to convert infix expression //to postfix expression function infixToPostfix(s) { let st = []; //For stack operations, we are using JavaScript built in stack let result = ''; for(let i = 0; i < s.length; i++) { let c = s[i]; // If the scanned character is // an operand, add it to output string. if((c>= 'a' && c<= 'z') || (c>= 'A' && c<= 'Z') || (c>= '0' && c<= '9')) result += c; // If the scanned character is an // ‘(‘, push it to the stack. else if(c == '(') st.push('('); // If the scanned character is an ‘)’, // pop and to output string from the stack // until an ‘(‘ is encountered. else if(c == ')') { while(st[st.length - 1] != '(') { result += st[st.length - 1]; st.pop(); } st.pop(); } //If an operator is scanned else { while(st.length != 0 && prec(s[i]) <= prec(st[st.length - 1])) { result += st[st.length - 1]; st.pop(); } st.push(c); } } // Pop all the remaining elements from the stack while(st.length != 0) { result += st[st.length - 1]; st.pop(); } console.log(result + ''); } let exp = 'a+b*(c^d-e)^(f+g*h)-i'; infixToPostfix(exp); // This code is contributed by decode2207.>#include using namespace std; // Function to return precedence of operators int prec(char c) c == '*') return 2; else if (c == '+' // Function to return associativity of operators char associativity(char c) { if (c == '^') return 'R'; return 'L'; // Default to left-associative } // The main function to convert infix expression // to postfix expression void infixToPostfix(string s) { stackst; rezultat niza; za (int i = 0; i< s.length(); i++) { char c = s[i]; // If the scanned character is // an operand, add it to the output string. if ((c>= 'a' && c<= 'z') || (c>= 'A' && c<= 'Z') || (c>= '0' && c<= '9')) result += c; // If the scanned character is an // ‘(‘, push it to the stack. else if (c == '(') st.push('('); // If the scanned character is an ‘)’, // pop and add to the output string from the stack // until an ‘(‘ is encountered. else if (c == ')') { while (st.top() != '(') { result += st.top(); st.pop(); } st.pop(); // Pop '(' } // If an operator is scanned else { while (!st.empty() && prec(s[i]) < prec(st.top()) || !st.empty() && prec(s[i]) == prec(st.top()) && associativity(s[i]) == 'L') { result += st.top(); st.pop(); } st.push(c); } } // Pop all the remaining elements from the stack while (!st.empty()) { result += st.top(); st.pop(); } cout << result << endl; } // Driver code int main() { string exp = 'a+b*(c^d-e)^(f+g*h)-i'; // Function call infixToPostfix(exp); return 0; }>
Izhodabcd^e-fgh*+^*+i->Časovna zapletenost: O(N), kjer je N velikost infiksnega izraza
Pomožni prostor: O(N), kjer je N velikost infiksnega izraza









