家庭作业:找出一个计算器公式

时间:2009-12-05 21:52:23

标签: objective-c

所以我之前用C语言编写了简单的数学程序,但我手头的任务对于我目前的知识来说有点复杂。

我需要在这里找到计算器(计算器:http://ohts.wustl.edu/risk/calculator.html,附录中有一点涵盖它:http://ohts.wustl.edu/risk/formula.html)并将其编程为ObjC程序。我很难弄清楚这个的重量/配方。希望在正确的方向上找到帮助,以便我能够解决这个问题。

1 个答案:

答案 0 :(得分:0)

所有的实现都只是C,在NSNumber中包装双精度是没有意义的。

#import <Cocoa/Cocoa.h>

@interface GRE:NSObject
@end
@implementation GRE
+ (NSNumber *) compute:(double) age :(double) iop :(double) cct :(double) vcd :(double) psd
{
        double meanage =  5.64301, betaage = 0.23260;
        double meaniop =  24.1386, betaiop = 0.09025;
        double meancct = -14.3349, betacct = 0.71503;
        double meanpsd =  9.76001, betapsd = 0.12376;
        double meanvcd =  3.60828, betavcd = 0.17689;
        double S0_t = 0.91831;

        double t_cct = -1.0 * (cct/40.0);
        double t_psd = psd / .2;
        double t_vcd = vcd / 0.1;

        double agedecade = age / 10.0;
        double xbetamean = (agedecade - meanage) * betaage +
                (iop - meaniop) * betaiop +
                (t_cct - meancct) * betacct +
                (t_psd - meanpsd) * betapsd +
                (t_vcd - meanvcd) * betavcd;

        double risk = 1 - pow(S0_t, exp(xbetamean));
        return [NSNumber numberWithDouble:risk];
}
@end
int main()
{
        NSAutoreleasePool * p = [NSAutoreleasePool new];

        NSLog(@"Risk: %@", [GRE compute:(55) :(24) :(540): (.40) : (2.2)]);

        [p release];
}