Last active
May 1, 2020 06:12
-
-
Save robsonalves/aa5b1978762eb19d5101352da8919ecb to your computer and use it in GitHub Desktop.
Revisions
-
robsonalves revised this gist
May 1, 2020 . 1 changed file with 24 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,17 +7,37 @@ public static void Main() { //Console.Write("Infix Formule: "); //string infix = Console.ReadLine(); string infix = "4.5+a5+.1215 + 1"; String postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "(5.0 / .9)*(fahrenheit - 32)"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "3.1123123 * diameter"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "pi*r^2"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "a + b * c ^ d - e"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "a ^ b + c * d"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); } public static string InfixToPostfix(string exp) -
robsonalves revised this gist
May 1, 2020 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,15 +7,15 @@ public static void Main() { //Console.Write("Infix Formule: "); //string infix = Console.ReadLine(); string infix = "4.5+a5+.1215 + 1"; String postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "(5.0 / .9)*(fahrenheit - 32)"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); } @@ -59,7 +59,8 @@ public static string InfixToPostfix(string exp) while (stack.Count!=0 ) result += " " + stack.Pop(); //return result.Trim(); return System.Text.RegularExpressions.Regex.Replace(result,@"\s+"," "); } public static int precedence(char operand) -
robsonalves revised this gist
May 1, 2020 . 1 changed file with 20 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,11 +5,19 @@ public class Program { public static void Main() { //Console.Write("Infix Formule: "); //string infix = Console.ReadLine(); string infix = "4.5+a5+.1215 + 1"; String postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); infix = "(5.0 / .9)*(fahrenheit - 32)"; postfix = InfixToPostfix(infix); Console.WriteLine("InFix :\t" + infix); Console.WriteLine("PostFix :\t" + postfix); } public static string InfixToPostfix(string exp) @@ -20,7 +28,7 @@ public static string InfixToPostfix(string exp) for (int i = 0; i<exp.Length; ++i) { char c = exp[i]; if (Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c) || c == '.') result += c; else if (c == '(') @@ -29,7 +37,7 @@ public static string InfixToPostfix(string exp) else if (c == ')') { while (stack.Count!=0 && stack.Peek() != '(') result += " " + stack.Pop(); //Close () and clean from stack if (stack.Count!=0 && stack.Peek() != '(') @@ -40,12 +48,16 @@ public static string InfixToPostfix(string exp) else // an operator is encountered { while (stack.Count!=0 && precedence(c) <= precedence(stack.Peek())) { result += " " + stack.Pop(); } result += " "; stack.Push(c); } } while (stack.Count!=0 ) result += " " + stack.Pop(); return result; } -
robsonalves created this gist
May 1, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ using System; using System.Collections.Generic; public class Program { public static void Main() { Console.Write("Infix Formule: "); string infix = Console.ReadLine(); String postfix = InfixToPostfix(infix); System.Console.WriteLine("InFix :\t" + infix); System.Console.WriteLine("PostFix :\t" + postfix); } public static string InfixToPostfix(string exp) { String result = String.Empty; Stack<char> stack = new Stack<char>(); for (int i = 0; i<exp.Length; ++i) { char c = exp[i]; if (Char.IsLetterOrDigit(c)) result += c; else if (c == '(') stack.Push(c); else if (c == ')') { while (stack.Count!=0 && stack.Peek() != '(') result += stack.Pop(); //Close () and clean from stack if (stack.Count!=0 && stack.Peek() != '(') return "Invalid Expression"; // invalid expression else stack.Pop(); } else // an operator is encountered { while (stack.Count!=0 && precedence(c) <= precedence(stack.Peek())) result += stack.Pop(); stack.Push(c); } } while (stack.Count!=0 ) result += stack.Pop(); return result; } public static int precedence(char operand) { switch (operand) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; } return -1; } }