fcalc.c

来自「CD_《Palm OS编程实践》」· C语言 代码 · 共 392 行

C
392
字号
//////////////////////////////////////////////////////////////////////////////
// fcalc.c
// Code for the "calc" form.
// Copyright (c) 1999, Robert Mykland.  All rights reserved.
//////////////////////////////////////////////////////////////////////////////

//////////////
// Includes //
//////////////

#include "app.h"	// The definitions for this application
#include "calc.h"	// Calculator guts

///////////////////////
// Global Prototypes //
///////////////////////

void	calcInit( void );
void	calcStop( void );
Boolean	calcFormEventHandler( EventPtr spEvent );
void	calcDisplay( char* cpNumber );
void	calcSignalError( void );

/////////////////////
// Local Variables //
/////////////////////

static char		cComma;		// The comma symbol we're using
static char		cPoint;		// The decimal point we're using
static Handle	hNumber;	// Handle to the number in our edit field

//////////////////////
// Global Functions //
//////////////////////

//----------------------------------------------------------------------------
void calcInit(
//----------------------------------------------------------------------------
// Initializes this module.
//----------------------------------------------------------------------------
void )
//----------------------------------------------------------------------------
{
	DWord	dChoice;
	char*	cpNumber;
	
	// Find MathLib
	if( (SysLibFind( MathLibName, &MathLibRef ) == 0) ||
			SysLibLoad( LibType, MathLibCreator, &MathLibRef ) ||
			MathLibOpen( MathLibRef, MathLibVersion ) )
	{
		FrmAlert( MathLibErrorAlert );
		sendStopEvent();
	}
     	
	// Allocate our field chunk
	hNumber = MemHandleNew( MAX_NUMBER_SIZE );
	
	// Lock the memory, get the pointer
	cpNumber = MemHandleLock( hNumber );
	
	// Initialize it
	StrCopy( cpNumber, "0" );
	
	// Unlock the field's memory
	MemHandleUnlock( hNumber );
	
	// Get the number format
	dChoice = PrefGetPreference( prefNumberFormat );
	
	// Handle the number format
	switch( dChoice )
	{
		default:
			cComma = ',';
			cPoint = '.';
		break;
		
		case nfPeriodComma:
			cComma = '.';
			cPoint = ',';
		break;
		
		case nfSpaceComma:
			cComma = ' ';
			cPoint = ',';
		break;
		
		case nfApostrophePeriod:
			cComma = '\'';
			cPoint = '.';
		break;
		
		case nfApostropheComma:
			cComma = '\'';
			cPoint = ',';
		break;
	}
	
	// We're done
	return;
}		

//----------------------------------------------------------------------------
void calcStop(
//----------------------------------------------------------------------------
// Cleans up stuff from this module.
//----------------------------------------------------------------------------
void )
//----------------------------------------------------------------------------
{
	UInt uiUseCount;
	
	// Close MathLib
	MathLibClose( MathLibRef, &uiUseCount );
	if( uiUseCount == 0 )
		SysLibRemove( MathLibRef );
		
	// Free the number field chunk
	MemHandleFree( hNumber );
	
	// We're done
	return;
}

//----------------------------------------------------------------------------
Boolean calcFormEventHandler(
//----------------------------------------------------------------------------
// Handles events for this form.
// Returns true if it fully handled the event.
//----------------------------------------------------------------------------
EventPtr spEvent )
//----------------------------------------------------------------------------
{
	// Handle the event
	switch( spEvent->eType )
	{
		// The form is opened
		case frmOpenEvent:
		{
			FormPtr		spForm;
			Word		wIndex;
			FieldPtr	spField;
			ControlPtr	spButton;
			char		caPoint[2];
					
			// Get the field
			spForm = FrmGetActiveForm();
			wIndex = FrmGetObjectIndex( spForm, calcNumberField );
			spField = FrmGetObjectPtr( spForm, wIndex );
			
			// Draw the field
			FldSetTextHandle( spField, hNumber );
			FldDrawField( spField );

			// Get the decimal point button			
			wIndex = FrmGetObjectIndex( spForm, calcPointButton );
			spButton = FrmGetObjectPtr( spForm, wIndex );

			// Draw the decimal point button
			caPoint[0] = cPoint;
			caPoint[1] = '\0';
			CtlSetLabel( spButton, caPoint );
		}
		break;
		
		// A control was selected
		case ctlSelectEvent:
		{
			// Handle the different buttons
			switch( spEvent->data.ctlSelect.controlID )
			{
				case calcAddButton:
					calcAdd();
				break;
				
				case calcChangesignButton:
					calcChangeSign();
				break;
				
				case calcClearButton:
					calcClear();
				break;
				
				case calcDivideButton:
					calcDivide();
				break;
				
				case calcDoneButton:
				{
					char*	cpNumber;
					
					// Lock the memory, get the pointer
					cpNumber = MemHandleLock( hNumber );
	
					// Copy to the clipboard
					ClipboardAddItem( clipboardText, cpNumber,
							StrLen( cpNumber ) );
					
					// Unlock the field's memory
					MemHandleUnlock( hNumber );
	
					// Exit the application
					sendStopEvent();
				}
				break;
				
				case calcEightButton:
					calcAppend( 8 );
				break;
				
				case calcEqualsButton:
					calcEquals();
				break;
				
				case calcExponentButton:
					calcExponent();
				break;
				
				case calcFieldborderButton:
					// Ignore this button
					return( true );
					
				case calcFiveButton:
					calcAppend( 5 );
				break;
				
				case calcFourButton:
					calcAppend( 4 );
				break;
				
				case calcMultiplyButton:
					calcMultiply();
				break;
				
				case calcNineButton:
					calcAppend( 9 );
				break;
				
				case calcOneButton:
					calcAppend( 1 );
				break;
				
				case calcPointButton:
					calcPoint();
				break;
				
				case calcSevenButton:
					calcAppend( 7 );
				break;
				
				case calcSixButton:
					calcAppend( 6 );
				break;
				
				case calcSubtractButton:
					calcSubtract();
				break;

				case calcThreeButton:
					calcAppend( 3 );
				break;
				
				case calcTwoButton:
					calcAppend( 2 );
				break;
				
				case calcZeroButton:
					calcAppend( 0 );
				break;
			}
		}
		break;
		
		// A menu item was selected
		case menuEvent:
		
			// Handle the menu event
			calcFormMenuEventHandler( spEvent );
			return( true );
	}
	
	// We're done
	return( false );
}

//----------------------------------------------------------------------------
void calcDisplay(
//----------------------------------------------------------------------------
// Displays the number in the field.
// IMPORTANT: A leading + or - sign is assumed for all numbers passed to
// this function!
//----------------------------------------------------------------------------
char* cpNumber )
//----------------------------------------------------------------------------
{
	char*		cpSrc;
	int			iIntSize;
	char*		cpDest;
	char		caNumber[40];
	char*		cpText;
	FormPtr		spForm;
	Word		wIndex;
	FieldPtr	spField;

	// Find the end of the number and determine exponent and decimal
	iIntSize = -1;	// To account for leading sign
	for( cpSrc = cpNumber; *cpSrc != '\0'; cpSrc++ )
	{
		if( (*cpSrc == '.') || (*cpSrc == 'e') )
			break;
		iIntSize++;
	}

	// Start source and destination
	cpSrc = cpNumber;
	cpDest = caNumber;
	
	// Handle the leading sign
	if( *cpSrc++ == '-' )
		*cpDest++ = '-';
	
	// Copy the integer part
	while( iIntSize )
	{
		do
		{
			*cpDest++ = *cpSrc++;
			iIntSize--;
		} while( (iIntSize % 3) != 0 );
		if( iIntSize == 0 )
			break;
		*cpDest++ = cComma;
	}
	
	// Do the decimal point
	if( *cpSrc == '.' )
	{
		cpSrc++;
		*cpDest++ = cPoint;
	}
	
	// Copy the decimal part
	while( *cpSrc && (*cpSrc != 'e') )
		*cpDest++ = *cpSrc++;		
		
	// Build up the exponent if any
	if( *cpSrc == 'e' )
	{
		*cpDest++ = ' ';
		while( *cpSrc )
			*cpDest++ = *cpSrc++;
	}

	// Zero delimit the string
	*cpDest = '\0';
	
	// Change the string in the handle
	// Lock the memory, get the pointer
	cpText = MemHandleLock( hNumber );
	
	// Initialize it
	StrCopy( cpText, caNumber );
	
	// Unlock the field's memory
	MemHandleUnlock( hNumber );
	
	// Get the field pointer
	spForm = FrmGetActiveForm();
	wIndex = FrmGetObjectIndex( spForm, calcNumberField );
	spField = FrmGetObjectPtr( spForm, wIndex );
	
	// Refresh the field
	FldSetTextHandle( spField, hNumber );
	FldDrawField( spField );
	
	// We're done
	return;
}

//----------------------------------------------------------------------------
void calcSignalError(
//----------------------------------------------------------------------------
// Signals an error.
//----------------------------------------------------------------------------
void )
//----------------------------------------------------------------------------
{
	SndPlaySystemSound( sndError );
	return;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?