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

时间:2010-12-18 03:46:13

标签: objective-c 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;
}

3 个答案:

答案 0 :(得分: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

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

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

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

Doctor.h

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

@interface Doctor : NSObject {
    NSMutableArray* _diagnoses;
}

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

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

@end

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

Differential.h

#import <Foundation/Foundation.h>

@interface Differential : NSObject {}

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

@end

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

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];
}

答案 1 :(得分:0)

虽然您发布的代码存在一些问题,但最大的问题是使用for循环,使用while循环会更自然。 for通常用于迭代(例如,读取或写入数组中的每个元素)。 while通常用于重复多次(但无限期)任务。有几种不同的方法可以实现这一点,但一个简单的修改如下:

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

    int menu_input = 0;

    while(menu_input != 2){
     //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", &menu_input);
 }

return 0;

}

正如您在帖子中所提到的,如果在现实世界环境中使用它,那么做一些基本的输入检查会很好。

答案 2 :(得分:0)

您是否正在学习Objective-C来编写Mac或iPhone程序?我会这样认为,因为这可以说是人们学习它的主要原因。如果您还没有,那么您应该查看Apple Developer网站,其中有很多有用的教程等。 我认为您应该尝试将其转换为GUI应用程序,因为您将使用更多的Objective-C和Cocoa。除了NSLog()(以及它们内部的NSStrings)之外,您确实编写了一个C程序。 Here is a nice tutorial.