Four Operations Based on GUI

Keywords: C# Windows git Programming ascii

Four Operations Based on GUI

Li Zhiqiang 201421123028 Lian Yonggang 201421123014 Lin Dialect 201421123023

Coding address https://git.coding.net/lizhiqiang0x01/GUI-sizeyunsuan.git

I. Brief description of Title requirements:

1. In addition to integers, four operations of true fraction should be supported, such as 1/6 + 1/8 = 7/24.

2. Operators are +,,,,

3. It is also required to be able to process user input, judge errors and score the correct rate of statistics.

4. Requirements to be able to handle true scores entered by users, such as 1/2, 5/12, etc.

5. Program Based on GUI Interface

6. Record the total number of users'errors and corrections. When the program exits and restarts, it can save the number of previous errors and calculate incrementally on this basis.

7. It has the function of timing, which can display the time consumed by users after they start answering questions.

8. The interface supports Simplified Chinese/Traditional Chinese/English. Users choose one.

2. Implementation steps:

a. Demand Analysis

Writing a program can generate a GUI interface, which can switch between simplified Chinese/traditional Chinese/English. In the process of doing a question, it is necessary to time and display the user's answer time. When the user enters the answer, it is necessary to filter the input parameters. When the user enters the illegal text, such as #,@, I don't know, etc., it will be detected and given the phase. Corresponding error prompts, after the user has finished answering questions, it is necessary to display various parameters after the user has answered questions and realize the function of checking the wrong question bank.

b. Functional design

Logical Structural Diagram

Basic functions

Timing function, switching language function, compatible acquisition of fractional and integer parameters function, generation of true fractional questions and the simplest fraction function, operation function, check answer function.

Extended functionality

The function of filtering illegal characters, the function of topic progress, and the function of generating user's wrong question bank

c. Design and Implementation

In order to meet the functional requirements, the following eight functions are mainly written:

public void add();      //Addition operation
public void sub();      //Subtraction operation
public void mul();      //Multiplication operation
public void div();      //Division
public void ran();      //Generating topics
public void check();    //Check answer
public string StringClear(string ai)   //Filtering illegal characters
public void thread()  //Timing function

d. Code description [source file]

  • Form Generation Function
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(154, 81);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(143, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "Please enter the number of questions (100 full marks)";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(25, 201);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(35, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "v1.00";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(178, 166);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "Begin to do exercises";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(165, 118);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 21);
            this.textBox2.TabIndex = 5;
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.radioButton1.Checked = true;
            this.radioButton1.Location = new System.Drawing.Point(13, 13);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(47, 16);
            this.radioButton1.TabIndex = 6;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "Chinese";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(13, 36);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(65, 16);
            this.radioButton2.TabIndex = 7;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "English";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
            // 
            // radioButton3
            // 
            this.radioButton3.AutoSize = true;
            this.radioButton3.Location = new System.Drawing.Point(13, 59);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(71, 16);
            this.radioButton3.TabIndex = 8;
            this.radioButton3.Text = "Traditional Chinese";
            this.radioButton3.UseVisualStyleBackColor = true;
            this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(443, 255);
            this.Controls.Add(this.radioButton3);
            this.Controls.Add(this.radioButton2);
            this.Controls.Add(this.radioButton1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Chinese";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

 

  • Timing function
        public void thread()   //Creating Threads to Realize Timing Function
        {
            int sec = 0;
            int min = 0;
            int hour = 0;
            
            while (true)
            {
                sec++;
                if (sec >=60)
                {
                    min++;
                    sec =0;
                    if (min >=60)
                    {
                        hour++;
                        min =0;
                    }
                }
                s.label10.Text = hour + ":" + min + ":" + sec;
                Thread.Sleep(1000);
                if (s.next == 1) {
                    break; 
                }
            }

        }

            ThreadStart th = new ThreadStart(thread);
            Console.WriteLine("In Main: Creating the Child thread");
            Thread the = new Thread(th);
            the.Start();
  • Switching Language Function
        private void radioButton2_CheckedChanged(object sender, EventArgs e)    //Switching to English
        {
            this.label1.Text = "Please enter the subject number";
            this.button1.Text = "start";
            s.label8.Text = "Time";
            s.label1.Text = "Schedule";
            s.button1.Text = "NEXT";
            this.Text = "English";
            s.Text = "English";
        }


        private void radioButton1_CheckedChanged(object sender, EventArgs e)  //Switch to Simplified Chinese
        {
            this.label1.Text = "Please enter the number of questions (100 full marks)";
            this.button1.Text = "Begin to do exercises";
            s.label8.Text = "time";
            s.label1.Text = "Topic progress";
            s.button1.Text = "Next question";
            this.Text = "Chinese";
            s.Text = "Chinese";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)  //Switch to Chinese Traditional Style
        {
            this.label1.Text = "Please enter the number of questions (100 full marks)";
            this.button1.Text = "Begin to do exercises";
            s.label8.Text = "Time";
            s.label1.Text = "Topic progress";
            s.button1.Text = "Next topic";
            this.Text = "Traditional Chinese";
            s.Text = "Traditional Chinese";
        }
  • Filtration of Illegal Characters
        public string StringClear(string ai)
        {
            bool temp = bolnum(ai);
            if (temp == true)
            {
                return ai;
            }
            else
            {
                cro = 1;
                if (this.Text == "Chinese")
                {
                    MessageBox.Show("Input error, please enter character correctly! For example: 12,1/2");
                }
                else if (this.Text == "English")
                {
                    MessageBox.Show("Input error, please correct input character! For example: 12,1/2");
                }
                else
                {
                    MessageBox.Show("Input error, please enter character correctly! For example: 12,1/2");
                }
                this.textBox1.Text = "";
                return "";
            }
        }

        public bool bolnum(string temp)   //If it is an illegal character, return false
        {
            for (int i = 0; i < temp.Length; i++)
            {
                byte tempByte = Convert.ToByte(temp[i]);
                if ((tempByte > 46) && (tempByte < 58))
                    return true;
            }
            return false;
        }
  • Generating User's Wrong Question Base Function
      FileStream f = new FileStream("cuoti.txt", FileMode.Append, FileAccess.Write);
      StreamWriter fi = new StreamWriter(f);
      fs = "Your answer:" + this.label2.Text + this.label4.Text + this.label5.Text + this.label9.Text + ai + "    Correct answer:" + au + "      " + "\r\n";
      fi.Write(fs);
      fi.Flush();
      fi.Close();
      f.Close();
  • Checking Answer Function
        public void check()
        {
            int w = 1;
            for (int i = 2; i < 100; i++)
            {
                if (r1 % i == 0 && r2 % i == 0)
                {
                    w = i;
                }
            }
            r1 = r1 / w; r2 = r2 / w;
            ai = StringClear(this.textBox1.Text);
            if (cro != 1)
            {
                if (r2 == 1)
                    au = this.r1.ToString();
                if (r2 != 1)
                    au = r1 + "/" + r2;
                if (ai == au)
                {
                    n1++;
                }
                if (ai != au)
                {
                    FileStream f = new FileStream("cuoti.txt", FileMode.Append, FileAccess.Write);
                    StreamWriter fi = new StreamWriter(f);
                    fs = "Your answer:" + this.label2.Text + this.label4.Text + this.label5.Text + this.label9.Text + ai + "    Correct answer:" + au + "      " + "\r\n";
                    fi.Write(fs);
                    fi.Flush();
                    fi.Close();
                    f.Close();
                    if (this.Text == "Chinese")
                    {
                        MessageBox.Show("Wrong answer! Correct answer:" + au);
                    }
                    else if (this.Text == "English")
                    {
                        MessageBox.Show("Wrong answer!The correct answer: " + au);
                    }
                    else
                    {
                        MessageBox.Show("Wrong answer! Correct answer:" + au);
                    }
                }
            }
        }

 

e. Test run

 

Three, PSP

 

PSP2.1 Personal Software Process Stages Estimated time(h) actual time(h)
Planning plan 1 1.5
· Estimate Estimate how long this task will take 24 40
Development Development 24 34
· Analysis Needs analysis (including learning new technologies) 1 1.2
· Design Spec Generating Design Documents 7 10
· Design Review design review 0.5 0.5
· Coding Standard Code specification 0.5 0.8
· Design Concrete design 1.5 1.5
· Coding Specific coding 20 30
· Code Review Code Review 10 15
· Test Testing (self-testing, code modification, submission of modifications) 1 3
Reporting Presentation 20 25
· Test report 1 1.5
· Calculating workload 0.5 1
· Propose process improvement plan 2 4

 

Four, summary

Due to the GUI interface made of C++ and the new timer function added to the content, three problems have been encountered in this programming.

Question 1: How to generate GUI interface

The main way to solve this question is to automatically generate form controls with the help of VS2013 platform, and then read the auto-generated source code, which makes me learn a lot from it.

Question 2: Timing function

Because of the timing function, time changes dynamically on the form, which means that threads are used to ensure this state.

Question 3: Filtering function

If the user enters the answer, the illegal character will be filtered and prompted. In this case, the user's input value will be transformed into char, and then it will be compared with ASCII value to achieve this function.

Posted by Vincent III on Tue, 16 Apr 2019 16:54:32 -0700