有没有更清晰的方法来编写这个Objective-c代码?

Jak*_*ake 4 c objective-c

我是Objective-c和编程的新手.我是一名护理人员,我决定学习目标课程.我对c有一些经验,这就是为什么这个程序以这种方式编码的原因.我想知道是否有一种更有效的方法来使用objective-c进行编码?谢谢.(程序编译时没有错误,所以如果在某处出现语法错误,可能是因为我是新手在代码块内的板上转义字符)


#import <Foundation/Foundation.h>

void calcDiagnosis (float pHInput, int paCO2Input, int hCO3Input);

int main (int argc, const char * argv[]){
 int i;
 int repeat;
 i = 0;
 for(i = 0; i < 3; i++){
 //Initialize lab value variables
 float pH;
 int paCO2;
 int hCO3;

 //Introduction
 NSLog(@"Welcome to the ABG Lab Value Interpreter v1.0\n");
 NSLog(@"Please enter the necessary values.\n");

 //Gather the necessary values
 NSLog(@"Enter the pH value:");
 scanf("%f", &pH);
 NSLog(@"Enter the PaCO2 value:");
 scanf("%i", &paCO2);
 NSLog(@"Enter the HCO3 value:");
 scanf("%i", &hCO3);
 calcDiagnosis (pH, paCO2, hCO3);

 //Control Loop
 NSLog(@"Again?\n 1: Yes\n 2: No");
 scanf("%i", &repeat);

 switch (repeat){
 case 1: 
  i = 0;
  break;
 case 2:
  i = 3;
  break;
  }
 }

        return 0;
}

void calcDiagnosis (float pHInput, int paCO2Input, int hCO3Input)
{
 //Transfer the arguments to new variables
 float pH = pHInput;
 int paCO2 = paCO2Input;
 int hCO3 = hCO3Input;

 //////////////////////////////////
 //Diagnose Respiratory Acidosis//
 ////////////////////////////////

  //Acute
 if ((pH < 7.35) && (paCO2 > 45) && (hCO3 >=22 && hCO3 <=26)) {
  NSLog(@"Acute Respiratory Acidosis");
  }
  //Partially Compensated
 if ((pH < 7.35) && (paCO2 > 45) && (hCO3 >26)) {
  NSLog(@"Partially Compensated Respiratory Acidosis");
  }
  //Compensated
 if ((pH >= 7.35 && pH <= 7.45) && (paCO2 > 45) && (hCO3 >26)) {
  NSLog(@"Compensated Respiratory Acidosis");
  }

 ///////////////////////////////////
 //Diagnose Respiratory Alkalosis//
 /////////////////////////////////

  //Acute
 if ((pH > 7.45) && (paCO2 < 35) && (hCO3 >=22 && hCO3 <=26)) {
  NSLog(@"Acute Respiratory Alkalosis");
  }
  //Partially Compensated
 if ((pH > 7.45) && (paCO2 < 35) && (hCO3 <22)) {
  NSLog(@"Partially Compensated Respiratory Alkalosis");
  }
  //Compensated
 if ((pH >= 7.35 && pH <= 7.45) && (paCO2 < 35) && (hCO3 <22)) {
  NSLog(@"Compensated Respiratory Alkalosis");
  }

 //////////////////////////////////
 //Diagnose Metabolic Acidosis////
 ////////////////////////////////

  //Acute
 if ((pH < 7.35) && (paCO2 >= 35 && paCO2 <= 45) && (hCO3 <22)) {
  NSLog(@"Acute Metabolic Acidosis");
  }
  //Partially Compensated
 if ((pH < 7.35) && (paCO2 < 35) && (hCO3 >22)) {
  NSLog(@"Partially Compensated Metabolic Acidosis");
  }
  //Compensated
 if ((pH >= 7.35 && pH <= 7.45) && (paCO2 < 35) && (hCO3 <22)) {
  NSLog(@"Compensated Metabolic Acidosis");
  }

 //////////////////////////////////
 //Diagnose Metabolic Alkalosis///
 ////////////////////////////////

  //Acute
 if ((pH > 7.45) && (paCO2 >= 35 && paCO2 <= 45) && (hCO3 >26)) {
  NSLog(@"Acute Metabolic Alkalosis");
  }
  //Partially Compensated
 if ((pH > 7.45) && (paCO2 > 45) && (hCO3 >26)) {
  NSLog(@"Partially Compensated Metabolic Alkalosis");
  }
  //Compensated
 if ((pH >= 7.35 && pH <= 7.45) && (paCO2 > 45) && (hCO3 >26)) {
  NSLog(@"Compensated Metabolic Alkalosis");
  }

 //////////////////////
 //Diagnosis Normal///
 ////////////////////
 if ((pH >= 7.35 && pH <= 7.45) && (paCO2 >= 35 && paCO2 <= 45) && (hCO3 >= 22 && hCO3 <= 26)) {
  NSLog(@"Normal Values");
  }
 return;
}

Run Code Online (Sandbox Code Playgroud)

aep*_*yus 6

这可能是一个棘手的问题.随着您获得更多经验,您将更加熟悉更高级的概念.您正在处理的问题实际上非常复杂,并且是一个很好的培训工具.

您最大的问题是您当前的解决方案不使用任何面向对象,这可能使将来更难以维护和/或扩展.

最终,最佳代码结构的问题可以有很多答案,你可能不知道哪个更好,直到你为程序添加更多功能.

我已经重新渲染了你的程序,我觉得它是一个坚实的终极游戏结构(而不​​是为了更温顺的中间步骤而拍摄).不幸的是,刚开始时这可能是一个小小的飞跃.

此解决方案中有两个高级概念,面向对象编程和选择器.选择器是一个非常强大的工具,允许您使用变量传递程序周围的实际指令.在您的情况下,您可以将if语句存储在诊断对象中.

无论如何,请随时询问有关以下内容的任何问题:

Vitals.h

#import <Foundation/Foundation.h>

@interface Vitals : NSObject {
    float _pH;
    int _paCO2;
    int _hCO3;
}

- (id) initWithPH:(float)pH paCO2:(int)paCO2 hCO3:(int)hCO3;

- (float) pH;
- (int) paCO2;
- (int) hCO3;

@end
Run Code Online (Sandbox Code Playgroud)

Vitals.m

#import "Vitals.h"

@implementation Vitals

- (id) initWithPH:(float)pH paCO2:(int)paCO2 hCO3:(int)hCO3 {
    if (self = [super init]) {
        _pH = pH;
        _paCO2 = paCO2;
        _hCO3 = hCO3;
    }
    return self;
}

- (float) pH {return _pH;}
- (int) paCO2 {return _paCO2;}
- (int) hCO3 {return _hCO3;}

@end
Run Code Online (Sandbox Code Playgroud)

Diagnosis.h

#import <Foundation/Foundation.h>
@class Vitals;

@interface Diagnosis : NSObject {
    NSString* _name;
    id _delegate;
    SEL _test;
}

- (id) initWithName:(NSString*)name delegate:(id)delegate test:(SEL)test;

- (NSString*) name;

- (BOOL) test:(Vitals*)vitals;

@end
Run Code Online (Sandbox Code Playgroud)

Diagnosis.m

#import "Diagnosis.h"

@implementation Diagnosis

- (id) initWithName:(NSString*)name delegate:(id)delegate test:(SEL)test {
    if (self = [super init]) {
        _name = [name retain];
        _delegate = delegate;
        _test = test;
    }
    return self;
}
- (void) dealloc {
    [_name release];
    [super dealloc];
}

- (NSString*) name {return _name;}

- (BOOL) test:(Vitals*)vitals {
    return [(NSNumber*)[_delegate performSelector:_test withObject:vitals] boolValue];
}

@end
Run Code Online (Sandbox Code Playgroud)

Doctor.h

#import <Foundation/Foundation.h>
@class Vitals;
@class Diagnosis;

@interface Doctor : NSObject {
    NSMutableArray* _diagnoses;
}

- (void) learnDiagnosis:(Diagnosis*)diagnosis;

- (Diagnosis*) diagnose:(Vitals*)vitals;

@end
Run Code Online (Sandbox Code Playgroud)

Doctor.m

#import "Diagnosis.h"
#import "Doctor.h"

@implementation Doctor

- (id) init {
    if (self = [super init]) {
        _diagnoses = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void) dealloc {
    [_diagnoses release];
    [super dealloc];
}

- (void) learnDiagnosis:(Diagnosis*)diagnosis {
    [_diagnoses addObject:diagnosis];
}

- (Diagnosis*) diagnose:(Vitals*)vitals {
    for (Diagnosis* diagnosis in _diagnoses) {
        if ([diagnosis test:vitals])
            return diagnosis;
    }
    return 0;
}

@end
Run Code Online (Sandbox Code Playgroud)

Differential.h

#import <Foundation/Foundation.h>

@interface Differential : NSObject {}

- (void) teach:(Doctor*)doctor;

@end
Run Code Online (Sandbox Code Playgroud)

Differential.m

#import "Vitals.h"
#import "Diagnosis.h"
#import "Doctor.h"
#import "Differential.h"

@implementation Differential

- (NSNumber*) acuteRespiratoryAcidosis:(Vitals*)vitals {
    return [NSNumber numberWithBool:(([vitals pH] < 7.35) && ([vitals paCO2] > 45) && ([vitals hCO3] >=22 && [vitals hCO3] <=26))];
}
- (NSNumber*) partiallyCompensatedResporatoryAcidosis:(Vitals*)vitals {
    return [NSNumber numberWithBool:(([vitals pH] < 7.35) && ([vitals paCO2] > 45) && ([vitals hCO3] >26))];
}

- (void) teach:(Doctor*)doctor {
    Diagnosis* diagnosis;

    diagnosis = [[Diagnosis alloc] initWithName:@"Acute Respiratory Acidosis" delegate:self test:@selector(acuteRespiratoryAcidosis:)];
    [doctor learnDiagnosis:diagnosis];
    [diagnosis release];

    diagnosis = [[Diagnosis alloc] initWithName:@"Partially Compensated Respiratory Acidosis" delegate:self test:@selector(partiallyCompensatedResporatoryAcidosis:)];
    [doctor learnDiagnosis:diagnosis];
    [diagnosis release];
}

@end
Run Code Online (Sandbox Code Playgroud)

Sandbox.h

#import <Foundation/Foundation.h>
#import "Vitals.h"
#import "Diagnosis.h"
#import "Doctor.h"
#import "Differential.h"

void run () {
    float pH=7.2;
    int paCO2=47;
    int hCO3=25;

    Doctor* doctor = [[Doctor alloc] init];
    Differential* differential = [[Differential alloc] init];
    [differential teach:doctor];

    Vitals* vitals = [[Vitals alloc] initWithPH:pH paCO2:paCO2 hCO3:hCO3];

    Diagnosis* diagnosis = [doctor diagnose:vitals];
    NSLog(@"%@",[diagnosis name]);

    [vitals release];
    [differential release];
    [doctor release];
}
Run Code Online (Sandbox Code Playgroud)