| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 
 | /* CalcBrain.m: Brain of Calculator.app
   Copyright (C) 1999 Free Software Foundation, Inc.
   Author:  Nicola Pero <n.pero@mi.flashnet.it>
   Date: 1999
   
   This file is part of GNUstep.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#include "CalcBrain.h"
#include "CalcFace.h"
#include <math.h>
@implementation CalcBrain: NSObject
-(id) init
{
  [super init];
  result = 0;
  enteredNumber = 0;
  operation = none;
  fractionalDigits = 0;
  decimalSeparator = NO;
  editing = YES;
  face = nil;
  return self;
}
-(void) dealloc
{
  if (face) 
    [face release];
  [super dealloc];
}
// Set the corresponding face
-(void) setFace: (CalcFace *)aFace
{
  face = [aFace retain];
  [face setDisplayedNumber: enteredNumber withSeparator: decimalSeparator
	fractionalDigits: fractionalDigits];
}
// The various buttons 
-(void) clear: (id)sender
{
  result = 0;
  enteredNumber = 0;
  operation = none;
  fractionalDigits = 0;
  decimalSeparator = NO;
  editing = YES;
  [face setDisplayedNumber: 0 withSeparator: NO fractionalDigits: 0];
}
-(void) equal: (id)sender
{
  switch (operation)
    {
    case none: 
      result = enteredNumber;
      enteredNumber = 0;
      decimalSeparator = NO;
      fractionalDigits = 0;
      return;
      break;
    case addition:
      result = result + enteredNumber;
      break;
    case subtraction:
      result = result - enteredNumber;
      break;
    case multiplication:
      result = result * enteredNumber;
      break;
    case division:
      if (enteredNumber == 0)
	{
	  [self error];
	  return;
	}
      else 
	result = result / enteredNumber;
      break;
    }
  [face setDisplayedNumber: result 
	withSeparator: (ceil(result) != result)      
	fractionalDigits: 7];
  enteredNumber = result;
  operation = none;
  editing = NO;
}
-(void) digit: (id)sender
{
  if (!editing)
    {
      enteredNumber = 0;
      decimalSeparator = NO;
      fractionalDigits = 0;
      editing = YES;
    }
  if (decimalSeparator)
    {
      enteredNumber = enteredNumber 
	+ ([sender tag] * pow (0.1, 1+fractionalDigits));
      fractionalDigits++;
    }
  else
    {
      enteredNumber = enteredNumber * 10 + ([sender tag]);
      // Check overflow
      if (enteredNumber > pow (10, 15))
	{
	  [self error];
	  return;
	}
    }
  [face setDisplayedNumber: enteredNumber withSeparator: decimalSeparator
	fractionalDigits: fractionalDigits];
}
-(void) decimalSeparator: (id)sender
{
  if (!editing)
    {
      enteredNumber = 0;
      decimalSeparator = NO;
      fractionalDigits = 0;
      editing = YES;
    }
  if (!decimalSeparator)
    {
      decimalSeparator = YES;
      [face setDisplayedNumber: enteredNumber withSeparator: YES
	    fractionalDigits: 0];
    }
}
-(void) operation: (id)sender
{
  if (operation == none)
    {
      result = enteredNumber;
      enteredNumber = 0;
      decimalSeparator = NO;
      fractionalDigits = 0;
      operation = [sender tag];
    }
  else // operation
    {	 
      [self equal: nil];
      [self operation: sender];
    }
}
-(void) squareRoot: (id)sender
{
  if (operation == none)
    {
      result = sqrt (enteredNumber);
      [face setDisplayedNumber: result 
	    withSeparator: (ceil(result) != result)
	    fractionalDigits: 7];
      enteredNumber = result;
      editing = NO;
      operation = none;  
    }
  else // operation
    {	 
      [self equal: nil];
      [self squareRoot: sender];
    }
} 
-(void) error
{
  result = 0;
  enteredNumber = 0;
  operation = none;
  fractionalDigits = 0;
  decimalSeparator = NO;
  editing = YES;
  [face setError];
}
@end
 |