1228.1--Calculator (MVC design mode not used)

Source: Internet
Author: User

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.