在编1元2次方程计算器器的时候为什么都是label1

&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!fsjoy1983 的BLOG
用户名:fsjoy1983
文章数:301
评论数:298
访问量:1132684
注册日期:
阅读量:5863
阅读量:12276
阅读量:377466
阅读量:1070287
51CTO推荐博文
这是界面图:650) this.width=650;" onclick='window.open("/viewpic.php?refimg=" + this.src)'
src="../attachment/322754.png" alt="" border="0" />这个可以在design界面里拖动组件,修改组件属性值来实现。然后是计算器的算法(仿照win下的计算器算法):问题描述:输入一个数,然后按某个运算符,再输入另一个数,按等号即可得出结果。只按照输入顺序计算结果,而非按照运算符优先级来得出结果,即,如果按顺序输入2+8*3=会得到30,而非26.算法描述:定义三个全局变量,分别是first(Number), second(Number), symbol(String)first代表二目运算中第一个数,second代表第二个数,symbol代表运算符(+-*/)(初始化之后flex对Number型默认值为NaN, String型默认值为null)1.二目运算第一个数的输入数字阶段,对于小数点(.)按钮,如果输入一次之后enable属性设为false.2.输入运算符(+-*/),将第一步输入的数字保存到first变量中,并将此运算符保存到symbol中,小数点(.)按钮enable属性为true.3.二目运算第二个数字输入阶段,对于小数点(.)按钮,如果输入一次之后enable属性设为false.4.&&&& 4.1如果输入等号(=),将前面存储的first, second, symbol按照相应规则运算得出结果,这个结果存储到first中,second和symbol分别设为NaN和null。小数点按钮enable属性为true。然后可以转入步骤2.&&&& 4.2 如果输入运算符(+-× /) ,将first, second, symbol按照规则运算出结果,结果保存到first中,second设为NaN, 将输入运算符保存到symbol中,小数点按钮enable属性为true。然后可以转入步骤3。下面是整个计算器的代码及注释:&?xml version="1.0" encoding="utf-8"?&&mx:Application xmlns:mx="/2006/mxml" layout="horizontal"&&mx:Script&&& &&![CDATA[& && &&& &&& &&& &public var first:N //定义二目运算第一个数的存储变量&& &&& &public var second:N//第二个数的存储变量&& &&& &public var symbol:S& //运算符& &&&&&&& public var display_content:String='0' //input_Text显示内容&& &&& &private function addText(str:String):void//输入数字时,显示内容&&&&&&& {&&&&&&&&&&& display_content+= //每点一个数字,显示内容增加进去&&&&&&&& &&& &var myFloat:Number = parseFloat(display_content);//将显示内容转换为Number型&&&&&&&& &&& &if(str!='.')&&&&&&&& &&& &&& &{&&&&&&&& &&& &&& &&& &txt_display.text = myFloat.toString();//将刚转换的Number型转换为String型,显示到input_Text中 && &&&&&&&& &&& &&& &}&&&&&&&& &&& &else//处理小数点情况&&&&&&&& &&& &&& &{&&&&&&&& &&& &&& &&& &txt_display.text = myFloat.toString()+".";&&&&&&&& &&& &&& &&& &bt_dot.enabled=&&&&&&&& &&& &&& &}&&&&&&&& &&& &&& &&&&&&&& }//C按钮的功能&&&&&&& private function c():void&&&&&&& {&&&&&& &&& &display_content ='0';&&&&&& &&& &txt_display.text='0';&&&&&& &&& &first=NaN;&&&&&& &&& &second=NaN;&&&&&& &&& &symbol=&&&&&& &&& &bt_dot.enabled=&&&&&&& }//CE按钮功能&&&&&&& private function ce():void&&&&&&& {&&&&&& &&& &display_content='0';&&&&&& &&& &txt_display.text='0';&&&&&& &&& &second=NaN;&&&&&& &&& &bt_dot.enabled=&&&&&&& }//运算功能&&&&&&& private function cal():void&&&&&&& {&&&&&& &&& &second=parseFloat(txt_display.text);&&&&&& &&& &switch (symbol)&&&&&& &&& &&& &&& &{&&&&&& &&& &&& &&& &&& &case "+":&&&&&& &&& &&& &&& &&& &first=first+&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &case "-":&&&&&& &&& &&& &&& &&& &first=first-&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &case "*":&&&&&& &&& &&& &&& &&& &first=first*&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &case "/":&&&&&& &&& &&& &&& &&& &&&&&&& &&& &&& &&& &&& &&& &first=first/&&&&&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &default:&& &&& &&& &&& &&& &&& &// do nothing&& &&& &&& &&& &&& &}&& &&& &&& &&& &txt_display.text=first.toString();&&&&&& &&& &&& &display_content='0';&&&&&& &&& &&& &symbol=&&&&&& &&& &&& &second=NaN;&&&&&& &&& &&& &bt_dot.enabled=&&&&&&& }//输入运算符(+-× /)符号处理&&&&&&& private function process_symbol(str:String):void&&&&&&& {&&&&&& &&& &if(first.toString()=='NaN')&&&&&& &&& &&& &{&&&&&& &&& &&& &&& &first=parseFloat(txt_display.text);&&&&&& &&& &&& &&& &if(str!="=")&&&&&& &&& &&& &&& &&& &{ symbol= }&&&&&& &&& &&& &&& &display_content='0';&&&&&& &&& &&& &&& &bt_dot.enabled=&&&&&& &&& &&& &}&&&&&& &&& &else if(second.toString()=='NaN' )&&&&&& &&& &{&&&&&& &&& &&& &symbol=&&&&&& &&& &}&&&&&& &&& &else&&&&&& &&& &{&&&&&& &&& &&& &&& &cal();&&&&&& &&& &}&&&&&&& }&&&&&& &//输入等号时 &&&&&& &&&&&&& private function equals():void&&&&&&& {&&&&&& &&& &if(first.toString()=='NaN')&&&&&& &&& &{&&&&&& &&& &&& &first=parseFloat(txt_display.text);&&&&&& &&& &&& &display_content='0';&&&&&& &&& &&& &bt_dot.enabled=&&&&&& &&& &}&&&&&& &&& &&&&&&& &&& &else &&&&&& &&& &{&&&&&& &&& &&& &cal();&&&&&& &&& &&& &&&&&&& &&& &}&&&&&&& }&&&&&& &&& &]]&&/mx:Script&//组件代码:&& &&mx:Canvas width="309" height="354"&&& &&& &&mx:Panel y="10" width="273.5" height="334" layout="absolute" title="calculator" horizontalAlign="center" verticalAlign="middle" backgroundColor="#AFB6B9" horizontalCenter="-2"&&& &&& &&& &&mx:TextInput x="10.75" y="29" width="231" id="txt_display" text="0" textAlign="right"/&&& &&& &&& &&mx:Button x="13" y="98" label="7" width="36" height="31" color="#0B0C0F" id="bt_7" click="addText('7')"/&&& &&& &&& &&mx:Button x="57" y="98" label="8" width="36" height="31" id="bt_8" click="addText('8')"/&&& &&& &&& &&mx:Button x="101" y="98" label="9" width="36" height="31" id="bt_9" click="addText('9')"/&&& &&& &&& &&mx:Button x="145" y="98" label="+" width="36" height="31" color="#0DC5ED" id="bt_plus" click="process_symbol('+')"/&&& &&& &&& &&mx:Button x="145" y="137" label="-" width="36" height="31" color="#089D5C" id="bt_minus" click="process_symbol('-')"/&&& &&& &&& &&mx:Button x="145" y="176" label="*" width="36" height="31" color="#F018CA" id="bt_multiply" click="process_symbol('*')"/&&& &&& &&& &&mx:Button x="145" y="215" label="/" width="36" height="31" color="#67B0BF" id="bt_divide" click="process_symbol('/')"/&&& &&& &&& &&mx:Button x="180" y="59" label="C" width="58" height="31" color="#FB2407" id="bt_c" click="c()"/&&& &&& &&& &&mx:Button x="189" y="98" label="=" width="49" height="148" id="bt_equal" click="equals()"/&&& &&& &&& &&mx:Button x="103.75" y="59" label="CE" width="58" height="31" id="bt_ce" click="ce()"/&&& &&& &&& &&mx:Button x="13" y="137" label="4" width="36" height="31" id="bt_4" click="addText('4')"/&&& &&& &&& &&mx:Button x="57" y="137" label="5" width="36" height="31" id="bt_5" click="addText('5')"/&&& &&& &&& &&mx:Button x="101" y="137" label="6" width="36" height="31" id="bt_6" click="addText('6')"/&&& &&& &&& &&mx:Button x="13" y="176" label="1" width="36" height="31" id="bt_1" click="addText('1')"/&&& &&& &&& &&mx:Button x="57" y="176" label="2" width="36" height="31" id="bt_2" click="addText('2')"/&&& &&& &&& &&mx:Button x="101" y="176" label="3" width="36" height="31" id="bt_3" click="addText('3')"/&&& &&& &&& &&mx:Button x="101" y="215" label="." width="36" height="31" id="bt_dot" click="addText('.')"/&&& &&& &&& &&mx:Button x="13" y="215" label="0" width="80" height="31" id="bt_0" click="addText('0')"/&&& &&& &&/mx:Panel&&& &&/mx:Canvas&&& &&/mx:Application&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)
17:35:09 22:50:12 09:37:25 00:36:07 23:37:18用iOS 做一个简易计算器 (功能完备)
源代码(.m文件)
#import "ZKJAppDelegate.h"
@interface
ZKJAppDelegate ()
@property (retain,nonatomic)
UIView *containV
@property (retain,
nonatomic) UIButton *
@property (retain,
nonatomic) UILabel *
@property (retain,
nonatomic) NSMutableString *
//保存字符
@property (assign,nonatomic)
double num1,num2,num3,num4;
//num1保存输入数值,2,保存运算前数值,3是运算结果,4判断进行何种运算
@implementation ZKJAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
// Override point for customization after application launch.
self.containView = [[UIView
alloc] initWithFrame:CGRectMake(0,
self.containView.backgroundColor = [UIColor
clearColor];
[self.window
addSubview:self.containView];
[self.containView
self.label = [[UILabel
alloc] initWithFrame:CGRectMake(30,
190, 50)];
self.label.backgroundColor = [UIColor
lightGrayColor];
self.label.textAlignment
= UITextAlignmentL
self.label.font
systemFontOfSize:32];
[self.containView
addSubview:self.label];
[self.label
release ];
UIButton *backButton = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
backButton.backgroundColor = [UIColor
lightGrayColor];
[backButton setFrame:CGRectMake(225,
[backButton setTitle:@"back"
forState:UIControlStateNormal];
[backButton addTarget:self
action:@selector(backClick:)
forControlEvents:UIControlEventTouchUpInside];
[self.containView
addSubview:backButton];
//添加1 - 9 的按钮
NSArray *array = [NSArray
arrayWithObjects:@"1",
@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",
int n = 0;
for (int i =
0 ; i < 3; i&#43;&#43; ) {
for (int j =
0 ; j < 3; j&#43;&#43; ) {
self.button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
self.button.frame =
CGRectMake(30 &#43;
65 * j , 150 &#43;
65 * i, 60 ,
self.button.backgroundColor = [UIColor
lightGrayColor];
[self.button
setTitle:array[n&#43;&#43;] forState:UIControlStateNormal];
[self.containView
addSubview:self.button];
[self.button
addTarget:self
action:@selector(one:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button0 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[button0 setFrame:CGRectMake(30,
button0.backgroundColor = [UIColor
lightGrayColor];
[button0 setTitle:@"0"
forState:UIControlStateNormal];
[button0 addTarget:self
action:@selector(one:)
forControlEvents:UIControlEventTouchUpInside] ;
[self.containView
addSubview:button0];
UIButton *point = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[point setFrame:CGRectMake(95,
point.backgroundColor = [UIColor
lightGrayColor];
[point setTitle:@"."
forState:UIControlStateNormal];
[point addTarget:self
action:@selector(one:)
forControlEvents:UIControlEventTouchUpInside] ;
[self.containView
addSubview:point];
[point release];
//添加删除
UIButton *deleteButton = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[deleteButton setFrame:CGRectMake(160,
deleteButton.backgroundColor = [UIColor
lightGrayColor];
[deleteButton setTitle:@"delete"
forState:UIControlStateNormal];
[deleteButton addTarget:self
action:@selector(deleteClick:)
forControlEvents:UIControlEventTouchUpInside];
[self.containView
addSubview:deleteButton];
[backButton release];
//添加运算符
NSArray *array1 = [NSArray
arrayWithObjects:@"&#43;",@"-",@"*",@"/",
for (int i =
0 ; i < 4; i&#43;&#43; ) {
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(225,
150 &#43; 65 * i,
60 , 60)];
button.backgroundColor = [UIColor
lightGrayColor];
[button setTitle:array1[i]
forState:UIControlStateNormal];
[self.containView
addSubview:button];
[button addTarget:self
action:@selector(two:)
forControlEvents:UIControlEventTouchUpInside];
[button release];
//添加等号
UIButton *istoButton = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[istoButton setFrame:CGRectMake(30,
istoButton.backgroundColor = [UIColor
lightGrayColor];
[istoButton setTitle:@"="
forState:UIControlStateNormal];
[istoButton addTarget:self
action:@selector(isto: )
forControlEvents:UIControlEventTouchUpInside];
[self.containView
addSubview:istoButton];
self.string = [[NSMutableString
alloc] init];
self.window.backgroundColor = [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
//键盘输入 数字事件
- (void)one:(id)sender {
if ([self.string
hasPrefix:@"&#43;"] || [self.string
hasPrefix:@"-"] || [self.string
hasPrefix:@"*"] || [self.string
hasPrefix:@"/"]) {
[self.string
setString:@""];
//*********
[self.string
appendString:[sender currentTitle]];//数字连续输入;;;
self.label.text
= [NSString
stringWithString:self.string];
self.num1 = [self.label.text
doubleValue];//将字符串转化为基本数据类型并赋给num1;
NSLog(@"%f",self.num1);
//运算符事件
- (void)two:(id)sender{
[self.string
setString:@""];
//*********
[self.string
appendString:[sender currentTitle]];
self.label.text =
if ([self.string
hasPrefix:@"&#43;"]) {
self.num2 =
self.num1;
self.num4 =
} else if ([self.string
hasPrefix:@"-"]){
self.num2 =
self.num1;
self.num4 =
} else if ([self.string
hasPrefix:@"*"]) {
self.num2 =
self.num1;
self.num4 =
} else if ([self.string
hasPrefix:@"/"]) {
self.num2 =
self.num1;
self.num4 =
//等号事件
- (void)isto:(id)sender{
if (self.num4 ==
self.num3 =
self.num2 &#43; [self.label.text
doubleValue];
self.label.text = [NSString
stringWithFormat:@"%f",self.num3];
self.num3 = 0;
[self.string
setString:@""];
} else if (self.num4 ==
self.num3 =
self.num2 - [self.label.text
doubleValue];
self.label.text = [NSString
stringWithFormat:@"%f",self.num3];
self.num3 = 0;
} else if (self.num4 ==
self.num3 =
self.num2 * [self.label.text
doubleValue];
self.label.text = [NSString
stringWithFormat:@"%f",self.num3];
self.num3 = 0;
} else if (self.num4 ==
self.num3 =
self.num2 / [self.label.text
doubleValue];
self.label.text = [NSString
stringWithFormat:@"%f",self.num3];
self.num3 = 0;
//全部删除的响应事件
- (void)deleteClick:(id)sender{
[self.string
setString:@"" ];
[self.string appendString:[sender currentTitle]];
self.label.text =
//消除一个数的响应事件
- (void)backClick:(id)sender{
NSMutableString
if ([self.string
length] > 0) {
NSInteger n = [self.string
[self.string
deleteCharactersInRange:NSMakeRange(n -
self.label.text =
- (void)applicationWillResignActive:(UIApplication *)application
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application
and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
- (void)applicationDidEnterBackground:(UIApplication *)application
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- (void)applicationWillEnterForeground:(UIApplication *)application
// Called as part of the transition from the background t here you can undo many of the changes made on entering the background.
- (void)applicationDidBecomeActive:(UIApplication *)application
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
- (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- (void)dealloc{
[self.window
[super dealloc];
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 5险1金计算器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信