discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

my calculator has problems


From: umen
Subject: my calculator has problems
Date: Fri, 08 Apr 2016 15:01:57 -0400

Hi people,

I'm trying to write a basic calculator using GNUstep+ObjC and I get stucked. I really don't know where is the problem in my code. May you help me?

I'm sending here what I have implemented until now:

@interface UCalc : NSObject
{
  char operation; //just a char to validate the operation
  id number1, number2; //number1 = the current button pressed, number2 = the old number
  id result; //the calculator display
}
- (void) Add: (id)sender;
@end


@implementation UCalc
- (void) Add: (id)sender
{
  operation = '+';
  number2 = number1; //when you press the add button the current number becomes the old number
  number1 = @"0";
}

- (void) Button0: (id)sender
{
  number1 = @"0";
  [result setStringValue: number1];
}

- (void) Button1: (id)sender
{
  //if there is no number yet
  if (number1 == nil || [number1 isEqual: @"0"]){
    [result setStringValue: @""];
    number1 = @"1";
  }
  //else append the current number in itself, so we can get number > 9
  else{
    number1 = [number1 stringByAppendingFormat: @"1"];
  }
  [result setStringValue: number1];
}


- (void) Button2: (id)sender
{
  if (number1 == nil || [number1 isEqual: @"0"]){
    [result setStringValue: @""];
    number1 = @"2";
  }
  else{
    number1 = [number1 stringByAppendingFormat: @"2"];
  }
  [result setStringValue: number1];
}

All the others number buttons are implemented in the same way...

The problem is in Equal button:

- (void) Equal: (id)sender{
  switch(operation){
    case '+':
    //trying  to do this: number1 = number2 + number1 and then print in the display result
    [number1 setDoubleValue: [number2 doubleValue] + [number1 doubleValue]];
      [result setStringValue: number1];
      break;
   }
}

I already have created the window in Gorm, so everything is implemented there. The problem in this code is in the number1 = number2 + number1 in Equal method, I don't know why it's not assigning to number1 the operation correctly and then result working correctly. Any idea about what I'm doing wrong? Everything?

Sorry my english. Thanks.

Attachment: UCalc.h
Description: Text Data

Attachment: UCalc.m
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]