C++. Development of a program for calculating an expression implemented by a single function. The PictureBox control




Development of a program for calculating an expression implemented by a single function. The PictureBox control

 


Contents


Search other websites:

Task

Calculate the values of x by defining and using the corresponding function:

 

Consideration

If you look closely at the expression, you can see the similarity between each of its terms. In the general case, each term can be represented by the expression

where a is a number. In our case, a = 6, 13, 22.

Therefore, it is advisable to implement a function that receives the variable a as input parameter and calculates the given expression.

 

Instructions

1. Run MS Visual Studio. Create the project as Windows Forms Application

Therefore, it is advisable to implement a function that receives the variable a as input parameter and calculates the given expression.

After downloading MS Visual Studio, you need to create a project using the Windows Forms Application template. A detailed example of creating a project using the Windows Forms Application template is described in the topic:

It should be recalled that the project is created by the command

File->New Project...

As a result, the “New Project” window will open, shown in Figure 1. This window specifies the “Visual C++” tab and selects the “Windows Forms Application” template. The Name field specifies the name of the project, for example MyApp01. The “Location:” field specifies the folder where the working files of the project will be placed (in our case, this folder is D:\Programs\CPP).

Figure 1. The window of creating a new project

After selecting OK, the system will create a new form, as shown in Figure 2.

Figure 2. The form of application after creation

 

2. Designing the main form

2.1. Placing controls on the form

In accordance with the condition of the task, nothing is entered in the program, only calculation based on known values is performed. With this in mind, the following controls are placed on the form:

  • one control of the Label type. Automatically an object (variable) of the Label class named label1 is created. Using this name you can access the result;
  • one control of Button type. An object named button1 is automatically created. This control will display the text of the task;
  • one control of PictureBox type (Figure 3). This control is used to visualize a complex formula. The formula is represented as a file with the extension *.jpg. The result is an object called pictureBox1;
  • one control of the Label type at the bottom of the form. This control will print the result.

After placing the controls Label, Button, PictureBox, the form of the program will have the form, as shown in Figure 3.

Figure 3. The form of application after placement of controls

 

2.2. Configuring the controls of types Form, Label, Button

You need to configure the following properties of the controls:

  • in the control1 label1, property Text = “Calculate the value of x”;
  • in the control1 button1, property Text = “Calculate”;
  • in the control1 label2 property Text = “Result”;
  • in the Form1 property Text = “Expression calculating program”.

In addition, you can configure other properties at your own discretion.






 

2.3. Setting up the PictureBox control

Since the PictureBox control is designed to display a picture on a form, you must first select this picture. This is done by clicking on the arrow to the right in the upper right corner of the PictureBox, as shown in Figure 4.

Figure 4. Selecting the PictureBox task menu

The “Choose Image …” command is selected in the PictureBox task menu. As a result, a standard Windows “Open” window opens, in which you need to specify a picture file that contains the formula.

In our case, the file with the formula can be taken here. This file was prepared earlier using a graphical editor.

After selecting a drawing file, the application form will look like shown in Figure 5.

Figure 5. The form of application after loading the picture file

As you can see in Figure 5, only part of the formula is displayed. To improve the situation, perform the following steps:

  • activate (highlight) the PictureBox control;
  • use the mouse to increase the width of the PictureBox window;
  • set the SizeMode property to StretchImage (scaling) as shown in Figure 6.

Figure 6. The main form after configuration and SizeMode property

 

3. Adding the text of function to a program code

3.1. Adding the text of function to Form1.h

The expression evaluation function can be placed in the module “Form1.h” (optional). Let this function be called Calc. In accordance with the condition of the problem and the considerations, the function must receive one parameter of an integer type. Therefore, the function will return a double value.

Since, all the work is done in one form, then the implementation of the function body will be inside the code of the Form1 class.

Thus, in the lower part of the body of the Form1 class, you need to add the following program code

// implementation of Calc function
double Calc(int a)
{
    double res, t;
    t = (double)a;
    res = (Math::Sqrt(t)+t)/2.0;
    return res;
}

The function body uses the function Sqrt(), which calculates the root square.

 

3.2. The text of the module “Form1.h” of the main form of the program

At the moment, the text of the Form1 class in abbreviated form is as follows:

#pragma once

namespace MyApp01 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
        public:
        Form1(void)
        {
            ...
        }

        protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            ...
        }

        private: System::Windows::Forms::Button^ button1;
        protected:
        private: System::Windows::Forms::Label^ label1;
        private: System::Windows::Forms::PictureBox^ pictureBox1;
        private: System::Windows::Forms::Label^ label2;

        private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            ...
        }
#pragma endregion

        // implementation of Calc() function
        double Calc(int a)
        {
            double res, t;
            t = (double)a;
            res = (Math::Sqrt(t)+t)/2.0;
            return res;
        }
    };
}

 

4. Programming the click event handler on the “Calculate” button

The last stage of program development is programming the Click event on the “Calculate” button. An example of programming the click event on a button is described in detail in the topic:

In our case, the event handler will look like this:

private: System::Void button1_Click(System::Object sender, System::EventArgs e)
{
    // calculation
    double x;
    x = Calc(6) + Calc(13) + Calc(22); // the call of function
    label2->Text = "Result = " + x.ToString();
}

 

5. Run the program

Now you can run the program for execution. Figure 7 shows the output of the program

Figure 7. Executing the program

 


Related topics