我是新来的Objective-C和一般编程。我是一名医务人员,我决定学习客观的程序。我有一些c的经验,这就是为什么这个程序是这样编码的。我想知道是否有一个更有效的方法来编码这与Objective-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;
}
发现如果用户输入超出这些范围的值,则不提供诊断,程序会重新启动。我找到了解决这个问题的方法,但仍然有一堆“if”语句。 – Jake 2010-12-18 04:11:33
现在我觉得自己像一个白痴,因为如果你输入任何非目标值(即一个字母而不是数字)程序崩溃...我知道如何解决这个...猜猜我需要停止懒惰和代码正确。 – Jake 2010-12-18 04:26:49