Results 1 to 5 of 5

Thread: Simple C# calculator that functions from single text box

  1. #1
    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?

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,572,345.25259 seconds with 16 queries