2014-01-11 54 views
0

我無法獲得與我的代碼匹配的所有細節。基本上我需要小數點功能添加到我的應用程序,我不知道如何:Xcode 5:將小數點添加到iOS計算器應用程序

下面是我ViewController.h

int Method; 
int SelectNumber; 
float RunningTotal; 

@interface ViewController : UIViewController 
{ 

IBOutlet UILabel *Screen; 
} 

-(IBAction)Number1:(id)sender; 
-(IBAction)Number2:(id)sender; 
-(IBAction)Number3:(id)sender; 
-(IBAction)Number4:(id)sender; 
-(IBAction)Number5:(id)sender; 
-(IBAction)Number6:(id)sender; 
-(IBAction)Number7:(id)sender; 
-(IBAction)Number8:(id)sender; 
-(IBAction)Number9:(id)sender; 
-(IBAction)Number0:(id)sender; 

-(IBAction)Times: (id)sender; 
-(IBAction)Divide: (id)sender; 
-(IBAction)Subtract: (id)sender; 
-(IBAction)Plus: (id)sender; 
-(IBAction)Equals: (id)sender; 
-(IBAction)AllClear: (id)sender; 
-(IBAction)Decimal:(id)sender; 

@end 

這裏是我ViewController.m

#import "ViewController.h" 

@interface ViewController() 
@end 
@implementation ViewController 

-(IBAction)Number1:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 1; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 

-(IBAction)Number2:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 2; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number3:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 3; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number4:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 4; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number5:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 5; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number6:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 6; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number7:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 7; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number8:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 8; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number9:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 9; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 
-(IBAction)Number0:(id)sender{ 

    SelectNumber = SelectNumber *10; 
    SelectNumber = SelectNumber + 0; 
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber]; 

} 

-(IBAction)Times: (id)sender{ 

    if (RunningTotal == 0) { 

     RunningTotal = SelectNumber; 
    } 

    else{ 

     switch (Method) { 
      case 1: 
       RunningTotal = RunningTotal * SelectNumber; 
       break; 
      case 2: 
       RunningTotal = RunningTotal/SelectNumber; 
       break; 
      case 3: 
       RunningTotal = RunningTotal - SelectNumber; 
       break; 
      case 4: 
       RunningTotal = RunningTotal + SelectNumber; 
       break; 
      default: 
       break; 
     } 


    } 

    Method = 1; 
    SelectNumber = 0; 
} 
-(IBAction)Divide: (id)sender{ 

    if (RunningTotal == 0) { 

     RunningTotal = SelectNumber; 
    } 

    else{ 

     switch (Method) { 
      case 1: 
       RunningTotal = RunningTotal * SelectNumber; 
       break; 
      case 2: 
       RunningTotal = RunningTotal/SelectNumber; 
       break; 
      case 3: 
       RunningTotal = RunningTotal - SelectNumber; 
       break; 
      case 4: 
       RunningTotal = RunningTotal + SelectNumber; 
       break; 
      default: 
       break; 
     } 


    } 

    Method = 2; 
    SelectNumber = 0; 
} 
-(IBAction)Subtract: (id)sender{ 

    if (RunningTotal == 0) { 

     RunningTotal = SelectNumber; 
    } 

    else{ 

     switch (Method) { 
      case 1: 
       RunningTotal = RunningTotal * SelectNumber; 
       break; 
      case 2: 
       RunningTotal = RunningTotal/SelectNumber; 
       break; 
      case 3: 
       RunningTotal = RunningTotal - SelectNumber; 
       break; 
      case 4: 
       RunningTotal = RunningTotal + SelectNumber; 
       break; 
      default: 
       break; 
     } 


    } 


    Method = 3; 
    SelectNumber = 0; 
} 
-(IBAction)Plus: (id)sender{ 

    if (RunningTotal == 0) { 

     RunningTotal = SelectNumber; 
    } 

    else{ 

     switch (Method) { 
      case 1: 
       RunningTotal = RunningTotal * SelectNumber; 
       break; 
      case 2: 
       RunningTotal = RunningTotal/SelectNumber; 
       break; 
      case 3: 
       RunningTotal = RunningTotal - SelectNumber; 
       break; 
      case 4: 
       RunningTotal = RunningTotal + SelectNumber; 
       break; 
      default: 
       break; 
     } 


    } 


    Method = 4; 
    SelectNumber = 0; 
} 
-(IBAction)Equals: (id)sender{ 

    if (RunningTotal == 0) { 

     RunningTotal = SelectNumber; 
    } 

    else{ 

     switch (Method) { 
      case 1: 
       RunningTotal = RunningTotal * SelectNumber; 
       break; 
      case 2: 
       RunningTotal = RunningTotal/SelectNumber; 
       break; 
      case 3: 
      RunningTotal = RunningTotal - SelectNumber; 
      break; 
     case 4: 
      RunningTotal = RunningTotal + SelectNumber; 
      break; 
     default: 
      break; 
    } 


} 

Method = 0; 
SelectNumber = 0; 
Screen.text = [NSString stringWithFormat:@"%.2f", RunningTotal]; 

} 
-(IBAction)AllClear: (id)sender{ 

    Method = 0; 
    RunningTotal = 0; 
    SelectNumber = 0; 

    Screen.text = [NSString stringWithFormat:@"0"]; 

} 
-(IBAction)Decimal:(id)sender{ 

    //I Need this Bit 

} 

所有viewdidload的東西都在下面,但我認爲這是無關緊要的

+0

你知道'Integer'是什麼嗎?你想改變你的計算器來使用浮點數學。你的結果存儲在哪個變量中?它現在是什麼類型?它應該是什麼類型?這是你的第一天編程嗎? –

回答

0

你已經寫了switch語句多次,這使得你的代碼更長。在每個算術運算符按鈕上給出一個方法編號。在單擊等於按鈕時,使switch語句執行。所以你的代碼會變得太少。並請簡要說明你的問題,你想要什麼。

如果您想要點擊小數點按鈕的時間段。

-(IBAction)Decimal:(id)sender{ 

    Screen.text = [NSString stringWithFormat:@"%i.0", SelectNumber]; 

}