iOS計算器的代碼實現

ygw3 9年前發布 | 4K 次閱讀 Objective-C IOS

-(void)viewDidLoad

{

//初始化字符串

self.num1 = [[NSMutableString alloc]init];

self.num2 = [[NSMutableString alloc]init];

self.tempStr = [[NSMutableString alloc]init];





}

//監聽數字按鈕0-9和點,點的tag為10

//判斷輸入數字前有幾個0,只取一個零,小數點只取一個

-(IBAction)numbers:(id)sender

{

//處理0的問題

//以零開頭,下次輸入的非0,則清空0

 //如果以0.開頭,則不清空

if ([self.tempStr hasPrefix:@"0"] && [sender tag] > 0 && [sender tag] <10 && ![self.tempStr hasPrefix:@"0."])

{

    self.tempStr = [NSMutableString stringWithString:@""];

}

else if ([self.tempStr hasPrefix:@"0"] && [sender tag] == 0 && ![self.tempStr hasPrefix:@"0."])

{

    //如果是以0開頭,但是不是以0.開頭,則直接返回

    return;



}

//處理小數點的問題

//如果小數點是第一輸入的數字

if ([sender tag] == 10 && self.tempStr.length == 0)

{

    self.tempStr = [NSMutableString stringWithString:@"0"];

}

    //每輸入一次,拼接一次字符串

if([sender tag] == 10 )//取小數點

{

    //小數點只允許輸入一次

    //遍歷字符串tempStr,如果有小數點,則直接return

    for (int i = 0; i < self.tempStr.length ; i++)

    {

        char c = [self.tempStr characterAtIndex:i];

        if (c == '.')

        {

            return;

        }

    }

     [self.tempStr appendString:@"."];

}

else

{

     [self.tempStr appendFormat:@"%ld",[sender tag]];

}

_show.text = self.tempStr;

}

//刪除按鈕

-(IBAction)deleteNums:(id)sender

{

if (_show.text.length == 1)//刪除顯示屏中所有的數字

{

    _show.text = @"0";

    //清空tempStr

    self.tempStr = [NSMutableString stringWithString:@""];

    return;

}

//將顯示屏中的現有數字賦值給字符串

// NSMutableString *str = [[NSMutableString alloc]init];

self.tempStr =[NSMutableString stringWithFormat:@"%@", _show.text] ;

[self.tempStr deleteCharactersInRange:NSMakeRange(self.tempStr.length-1, 1)];

_show.text = self.tempStr;

}

//監聽加減乘除 calculate "=" 11 "+" 12 "-" 13 "*" 14 "/" 15 正負為16 %為17

-(IBAction)calculate:(id)sender

{

//清空tempStr

self.tempStr = [NSMutableString stringWithString:@""];

if(0 == _count)//如果是第一次輸入

{

    _cal = [NSNumber numberWithLong:[sender tag]];

    self.num1 = [NSMutableString stringWithFormat:@"%@",_show.text];

}

else//不是第一次輸入,則計算

{

    self.num2 = [NSMutableString stringWithFormat:@"%@", _show.text];

    int calculate = [_cal intValue];

    switch (calculate) {

        case 12://將加后的結果顯示

            _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] + [self.num2 intValue])];

            break;

        case 13://將減后的結果顯示

            _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] - [self.num2 intValue])];

            break;

        case 14://將乘后的結果顯示

            _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] * [self.num2 intValue])];

            break;

        case 15://將除后的結果顯示

            _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] / [self.num2 intValue])];

            break;

        default:

            break;

    }

    self.num1 = [NSMutableString stringWithFormat:@"%@", _show.text];

    _cal = [NSNumber numberWithLong:[sender tag]];

}

_count = @1;

if ([sender tag] == 16 || [sender tag] == 17)//單目運算正負和%

{

    switch ([sender tag]) {

        case 16://將乘負后的結果顯示

            _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] *(-1))];

            break;

        case 17://將取百分后的結果顯示

            _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] / 100)];

            break;

        default:

            break;

    }

    self.num1 = [NSMutableString stringWithFormat:@"%@", _show.text];

}



}</pre>

 本文由用戶 ygw3 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!