I’m new to objective-c and programming in general. I’m a paramedic and I’ve decided to learn to program in objective. I’ve got some experience with c, which is why this program is coded this way. I was wondering if there were a more efficient way to code this with objective-c? Thank you. (The program compiles without errors, so if there’s a syntax error somewhere in there it’s probably because i’m new to escaping characters on boards within the code blocks)
#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;
}
This can be a difficult question. As you get more experienced you will become more comfortable with more advanced concepts. The problem you are working on is actually quite sophisticated and makes for a great training tool.
Your biggest issue is that your current solution does not use any object orientation, which can make it more difficult to maintain and/or expand in the future.
Ultimately, the question of the optimal code structure can have many answers and you may not know which is better until farther down the line until you have added more functionality to your program.
I have re-rendered your program, in what I feel is a solid end game structure (as opposed to shooting for a more meek intermediate step). Unfortunately, this may be a bit of a leap when just starting out.
There are two advanced concepts in this solution, object oriented programming and selectors. Selectors are an incredibly powerful tool that allow you to pass actual instructions around your program using variables. In your case, you can store the if statements in your Diagnosis Objects.
At any rate, please feel free to ask any questions about the following:
Vitals.h
Vitals.m
Diagnosis.h
Diagnosis.m
Doctor.h
Doctor.m
Differential.h
Differential.m
Sandbox.h