『Ohgas'Fortune』

2017/10/19

こちらの問題ときました。
Problem A : Ohgas' Fortune
資金運用から単利と複利を作成しました。
データセットの金利パラメータによって単利と複利を切り替えながら計算しました。

#import <Foundation/Foundation.h>

@interface FundManagement : NSObject
@property unsigned long blance;
@property double charge;
@property int rate;
-(id)init;
-(void)oneyearlater;
-(void)nyearlater:(int)y;
-(unsigned long)deposit;
@end

@implementation FundManagement
@synthesize blance;
@synthesize charge;
@synthesize rate;
-(id)init
{
    if((self = [super init]) == nil){
        
    }
    blance = charge = rate = 0;
    return(self);
}
-(void)oneyearlater{return;}
-(void)nyearlater:(int)y
{
    for(int i = 1; i <= y; i++){
        [self oneyearlater];
    }
    return;
}
-(unsigned long)deposit{return(0);}
@end

//複利
@interface CompoundInterest : FundManagement
-(void)oneyearlater;
-(unsigned long)deposit;
@end

@implementation CompoundInterest
-(void)oneyearlater
{
    self.blance = self.blance + (self.blance * self.charge) - self.rate;
    return;
}
-(unsigned long)deposit{return(self.blance);}
@end

//単利
@interface SimpleInterest : FundManagement
@property long interset;
-(id)init;
-(void)oneyearlater;
-(unsigned long)deposit;
@end

@implementation SimpleInterest
@synthesize interset;
-(id)init
{
    if((self = [super init]) == nil){
        
    }
    interset = 0;
    return(self);
}
-(void)oneyearlater
{
    interset += self.blance * self.charge;
    self.blance -= self.rate;
}
-(unsigned long)deposit{return(self.blance + interset);}
@end

#define SIZE 100
struct MANAGEMENT{
    int interset;
    double charge;
    int rate;
};

struct DATESET{
    long blance;
    int y;
    int n;
    struct MANAGEMENT arr[SIZE];
}dataset[SIZE];

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        int m, i,j;
        scanf("%d", &m);
        for(i = 0; i < m; i++){
            scanf("%ld", &dataset[i].blance);
            scanf("%d", &dataset[i].y);
            scanf("%d", &dataset[i].n);
            for(j = 0; j < dataset[i].n; j++){
                scanf("%d %lf %d",
                      &dataset[i].arr[j].interset,
                      &dataset[i].arr[j].charge,
                      &dataset[i].arr[j].rate
                );
            }
        }
        
        FundManagement *mg;
        CompoundInterest *ci = [[CompoundInterest alloc] init];
        SimpleInterest *si = [[SimpleInterest alloc] init];
        for(i = 0; i < m; i++){
            for(j = 0; j < dataset[i].n; j++){
                mg = (dataset[i].arr[j].interset)?(ci):(si);
                mg.blance = dataset[i].blance;
                mg.charge = dataset[i].arr[j].charge;
                mg.rate = dataset[i].arr[j].rate;
                [mg nyearlater:dataset[i].y];
                printf("%ld
", [mg deposit]);
            }
        }
    }
    return 0;
}