Class Library Calculator RRS feed

  • Question

  • I am new to C# and have access to a simple App that I am struggling to understand and wonder if someone can help please?
    The App is a simple Class Library calculator App which is supposed to calculate result from a simple line read from a file.
    e.g. the input line is: add 2,multiply 3,apply 3. So the App calculates a result of 15. i.e. (3 + 2) * 3 = 15.
    The NUnit test it contains works fine but as I am new to C# I am struggling to understand how the App works.
    I would like to design and develop a simple Form containing a textbox where the user can enter the line (as above) i.e. add 2,multiply 3,apply 3. And when the user clicks on a button called "Calculate" the click event should go away and use the Calculator class (or combination of classes) and then display the result 15 in a label box on the Form. However, when I try to instantiate the Calculator class I get various error which I am struggling to fix, therefore request if somone can simply deign this simple form for me and perhaps with some explanation of how the App works?

    Thursday, November 7, 2019 11:07 AM

All replies

  • Humm.... line calculator used to be one of the common CS homework.

    It involves basic parameter parsing (separation of digit and operands), command tree building (say, if you see "3 + 2 * 3", you know that you should calculate "2 * 3" before "2 + ?", or when you see "(" you know the remaining operation should be executed one level earlier until ")" is encountered) or stack operation (in this way you execute the commands as the string is parsed, and push data into stack if you need to temporarily go deep one level, and pop back when done), the remaining is just to use a switch statement to execute the commands in order defined in tree/stack.

    There are about 2 to 3 "model answer" implementations that I've seen for this topic.

    Thursday, November 7, 2019 2:11 PM
  • For better or worse, Microsoft provides multiple .NET user interface libraries for designing Windows GUI based applications, and there exists some degree of overlap in functionality. A partial list is Windows Forms, WPF (which stands for Windows Presentation Foundation), and UWP. There are others, but those are main ones that I can think of off the top of my head (and I also listed them in chronological order of release).

    For the purpose of your problem, I would suggest using Windows Forms. It is by far the easiest to use and wrap one's head around (in the beginning). At a high-level, most frameworks work the same way. You describe what user-interface elements you want, how they should look, then attach event-handlers to to them to respond to events.

    To make that a little less abstract, in your case, you mention a text input box (known as a TextBox in Windows Form lingo) and you also mention a calculate button.  Starting with a blank form in the Windows Form designer, you would place a TextBox and a Button.  Those two items are user-interface (UI) elements. Another common phrase for these UI elements are controls.. UI elements have three different ways of interacting with them:  Properties, Methods, and Events.  Properties  are things you can change on the item, sometimes, but not necessarily always, these changes can and will be reflected visually by the control.  Methods provide another way to gain access to a control's functionality, usually more complex. And finally Events are callbacks that happen when the control wants to notify you that the user interacted with your control in some way, such as clicking a button, or typing text into a TextBox.

    I would suggest going to Youtube and searching for Windows Forms Tutorial. There are several good ones.

    • Edited by Aquineas Thursday, November 7, 2019 2:36 PM
    Thursday, November 7, 2019 2:33 PM
  • Hi Moodhi,

    Thank you for posting here.

    For your question, I find two related reference about C# calculator.

    1. Simple Calculator using a Class in C#
    2. C# Calculator Class

    Hope them can help you.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 2, 2020 9:25 AM