using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class Simplecalcan : MonoBehaviour
{
[SerializeField] InputField firstnum;
[SerializeField] InputField secondnum;
[SerializeField] InputField thesign;
[SerializeField] Text text;
public void Calculator()
{
if (thesign.text == "*")
{
int firstnum2 = Convert.ToInt32(firstnum);
int secondnum2 = Convert.ToInt32(secondnum);
int rezult = firstnum2 * secondnum2;
string rezult2 = rezult.ToString();
text.text = rezult2;
}
else if (thesign.text == "+")
{
int firstnum2 = Convert.ToInt32(firstnum);
int secondnum2 = Convert.ToInt32(secondnum);
int rezult = firstnum2 + secondnum2;
string rezult2 = rezult.ToString();
text.text = rezult2;
}
else if (thesign.text == "/")
{
int firstnum2 = Convert.ToInt32(firstnum);
int secondnum2 = Convert.ToInt32(secondnum);
int rezult = firstnum2 / secondnum2;
string rezult2 = rezult.ToString();
text.text = rezult2;
}
else if (thesign.text == "-")
{
int firstnum2 = Convert.ToInt32(firstnum);
int secondnum2 = Convert.ToInt32(secondnum);
int rezult = firstnum2 - secondnum2;
string rezult2 = rezult.ToString();
text.text = rezult2;
}
}
}
using System;
using UnityEngine;
using UnityEngine.UI;
public class SimpleCalculator : MonoBehaviour
{
[SerializeField] private InputField _firstInput;
[SerializeField] private InputField _secondInput;
[SerializeField] private InputField _signInput;
[SerializeField] private Text _text;
// Methods
public void Calculator()
{
int a = int.Parse(_firstInput.text);
int b = int.Parse(_secondInput.text);
_text.text = GetResult(a, b).ToString();
}
private float GetResult(int a, int b)
{
switch(_signInput.text)
{
case "+": return Add(a, b);
case "-": return Substract(a, b);
case "*": return Multiply(a, b);
case "/": return Divide(a, b);
default: throw new Exception("Invalid sign");
}
}
private float Add(int a, int b) => a + b;
private float Substract(int a, int b) => a - b;
private float Multiply(int a, int b) => a * b;
private float Divide(int a, int b) => a / b;
}