Currently need help with my calculator app code. The calculatior is not doing mu
ID: 3766876 • Letter: C
Question
Currently need help with my calculator app code. The calculatior is not doing multiplication or additon. All it does is clear the integer entered and gives the value for pie. The code is in swift
import UIKit
class SecondViewController: UIViewController
{
@IBOutlet weak var display: UILabel!
var displayValue: Double = 0.0
var UserWorking = false
let a = M_PI
var userIsInTheMIddleofTypingNumber = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsInTheMIddleofTypingNumber {
display.text = display.text! + digit
} else {
display.text = digit
userIsInTheMIddleofTypingNumber = true
}
}
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMIddleofTypingNumber {
enter()
}
switch operation {
case "": performOperation { $0 * $1 }
case "": performOperation { $1 / $0 }
case "": performOperation { $0 + $1 }
case "": performOperation { $1 - $0 }
case "": performOperation { sqrt($0) }
case "cos": performOperation { cos($0) }
case "sin": performOperation { sin($0) }
default: break
}
}
func performOperation(operation: (Double, Double) -> Double) {
if operandStack.count >= 2
{
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
func performOperation(operation: Double -> Double!) {
if operandStack.count >= 1
{
displayValue = operation(operandStack.removeLast())
enter()
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMIddleofTypingNumber = false
operandStack.append(displayValue)
print("operandStack = (operandStack)")
}
var decimalPressed = false
@IBAction func decimal()
{
UserWorking = true
if decimalPressed == false
{
display.text! = display.text! + "."
decimalPressed = true
}
}
@IBAction func myPi()
{
UserWorking = true
if display.text != "0"
{
enter()
display.text = "(a)"
enter()
}
}
@IBAction func myClear()
{
UserWorking = false
operandStack.removeAll()
display.text = "0"
enter()
}
}
Explanation / Answer
# include <iostream>
using namespace std;
int main()
{
char o;
float num1,num2;
cout << "Enter operator either + or - or * or /: ";
cin >> o;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(o) {
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
cout << "Error! operator is not correct";
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.