Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-01-2009
Member
 
Join Date: Nov 2008
Posts: 1,066
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?
Reply With Quote
  #2  
Old 26-01-2009
Member
 
Join Date: Feb 2008
Posts: 194
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.
Reply With Quote
  #3  
Old 26-01-2009
Member
 
Join Date: Nov 2008
Posts: 1,066
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.
Reply With Quote
  #4  
Old 26-01-2009
Member
 
Join Date: Feb 2008
Posts: 194
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.
Reply With Quote
  #5  
Old 26-01-2009
Member
 
Join Date: May 2008
Posts: 685
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Simple C# calculator that functions from single text box"
Thread Thread Starter Forum Replies Last Post
How to fix maximum text in single cell of Microsoft Excel Irritator MS Office Support 2 03-02-2012 07:47 PM
Simple Instant Text Message Application - Mac / PC Leiff Windows Software 5 23-05-2010 02:14 AM
Perl Simple Functions Osman Software Development 4 01-12-2009 04:49 PM
What is text link ads calculator Leoniee Technology & Internet 3 10-08-2009 07:30 PM
Single database & multiple functions in web services. Kiran123 Software Development 2 31-01-2009 04:24 PM


All times are GMT +5.5. The time now is 06:53 PM.