[Problem] Homebrew Calculator Problem

Discussion in 'iOS Development' started by cocotutch, Jan 29, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Hiya,

    I made a simple 4 function calculator in iPhone SDK (official) and I have a small problem. Whenever I enter too many digits, it still lets me, but if I set an operator to it (+, -, x, /) the total equals something like "3.14545477+34e" whereas I want it to be "475872542725842578" as in, what the number would be if I added it or multiplied it without that cryptic error.

    I have my code:

    Code:
    // Calculator.m - the simple 4 function calc for OS X iPhone.
    
    #import "Calculator.h"
    
    const NSString *operators = @"+-x/";
    const NSString *Equals = @"=";
    const NSString *Digits = @"0123456789.";
    const NSString *Delete = @"Del";
    const NSString *Clear = @"Clear";
    
    @implementation iCalculator
    -(NSString *) operator
    {
    	
    	return _operator;
    }
    
    -(void) setOperator: (NSString *) operator
    {
    	if(_operator!= operator)
    	{
    		[_operator release];
    		_operator= [operator retain];
    	}
    }
    -init
    {
    	if(self= [super init])
    	{
    		_display=[[NSMutableString stringWithCapacity:20] retain];
    		_operator=nil;
    	}
    	return self;
    }
    
    - (void) dealloc
    {
    	[_display release];
    	[_operator release];
    	[super dealloc];
    }
    -(void) input:(NSString *) character
    {
    	static BOOL last_character_is_operator =NO;
    	if( [Digits rangeOfString: character].length ) // if input character is a digit
    	{
    		if(last_character_is_operator)
    		{
    			[_display setString: character];// set the display to the current character digit
    			last_character_is_operator=NO;
    		}
    		else if(![character isEqualToString:@"."]
    				||[_display rangeOfString:@"."].location ==NSNotFound)// if input character is not a decimal point (all digits) and last input was an operator
    		{
    			[_display appendString:character];
    		}
    	}
    	else if([operators rangeOfString: character].length||
    			[character isEqualToString: (NSString*)Equals] )// if input character is an operator or equals
    	{
    		if(!_operator && ![character isEqualToString:(NSString*)Equals])// if operator is null, and input is not equals
    		{
    			_operand = [[self displayValue] doubleValue];
    			[self setOperator: character];
    		}
    		else
    		{
    			if(_operator)
    			{
    				double operand2=[[self displayValue] doubleValue];
    				switch ([operators rangeOfString: _operator].location) 
    				{
    					case 0:
    						_operand += operand2;
    						break;
    					case 1:
    						_operand -= operand2;
    						break;
    					case 2:
    						_operand *= operand2;
    						break;
    					case 3:
    						_operand /= operand2;
    						break;
    					default:
    						break;
    				}
    				[_display setString:[[NSNumber numberWithDouble:_operand] stringValue] ];
    			}
    			[self setOperator:([character isEqualToString:(NSString*)Equals])? nil:character];
    		}
    		last_character_is_operator=YES;
    	}
    	else if([character isEqualToString:(NSString*) Delete])
    	{
    		NSInteger index_of_char_to_remove=[_display length]-1;
    		if(index_of_char_to_remove>=0)
    		{
    			[_display deleteCharactersInRange:NSMakeRange(index_of_char_to_remove, 1)];
    			last_character_is_operator=NO;
    		}
    	}
    	else if([character isEqualToString:(NSString*)Clear])
    	{
    		if([_display length])
    		{
    			[_display setString:[NSString string]];// return an empty string
    		}
    		else
    		{
    		setOperator:nil;
    		}
    	}
    }
    
    -(NSString *) displayValue
    {
    	if([_display length])
    	{
    		return [[_display copy] autorelease] ;
    	}
    	return @"0";
    }
    @end
    
    

    It works, I have the UI and everything else works, I also want it to be scientific, but that I don't know how to do.

    Any help?

    cocotutch
  2. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Edit:

    Incase nobody understands what is wrong..

    Picture 1.png

    cocotutch​
  3. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    So you want scientific functions, or you want your answer to appear with the "e+16" type thing?
  4. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I want the answer to be without the "e+16" bit, just a whole number. It's a multi-lined calculator, since I made it so, but it still did it when it was a single liner.

    I also want to know how to include "tan, cos, sin and pi" sort functions to buttons.

    Is it possible?
  5. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
  6. ZunePod

    ZunePod Banned

    Joined:
    Aug 28, 2008
    Messages:
    1,340
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Well for the Pi function, you just need to find Pi to about 100 decimal places, after that the difference is too minute to change anything
  7. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I don't really want Pi right now. What I really want to happen is the 'eradication' of the solution when I enter "999999999999999999999+99999999999999999999999" and it gives me an algebraic "2e+20". I want it to actually list what the number would be instead of it giving me an answer like that. I.E. Not 1+1 = 0.2224+e7, I want it to be 1+1=2.

    cocotutch
  8. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Bump.

    Anyone? All I need is for that error to be hidden.

    Better Words:

    When I Add these two numbers together: 7441 + 5587, it should equal 13028 on the iPhone. But, I get an algebraic answer such as "2e+12". I do not want the algebraic solution. I want it to have 13028, like it should.

    But, I can see that nobody knows, or is not willing to help this time. I have now deleted the project and zapped the iPhone Simulator of all my other Apps.

    cocotutch
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Way to give up. That's the spirit!

    The basic problem here is a disunderstandment of math. "2e+12" is not an algebraic answer. It most certainly does not mean "divide each side by 2; e equals 6."

    The fundamental problem here is misuse of variable types, failure to set precision, failure to tell the compiler to optimize the floating point numbers, and ignoring the basic math.h header
    #include <math.h>

    No one answered you because they either: a, didn't know; or b, assumed you could find the answer very easily by either searching google or reading already working source code.

    It's pathetic if you're going to give up this easily. Actually man up and figure things out. Don't he afraid to get dirty while looking for solutions to your problems.
  10. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I haven't really given up. Its just I have included "math.h" and it still gives me that garble. Anyway, I sorta don't need this app anymore because I got my TI nSpire CAS calculator yesterday and I use that for Method Math, no need for my "homebrew calculator" anymore.

    Please Register or Log in to view images



    Thanks anyway,

    Please Register or Log in to view images

    Now I'm off to be a 3 year old and watch The Land Before Time (1988)

    Please Register or Log in to view images

Share This Page