===============================================ViewController.h中的代码===========================================================
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//显示框
__weak IBOutletUILabel *show;
//用两个字符串来接收需要计算的两个数
NSMutableString *str1;
NSMutableString *str2;
//用来接收需要计算的两个值
CGFloat number1;
CGFloat number2;
CGFloat number3;
//用来接收 + - * /
char symbol;
char symbol1;
//用来判断是否进行了小数计算
char point;
//是否按了 =
NSInteger equal;
//是否进行的是除法运算
NSInteger division;
//用来接收计算后的值
CGFloat result;
//启动时显示数字串
NSInteger numberShow;
NSInteger isNumber1;
NSInteger isNumber2;
}
- (IBAction)one:(UIButton *)sender;
- (IBAction)two:(UIButton *)sender;
- (IBAction)three:(UIButton *)sender;
- (IBAction)four:(UIButton *)sender;
- (IBAction)five:(UIButton *)sender;
- (IBAction)six:(UIButton *)sender;
- (IBAction)seven:(UIButton *)sender;
- (IBAction)eight:(UIButton *)sender;
- (IBAction)nine:(UIButton *)sender;
- (IBAction)zero:(UIButton *)sender;
- (IBAction)point:(UIButton *)sender;
- (IBAction)returnZero:(UIButton *)sender;
- (IBAction)sum:(UIButton *)sender;
- (IBAction)minus:(UIButton *)sender;
- (IBAction)multiplication:(UIButton *)sender;
- (IBAction)division:(UIButton *)sender;
- (IBAction)equal:(UIButton *)sender;
- (IBAction)back:(UIButton *)sender;
@end
===============================================ViewController.m中的代码===========================================================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//初始化一些参数
- (void)viewDidLoad {
[superviewDidLoad];
//将 str1 str2 初始化
str1 = [[NSMutableStringalloc]initWithCapacity:0];
str2 = [[NSMutableStringalloc]initWithCapacity:0];
//将显示框设置成右对齐
show.textAlignment =NSTextAlignmentRight;
//将 symbol设置一个字符,用来与 + - * / 区分,用来判断是需要计算的第一个数还是第二个数
symbol = '0';
symbol1 = '0';
//将 point设置一个字符,用来与 . 区分,用来判断是否进行了小数的运算
point = '0';
//按了等号为 1 ,没按为0
equal = 0;
//除法运算是 1,不是的话为0
division = 0;
isNumber1 = 0;
isNumber2 = 0;
//计算器启动时显示数字变化
numberShow = 0;
[NSTimerscheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(changeNumber:)
userInfo:nil
repeats:YES];
}
//系统自带的一个方法,没有用到
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//启动动画
-(void)changeNumber:(NSTimer *)timer{
NSInteger num =11111111111111;
if (numberShow ==10) {
[timer invalidate];
show.text =@"0";
}else{
if (numberShow ==0) {
show.text =@"00000000000000";
}else{
show.text = [NSStringstringWithFormat:@"%ld",num*numberShow];
}
numberShow++;
}
}
//==================1~0和. 的button 设置==================