Looking to create a 4-pin objective-c temperature converter console program

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:

 #import <Cocoa/Cocoa.h> @interface Temperature : NSObject { double kelvin; } //Instance methods to convert argument to kevin -(void) setFahrenheitValue: (double) f; -(void) setCelsiusValue: (double) c; -(void) setKelvinValue: (double) k; -(void) setRankineValue: (double) r; //Instance methods for extracting the kelvin value using any scale -(double) fahrenheitValue; -(double) celsiusValue; -(double) kelvinValue; -(double) rankineValue; @end 

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

+4
source share
1 answer

As for your class, one of the approaches that I briefly touched on in the previous question could be as follows:

  • It must store the value using only one temperature scale. Keep in mind that Rankin refers to Fahrenheit, that Kelvin refers to Celsius. I mean that Rankin is just Fahrenheit, but with a different offset, and Kelvin is just Celsius, but with a different offset (Rankin and Kelvin think that 0 Β° is absolute zero ). From a scientific point of view, Kelvin would be a good choice for a base scale, as it is an international standard for measuring temperature (the United States, Burma and Liberia are the only countries that have not adopted the SI system). There are several pros and cons to using only one temperature scale:

    • There will be a loss of accuracy because you convert twice (once to convert the original scale to kelvin, and then another conversion to convert to the scale you want)

    • However, implementing your class will be simpler because there is only one variable.

  • You only need one instance of your converter class. When you read input from the command line, you indicate to the instance of the converter class the dimension as well as the scale that the user has selected. For example, your Converter interface may use the following methods:

     @interface Temperature : NSObject { double kelvin; } - (void) setCelsiusValue:(double) c; // kelvin = (c + 273.15) - (void) setKelvinValue:(double) k; // kelvin = k - (void) setFahrenheitValue:(double) f; // kelvin = (f + 459.67) Γ— 5⁄9 - (void) setRankineValue:(double) r; // kelvin = (f Γ— 5/9) @end 

    All of these methods should convert the argument to the Kelvin scale.

  • Your class should also implement ways to extract values ​​using any scale. For this purpose, he could use the following methods:

     @interface Temperature : NSObject { double kelvin; } // include methods from above - (double) celsiusValue; // return kelvin - 273.15 - (double) kelvinValue; // return kelvin - (double) fahrenheitValue; // return kelvin Γ— 9⁄5 βˆ’ 459.67 - (double) rankineValue; // return kelvin Γ— 9/5 @end 

    Each of these methods must convert the Kelvin value to an appropriate scale using the appropriate formula.

Regarding user interaction, usually NSLog is only used to record information about your application at runtime (for example, to check what the value of an object is or to determine the execution point in your application). For questions, more printf usually used.

You can print this menu:

 Please choose a source temperature scale: [1] Celsius [2] Kelvin [3] Fahrenheit [4] Rankine 

Then print another menu for the target temperature scale, and then ask the user to measure the source temperature. As a rule, you should not ask the user for a complete word, because it is easier to make a mistake than a prime number. There is also a special programming construct called switch . It has some caveats, but works great with multiple-choice questions and answers. After you read the user input using scanf , you can do this:

 int prompt; double sourceTemp; // use scanf to read values into prompt and sourceTemp variables Temperature *converter = [[Temperature alloc] init]; switch(prompt) { case 1: // the user chose celsius [converter setCelsiusValue:sourceTemp]; break; // if you don't break, it will execute all other cases case 2: // user chose kelvin [converter setKelvinValue:sourceTemp]; break; case 3: [converter setFahrenheitValue:sourceTemp]; break; case 4: [converter setRankineValue:sourceTemp]; break; } // prompt the user again here to determine the target temperature scale // use the same menu again and read the user input into the prompt variable case (prompt) { case 1: // user wants to convert to celsius printf("%f degrees C\n", [converter celsiusValue]); break; case 2: // user wants kelvin printf("%f degrees K\n", [converter kelvinValue]); break; case 3: printf("%f degrees F\n", [converter fahrenheitValue]); break; case 4: printf("%f degrees R\n", [converter rankineValue]); break; } 

As for scanf , it does two things. Firstly, it scans the input data into the provided variables, and secondly, it returns the number of successfully checked elements. For instance:

 int result; int first; int second; printf("Please type two numbers: "); result = scanf("%d %d", &first, &second); 

If scanf successfully read two numbers, then result should be 2. If it is 2, you can be sure that first and second contain good data, you can be sure that scanf successfully scanned the input. Similarly, if you only ask scanf to read a single number, then if result is 1, then it will successfully read that number.

If result is 1, it means that he only scanned a single number and then ran into a problem. For example, if the user typed "100 abc", scanf will be able to read only one number, but we asked for two. In this case, it will return 1 to tell us that it scanned only one unit.

If result is 0, then this means that scanf not able to scan anything that matches what was requested (if the user entered "abc abc", for example).

Finally, result will be EOF (end of file) if there was no data to read.

So, it is important to check the return value of scanf calls to make sure that the user has entered something valid. If the result is 0, you know that the user entered something invalid, and if the result is EOF , then there was a problem reading any data at all. So, we could set this setting:

 int result; int first; int second; printf("Please type two numbers: "); result = scanf("%d %d", &first, &second); if (result == 0) printf("I couldn't understand what you gave me, I need numbers!\n"); else if (result == 1) printf("I was only able to read one number!\n"); else if (result == EOF) printf("Whoops, there was some problem trying to read your input\n"); else if (result == 2) // scanf was able to read two numbers printf("Great! The sum of those two numbers is: %d\n", first + second); 
+4
source

All Articles