As some of you may have seen, I experimented with a program moving from Fahrenheit to Celsius and vice versa in Objective-C based on console input and output. I just do this to learn the language better, and I think this is a good way to demonstrate some of the things that I was exposed to.
So, in any case, I want to include in my program both the Kelvin and Rankin scales, a good proposal that I would like to consider on the proposal of dreamlax.
Now, up to this point, my program has functioned relatively well for conversion between Fahrenheit and Celsius, but if I need to convert between these four different scales, I will have to work on it differently in my opinion.
First of all, I have to give the end user a choice of their initial temperature, the temperature that they want to convert.
To do this, I need to request them with an NSLog or printf statement in which they should do this.
Now I would prefer that they print Fahrenheit, Celsius, Kelvin or Rankin, based on what they would ever want, rather than 0-3. Now I do not think scanf is capable, but if I am mistaken, I would like to know if it has this function, or is it another method that I can use to add this to my program.
Then I got a little lost. Obviously, I will have to instruct them to choose the target temperature, in other words, the temperature to which they want to convert.
I need help with recommendations on how to organize this in my program. Should I use only 1 class? Also, I think I could configure it like this:
string baseTemp string targetTemp NSLog(@"Please type Fahrenheit, Celsius, Kelvin or Rankine to select your base temperature"); scanf or some other method here("string format specifier here" &baseTemp); NSLog(@"Please type Fahrenheit, Celsius, Kelvin or Rankine to select your target temperature"); scanf or some other method here("string format specifier here" &targetTemp);
Now I need the decision maker to decide if there are Fahrenheit == baseTemp and Celsius == targetTemp, then adjust the specific formula accordingly.
This can become tedious, and I first of all need a special solution or just another simpler method.
Sorry for the probably rather inconvenient code, I'm trying to use what I have learned so far here.
Any suggestions, tricks, ideas and everything in between are welcome.
Thanks!!
UPDATE UPDATE: UPDATE:
The code for the class.h, class.m, and main.m program files contains the final code, at least for this project that dreamlax worked very hard with me to help us achieve our goal. Thanks again dreamlax !!
class.h:
class.m:
#import "class.h" @implementation Temperature; -(void) setFahrenheitValue: (double) f { kelvin = ((f + 459.67)*(5.0/9.0)); } -(void) setCelsiusValue: (double) c { kelvin = (c + 273.15); } -(void) setKelvinValue: (double) k { kelvin = k; } -(void) setRankineValue: (double) r { kelvin = (r*(5.0/9.0)); } -(double) fahrenheitValue { return ((kelvin*(9.0/5.0)) - 459.67); } -(double) celsiusValue { return (kelvin - 273.15); } -(double) kelvinValue { return (kelvin); } -(double) rankineValue { return (kelvin * (9.0/5.0)); } @end
main.m:
#import <Cocoa/Cocoa.h> #import "class.h" int main(int argc, char *argv[]) { int result; int prompt; double sourceTemp; printf("Please choose a source temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n"); result = scanf("%i", &prompt); if (result != 1) printf("I couldn't understand your input, I need only one number!"); else if (result == EOF) printf("I apologize, I encountered an error when trying to read your input."); else if (result == 1) { printf("\nNow, please enter the temperature you would like to convert:\n\n"); scanf("%lf", &sourceTemp); Temperature *converter = [[Temperature alloc] init]; switch (prompt) { case 1: //end-user chooses Fahrenheit [converter setFahrenheitValue:sourceTemp]; break; case 2: //end-user chooses Celsius [converter setCelsiusValue:sourceTemp]; break; case 3: //end-user chooses Kelvin [converter setKelvinValue:sourceTemp]; break; case 4: //end-user chooses Rankine [converter setRankineValue:sourceTemp]; break; } printf("\nNow, please choose a target temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n"); scanf("%i", &prompt); switch (prompt) { case 1: //end-user chooses Fahrenheit printf("%lf degrees Fahrenheit\n", [converter fahrenheitValue]); break; case 2: //end-user chooses Celsius printf("%lf degrees Celsius\n", [converter celsiusValue]); break; case 3: //end-user chooses Kelvin printf("%lf degrees Kelvin\n", [converter kelvinValue]); break; case 4: //end-user chooses Rankine printf("%lf degrees Rankine\n", [converter rankineValue]); break; } } }
Just the idea I'm trying to implement is to get a final prompt that displays the converted temperature in order to display the following:
Suppose that the end user has selected Fahrenheit as his / her starting temperature, wanting to convert 212 degrees into his target Celsius temperature scale. Obviously, conversions should be equal to 100 degrees Celsius, but I think that the program will better display the result as follows:
212 degrees Fahrenheit 100 degrees Celsius.
Now I have made the values ββthat need to be replaced with variables in bold. I have 212 and 100 values ββthat are easily resolved, because 100 was there first, and 212 can be easily eliminated by replacing it with string formatting of the sourceTemp variable.
Now the Fahrenheit line is a little different.
I tried setting something new in our original switch like this:
switch (prompt) { case 1: //end-user chooses Fahrenheit [converter setFahrenheitValue:sourceTemp]; sourceTempText = 1; break; case 2: //end-user chooses Celsius [converter setCelsiusValue:sourceTemp]; sourceTempText = 2; break; case 3: //end-user chooses Kelvin [converter setKelvinValue:sourceTemp]; sourceTempText = 3; break; case 4: //end-user chooses Rankine [converter setRankineValue:sourceTemp]; sourceTempText = 4; break; }
OK, so I added to each other case, setting a new variable named sourceTempText to 1-4, the same value that the end user decided to choose for his initial temperature.
Now here is how I tried to display the final prompt to the end user with the last switch:
switch (prompt2) { case 1: //end-user chooses Fahrenheit printf("%lf degrees sourceTempText is %lf degrees Fahrenheit\n", sourceTemp, [converter fahrenheitValue]); break; case 2: //end-user chooses Celsius printf("%lf degrees sourceTempText is %lf degrees Celsius\n", sourceTemp, [converter celsiusValue]); break; case 3: //end-user chooses Kelvin printf("%lf degrees sourceTempText is %lf degrees Kelvin\n", sourceTemp, [converter kelvinValue]); break; case 4: //end-user chooses Rankine printf("%lf degrees sourceTempText is %lf degrees Rankine\n", sourceTemp, [converter rankineValue]); break; }
Now I'm not sure if I can insert sourceTempText into a string, as I have here, instead, maybe I need to use a string formatter, but I'm not sure. It should be easy to fix, I just wanted to throw it here! :)
Hopefully the next project will be ... GUI GUI GUI! Xd