This is in Objective C for Iphone app. I have coded this but got stuck and have
ID: 665807 • Letter: T
Question
This is in Objective C for Iphone app.
I have coded this but got stuck and have a few questions. So, if you could help me out here.
Questions:
1) How to show negative result for when you do small number - big number
2) How to make the sqrt work, what libraries to include and how?
3) How to make it display an imaginary result when user tries sqrt of -ve number, like (sqrt -2 = 1.414 i)
4) How to make it display NaN when you divide by 0
5) How do I make the display the computation on top of the Result: UILabel
/*Start of the program*/
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Result: UILabel!
var firstNum = Float()
var secondNum = Float()
var isTypingNum = false
var total = Float()
var operation = ""
@IBAction func Numbers(sender: AnyObject) {
var num = sender.currentTitle
if isTypingNum == true {
Result.text = Result.text! + num!!
} else {
Result.text = num;
}
isTypingNum = true
}
@IBAction func Decimal(sender: AnyObject) {
var decpt = sender.currentTitle
Result.text = Result.text! + decpt!!
}
@IBAction func Operations(sender: AnyObject) {
isTypingNum = false
operation = sender.currentTitle!!
firstNum = (Result.text! as NSString).floatValue
}
@IBAction func Equals(sender: AnyObject) {
secondNum = (Result.text! as NSString).floatValue
if operation == "+"{
total = firstNum + secondNum
}
else if operation == "-"{
if(firstNum > secondNum){
total = firstNum - secondNum
} else {
total = secondNum - firstNum
Result.text = "(total)"
}
}
else if operation == "*"{
total = firstNum * secondNum
}
else if operation == "÷"{
if(secondNum != 0){
total = firstNum / secondNum
} else {
Result.text = "NaN"
}
}
else if operation == ""{
total = sqrt(firstNum)
}
else{
total = (firstNum * secondNum)/100
}
Result.text = "(total)"
}
@IBAction func AllClear(sender: AnyObject) {
firstNum = 0
secondNum = 0
total = 0
Result.text = "(0)"
}
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.
}
}
Explanation / Answer
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Result: UILabel!
var firstNum = Float()
var secondNum = Float()
var isTypingNum = false
var total = Float()
var operation = ""
@IBAction func Numbers(sender: AnyObject) {
var num = sender.currentTitle
if isTypingNum == true {
Result.text = Result.text! + num!!
} else {
Result.text = num;
}
isTypingNum = true
}
@IBAction func Decimal(sender: AnyObject) {
var decpt = sender.currentTitle
Result.text = Result.text! + decpt!!
}
@IBAction func Operations(sender: AnyObject) {
isTypingNum = false
operation = sender.currentTitle!!
firstNum = (Result.text! as NSString).floatValue
}
@IBAction func Equals(sender: AnyObject) {
secondNum = (Result.text! as NSString).floatValue
if operation == "+"{
total = firstNum + secondNum
}
else if operation == "-"{
if(firstNum > secondNum){
total = firstNum - secondNum
} else {
total = secondNum - firstNum
Result.text = "(total)"
}
}
else if operation == "*"{
total = firstNum * secondNum
}
else if operation == "÷"{
if(secondNum != 0){
total = firstNum / secondNum
} else {
Result.text = "NaN"
}
}
else if operation == ""{
total = sqrt(firstNum)
}
else{
total = (firstNum * secondNum)/100
}
Result.text = "(total)"
}
@IBAction func AllClear(sender: AnyObject) {
firstNum = 0
secondNum = 0
total = 0
Result.text = "(0)"
}
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.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.