Simple C# calculator that functions from single text box
Code:
public void Compile (string Code)
{
try
{
string sCode = @ "
using System;
using System.Windows.Forms;
class Calc
{
public void Main ()
{
MessageBox.Show(Convert.ToString(" + Code + @"));
}
} ";
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
parameters.MainClass = "Calc.Main";
parameters.IncludeDebugInformation = false;
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies ())
{
parameters.ReferencedAssemblies.Add (asm.Location);
}
CompilerResults results = compiler.CompileAssemblyFromSource(parameters, sCode);
object o = results.CompiledAssembly.CreateInstance ("Calc");
Type type = o.GetType ();
MethodInfo m = type.GetMethod("Main");
m.Invoke (o, null);
}
catch
{
}
}
I have almost the same code in another program, but where do I use the code to dynamic code view, just for fun.
In summary of the calculator:
It extracts the text from a text box, when it is pressed a button, the text is then "fed" into the feature that shows what's the answer in a messageboxes.
Q: How advanced is it to create a bool function that verify the text as a calculation? Is it only to try to compile the code and return true if it went well, and false if there are any error messages?
Re: Simple C# calculator that functions from single text box
okay, so let me just get this right: you have a text box that contains the text "2 + 4" or "3 * 6", and the code that you use to get the answer to pop up in a text box? I really hope I have misunderstood something powerful, for it must be the worst, slow thing, hopeless solution to such a problem can be found at.
It is so easy that you only have "a + b", "ab", "a * b" or "a/b", I would perhaps done something like:
Code:
class Program
{
public delegate double MyDelegate(double a, double b);
static void Main (string[] args)
{
string math = "2 - 3";
Console.WriteLine (Parse(math));
math = "12432 * 234234";
Console.WriteLine (Parse(math));
math = "1345 / 123542";
Console.WriteLine (Parse(math));
Console.Read ();
}
static double Parse (string math)
{
string pattern = @"^\s*(?<a>\d+)\s*(?<operator>(\+|\-|\*|\/))\s*(?<b>\d+)\s*$";
if (Regex.IsMatch(math, pattern, RegexOptions.IgnorePatternWhitespace))
{
Match match = Regex.Match (math, pattern, RegexOptions.IgnorePatternWhitespace);
MyDelegate del = GetDelegate(match.Groups["operator"].Value);
return del(double.Parse(match.Groups["a"].Value), double.Parse(match.Groups["b"].Value));
}
else
throw new Exception ("parameter does not seem valid");
}
static MyDelegate GetDelegate (string op)
{
switch (op)
{
case "+":
return new MyDelegate (Add);
case "-":
return new MyDelegate (Subtract);
case "*":
return new MyDelegate (Multiply);
case "/":
return new MyDelegate (Divide);
default:
throw new Exception ("Invalid operator");
}
}
static double Add (double a, double b)
{
return a + b;
}
static double Subtract (double a, double b)
{
return a - b;
}
static double Multiply (double a, double b)
{
return a * b;
}
static double Divide (double a, double b)
{
if (b! = 0)
return (a/b);
throw new DivideByZeroException ();
}
}
However, there is no good solution if the problem is not always as static.
Re: Simple C# calculator that functions from single text box
I have also with sin, cos, tan and log. Code so you can calculate long calculations.
Code:
2+2*(Cos(3)/Tan(2))+5*10
It works, but my calculator is much faster.
Anyways I will try your code later on, and modify it to my use. I have a lot left to learn, and I just do not stop learning.
I have some code too (with tan, sin cos etc), but it is really not so relevant for the case getting now.
Re: Simple C# calculator that functions from single text box
My suggestion is to bring out the innermost parenthesis, then make a list of instructions that you sort by the operator, and then repeat.
You should be able to parse these terms, I would not bet on either my or your solution. But you need a different approach to the problem.
Re: Simple C# calculator that functions from single text box
One solution is to create a .vbs script, and then run it. It will also run much faster.