2010-12-18 104 views
4

我是新来的Objective-C和一般编程。我是一名医务人员,我决定学习客观的程序。我有一些c的经验,这就是为什么这个程序是这样编码的。我想知道是否有一个更有效的方法来编码这与Objective-C?谢谢。 (该程序编译没有错误,所以如果在有语法错误的地方可能是因为我是新来的代码块内的板转义字符)有没有更清晰的方法来编写这个Objective-C代码?


#import &ltFoundation/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 &lt22)) { 
    NSLog(@"Partially Compensated Respiratory Alkalosis"); 
    } 
    //Compensated 
if ((pH >= 7.35 && pH <= 7.45) && (paCO2 < 35) && (hCO3 &lt22)) { 
    NSLog(@"Compensated Respiratory Alkalosis"); 
    } 

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

    //Acute 
if ((pH < 7.35) && (paCO2 >= 35 && paCO2 <= 45) && (hCO3 &lt22)) { 
    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 &lt22)) { 
    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; 
} 

+0

发现如果用户输入超出这些范围的值,则不提供诊断,程序会重新启动。我找到了解决这个问题的方法,但仍然有一堆“if”语句。 – Jake 2010-12-18 04:11:33

+0

现在我觉得自己像一个白痴,因为如果你输入任何非目标值(即一个字母而不是数字)程序崩溃...我知道如何解决这个...猜猜我需要停止懒惰和代码正确。 – Jake 2010-12-18 04:26:49

回答

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 

诊断is.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 

医生。米

#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]; 
} 
+0

哇...我有很多东西要学。非常感谢你。 – Jake 2010-12-18 05:35:45

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; 

}

在你的文章中提到,虽然,它将使你做一些基本的输入检查是否在真实世界环境中使用。

+0

非常感谢。我非常着急编写一些有用的代码,我甚至没有考虑while循环。我猜这些if语句是非常必要的吗?再次感谢。 – Jake 2010-12-18 04:44:37

0

你学习Objective-C编写的Mac或iPhone程序?我会这样认为,因为这可以说是人们学习它的主要原因。如果你还没有,你应该看看苹果开发者网站,他们有很多有用的教程,等等。 我认为你应该尝试把它变成一个GUI应用程序,因为你将会使用更多的Objective-C和Cocoa 。你已经写了一个C程序,除了NSLog()(和它们中的NSStrings)之外。 Here is a nice tutorial.

+0

我正在计划编写iPhone应用程序。问题是我正在使用PC并且目前无法访问Xcode ......这是与一些朋友的合资企业......不幸的是,我是具有编程经验的人(赢得了州竞争在高中... 10年前)。我的朋友有一台Macbook Pro,但我很想等到明天我可以访问它。使用GNUStep在这里编译代码,所以我没有访问漂亮的东西。这是我通过我和我一起学习的几本Objective-C和Cocoa书籍的一种方式。尽管感谢您的链接。你们都很棒。 – Jake 2010-12-18 05:31:46

相关问题