How to create an application to solve an expression

Total Post:26

Points:182
C# 
C#
 997  View(s)
Ratings:
Rate this:
Hi Buddy

Please help me how to create an application to solve an expression.

Thanks
  1. Post:24

    Points:168
    Re: How to create an application to solve an expression

    Hi Simond,

    I have generated the code to solve the expression which is written in the textbox.  Please review it and let us know if any information is required.


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private object CalculateExpression(string eqn)
            {
                string test = eqn.Replace("{", "(").Replace("}", ")").Replace("[", "(").Replace("]", ")");
                string test2 = test.Replace(")", ")*").Replace("(", "*(");
                string test3 = test2.Replace("**", "*");
                char[] ch = new char[100];
                ch = test3.ToCharArray();
                int a = test3.Length;
                a = a - 1;
                if (ch[a] == '*')
                {
                    ch[a] = ' ';
                }
                if (ch[0] == '*')
                {
                    ch[0] = ' ';
                }
                string test4 = new string(ch);
     
                string test5 = test4.Replace("/*", "/").Replace("*/", "/").Replace("+*", "+").Replace("*+", "+").Replace("-*", "-").Replace("*-", "*").Replace("(*", "(").Replace("*)", ")").Replace("**", "*");
                eqn = test5.Trim();
     
                DataTable dt = new DataTable();
                var result = dt.Compute(eqn, string.Empty);
                return result;
            }
     
     
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
     
                    string result = Convert.ToString(CalculateExpression(textBox1.Text.Trim()));
                    textBox2.Text = result;
                    textBox2.ForeColor = Color.Blue;
                }
                catch (Exception ex)
                {
                    textBox2.Text = "Please Review your Expression" + ex.Message.ToString();
                    textBox2.ForeColor = Color.Red;
                }
            }
        }
    }

      Modified On Apr-11-2018 05:47:04 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!