Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new to C# and  I'm now doing scientific calculator assignment. Can you please help me from calculation from keyboard input in detail because my code doesn't work? I  want to know all about from keyboard input.Also I don't know how to make multiopreation like 2+6+9-6*5. Sorry if I've grammar wrongs. Thank you so much all.


What I have tried:

private void Main_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == Convert.ToChar(Keys.Escape))
           {
               txtDisplay.Clear();
           }
           if (e.KeyChar == Convert.ToChar(Keys.Back))
               btnBack_Click(sender, e);

           if ((txtDisplay.Text == "0") || eqClick == false)
           {
               switch (e.KeyChar.ToString())
               {
                   case "+":
                       btnPlus.PerformClick();
                       break;
                   case "-":
                       btnMinus.PerformClick();
                       break;
                   case "*":
                       btnMultiply.PerformClick();
                       break;
                   case "/":
                       btnDivide.PerformClick();
                       break;
                   case "=":
                       btnEqual.PerformClick();
                       break;
                   case ".":btnDot.PerformClick();
                       break;
               }

           }
       }
Posted
Updated 5-Sep-18 22:02pm

1 solution

I hate to say it, but you are doing this all wrong.

You have two problems: the first is that you are "forcing" click events where you don't need to - a much better solution is to have your click event handlers call a method, and then call the same method from your other code. This separates the user interface from the processing code, which means it becomes a lot easier to change things later.

The second is that what you are trying to do is not really going to work. You give an example of "2+6+9-6*5" as a "multi-operation", but you would need to define what should happen as a result: 55, or -13 before you go any further.
The first result is trivial: each time you get an operator, you evaluate the data before it. So you get
2
2+          Evaluate: 2 == 2
2+6
2+6+        Evaluate: 2 + 6 == 8
  8+9
  8+9-      Evaluate: 8 + 9 == 17
   17-6
   17-6*    Evaluate: 17-6 == 11
     11*5
     11*5=  Evaluate: 11*5 == 55

But for the second one your can't do that because you need to include operator precedence: "*" is "more important" than "+" so you have to evaluate (6*5) before you can do the rest - and that is a whole load more complicated.

What I'd suggest is that you have a look at how other calculators work: not just Calc.EXE, but Microsoft Mathematics as well: Download Microsoft Mathematics 4.0 from Official Microsoft Download Center[^] and work out what you want to achieve before leaping into code as you have done so far!
 
Share this answer
 

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