2012-03-30 196 views
2

我一直在寻找一个科学计算器教程,但我找不到任何东西。我的目标是学习如何编写类似iPhone中的计算器应用程序。是否知道任何可以帮助我学习如何将科学函数添加到计算器应用程序的教程。请记住,我已经尝试了各种版本的基本计算器,并且已经通过了一些这些教程,只是想学习,并且可能会使用这些方法来添加科学函数,如rad,sin,cos和其他一些东西应用程序。我会很满意的,我可以得到任何帮助。iPhone/ipad科学计算器教程

AP

编辑:

因为有关于如何建立一个科学计算器在那里(至少我还没有发现具体的还没有任何东西)没有真材实料这里是运行的如何向下建立一个。

首先创建一个NSObject类来保存您的操作方法。 (这是可选的,如果你喜欢,你可以将它设置在你的getter(.m)文件的顶部)。然后声明以下头文件。

{ 
double operand; 
double savedNumber; 
NSString *waitingOperation; 
double waitingOperand; 
} 

- (void)setOperand:(double)aDouble; 
- (double)performOperation:(NSString *)operation; 
- (void)performWaitingOperation; 

@end 

然后在NSObject类的.m文件中声明操作函数的方法。类似如下:

-(void)setOperand:(double)num 
{ 
operand=num; 
} 
-(double)performOperation:(NSString *)operation 
{ 
if([operation isEqual:@"sqrt"]){ 
    operand = sqrt(operand); 
}else if ([operation isEqual:@"1/x"]){ 
    if(operand) 
     operand = 1/operand; 
}else if ([operation isEqual:@"2/x"]){ 
    if(operand) 
     operand = 2/operand; 
}else if ([operation isEqual:@"3/x"]){ 
    if(operand) 
     operand = 3/operand; 
}else if ([operation isEqual:@"+/-"]){ 
    if(operand) 
     operand = (-1) * operand; 
}else if ([operation isEqual:@"sin"]){ 
    operand = sin(operand); 
}else if ([operation isEqual:@"sinh"]){//you can add more scientific functions in the same way to this list. 
    operand = sinh(operand); 
}else if ([operation isEqual:@"cos"]){ 
    operand = cos(operand); 
}else if ([operation isEqual:@"cosh"]){ 
    operand = cosh(operand); 
}else if ([operation isEqual:@"tan"]){ 
    operand = tan(operand); 
}else if ([operation isEqual:@"tanh"]){ 
    operand = tanh(operand); 
}else if ([operation isEqual:@"M"]){ 
    savedNumber = operand; 
}else if ([operation isEqual:@"M+"]){ 
    savedNumber += operand; 
}else if ([operation isEqual:@"M-"]){ 
    savedNumber -= operand; 
}else if ([operation isEqual:@"Recall"]){ 
    [self setOperand:savedNumber]; 
}else if ([operation isEqual:@"C"]){ 
    savedNumber = 0; 
    operand = 0; 
    waitingOperand = 0; 
    savedNumber= 0; 
}else { 
    [self performWaitingOperation]; 
    waitingOperation = operation;  
    waitingOperand = operand; 
} 
return operand; 
    } 
    - (void) performWaitingOperation 
    { 
if([@"+" isEqual:waitingOperation]){ 
    operand = waitingOperand + operand ; 
} else if([@"-" isEqual:waitingOperation]){ 
    operand = waitingOperand - operand ; 
}else if([@"X" isEqual:waitingOperation]){ 
    operand = waitingOperand * operand ; 
}else if([@"/" isEqual:waitingOperation]){ 
    if(operand) 
     operand = waitingOperand/operand ; 
} 
    } 

现在,你是想显示你需要访问所有这些功能,并在你的店铺显示它们,如按钮和文本字段等计算器视图控制器。这里是.h文件

IBOutlet UILabel *display; 
SCalcAI *ai; 
BOOL userIsInTheMiddleOfTypingNumber; 
} 

- (IBAction) digitPressed: (UIButton *)sender; 
- (IBAction) operationPressed: (UIButton *) sender; 

记住要将NSObject类头文件导入到此文件的顶部,否则会出现错误。那么在.m文件中你需要声明这些函数。

-(NameofyourNSOBjectClass *) ai 
{ 
if(!ai) 
{ 
ai = [[SCalcAI alloc] init]; 
} 
return ai; 
    } 

    - (IBAction) digitPressed: (UIButton *)sender 
    { 

NSString *digit = [[sender titleLabel] text]; 
    if(userIsInTheMiddleOfTypingNumber){ 
     [display setText:[[display text] stringByAppendingString:digit]]; 
    } else{ 
     [display setText:digit]; 
     userIsInTheMiddleOfTypingNumber = YES; 
    } 

} 
- (IBAction) operationPressed: (UIButton *) sender 
{ 
    if(userIsInTheMiddleOfTypingNumber){ 
     [[self ai] setOperand:[[display text] doubleValue]]; 
     userIsInTheMiddleOfTypingNumber = NO; 
    } 
    NSString *operation = [[sender titleLabel] text]; 
    double result = [[self ai] performOperation:operation]; 
    [display setText:[NSString stringWithFormat:@"%g", result]]; 
} 

然后将所有按钮连接到他们需要在故事板或xib中连接的操作。就是这样。以上是整个事情的骨架版本,但我相信你可以在此基础上进行构建。如果您需要帮助,请发送消息给我。

+0

这是时髦:d我会设置一个XCode项目,测试了这一点:看到这一切是如何走到一起。如果你在同一时间有一个git仓库和你的工作示例应用程序会很好。作为使我的代码非常可怕的事情之一,我试图为+9按钮创建委托方法,其中包括10个数字,基本函数,然后是tan,sqrt等。必须有一种组合方式一切都很好,而不是通过UIButtons连接到UIActions。唉,你的代码在这里让我开始使数学代码看起来不错。 – Pavan 2014-02-11 20:35:16

+0

嘿,我有几个问题: 1)除了保持代码清洁,是否有任何其他原因,你有'waitingOperation'方法分开,而不是在相同的方法? 2)另外,你能解释一下'operand','savedNumber','waitingOperation'和'waitingOperand'是关于什么的? – Pavan 2014-02-11 21:23:59

+0

@Pavan 1)没有真正的原因,我只是喜欢干净和平易近人的代码。保存的数字是指当您想要切换操作(如一个功能)时,然后按住同时立即创建另一个操作。等待操作正在使用保存的数字并完成该操作,等待操作数是等待发生的操作,因此需要等于操作数。另外你还有等待操作。在这种方法中,你不需要在getter文件的顶部声明main函数为常量,这就是我为什么这样写的原因。我相信你能做得比我做得更好。 – 2014-02-12 03:52:48

回答

3

Standford iOS programming class cs193p有一个构建简单计算器的示例。可能是一个开始的好地方。伟大的课程,伟大的视频。 Paul Hagarty是一位非常好的导师。一旦你完成了核心任务,科学功能就不会难以添加。

两个资源: http://www.tusharsharma.in/project/open-source/mathematical-calculator-iphone4/

http://emplementation.blogspot.com/2010/11/ios-development-tutorials-on-itune-u.html

+0

我已经度过了保罗在他的演讲中所做的一切。但我无法弄清楚如何添加科学功能。他的讲座停止创建一个简单的计算器。我也浏览了iOS的文档,发现了一组非常好的代码,但它又是一个基本的代码。我希望通过发现如何增加科学功能来学习更多。但谢谢你的回复。 – 2012-03-30 20:20:19

+0

查看我刚刚添加的资源 – 2012-03-30 20:30:30

+0

感谢托德。我会看看这些源代码。我认为这些内容将引导我走向正确的方向。我想我有一些功课要做。再次感谢。 – 2012-03-31 01:56:24