using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication129 { public class Element { public int number; public string s; public Element next; } public class Stack { public Element head; public int count; public Stack() { head = null; count = 0; } public void push(Element e) { e.next = head; head = e; count++; } public Element Pop() { Element e; if (count == 0) return null; else { count--; e = head; head = head.next; return e; } } public Element Top() { if (count == 0) return null; else return head; } public int Count() { return count; } public Boolean Is_Empty() { if (count == 0) return true; else return false; } } class Program { public string InfixToPostfix(string infix) { Stack ph = new Stack(); Element e = new Element(); string temp = null; char h; for (int i = 0; i < infix.Length; i++) { e = new Element(); h = infix[i]; if (char.IsDigit(h)) { temp = temp + h.ToString(); } else if (h == '(') { e.s = h.ToString(); ph.push(e); } else if ((h == '+') || (h == '-')) { if ((ph.Is_Empty() == true) || (ph.Top().s == "(")) { e.s = h.ToString(); ph.push(e); } else if ((ph.Top().s == "+") || (ph.Top().s == "-") || (ph.Top().s == "*") || (ph.Top().s == "/")) { while ((ph.Is_Empty() == false) && (ph.Top().s != "(")) { e = ph.Pop(); temp = temp + e.s; } e.s = h.ToString(); ph.push(e); } } else if ((h == '*') || (h == '/')) { if ((ph.Is_Empty() == true) || (ph.Top().s == "(")) { e.s = h.ToString(); ph.push(e); } else if ((ph.Top().s == "+") || (ph.Top().s == "-")) { e.s = h.ToString(); ph.push(e); } } else if (h == ')') { while ((ph.Top().s) != "(") { e = ph.Pop(); temp = temp + e.s; } e = ph.Pop(); } else if (h == '=') { e = new Element(); while (ph.Is_Empty() == false) { e = ph.Pop(); temp = temp + e.s; } } } return temp; } public int Evalution(string m) { Stack ph = new Stack(); Element e = new Element(); int B; Char h; for (int i = 0; i < m.Length;i++ ) { h = m[i]; if (Char.IsDigit(h)) { e.s = h.ToString(); ph.push(e); } else int z1 = Convert.ToInt32(ph.Pop().s); int z = Convert.ToInt32(ph.Pop().s); if(h == '+') { B = z1 + z; } else if (h=='-') { B = z1 - z; } else if(h=='*') { B = z1 * z; } else if(h=='/') { B = r / z; } return B ; } } static void Main(string[] args) { string s = "(8+9-1)*6-(1+0)="; Program t = new Program(); string m = t.InfixToPostfix(s); int RHZ = t.Evalution(m); Console.WriteLine(m); Console.WriteLine(RHZ);// Rowda Abedulsamad ; Hazar Alabdalah; Zena Attasi; Rabeea Muty; Hany Nwaya. } } }