请写出能在iOS运行的计算器代码
时间: 2023-08-31 07:43:31 浏览: 154
IOS应用源码之计算器的源代码.zip
### 回答1:
很抱歉,作为一个自然语言处理模型,我无法写出完整的代码,但是下面是一个简单的计算器的 Objective-C 代码示例:
```
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double value1, value2;
char operator;
NSLog (@"请输入第一个数字和算术运算符:");
scanf ("%lf %c", &value1, &operator);
NSLog (@"请输入第二个数字:");
scanf ("%lf", &value2);
if ( operator == '+' )
NSLog (@"%.2f + %.2f = %.2f", value1, value2, value1 + value2);
else if ( operator == '-' )
NSLog (@"%.2f - %.2f = %.2f", value1, value2, value1 - value2);
else if ( operator == '*' )
NSLog (@"%.2f * %.2f = %.2f", value1, value2, value1 * value2);
else if ( operator == '/' )
NSLog (@"%.2f / %.2f = %.2f", value1, value2, value1 / value2);
else
NSLog (@"无效的运算符");
[pool drain];
return 0;
}
```
这是一个简单的计算器代码示例,它可以实现加减乘除运算,你可以根据需要扩展其他功能。
### 回答2:
iOS上运行的计算器代码可以使用Swift语言编写。以下是一个简单的计算器代码示例:
```swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var displayLabel: UILabel!
var operand1: Double = 0
var operand2: Double = 0
var operation: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func numberButtonPressed(_ sender: UIButton) {
let digit = sender.currentTitle!
displayLabel.text! += digit
}
@IBAction func operationButtonPressed(_ sender: UIButton) {
operation = sender.currentTitle!
operand1 = Double(displayLabel.text!)!
displayLabel.text = ""
}
@IBAction func equalsButtonPressed(_ sender: UIButton) {
operand2 = Double(displayLabel.text!)!
var result: Double = 0
switch operation {
case "+":
result = operand1 + operand2
case "-":
result = operand1 - operand2
case "×":
result = operand1 * operand2
case "÷":
result = operand1 / operand2
default:
break
}
displayLabel.text = String(result)
}
@IBAction func clearButtonPressed(_ sender: UIButton) {
displayLabel.text = ""
operand1 = 0
operand2 = 0
operation = ""
}
}
```
这个代码示例包括一个ViewController类,它继承自UIViewController。它具有数字按钮、运算符按钮、等于按钮和清除按钮的响应函数。
CalculatorViewController类中的一些重要成员变量包括`operand1`和`operand2`,用于存储操作数的值,`operation`用于存储当前选择的运算符。
在数字按钮和操作按钮的响应函数中,将所点击的按钮的标题添加到显示标签上。
在等于按钮的响应函数中,将当前显示标签上的文本转换为数字,并根据存储的运算符执行相应的计算。然后,将结果显示在显示标签上。
清除按钮的响应函数将显示标签上的内容、操作数和运算符重置为初始状态。
这是一个简单的iOS计算器代码示例,可以根据需要进行扩展和改进。
### 回答3:
以下是一个简单的iOS计算器代码示例:
```swift
import UIKit
class ViewController: UIViewController {
// 创建计算器显示区域的标签
let displayLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 40)
label.text = "0"
label.textAlignment = .right
return label
}()
var currentNumber = ""
var previousNumber = ""
var operation = ""
override func viewDidLoad() {
super.viewDidLoad()
// 设置背景颜色
view.backgroundColor = .white
// 设置计算器显示区域的布局和约束
displayLabel.frame = CGRect(x: 20, y: 100, width: view.frame.size.width - 40, height: 100)
view.addSubview(displayLabel)
// 设置数字按钮
for i in 0..<10 {
let button = UIButton(type: .system)
button.setTitle("\(i)", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
button.frame = CGRect(x: CGFloat((i % 3)) * (view.frame.size.width/3), y: CGFloat(i / 3) * (view.frame.size.width/3) + 220, width: view.frame.size.width/3, height: view.frame.size.width/3)
button.addTarget(self, action: #selector(numberButtonTapped), for: .touchUpInside)
view.addSubview(button)
}
// 设置运算符按钮
let operators = ["+", "-", "*", "/"]
for (index, operator) in operators.enumerated() {
let button = UIButton(type: .system)
button.setTitle(operator, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
button.frame = CGRect(x: view.frame.size.width - 80, y: CGFloat(index) * (view.frame.size.width/3) + 220, width: 60, height: view.frame.size.width/3)
button.addTarget(self, action: #selector(operatorButtonTapped), for: .touchUpInside)
view.addSubview(button)
}
// 设置等号按钮
let equalsButton = UIButton(type: .system)
equalsButton.setTitle("=", for: .normal)
equalsButton.titleLabel?.font = UIFont.systemFont(ofSize: 30)
equalsButton.frame = CGRect(x: view.frame.size.width - 80, y: CGFloat(operators.count) * (view.frame.size.width/3) + 220, width: 60, height: view.frame.size.width/3)
equalsButton.addTarget(self, action: #selector(equalsButtonTapped), for: .touchUpInside)
view.addSubview(equalsButton)
}
// 当数字按钮被点击时调用
@objc func numberButtonTapped(_ sender: UIButton) {
if let numberText = sender.titleLabel?.text {
currentNumber += numberText
displayLabel.text = currentNumber
}
}
// 当运算符按钮被点击时调用
@objc func operatorButtonTapped(_ sender: UIButton) {
if let operatorText = sender.titleLabel?.text {
previousNumber = currentNumber
currentNumber = ""
operation = operatorText
}
}
// 当等号按钮被点击时调用
@objc func equalsButtonTapped(_ sender: UIButton) {
let number1 = Double(previousNumber) ?? 0.0
let number2 = Double(currentNumber) ?? 0.0
var result: Double
switch operation {
case "+":
result = number1 + number2
case "-":
result = number1 - number2
case "*":
result = number1 * number2
case "/":
result = number1 / number2
default:
result = 0.0
}
displayLabel.text = String(result)
currentNumber = ""
previousNumber = ""
operation = ""
}
}
```
这个计算器可以处理基本的加、减、乘、除运算,并在屏幕上显示结果。界面使用了按钮和标签来显示数字和运算符,并实现了按钮点击事件的处理。
阅读全文