Advertisement
Muk99

Bhaskara

Aug 11th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public class Bhaskara : MonoBehaviour {
  6.  
  7.     public Text textA, textB, textC, textResults;
  8.  
  9.     public void Calculate() {
  10.         try {
  11.             var a = float.Parse(textA.text);
  12.             var b = float.Parse(textB.text);
  13.             var c = float.Parse(textC.text);
  14.             var delta = Mathf.Pow(b, 2f) - 4f * a * c;
  15.             var result1 = -b + Mathf.Sqrt(delta) / 2f * a;
  16.             var result2 = -b - Mathf.Sqrt(delta) / 2f * a;
  17.             textA.text = "x1= " + result1 + "\nx2= " + result2;
  18.         }
  19.         catch(DivideByZeroException) {
  20.             textResults.text = "Cannot divide by zero";
  21.         }
  22.         catch {
  23.             textResults.text = "Error";
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement