1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help to make line calculator.

Discussion in 'C#' started by klimito, Nov 6, 2010.

  1. klimito

    klimito New Member

    Joined:
    Nov 6, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    So far i can only do 1 command at a time. e.g. 1+1 = 2, but not 1+1+1 = 3 or 1+1*3 = 4 (cause multiplication goes first). This one turns out that 1+1+1 = 5. I need it to make it do complicated task such as 2.3+6.2*3 = 20.9.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     namespace Calci
    {
        public partial class Form1 : Form
        {
            int k;
            public Form1()
            {
                InitializeComponent();
                tb.Text = "0";
                k = 0;
            }
            int preference(char a)
            {
                if (a == '+' || a == '-')
                    return 1;
                else if (a == 'x' || a == '/')
                    return 2;
                else if (a == '^')
                    return 3;
                else
                    return 0;
             }
            void evalExpression(String expr)
            {
                char[] delimiters = new char[] { '+', '-','x','/','%','^'};
                double result=0.0;
                string[] numbers = expr.Split(delimiters,StringSplitOptions.RemoveEmptyEntries);
                int no = numbers.GetUpperBound(0);
                no++;
                char[] op=new char[no];
                int opi=-1,nmi=0;
                for (int i = 0; i < expr.Length; i++)
                {
                    if (expr[i] == '+' || expr[i] == '-' || expr[i] == 'x' || expr[i] == '/' || expr[i] == '%' || expr[i] == '^')
                    {
                        if (opi == -1)
                            op[++opi] = expr[i];
                        else
                        {
                             if (preference(expr[i]) > preference(op[opi]))
                            {
                                op[++opi] = expr[i];
                            }
                            else
                            {
                                do
                                {
                                char o = op[opi];
                                double num1 = Double.Parse(numbers[nmi++]);
                                double num2 = Double.Parse(numbers[nmi]);
                                if (o == '+')
                                    result = result + (num1 + num2);
                                else if (o == '-')
                                    result = result + (num1 - num2);
                                else if (o == 'x')
                                    result = result + (num1 * num2);
                                else if (o == '/')
                                    result = result + (num1 / num2);
                                else if (o == '^')
                                {
                                    double power=1;
                                    for (int j = 1; j <= num2; j++)
                                        power = power * num1;
                                    result = result + power;
                                }
                                else if (o == '%')
                                {
                                    int modn1 = Convert.ToInt32(num1);
                                    int modn2 = Convert.ToInt32(num2);
                                    result = result + Convert.ToDouble(num1 % num2);
                                }
                                numbers[nmi] = Convert.ToString(result);
                                 opi--;
                                }while((opi>=0)&&(preference(expr[i])>preference(op[opi])));
                                op[++opi] = expr[i];
                            }
                         }
                    }
                }
                while (opi >= 0)
                {
                     char o = op[opi];
                    double num1 = Double.Parse(numbers[nmi++]);
                     double num2 = Double.Parse(numbers[nmi]);
                    if (o == '+')
                        result = result + (num1 + num2);
                    else if (o == '-')
                        result = result + (num1 - num2);
                    else if (o == 'x')
                        result = result + (num1 * num2);
                    else if (o == '/')
                        result = result + (num1 / num2);
                    else if (o == '^')
                    {
                        double power = 1;
                        for (int j = 1; j <= num2; j++)
                            power = power * num1;
                        result = result + power;
                    }
                    else if (o == '%')
                    {
                        int modn1 = Convert.ToInt32(num1);
                        int modn2 = Convert.ToInt32(num2);
                        result = result + Convert.ToDouble(num1 % num2);
                    }
                    numbers[nmi] = Convert.ToString(result);
                    opi--;
                }
                tb.Text = Convert.ToString(result);
             }
            private void b7_Click(object sender, EventArgs e)
            {
                if (tb.Text == "0")
                    tb.Text = b7.Text;
                else
                    tb.Text = tb.Text + b7.Text;
                if (k == 1)
                    k = 0;
            }
             private void b8_Click(object sender, EventArgs e)
            {
                if (tb.Text == "0")
                    tb.Text = b8.Text;
                else
                    tb.Text = tb.Text + b8.Text;
                if (k == 1)
                    k = 0;
            }
             private void bCE_Click(object sender, EventArgs e)
            {
                if (tb.Text == "0")
                    tb.Text = "0";
                else if (tb.Text.Length == 1)
                    tb.Text = "0";
                else
                    tb.Text = tb.Text.Remove(tb.Text.Length - 1);
                if (k == 1)
                    k = 0;
             }
             private void b9_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b9.Text;
                else
                    tb.Text = tb.Text + b9.Text;
                if (k == 1)
                    k = 0;
            }
             private void b4_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b4.Text;
                else
                    tb.Text = tb.Text + b4.Text;
                if (k == 1)
                    k = 0;
            }
             private void b5_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b5.Text;
                else
                    tb.Text = tb.Text + b5.Text;
                if (k == 1)
                    k = 0;
            }
             private void b6_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b6.Text;
                else
                    tb.Text = tb.Text + b6.Text;
                if (k == 1)
                    k = 0;
            }
             private void b1_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b1.Text;
                else
                    tb.Text = tb.Text + b1.Text;
                if (k == 1)
                    k = 0;
            }
             private void b2_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b2.Text;
                else
                    tb.Text = tb.Text + b2.Text;
             }
             private void b3_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b3.Text;
                else
                    tb.Text = tb.Text + b3.Text;
             }
             private void b0_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = b0.Text;
                else
                    tb.Text = tb.Text + b0.Text;
             }
             private void button11_Click(object sender, EventArgs e)
            {
                 if (tb.Text == "0")
                    tb.Text = "0";
                else if (tb.Text.EndsWith("+") || tb.Text.EndsWith("-") ||  tb.Text.EndsWith("X") || tb.Text.EndsWith("/") || tb.Text.EndsWith("%")  || tb.Text.EndsWith("^"))
                    tb.Text = "Error as 0.";
                else if (tb.Text.EndsWith("."))
                    tb.Text = "Error Check again";
                else
                    tb.Text = tb.Text + ".";
            }
             private void bequal_Click(object sender, EventArgs e)
            {
                evalExpression(tb.Text);
            }
             private void bplus_Click(object sender, EventArgs e)
            {
                tb.Text += "+";
             }
             private void bX_Click(object sender, EventArgs e)
            {
                tb.Text += "x";
            }
             private void bminus_Click(object sender, EventArgs e)
            {
                tb.Text += "-";
            }
             private void bdiv_Click(object sender, EventArgs e)
            {
                tb.Text += "/";
            }
             private void bmod_Click(object sender, EventArgs e)
            {
                tb.Text += "%";
            }
             private void bpow_Click(object sender, EventArgs e)
            {
                tb.Text += "^";
            }
             private void bCL_Click(object sender, EventArgs e)
            {
                tb.Text = "0";
            }
             private void Form1_Load(object sender, EventArgs e)
            {
             }
         }
    } 
    
    Thx. any help may hasten my to fix my mathematical errors.
     

Share This Page