• We just launched and are currently in beta. Join us as we build and grow the community.

C++ Tutorial: Math class in C++/CLI. Function calculator

hitrust

Data Ghost
Divine
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
169
Likes
35
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we are going to talk about Math class in Visual C++/CLI and we will use such methods : Math::Sin (computes sine), Math::Cos(computes cosine), Math::Tan (computes tangent). And also we will write a simple program, which computes some math functions.

Preparation

First of all you need to create a new Windows Forms Application project and prepare your main form for coding. In this program should be one Text Box, four Radio Buttons, two Labels and one Button Control. You can situate them as you want; the example of design of my program is above. You don’t need make changes in the settings of any control, so let’s go to coding.

Code

The code of the Button Clicked Event is listed below:
  1. private

    :

    System::

    Void

    button1_Click(

    System::

    Object

    ^

    sender, System::

    EventArgs

    ^

    e)

    {

  2. double

    arg,res;
  3. arg=

    Convert::

    ToDouble

    (

    textBox1-

    >

    Text)

    ;

  4. if

    (

    radioButton1-

    >

    Checked)
  5. {
  6. res=

    Math::

    Sin

    (

    arg)

    ;
  7. }
  8. if

    (

    radioButton2-

    >

    Checked)
  9. {
  10. res=

    Math::

    Cos

    (

    arg)

    ;
  11. }
  12. if

    (

    radioButton3-

    >

    Checked)
  13. {
  14. res=

    Math::

    Tan

    (

    arg)

    ;
  15. }
  16. if

    (

    radioButton4-

    >

    Checked)
  17. {
  18. res=

    1

    /

    (

    Math::

    Tan

    (

    arg)

    )

    ;
  19. }

  20. MessageBox::

    Show

    (

    "Result: "

    +

    res.ToString

    (

    )

    )

    ;

  21. }

Here you can see a creation of two variables of type Double. We need them to store argument of the function and its result. Later we assign to a variable “arg” value of Text Box. To make this should convert this value from String^ type to Double. The next lines of code show us “if” cases. In this area of code the program checks which function is should be computed. For example, if the user checks first radio button, the program will compute sine function. There are no cotangent function in this class, so we just do this – “res = 1 / (Math::Tan (arg))”. After the program has computed the function value it shows a Message Box with result. Below you can see the example of the program’s work.

capture.png



Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,891

323,899

Top