Ajout première partie

This commit is contained in:
rick 2021-03-25 18:35:10 +01:00
parent 90f99960fe
commit 1254e2946a
Signed by: Rick
GPG key ID: 2B593F087240EE99
3 changed files with 104 additions and 0 deletions

45
jour14/jour14.m Normal file
View file

@ -0,0 +1,45 @@
#import "raindeer.h"
#import <Foundation/Foundation.h>
int main()
{
// obligatoire dans un main
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
NSString *path = @"input";
NSError *err;
NSString *contentFile = [[NSString alloc] initWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&err];
NSArray *lines = [contentFile componentsSeparatedByString:@"\n"];
NSMutableArray *listDeer = [[NSMutableArray alloc] init];
for (NSString *tmp in lines)
{
NSArray *parsedSpace = [tmp componentsSeparatedByString:@" "];
if ([parsedSpace count] > 2)
{
Raindeer *rd = [Raindeer initWithName:[parsedSpace objectAtIndex:0]
zatSpeed:[[parsedSpace objectAtIndex:3] integerValue]
boostTime:[[parsedSpace objectAtIndex:6] integerValue]
LunchTime:[[parsedSpace objectAtIndex:13] integerValue]];
[listDeer addObject:rd];
}
}
int ret = 0;
int tmp = 0;
for (Raindeer *rd in listDeer)
{
tmp = [rd calculDistance:2503];
if (tmp > ret)
ret = tmp;
[rd release];
}
NSLog(@"le résultat est : %d", ret);
//finir le main par ça
[contentFile release];
[listDeer release];
[myPool drain];
return 0;
}

19
jour14/raindeer.h Normal file
View file

@ -0,0 +1,19 @@
#import <Foundation/Foundation.h>
@interface Raindeer : NSObject
{
NSString *name;
int speed;
int boostTime;
int pause;
}
// permet de générer des setters et getter selon des attributs
// ici retain, nonatomic et assign
@property (retain, nonatomic) NSString *name;
@property (assign) int speed, boostTime, pause;
+ (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)timeSpeed LunchTime:(int)pause;
- (int) calculDistance:(int)time;
@end

40
jour14/raindeer.m Normal file
View file

@ -0,0 +1,40 @@
#import "raindeer.h"
#import <Foundation/Foundation.h>
@implementation Raindeer
// permet de créer les getters et setters à partir de property
@synthesize name, speed, boostTime, pause;
+ (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)boostTime LunchTime:(int)pause
{
Raindeer *new;
if ((new = [[Raindeer alloc] init]))
{
new.name = name;
new.speed = speed;
new.boostTime = boostTime;
new.pause = pause;
}
return new;
}
- (int) calculDistance:(int) time
{
int ret = 0;
while (time > 0)
{
int rest = boostTime - time;
ret += (rest > 0) ? speed * rest : speed * boostTime;
time -= boostTime + pause;
}
return ret;
}
- (void) dealloc
{
[name release];
[super dealloc];
}
@end