سایت جامع در باب کتب و جزوات رشته های ریاضی و کامپیوتر با دانلود مستقیم.
به ادامه مطلب كليك كنيد
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace InfixPostfixPrefix
{
class Converter
{
static int y =0;
static string _test;
static string ch;
public string show
{
get
{
return _test;
}
}
public void Main(string[] args)
{
}
//--------------------------------------
public void InfixToPostFix(string str)
{
Stack<string> op1 = new Stack<string>();
Stack<string> op2 = new Stack<string>();
string ch;
for (int i = 0; i < str.Length; i++)
{
ch = str.Substring(i, 1);
if (isoperator(ch))
{
if (op2.Count != 0)
{
if (precedence(op2.Peek(), ch))
op2.Push(ch);
else
{
Compute(op1, op2);
op2.Push(ch);
}
}
else
op2.Push(ch);
}
else
op1.Push(ch);
if (ch == ")")
{
op2.Pop();
if (op2.Count == 0)
{
_test = "warrning " + "(";
break;
}
while (op2.Peek() != "(")
{
Compute(op1, op2);
}
op2.Pop();
}
if (ch == "]")
{
op2.Pop();
if (op2.Count == 0)
{
_test = "warrning " + "[";
break;
}
while (op2.Peek() != "[")
{
Compute(op1, op2);
}
op2.Pop();
}
if (ch == "}")
{
op2.Pop();
if (op2.Count == 0)
{
_test = "warrning " + "{";
break;
}
while (op2.Peek() != "{")
{
Compute(op1, op2);
}
op2.Pop();
}
if (str.Length == (i + 1))
{
while (op1.Count != 1)
{
if (op1.Count != 0)
Compute(op1, op2);
if (op1.Count == 0)
{
_test = "WARRNING " + ch;
break;
}
}
if (y == 1)
{
int u = 0;
if (op1.Count == 1)
{
_test = op1.Pop();
string[] arraylist = new string[_test.Length];
int w;
w = (_test.Length - 1);
for (int e = w; e >= 0; e--)
{
arraylist = _test.Substring(e, 1);
u++;
}
_test = "";
for (int t = 0; t < u; t++)
{
_test += arraylist[t];
}
}
}
else
{
if (op1.Count == 1)
{
_test = op1.Pop();
}
}
}
}
}
//--------------------------------------
public void InfixToPrefix(string str)
{
string[] arraylist = new string[str.Length];
string obj = "";
for (int i = 0; i < str.Length; i++)
{
arraylist = str.Substring(i, 1);
}
for (int i = (str.Length - 1); i >= 0; i--)
{
switch (arraylist)
{
case ")":
arraylist = "(";
break;
case "(":
arraylist = ")";
break;
case "[":
arraylist = "]";
break;
case "]":
arraylist = "[";
break;
case "{":
arraylist = "}";
break;
case "}":
arraylist = "{";
break;
default:
break;
}
obj += arraylist;
}
y = 1;
InfixToPostFix(obj);
y = 0;
}
//--------------------------------------
private static bool precedence(string p, string ch)
{
int a1 = 0, a2 = 0;
switch (p)
{
case "-":
{
a1 = 1;
break;
}
case "+":
{
a1 = 1;
break;
}
case "*":
{
a1 = 2;
break;
}
case "/":
{
a1 = 2;
break;
}
case "^":
{
a1 = 3;
break;
}
case "(":
{
a1 = 0;
break;
}
case "[":
{
a1 = -1;
break;
}
case "{":
{
a1 = -2;
break;
}
}
switch (ch)
{
case "-":
{
a2 = 1;
break;
}
case "+":
{
a2 = 1;
break;
}
case "*":
{
a2 = 2;
break;
}
case "/":
{
a2 = 2;
break;
}
case "^":
{
a2 = 3;
break;
}
case "(":
{
a2 = 5;
break;
}
case ")":
{
a2 = 6;
break;
}
case "]":
{
a2 = 6;
break;
}
case "[":
{
a2 = 6;
break;
}
case "{":
{
a2 = 7;
break;
}
case "}":
{
a2 = 7;
break;
}
}
if (a2 > a1)
return true;
return false;
}
//--------------------------------------
private static void Compute(Stack<string> op1, Stack<string> op2)
{
try
{
string x1, x2;
x2 = op1.Pop();
x1 = op1.Pop();
ch = op2.Pop();
switch (ch)
{
case "+":
op1.Push(x1 + x2 + "+");
break;
case "-":
op1.Push(x1 + x2 + "-");
break;
case "*":
op1.Push(x1 + x2 + "*");
break;
case "/":
op1.Push(x1 + x2 + "/");
break;
default:
_test = "Warrning !!!!!!!! "+ch;
break;
}
throw new NotImplementedException();
}
catch
{
}
}
//-------------------------------------
private static bool isoperator(string p)
{
if (p == "+" || p == "-" || p == "*" || p == "/" || p == "(" || p == ")" || p == "[" || p == "]" || p == "{" || p == "}")
return true;
return false;
}
}
}