iOS實現小型計算器

2021-06-28 21:10:05 字數 3609 閱讀 1507



步驟:

1.開啟xcode,單機creat a new xcode project

3.填寫專案名稱單機next

4.viewcontroller.h中定義成員和方法

#import

@inte***ce viewcontroller : uiviewcontroller

@property(retain,nonatomic)uibutton *button;

@property(retain,nonatomic)uilabel *label;

@property(retain,nonatomic)nsmutablestring *string;

@property(assign,nonatomic)double num1,num2,num3,num4;

@end

4.viewcontroller.m

#import "viewcontroller.h"

@inte***ce viewcontroller ()

@end

@implementation viewcontroller

@synthesize button,label,string,num1,num2,num3,num4;//string儲存字元,顯示數值。num1是存輸入的數值,num2是存運算子前的數值,num3是運算結果,num4是判斷進行何種運算

- (void)viewdidload

} //單獨新增0

uibutton *button0=[uibutton buttonwithtype:uibuttontyperoundedrect];

[button0 setframe:cgrectmake(30, 345, 60, 60)];

[button0 settitle:@"0" forstate:uicontrolstatenormal];

[button0 addtarget:self action:@selector(one:) forcontrolevents:uicontroleventtouchupinside];

[self.view addsubview:button0];

//新增運算子

nsarray *array1=[nsarray arraywithobjects:@"+",@"-",@"*",@"/",nil];

for (int i=0; i<4; i++)

//新增=

uibutton *button2=[uibutton buttonwithtype:uibuttontyperoundedrect];

[button2 setframe:cgrectmake(160, 410, 125, 35)];

[button2 settitle:@"=" forstate:uicontrolstatenormal];

[button2 addtarget:self action:@selector(go:) forcontrolevents:uicontroleventtouchupinside];

[self.view addsubview:button2];

//新增清除鍵

uibutton *button3=[uibutton buttonwithtype:uibuttontyperoundedrect];

[button3 setframe:cgrectmake(30, 410, 125, 35)];

[button3 settitle:@"ac" forstate:uicontrolstatenormal];

[button3 addtarget:self action:@selector(clean:) forcontrolevents:uicontroleventtouchupinside];

[self.view addsubview:button3];

//新增.

uibutton *button4=[uibutton buttonwithtype:uibuttontyperoundedrect];

[button4 setframe:cgrectmake(95, 345, 60, 60)];

[button4 settitle:@"." forstate:uicontrolstatenormal];

[button4 addtarget:self action:@selector(one:) forcontrolevents:uicontroleventtouchupinside];

[self.view addsubview:button4];

//後退

uibutton *button5=[uibutton buttonwithtype:uibuttontyperoundedrect];

[button5 setframe:cgrectmake(160, 345, 60, 60)];

[button5 settitle:@"back" forstate:uicontrolstatenormal];

[button5 addtarget:self action:@selector(back:) forcontrolevents:uicontroleventtouchupinside];

[self.view addsubview:button5];

self.string=[[nsmutablestring alloc]init];//初始化可變字串,分配記憶體

// do any additional setup after loading the view, typically from a nib.

}-(void)one:(id)sender

self.label.text=[nsstring stringwithstring:string];//顯示數值

self.num1=[self.label.text doublevalue];//儲存輸入的數值

nslog(@"%f",self.num1);

}-(void)two:(id)sender

//判斷輸入是-號

else if([self.string hasprefix:@"-"])//hasprefix:判斷字串以減號開頭

//判斷輸入是*號

else if([self.string hasprefix:@"*"])//hasprefix:判斷字串以乘號開頭

//判斷輸入是/號

else if([self.string hasprefix:@"/"])//hasprefix:判斷字串以除號開頭 }

-(void)go:(id)sender

//判斷輸入是-號

else if(self.num4==2)

//判斷輸入是*號

else if(self.num4==3)

//判斷輸入是/號

else if(self.num4==4) }

//當按下清除建時,所有資料清零

-(void)clean:(id)sender

//返回鍵

-(void)back:(id)sender

}- (void)viewdidunload

- (bool)shouldautorotatetointe***ceorientation:(uiinte***ceorientation)inte***ceorientation

@end

C 實現小型複數計算器

小型複數計算器專案設計,供大家參考,具體內容如下 1 實現複數的設定和顯示。2 宣告乙個複數類complex,過載運算子 使之能用於複數的加 減 乘 除,運算子過載函式作為complex類的成員函式。3 宣告乙個複數類complex,過載運算子 使之能用於複數的加法運算。參加運算的兩個運算量可以都是...

iOS開發實現計算器功能

效果圖 masonry 使用陣列來自動約束 nsarray buttonarrayone buttonac,buttonleftbracket,buttonrightbracket,buttondivide withfixedspacing 每個view中間的間距 leadspacing 左最開始的...

vs2010編寫小型計算器 小型計算,手工計算

vs2010編寫小型計算器 程式設計再現了世界觀,而問題在於,在大多數情況下,再現的世界觀是一種單一文化,存在著以隱性和顯式障礙及守門形式表現的障礙。這是我認為 小型計算 如此重要的部分原因 使用者應具有足夠的控制權,以使其與代表他們的umwelt互動的 從道德的角度來說,編碼的絕對最佳案例是,您已...