using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace pemdas
{
class Program
{
static void Main(string[] args)
{
string exp = "";
Console.WriteLine("Sample Input: z / ( a + ( b - c ) * ( d - e ) ^ u ) ^ x");
Console.Write("Enter expression: ");
exp = Console.ReadLine();
//exp = "z / ( a + ( b - c ) * ( d - e ) ^ u ) ^ x";
Console.WriteLine(exp);
exp = exp.Replace(" ", ",");
Console.WriteLine("PostFix: " + Parse(exp));
Console.ReadLine();
}
public static string Parse(string exp)
{
int start = exp.LastIndexOf("(");
int end = 0;
if (start >= 0)
{
end = exp.IndexOf(")", start);
string subexp = exp.Substring(start, end - start + 1);
exp = exp.Replace(subexp, Parse(subexp.Substring(2, subexp.Length - 4)));
if(exp.IndexOf("(") >= 0)
exp = Parse(exp);
}
string[] items = exp.Split(',');
ArrayList aNum = new ArrayList();
ArrayList aOp = new ArrayList();
string operators = "^*/+-";
for (int x = 0; x < items.Length; x++)
{
if (operators.IndexOf(items[x]) >= 0)
{
aOp.Add(items[x]);
}
else
{
aNum.Add(items[x]);
}
}
return ToPostFix(aNum,aOp);
}
public static string ToPostFix(ArrayList num, ArrayList op)
{
string[] ops = new string[] { "^", "*", "/", "+", "-" };
for (int x = 0; x < ops.Length; x++)
{
int i = op.IndexOf(ops[x]);
if (i >= 0)
{
int j = i + 1;
num[i] = num[i].ToString() + " " + num[j].ToString() + " " + op[i].ToString();
num.RemoveAt(j);
op.RemoveAt(i);
}
}
return num[0].ToString();
}
}
}
I'm Back again this Thread to ask a question.
Hey Guys, My Calculator was solve. But i have the problem of this,
Do you see the code on the top, that's a C#, my problem how can I convert of that code's in to Java.
My Calculator Expression is running for MDAS rules but My Instructor said o me. That my program is need to calculate the PEMDAS rule's. I'v research for the solution of my problem, But the problem is C#. I dont how to conver this into Java.
SOME BODY CAN HELP ME TO CONVERT THIS