iOS:製作一個簡易的計算機

來源:互聯網
上載者:User

標籤:

初步接觸視圖,製作了一個簡易的計算機,基本上簡單的計算是沒有問題的,不是很完美,可能還有一些bug,再接再厲。

  1 //  2 //  ViewController.m  3 //  計算機  4 //  5 //  Created by ma c on 15/8/25.  6 //  Copyright (c) 2015年 bjsxt. All rights reserved.  7 //  8   9 #import "ViewController.h" 10  11 @interface ViewController () 12 @property (weak, nonatomic) IBOutlet UITextField *ResultField; 13 @property(nonatomic,assign)CGFloat temp; 14 @property(nonatomic,assign)CGFloat num1; 15 @property(nonatomic,assign)CGFloat num2; 16 @property(nonatomic,strong)NSMutableString *string; 17 @property(nonatomic,strong)NSArray *arr; 18 @end 19 @implementation ViewController 20 - (IBAction)buttonClear:(UIButton *)sender 21 { 22     [_string setString:@""];         //重新開始計算,文字框置空 23     self.ResultField.text = _string; 24 } 25 - (IBAction)button7:(UIButton *)sender 26 { 27     [_string appendString:@"7"]; 28     self.ResultField.text = _string; 29 } 30 - (IBAction)button8:(UIButton *)sender 31 { 32     [_string appendString:@"8"]; 33     self.ResultField.text = _string; 34 } 35 - (IBAction)button9:(UIButton *)sender 36 { 37     [_string appendString:@"9"]; 38     self.ResultField.text = _string; 39 } 40 - (IBAction)button4:(UIButton *)sender 41 { 42     [_string appendString:@"4"]; 43     self.ResultField.text = _string; 44 } 45 - (IBAction)button5:(UIButton *)sender 46 { 47     [_string appendString:@"5"]; 48     self.ResultField.text = _string; 49 } 50 - (IBAction)button6:(UIButton *)sender 51 { 52     [_string appendString:@"6"]; 53     self.ResultField.text = _string; 54 } 55 - (IBAction)button1:(UIButton *)sender 56 { 57     [_string appendString:@"1"]; 58     self.ResultField.text = _string; 59 } 60 - (IBAction)button3:(UIButton *)sender 61 { 62     [_string appendString:@"3"]; 63     self.ResultField.text = _string; 64 } 65 - (IBAction)button2:(UIButton *)sender 66 { 67     [_string appendString:@"2"]; 68     self.ResultField.text = _string; 69 } 70 - (IBAction)button0:(UIButton *)sender 71 { 72     [_string appendString:@"0"]; 73     self.ResultField.text = _string; 74 } 75  76 - (IBAction)buttonPoint:(UIButton *)sender 77 { 78     [_string appendString:@"."]; 79     self.ResultField.text = _string; 80 } 81  82 //觸發算數運算事件 83 - (IBAction)buttonDiv:(UIButton *)sender 84 { 85     [_string appendString:@"/"]; 86     self.ResultField.text = _string; 87 } 88  89 - (IBAction)buttonMul:(UIButton *)sender 90 { 91     [_string appendString:@"*"]; 92     self.ResultField.text = _string; 93 } 94  95 - (IBAction)buttonSub:(UIButton *)sender 96 { 97     [_string appendString:@"-"]; 98     self.ResultField.text = _string; 99 }100 101 - (IBAction)buttonAdd:(UIButton *)sender102 {103     [_string appendString:@"+"];104     self.ResultField.text = _string;105 }106 107 //做結果運算操作108 - (IBAction)buttonEqual:(UIButton *)sender109 {110 111     for(int i=0; i<[_string length]; i++)112     {113         114         self.arr = [[NSArray alloc]init];115         116         //只輸入一個數,不做運算117         if([_string length] == 1)118         {119             self.temp = [_string doubleValue];120             break;121         }122         123         124         //做加法運算125         if([_string characterAtIndex:i] == ‘+‘)126         {127             self.arr = [_string componentsSeparatedByString:@"+"];128             129             self.num1 = [self.arr[0] doubleValue];130             self.num2 = [self.arr[1] doubleValue];131             self.temp = self.num1 + self.num2;132             break;133         }134         135 136         //做減法運算137         if([_string characterAtIndex:(i+1)] == ‘-‘)138         {139             self.arr = [_string componentsSeparatedByString:@"-"];140            141             if([self.arr count] == 2)142             {143                 self.num1 = [self.arr[0] doubleValue];144                 self.num2 = [self.arr[1] doubleValue];145                 self.temp = self.num1 - self.num2;146             }147             else148             {149                 self.num1 = [self.arr[1] doubleValue];150                 self.num2 = [self.arr[2] doubleValue];151                 self.temp = -(self.num1 + self.num2);152             }153             break;154         }155         156         157         //做除法運算158         if([_string characterAtIndex:i] == ‘/‘)159         {160             self.arr = [_string componentsSeparatedByString:@"/"];161             self.num1 = [self.arr[0] doubleValue];162             self.num2 = [self.arr[1] doubleValue];163             self.temp = self.num1 / self.num2;164             break;165         }166         167         //做乘法運算168         if([_string characterAtIndex:i] == ‘*‘)169         {170             self.arr = [_string componentsSeparatedByString:@"*"];171             self.num1 = [self.arr[0] doubleValue];172             self.num2 = [self.arr[1] doubleValue];173             self.temp = self.num1 * self.num2;174             break;175         }176         177     }178     179     //輸出結果180     [_string setString:[NSString stringWithFormat:@"%.2f",self.temp]];181     self.ResultField.text = _string;182 }183 184 - (void)viewDidLoad {185     [super viewDidLoad];186 187     //建立一個可變的字串188     _string = [NSMutableString stringWithCapacity:20];189 }190 191 - (void)didReceiveMemoryWarning {192     [super didReceiveMemoryWarning];193     // Dispose of any resources that can be recreated.194 }195 196 @end

iOS:製作一個簡易的計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.