OGeek|极客世界-中国程序员成长平台

标题: ios - iPhone/ipad 科学计算器教程 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:39
标题: ios - iPhone/ipad 科学计算器教程

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

AP

编辑:

因为没有关于如何构建科学计算器的真实 Material (至少我还没有找到任何具体的东西)这里是如何构建一个的失败。

首先创建一个 NSObject 类来保存你的操作方法。 (这是可选的,如果您愿意,可以将其设置在您的 getter (.m) 文件之上。)您可以随意调用它。然后在头文件中声明如下。

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

- (void)setOperanddouble)aDouble; 
- (double)performOperationNSString *)operation;
- (void)performWaitingOperation;

@end

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

-(void)setOperanddouble)num
{
operand=num;
}
-(double)performOperationNSString *)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 ;
}
    }

现在在 View Controller 中,您尝试显示计算器,您需要访问所有这些功能并将它们显示在您的 socket 中,例如按钮和文本字段等。这是.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] performOperationperation]; 
    [display setText:[NSString stringWithFormat:@"%g", result]];
}

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



Best Answer-推荐答案


Standford iOS programming class cs193p 有一个构建简单计算器的示例。可能是一个很好的起点。很棒的类(class),很棒的视频。 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

关于ios - iPhone/ipad 科学计算器教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9949745/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4