using Unity to construct a calculator--first assignment of software engineering practice

Basic information

The Link Your Classhttp://t.csdnimg.cn/xdDRg
The Link of Requirement of This Assignmenthttp://t.csdnimg.cn/mt5IU
The Aim of This Assignmentcomplete the calculator with a users interface
MU STU ID and FZU STU ID<21126186_832101309>

Main code

The finished project code: https://github.com/18959242570/Calculator-by-Unity.git

PSP

My PSP of doing this calculator:

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate60minutes70minutes
Development
• Analysis30minutes25minutes
• Design Spec1 hours1.2hours
• Design Review1 hour1.5hours
• Coding Standard1.5hour1.2hours
• Design2.5hour3hours
• Coding2hours2hours
• Code Review30minutes45minutes
• Test20minutes40minutes
Reporting
• Test Repor15minutes30minutes
• Size Measurement20minutes21minutes
• Postmortem & Process Improvement Plan
Sum10.9hours12.7hours

Thinking before I start my project:

Task:

  1. If I want to make a calculator, first we need to know the place of the different button settle down.
  2. And we also need to think about the logical method to connect each number button with the opearator button.
  3. What software and the coding language we need to use.
  4. Where to find the relating information of the coding language and the logic method of a calculator.

During my project

Process
  1. The software I choose is unity and the coding language I choose is C#.

  2. First I open the unity to create a new project and thinking about what component I need to use.-- Because calculator is such a user interface, so I am going to use the UI Component to construct all of my project.

  3. The background using the image component of UI, and the number and the operator button I will use the button component of UI. And the following is the sample of all the Canvas component.

在这里插入图片描述
4. A layout owned by a distributed calculator should be planned.

5. using the C# script to write a function to connect two types of button. And put the written function into each On Click() button component to fulfill the function of the ±*/ and CE(clear).
在这里插入图片描述
6. Finish the whole code part and test each function of the calculator.

Code Descripion

Whole part (how to show the result after enter equal button):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    public TextMeshProUGUI textMeshPro, textMeshPro1;
    public Button _btn,_btn1,_btn2,_btn3;
    private float memory, memory2= 0;
    private bool plus, minus, mul, devide = false;

    public void num1()
    {

        Debug.Log("在遮挡范围内"); //check whether this function is run
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "1"; //add string 1 to the show text
            textMeshPro1.text += "1";

        }
    }
    public void num2()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "2";
            textMeshPro1.text += "2";
        }
    }
    public void num3()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "3";
            textMeshPro1.text += "3";
        }
    }
    public void num4()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "4";
            textMeshPro1.text += "4";
        }
    }
    public void num5()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "5";
            textMeshPro1.text += "5";
        }
    }
    public void num6()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "6";
            textMeshPro1.text += "6";
        }
    }
    public void num7()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "7";
            textMeshPro1.text += "7";
        }
    }
    public void num8()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "8";
            textMeshPro1.text += "8";
        }
    }
    public void num9()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "9";
            textMeshPro1.text += "9";
        }
    }
    public void num0()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += "0";
            textMeshPro1.text += "0";
        }
    }
    public void numDOT()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text += ".";
            textMeshPro1.text += ".";
        }
    }
    public void numPLUS()
    {
        plus = true;
        textMeshPro1.text += "+"; //add the operator to the show text

    }
    public void numMINUS()
    {
        minus = true;
        textMeshPro1.text += "-";
    }
    public void numMUL()
    {
        mul = true;
        textMeshPro1.text += "*";
    }
    public void numDEVIDE()
    {
        devide = true;
        textMeshPro1.text += "/";
    }
    public void CLEARM() //this function is to clear the memory of previous calculate
//fulfill the button CE's fuction
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))// find out the true gameobject
        {
            textMeshPro.text = null;
            memory2 = 0;
            memory = 0;
            textMeshPro1.text = null;
        }
    }
    public void CLEAR()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro.text = null;

        }
    }
    public void CLEAR0()
    {

        Debug.Log("在遮挡范围内");
        if (GameObject.FindGameObjectWithTag("Show"))
        {
            textMeshPro1.text = null;

        }
    }
    public void cal()
    {
        memory2 = float.Parse(textMeshPro.text);
        memory = memory2;
        CLEAR();
        if (plus || minus || devide || mul)
        {
            _btn.GetComponent<Button>().enabled = false;
            _btn1.GetComponent<Button>().enabled = false;
            _btn2.GetComponent<Button>().enabled = false;
            _btn3.GetComponent<Button>().enabled = false;
        }
        
            
        
    }
    public void buttEnable()// Enable the operator button
    {
        _btn.GetComponent<Button>().enabled = true;
        _btn1.GetComponent<Button>().enabled = true;
        _btn2.GetComponent<Button>().enabled = true;
        _btn3.GetComponent<Button>().enabled = true;
    }
    public void ent()// using the enter's variable to memorize the memory of the user's //input
    {
        memory2 = float.Parse(textMeshPro.text);
        if (plus == true)
        {
            textMeshPro1.text += "=";
            CLEAR0();
            memory += memory2;
            plus = false;

        }
        if (minus == true)
        {
            textMeshPro1.text += "=";
            CLEAR0();
            memory -= memory2;

            minus = false;
        }
        if (mul == true)
        {
            textMeshPro1.text += "=";
            CLEAR0();
            memory *= memory2;

            mul = false;
        }
        if (devide == true)
        {
            textMeshPro1.text += "=";
            CLEAR0();
            memory /= memory2;

            devide = false;
        }

        textMeshPro.text = memory.ToString();// Transfer number type float to string
        buttEnable();
    }
}

The main part includes num part, calculate part and enter part.

For the num part, if you click one button, the OnClick() will be triggered and play the num_() function to add a num string to the show text.

For the cal part, we classify two different memory, one memory just memorize the original number input to it, another memory will memorize the real-time number input.

For the enter part, we use bool type to test what operator are used and then do the relating operation. For instance, if we enter the plus operator, the plus operator will add to the show1 text and the bool variable plus will be set true. And in the ent() function, the if will find out that is plus operator triggered. And then do the operation memory +=memory2. Finally, we transfer the memory type to the string and add to the show text.

Final Result

Calculator representation

Summary

From this small project, I learned how to construct a calculator UI surface, how to link the different button again and further more, I have improved my C# coding rule and coding skills since this project.

Last but not least, I clearly know the logic in the calculator, the order of each button in the calculator and I also know the process of how to accomplish a small project. I think it will enrich my experience when I go to the company.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值