#import "ViewController.h"
typedef enum{
Kstausnum,
Kstausoperation
}kstaus;
typedef enum{
Koperationtypeadd = 1,
Koperationtypeminus,
Koperationtypemultiply,
Koperationtypedevide,
Koperationtypeequal,
Koperationtypenone
}koperationtype;
@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet UILabel *result_label;
@property (nonatomic,assign) long long firstparam; Record the first parameter
@property (nonatomic,assign) koperationtype lastoperation;//Save last operand
@property (nonatomic,assign) BOOL status;//record the current state
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Assigning an initial value
Self.firstparam = 0;
Self.lastoperation = Koperationtypenone;
Self.status = Kstausnum;
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
-(Ibaction) numbuttondidclicked: (UIButton *) Sender {
Get the number on the button clicked
int num = [Sender.titleLabel.text intvalue];
Determine whether to continue stitching or display alone
Long Long shownum;
if (Self.status = = Kstausnum) {
Get the results shown earlier
Long Long orgnum = [Self.result_label.text longlongvalue];
Calculate Final Display Results
Shownum = orgnum * + num;
}else{
Shownum = num;
Self.status = Kstausnum;
}
Show results
Self.result_label.text = [NSString stringwithformat:@ "%lld", Shownum];
}
Operation Key +-*÷
-(Ibaction) operationbuttondidclicked: (UIButton *) Sender {
if (self.status! = kstausoperation) {
Self.status = kstausoperation;
}
Self.status = kstausoperation;
There are two kinds of situations
1. First operation, only one operation is saved
2. Previous operations need to be calculated and saved this time
3. Judging is not the first time to operate
if (self.lastoperation! = Koperationtypenone) {
2. Previous operations need to be calculated, save this operation
[Self calculate];
}else{
The first parameter is entered and saved
Self.firstparam = [Self.result_label.text longlongvalue];
}
Save this one operation
if (Sender.tag = = koperationtypeequal) {
Self.lastoperation = Koperationtypenone;
}else{
Self.lastoperation = (koperationtype) Sender.tag;//Custom Tag Value
}
}
Calculation results
-(void) calculate{
int secondparam = [Self.result_label.text intvalue];
Long long result;
Switch (self.lastoperation) {
Case Koperationtypeadd:
result = Self.firstparam + Secondparam;
Break
Case Koperationtypeminus:
result = Self.firstparam-secondparam;
Break
Case Koperationtypemultiply:
result = Self.firstparam * SECONDPARAM;
Break
Case Koperationtypedevide:
result = Self.firstparam/secondparam;
Break
Default
Break
}
Show final Results
Self.result_label.text = [NSString stringwithformat:@ "%LLD", result];
The current result is the value of the first parameter of the next time
Self.firstparam = result;
Change operator
Self.lastoperation = Koperationtypenone;
}
-(Ibaction) resetbuttondidclicked: (UIButton *) Sender {
Self.result_label.text = @ "0";
Self.lastoperation = Koperationtypenone;
Self.status = Kstausnum;
}
@end
1228.1--Calculator (MVC design mode not used)