views:

124

answers:

2

I was wondering if it is feasible for me to connect this simple console program I have in objective-c into a very simple iPhone application.

Since I have no experience using Interface Builder, I'm not sure how long it would take for me to learn it.

Also, I believe my code would have to be adjusted to some iPhone APIs, rather than using the console for input and output.

Here is the program's main.m file, where all of its code is store:

//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit

#import <stdio.h>

@interface Converter: NSObject
{
//Instance variable which stores converting formula for objects
double formula;
}

//Declare instance methods for setting and getting instance variables
-(void) convert: (double) expression;
-(double) formula;

@end


@implementation Converter;

//Define instance method to set the argument (expression) equal to the instance variable
-(void) convert: (double) expression
{
formula = expression;
}

//Define instance method for returning instance variable, formula
-(double) formula
{
return formula;
}

@end

int main (int argc, char *argv[])
{

//Point two new objects, one for the Fahrenheit conversions and the other for the Celsius conversions, to the Converter class
Converter *fahrenheitConversion = [[Converter alloc] init];
Converter *celsiusConversion = [[Converter alloc] init];

//Declare two double variables holding the user-inputted data, and one integer variable for the if else statement   
double fahrenheit, celsius;
int prompt;

NSLog(@"Please press 0 to convert Celsius to Fahrenheit, or 1 to convert Fahrenheit to Celsius\n ");
scanf("%d", &prompt);
if(prompt == 0) {
    NSLog(@"Please enter a temperature in Celsius to be converted into Fahrenheit!:\n");
    scanf("%lf", &celsius);
    if(celsius < -273.15) {
        NSLog(@"It is impossible to convert temperatures less than −273.15 degrees Celsius, because this is absolute zero, the coldest possible temperature.");
    }
    else {
        [fahrenheitConversion convert: (((((celsius)*9)/5)+32))];
        NSLog(@"%lf degrees Celsius is %lf Fahrenheit", celsius, [fahrenheitConversion formula]);
    }
}

else {
    NSLog(@"Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
    scanf("%lf", &fahrenheit);
    if(fahrenheit < -459.67) {
        NSLog(@"It is impossible to convert temperatures less than −459.67 degrees Fahrenheit, because this is absolute zero, the coldest possible temperature.");
    }
    else {
        [celsiusConversion convert: ((((fahrenheit - 32)/9)*5))];
        NSLog(@"%lf degrees Fahrenheit is %lf Celsius", fahrenheit, [celsiusConversion formula]);
    }
}

return 0;
}
+1  A: 

Porting it should be really simple. Apple provides some good sample apps. your fastest road to success is probably to download a simple textfield + button sample and add your calculations. Excluding the time it takes to download and install you'll have it ready in 1-2 hours.

Good luck!

Merrimack
Hey thanks! Hopefully I can do this in that amount of time :)
BOSS
+3  A: 

How about this?

I tried not to change your code too much. It uses your original formulas and converter class, even though I was tempted to change it.

Screenshot:

alt text

Source Code:

ViewController interface:

#import <UIKit/UIKit.h>

@interface TempCalc_iPhoneViewController : UIViewController {

    UITextField *celciusTextField;
    UITextField *fahrenheitTextField;
    UILabel     *statusLabel;

    double minFahrenheit;
    double minCelcius;

    NSString *normalStatus;
    NSString *belowFahrenheitMessage;
    NSString *belowCelciusMessage;
}

@property (nonatomic,retain) IBOutlet UITextField *celciusTextField;
@property (nonatomic,retain) IBOutlet UITextField *fahrenheitTextField;
@property (nonatomic,retain) IBOutlet UILabel *statusLabel;

- (IBAction) convertCelciusToFahrenheit:(UITextField*)sender;
- (IBAction) convertFahrenheitToCelcius:(UITextField*)sender;

@end

ViewController implementation

#import "TempCalc_iPhoneViewController.h"
#import "Converter.h"

@implementation TempCalc_iPhoneViewController

@synthesize celciusTextField, fahrenheitTextField, statusLabel;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    minCelcius = -273.15;
    minFahrenheit = -459.67;

    normalStatus = @"Please type to convert temperature";
    belowFahrenheitMessage = @"impossible to convert temperatures less than −459.67 degrees Fahrenheit";
    belowCelciusMessage = @"impossible to convert temperatures less than −273.15 degrees Celsius";
}

// Delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    NSLog(@"User pressed the DONE button on the keyboard (any keyboard!)");
    [theTextField resignFirstResponder];
    return YES;
}

- (IBAction) convertCelciusToFahrenheit:(UITextField*)sender
{
    Converter *celciusConversion = [[Converter alloc] init];
    double celcius = [sender.text doubleValue];

    if(celcius < minCelcius) {
        NSLog(@"It is impossible to convert temperatures less than −273.15 degrees Celsius, because this is absolute zero, the coldest possible temperature.");
        self.statusLabel.text = belowCelciusMessage;
    }
    else
    {
        [celciusConversion convert: ((((celcius)*9)/5)+32)];
        NSString *fahrenheitValue = [NSString stringWithFormat:@"%lf", [celciusConversion formula]];
        NSLog(@"%lf degrees Celsius is %@ Fahrenheit", celcius, fahrenheitValue);
        self.fahrenheitTextField.text = fahrenheitValue;
        self.statusLabel.text = normalStatus;
    }

    [celciusConversion release];
}

- (IBAction) convertFahrenheitToCelcius:(UITextField*)sender
{
    Converter *fahrenheitConversion = [[Converter alloc] init];
    double fahrenheit = [sender.text doubleValue];

    if(fahrenheit < minFahrenheit) {
        NSLog(@"It is impossible to convert temperatures less than −459.67 degrees Fahrenheit, because this is absolute zero, the coldest possible temperature.");
        self.statusLabel.text = belowFahrenheitMessage;
    }
    else {
        [fahrenheitConversion convert: (((fahrenheit - 32)/9)*5)];
        NSString *celciusValue = [NSString stringWithFormat:@"%lf", [fahrenheitConversion formula]];
        NSLog(@"%lf degrees Fahrenheit is %@ Celsius", fahrenheit, celciusValue);
        self.celciusTextField.text = celciusValue;
        self.statusLabel.text = normalStatus;   }

    [fahrenheitConversion release];
}
Brock Woolf
WOW! Thank you so so much!! So it looks like the only files you had to mess with were the ViewController interface and implementation files? There was actually quite a lot of code in there you had to write :) Thank you so much again, I'm really learning a lot from this!
BOSS
Sure no problem. Happy iPhone coding :)
Brock Woolf
Yes. The only files I added code to were the ViewController.h and .m files. IBOutlets and IBActions were connected in Interface Builder
Brock Woolf
Awesome, thank you so much, yet again! :)
BOSS
Just wondering, sort of off topic here, but what changes would you have made to the program to either optimize it, or completely alter its structure? Mine is quite basic as you can tell because I'm trying to apply what I have learned from this book that I'm reading. Any suggestions I'm totally open and I appreciate them!
BOSS
@BOSS - I'd say Brock has done a good job of bringing this across to the iPhone, architecture-wise. He's followed the Model-View-Controller design pattern by placing this logic in a controller class and wiring up the appropriate views. The calculation you're doing is fairly simple, so I don't see a lot that would need to be optimized. You could possibly reshape the Converter class to be an NSValueTransformer, but that's not necessary.
Brad Larson