iPhoneホーナー法

Horner’s rule をつかった代数方程式の計算機という感じで、iPhoneアプリのサンプルコードを描いてみます。


動かすとこんな感じ

サンプルコード

#import “ViewController.h”

@interface ViewController () <UITextFieldDelegate>

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    [self createBlackBoard];

    [self createPolynomial];

    [self createEqualButton];

    [self createInputField];

}

– (void)createBlackBoard

{

    CALayer *boardFrame = [CALayer layer];

    boardFrame.frame = self.view.bounds;

    boardFrame.backgroundColor = [UIColor colorWithHue:0.3 saturation:0.8 brightness:0.4 alpha:1.0].CGColor;

    boardFrame.borderWidth = 20;

    boardFrame.borderColor = [UIColor brownColor].CGColor;

    [self.view.layer addSublayer:boardFrame];

}

– (void)createPolynomial

{

    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];

    paragraph.maximumLineHeight = 12.0f;;

    

    for (int i=0; i<5; i++) {

        float x = CGRectGetMaxX(self.view.bounds) – 90 * (i+1);

        float y = 50;

        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 90, 40)];

        

        NSString *str = @”a0″;

        if (i>1)

            str = [NSString stringWithFormat:@”a%dx%d +”, i,i];

        else if (i == 1)

            str = @”a1x +”;

        

        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:str];

        [atts addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, str.length)];

        

        [atts addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Chalkduster” size:22] range:NSMakeRange(0, str.length)];

        

        [atts addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@”Chalkduster” size:12]} range:NSMakeRange(1, 1)];

        if (str.length > 2) {

            [atts addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Chalkduster” size:30] range:NSMakeRange(2, 1)];

        }

        if (str.length > 3) {

            [atts addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@”Chalkduster” size:15], NSBaselineOffsetAttributeName:@10} range:NSMakeRange(3, 1)];

        }

        

        l.attributedText = atts;

        [self.view addSubview:l];

    }

}

– (void)createEqualButton

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.titleLabel.font = [UIFont fontWithName:@”Chalkduster” size:80];

    [btn setTitle:@”=” forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [btn sizeToFit];

    

    btn.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) + 50);

    [self.view addSubview:btn];

    

    [btn addTarget:self action:@selector(calc) forControlEvents:UIControlEventTouchUpInside];

}

– (void)createInputField

{

    for (int i=0; i<5; i++) {

        float x = CGRectGetMaxX(self.view.bounds) – 90 * (i+1);

        float y = 100;

        UITextField *f = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 50, 50)];

        f.tag = i+1;

        f.layer.borderWidth = 5;

        f.layer.borderColor = [UIColor brownColor].CGColor;

        f.layer.cornerRadius = 25;

        f.backgroundColor = [UIColor clearColor];

        f.keyboardType = UIKeyboardTypeNumberPad;

        f.textAlignment = NSTextAlignmentCenter;

        f.delegate = self;

        f.textColor = [UIColor whiteColor];

        f.placeholder = [NSString stringWithFormat:@”a%d”, i];

        f.font = [UIFont fontWithName:@”Chalkduster” size:20];

        [self.view addSubview:f];

    }

    

    

    UITextField *xf = [[UITextField alloc] initWithFrame:CGRectMake(80, 200, 50, 50)];

    xf.tag = 10;

    xf.layer.borderWidth = 5;

    xf.layer.borderColor = [UIColor brownColor].CGColor;

    xf.backgroundColor = [UIColor clearColor];

    xf.keyboardType = UIKeyboardTypeNumberPad;

    xf.textAlignment = NSTextAlignmentCenter;

    xf.delegate = self;

    xf.textColor = [UIColor whiteColor];

    xf.placeholder = [NSString stringWithFormat:@”X”];

    xf.font = [UIFont fontWithName:@”Chalkduster” size:20];

    [self.view addSubview:xf];

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    textField.text = [string substringToIndex:1];

    [textField resignFirstResponder];

    

    return YES;

}

– (void)calc

{

    float x = [((UITextField*)[self.view viewWithTag:10]).text floatValue];

    

    NSMutableArray *coefficient = [NSMutableArray array];

    for (int i=1; i<=5; i++) {

        float y = [((UITextField*)[self.view viewWithTag:i]).text floatValue];

        [coefficient addObject:@(y)];

    }

    

    float answer = [self hornersRule:coefficient x:x];

    

    UILabel *answerLabel = (UILabel*)[self.view viewWithTag:100];

    if (!answerLabel) {

        answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(350, 200, 140, 80)];

        answerLabel.tag = 100;

        answerLabel.font = [UIFont fontWithName:@”Chalkduster” size:30];

        [self.view addSubview:answerLabel];

    }

    answerLabel.text = [@(answer) stringValue];

}

– (float)hornersRule:(NSArray*)a x:(float)x

{

    float y=0;

    for (int i= a.count; i>0; i–) {

        y = [a[i-1] floatValue] + x * y;

    }

    return y;

}

@end