2010-12-18 63 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

相關問題