简单计算器设计代码

#import "AppDelegate.h"

#define kScreenWidth CGRectGetWidth([[UIScreen mainScreen] bounds])

#define kScreenHeight CGRectGetHeight([[UIScreen mainScreen] bounds])

#define kButtonWidth ((kScreenWidth - 5) / 4)

#define kButtonHeight kButtonWidth

#define kButtonOffsetX 1

#define kButtonOffsetY 1

#define kButtonOriginY (kScreenHeight - (kButtonHeight + kButtonOffsetY) * 5)

@interface AppDelegate ()

{

BOOL _isHave;

BOOL _isResult;

}

@property (retain,nonatomic)NSString *string;

@end

@implementation AppDelegate

-(void)dealloc{

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor blackColor];

[self.window makeKeyAndVisible];

/*

AC +- % /

7  8  9 *

4  5  6 -

1  2  3 +

0      .  =

*/

NSArray *titles = @[@[@"AC", @"+-", @"baifenhao", @"chuhao"], @[@"7", @"8", @"9", @"chenghao"], @[@"4", @"5", @"6", @"jianhao"], @[@"1", @"2", @"3", @"jiahao"], @[@"0",@"", @"dian", @"dengyu"]];

for (int i = 0; i < titles.count; i++) {

NSArray *subTitles = titles[i];

for (int j = 0; j < subTitles.count; j++) {

NSString *title = subTitles[j];

if ([title isEqualToString:@""]) {

continue;

}

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

aButton.tag=100+(10*i)+j;

CGRect frame = CGRectMake(kButtonOffsetX + (kButtonWidth + kButtonOffsetX) * j, kButtonOriginY + (kButtonHeight + kButtonOffsetY) * i, kButtonWidth, kButtonHeight);

if ([title isEqualToString:@"0"]) {

frame.size.width = frame.size.width * 2 + kButtonOffsetX;

}

aButton.frame = frame;

NSString *normalImageName = [subTitles[j] stringByAppendingString:@"_btn_nor"];

NSString *highlightedImageName = [subTitles[j] stringByAppendingString:@"_btn_highlighted"];

[aButton setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];

[aButton setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];

[aButton setTitle:title forState:UIControlStateNormal];

[aButton addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];

[self.window addSubview:aButton];

}

}

UITextField *aLabel=[[[UITextField alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-5*(kButtonHeight+kButtonOffsetY))]autorelease];

aLabel.tag=1000;

aLabel.backgroundColor=[UIColor blackColor];

aLabel.clearsOnBeginEditing=YES;

aLabel.enabled=NO;

[email protected]"0";

aLabel.textColor=[UIColor whiteColor];

aLabel.font=[UIFont fontWithName:@"Times New Roman" size:70];

aLabel.textAlignment=NSTextAlignmentRight;

[_window addSubview:aLabel];

return YES;

}

-(void)handleButtonAction:(UIButton *)abutton{

UITextField *label=(UITextField *)[_window viewWithTag:1000];

switch (abutton.tag) {

case 100:{

[email protected]"0";

label.clearsOnBeginEditing=YES;

break;

}case 101:{

float num=[label.text floatValue];

NSNumber *number=[NSNumber numberWithFloat:-num];

label.text=[NSString stringWithFormat:@"%@",number];

break;

}case 102:{

float num=[label.text floatValue];

NSNumber *number=[NSNumber numberWithFloat:(num/100)];

label.text=[NSString stringWithFormat:@"%@",number];

break;

}case 110:

case 111:

case 112:

case 120:

case 121:

case 122:

case 130:

case 131:

case 132:

case 140:

case 142:{

if (_isHave||[label.text isEqualToString:@""]||_isResult) {

[email protected]"";

label.text=[label.text stringByAppendingString:abutton.currentTitle];

}else{label.text=[label.text stringByAppendingString:abutton.currentTitle];

}

_isHave=NO;

_isResult=NO;

break;

}case 103:

case 113:

case 123:

case 133:{

self.string=[label.text stringByAppendingString:abutton.currentTitle];

_isHave=YES;

break;

}case 143:{

if ([self.string hasSuffix:@"jiahao"]) {

float value1=[self.string floatValue];

float value2=[label.text floatValue];

NSNumber *number=[NSNumber numberWithFloat:(value1+value2)];

label.text=[NSString stringWithFormat:@"%@",number];

[email protected]"";

_isResult=YES;

}else if([self.string hasSuffix:@"jianhao"]) {

float value1=[self.string floatValue];

float value2=[label.text floatValue];

NSNumber *number=[NSNumber numberWithFloat:(value1-value2)];

label.text=[NSString stringWithFormat:@"%@",number];

[email protected]"";

_isResult=YES;

}else if([_string hasSuffix:@"chenghao"]) {

float value1=[self.string floatValue];

float value2=[label.text floatValue];

NSNumber *number=[NSNumber numberWithFloat:(value1*value2)];

label.text=[NSString stringWithFormat:@"%@",number];

[email protected]"";

_isResult=YES;

}else if([self.string hasSuffix:@"chuhao"]) {

float value1=[self.string floatValue];

float value2=[label.text floatValue];

NSNumber *number=[NSNumber numberWithFloat:(value1/value2)];

label.text=[NSString stringWithFormat:@"%@",number];

[email protected]"";

_isResult=YES;

}

break;

}

default:

break;

}

}

时间: 2024-12-18 12:15:18

简单计算器设计代码的相关文章

Javascript 实现简单计算器实例代码

Javascript 实现简单计算器实例代码 这篇文章主要介绍了Javascript 实现简单计算器实例代码的相关资料,需要的朋友可以参考下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

简单计算器设计(WPF)

要求: 文本框居中,用户不能修改运算结果 当用户选择不同的运算类型时 下方GroupBox的标题与所选运算类型相对应 且文本框数字立即清空 单击[计算]按钮时 如果文本框输入的内容非法 结果文本框显示问号 运行效果: XAML: 后台代码: 1 namespace A._2._2 2 { 3 /// <summary> 4 /// MainWindow.xaml 的交互逻辑 5 /// </summary> 6 public partial class MainWindow : W

vue实现网页简单计算器实例代码

效果: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> </head> <body> <div id=&q

[C#]组合框设计windows加减乘除简单计算器应用

组合框使用方法,按照下面图片上提示,设计一个windows应用程序,实现不同运算. 界面设计: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace comb

javascript简单计算器代码分析

javascript简单计算器代码分析:也许网页中需要一个简单的计算器功能,这个时候就要掌握如何编写,起码应该会修改,下面就通过一个简单的实例介绍一下如何实现简单的计算器效果,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/&

大一C语言结课设计之《简单计算器》

/*===============================================*** 设计目的:简单计算器,计算形如10*(20.2-30.6)+5.0/2的表达式值 ** 简要原理:中缀式转逆波兰式(后缀式) ** IDE:Dev-Cpp 4.9.9.2 ** 注意事项:括号必须是英文状态的 ** 时间: 2014-6-17 \*===============================================*/ #include <stdio.h> #in

简单计算器的设计

#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *show; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typical

初学者如何做一个简单的计算器,代码分享

先新建一个类 startCalculator 声明如下 #import <Foundation/Foundation.h> @interface StartCalculator : NSObject //声明两个要计算的变量 @property float opValue1; @property float opValue2; //声明一个运算符 @property char op; //普通方法 //- (float) gzyWorkAdd; // //- (float) gzyWorkSu

简单计算器 (c语言课程设计)

可以实现简单的加减乘除四则运算 #include<stdio.h> #include<string.h> #define MAX 10100 int main() { int j,i,s,t,top,wrong; double n,m,sum; char fu,kongge,a; double zhan[MAX]; printf("-----------------------欢迎使用简单计算器-----------------------\n"); print