Q: Change numbers in text field to numbers to calculate
I'm making a Quadratic Solver. I want to use the value of a,b,c that the users enter in the text field to calculate x using the ormula x=(-b+-√(b^2-4ac))/2a.
Can anyone show me the way to change numbers in the text field to number can be calculated.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var a: UITextField!
@IBOutlet weak var b: UITextField!
@IBOutlet weak var c: UITextField!
@IBAction func solve(_ sender: AnyObject) {
if a.text == "1"
{equation.text = "x^2 + \(b.text!) x + \(c.text!) = 0"}
else
{equation.text = "\(a.text!) x^2 + \(b.text!) x + \(c.text!) = 0"}
if a.text==""
{equation.text="Please enter a"}
else if b.text==""
{equation.text="Please enter b"}
else if c.text==""
{equation.text="Please enter c"}
}
@IBOutlet weak var equation: UILabel!
@IBOutlet weak var x1: UILabel!
@IBOutlet weak var x2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
MacBook Pro, iOS 10, Xcode
Posted on Sep 25, 2016 9:00 PM