Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
I want to make calculator in windows store with c# language, But i have a problem, the calculator just can do two operation at once, for example: 1+2, 3*5. I want to make calculator that can do many operation at once, for example: 1+4+6+7, 6/3*3.

And here is the code:
C#
public sealed partial class MainPage : Page
    {
        string op1;
        string op2;
        string op;
        bool first = true;
        bool evaluated = false;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!evaluated)
            {
                Button button = (sender) as Button;
                string val = button.Content.ToString();

                switch (val)
                {
                    // values setting
                    case "1":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "2":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "3":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "4":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "5":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "6":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "7":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "8":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "9":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case "0":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;
                    case ".":
                        if (first) { op1 = op1 + val; FinalResult.Text = op1; } else { op2 = op2 + val; FinalResult.Text = op2; };
                        break;

                    // operators selection
                    case "+":
                        if (first) { first = false; } else { OperatorLocked(Convert.ToChar(val)); }
                        Expression.Text = op1 + " " + val;
                        FinalResult.Text = "0";
                        op = val;
                        break;
                    case "-":
                        if (first) { first = false; } else { OperatorLocked(Convert.ToChar(val)); }
                        Expression.Text = op1 + " " + val;
                        FinalResult.Text = "0";
                        op = val;
                        break;
                    case "*":
                        if (first) { first = false; } else { OperatorLocked(Convert.ToChar(val)); }
                        Expression.Text = op1 + " " + val;
                        FinalResult.Text = "0";
                        op = val;
                        break;
                    case "/":
                        if (first) { first = false; } else { OperatorLocked(Convert.ToChar(val)); }
                        Expression.Text = op1 + " " + val;
                        FinalResult.Text = "0";
                        op = val;
                        break;
                    case "%":
                        if (first)
                        {
                            new MessageDialog("Please select a statement of which you want to get the percentage.").ShowAsync();
                        }
                        else
                        {
                            if (op == "/")
                            {
                                // get percentage
                                FinalResult.Text = ((Convert.ToDouble(op1) / Convert.ToDouble(op2)) * 100).ToString();

                                op1 = "";
                                op2 = "";
                                op = "";
                                evaluated = true;
                            }
                            else
                            {
                                new MessageDialog("Only percentage is allowed.\n\nFor instance, Only division operator is allowed. value1 / value 2 and then press % to get their percentage.").ShowAsync();
                            }
                        }
                        break;
                    case "←":
                        if (first)
                        {
                            if (op1 != null && op1.Length > 0)
                            {
                                op1 = op1.Substring(0, op1.Length - 1);
                                FinalResult.Text = op1;
                            }
                            else
                            {
                                FinalResult.Text = "0";
                            }
                        }
                        else
                        {
                            if (op2 != null && op2.Length > 0)
                            {
                                op2 = op2.Substring(0, op2.Length - 1);
                                FinalResult.Text = op2;
                            }
                            else
                            {
                                FinalResult.Text = "0";
                            }
                        }
                        break;
                    case "=":
                        switch (op)
                        {
                            default:
                                if (!first)
                                {
                                    Evaluate(Convert.ToDouble(op1), Convert.ToDouble(op2), op);
                                    op1 = "";
                                    op2 = "";
                                    op = "";
                                    evaluated = true;
                                }
                                else
                                {
                                    new MessageDialog("Two operands must be selected and an operator must be selected, before you can evaluate them.").ShowAsync();
                                }
                                break;
                        }
                        break;
                }
            }
            else
            {
                FinalResult.Text = "0";
                Expression.Text = "";
                evaluated = false;

                Button_Click(sender, e);
            }
        }

        private void Evaluate(double op1, double op2, string op)
        {
            // Evaluate
            switch (op)
            {
                case "+":
                    FinalResult.Text = (op1 + op2).ToString();
                    Expression.Text = op1 + " + " + op2 + " =";
                    break;
                case "-":
                    FinalResult.Text = (op1 - op2).ToString();
                    Expression.Text = op1 + " - " + op2 + " =";
                    break;
                case "*":
                    FinalResult.Text = (op1 * op2).ToString();
                    Expression.Text = op1 + " x " + op2 + " =";
                    break;
                case "/":
                    FinalResult.Text = (op1 / op2).ToString();
                    Expression.Text = op1 + " / " + op2 + " =";
                    break;
            }
        }

        private void OperatorLocked(char op)
        {
            new MessageDialog(op.ToString() + " is currently not available. \n\nYou can only select two operands currently.");
        }
    }
}


Can anyone help me to make calculator that can do many operation at once, for example: 1+4+6+7, 6/3*3?
Posted
Updated 28-Oct-14 16:55pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Oct-14 22:32pm    
Artificial problem. First, for a calculator, one expression might be enough, second, if you can work with one expression, how a second one make any difference?
First of all, you need to picture some reasonable behavior for a calculator. Ideally, it should be a text box where one can write any expressions, with variables. It can output anything, probably in one line. I don't think there is anything else to discuss here. If you face some particular problem, ask some questions.
—SA
BillWoodruff 28-Oct-14 23:23pm    
I disagree with you on this one, Sergey; creating a calculator is a very good learning experience for newcomers to programming, which helps "ground" them in mapping something real-world to code, and familiarizes them with typical UI Controls. Sets the stage for bringing into awareness rules for operator precedence that most people take for granted.

But, once the "challenge" moves beyond simply applying some operator to the last two values entered to deferred execution of multiple operations which requires (for infix) enforcing explicit operator precedence and/or using parens to over-ride operator precedence: the problem gets very interesting for students, indeed.
Sergey Alexandrovich Kryukov 28-Oct-14 23:33pm    
I never said that the calculator cannot make a good exercise. It can, and it even can be useful as an application (it's enough to take a look at the miserable calculator bundled with Windows :-). So, I don't understand what are you disagree with. If you disagree with my phrase "I don't think there is anything else to discuss here", I an convinced enough to modify it into "I did't think there is anything else to discuss here". Your answer proved me wrong. However, that phrase wasn't completely serious. :-)
—SA

1 solution

There are good examples of working infix calculators, and good solutions for the task of parsing a complex string and generating executable C# code, here on CodeProject; I suggest you search for those articles and read them to get ideas: [^].

To make an infix calculator that really "works" you are going to need to implement the rules for operator precedence, and the use of parentheses to over-ride operator precedence. If you make a post-fix calculator (operators after operands, as used by HP, for example), you will not have this problem.

double x = 3 - 2 / 5 // => 2.0 because standard operator precedence invokes division first

double y = (3 - 2) / 5 // => 0.5 because the parentheses force the subtraction to be evaluated first

For infix calculation, most likely you will want to use a 'Stack, and I suggest you use two 'Stack objects: one for the operands, one for the operators.

So, your expression "1+4+6+7" gets parsed into:

operandStack 7 6 4 1 // top to bottom order
operatorStack + + + // top to bottom order

And the execution sequence starts at the top of the operator stack, pulling out two values from the operand stack, and adding them, and so forth.

For "6/3*3" however, you can't just simply create the stacks like this:

operandStack 3 3 6 // top to bottom order
operatorStack * / // top to bottom order

Because executing multiply first will violate the standard infix priority rule that multiply and divide have equal priority, and the first one encountered reading left to right is executed first.

I hope you can see from this brief example the complexity of what you will need to deal with if you write an infix calculator that implements priority of execution rules most people expect.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Oct-14 23:26pm    
I've been thinking: how all that can be related to the question? It's not really related, so I decided to vote 5. The question is pretty pointless, but the advice it pretty good.
—SA
BillWoodruff 28-Oct-14 23:34pm    
I think my response is completely relevant to the question asked ! I think the question asked is not pointless; also: the OP is struggling to implement a calculator (hopefully that's code they wrote themselves, not just pasted-in). The OP wants to implement parsing a string that will invoke multiple calculations: ergo, they will need to deal with operator precedence/parens.

Of course, thanks for the 5 :)
Sergey Alexandrovich Kryukov 28-Oct-14 23:53pm    
Maybe, I was exclusively focused on "1+2, 3*5" and "1+4+6+7, 6/3*3". I still think it's pointless, and I think (or, more correctly, suspect), that the whole question is about such "double" expressions separated by ','. This is not a useful feature. Did you pay attention for that?

(As I say, it would be much more useful to create a calculator with any number of variables. After all, the output option can specify what subset of variables to show on screen as output.)

And you are doing what I think is more relevant to the reasonable calculator, but not to that question: you explained what to do instead. Can you see my point?

—SA
Priscillia Agnes 29-Oct-14 1:05am    
i mean to make this calculator to do more than two calculation, for example 2+3+3+4, not just to do two calculation, for example: 2+3.
Sergey Alexandrovich Kryukov 29-Oct-14 1:59am    
2+3+3+4 is not two calculations. Your previous examples, "1+2, 3*5" and "1+4+6+7, 6/3*3" are really "two calculations. I think they are pointless. As to "2+3+3+4" and more complex expression, this answer gave you quite enough ideas to solve the problem.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900