98 917
Assignments Done
98.6%
Successfully Done
In April 2021

# Answer to Question #41738 in C# for sandeep

Question #41738
create a console-based calculator application. The calculator
should be able to solve basic mathematical calculations.
The calculator should store the result of an expression in a variable called &quot;ans&quot;, which can be
used as an input in other mathematical expressions, as shown in the following statements:
&gt;calculate 25*(3+5-(10/2))
ans=75
&gt;calculate ans+10
ans=85
&gt;calculate ans*10/5
1
2015-06-05T03:58:11-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleCalc
{
class Expression
{
string expr;
double ans;
delegate double Calc(double a, char o, double b);
Calc calc = delegate(double a, char o, double b)
{
switch (o)
{
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
case '%': return a%b;
default: return 0;
}
};
public double Ans { get { return ans; } }
public Expression(double res=0)
{
if (String.IsNullOrEmpty(expr)) GetExpr();
if (expr.Contains("Ans")) expr=expr.Replace("Ans", res.ToString());
while(!expr.Contains("quit"))
{
if (expr.Contains("calculate "))
{
expr = expr.Remove(0, 10);
if (expr.Any(c => char.IsLetter(c))) throw new Exception("Bad expression");
if (String.IsNullOrWhiteSpace(expr)) throw new Exception("Bad expression");
ans = Calculate();
Console.WriteLine("Ans={0}",ans);
try
{
Expression str = new Expression(ans);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
public Expression(string expr)
{
this.expr = expr;
}
public void GetExpr()
{
Console.Write("> ");
}
public double Calculate()
{
int index;
int i=expr.Length,len = 0;
Stack<int> brackets = new Stack<int>();
do
{
i--;
if (expr[i] == ')') brackets.Push(i);
if (expr[i] == '(')
{
len = brackets.Pop() - i;
if (brackets.Count == 0)
{
Expression e0 = new Expression(expr.Substring(i + 1, len - 1));
expr=expr.Remove(i, len+1);
expr = expr.Insert(i, (e0.Calculate()).ToString());
}
}
} while (i > 0);
index=expr.IndexOfAny(new char[]{ '+', '-' });
if (index > 0)
{
Expression e1 = new Expression(expr.Substring(0, index));
Expression e2 = new Expression(expr.Substring(index + 1));
expr = calc(e1.Calculate(), expr[index], e2.Calculate()).ToString();
}
else
{
index = expr.IndexOfAny(new char[] { '*', '/', '%' });
if (index > 0)
{
Expression e1 = new Expression(expr.Substring(0, index));
Expression e2 = new Expression(expr.Substring(index + 1));
expr = calc(e1.Calculate(), expr[index], e2.Calculate()).ToString();
}
}
try
{
ans = Convert.ToDouble(expr);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return ans;
}
}
class Program
{
static void Main(string[] args)
{
try
{
Expression str = new Expression();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Bye");
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!