Sample PawCalc programs. Copyright 2003 - 2005 Poul F. Williams. All rights reserved. The programs are separated by dashed lines. Copy each program to its own memo in the 'PawCalc' memo category on your PDA. Each program starts with a comment describing its use. Many of the programs use the last result (whatever is displayed in the result line in PawCalc) as input. For example, the "nextprime" program computes the next prime number starting at the last result. ---8<------------- Hello /* The simplest program in the world. */ say "Hello World!"; ---8<------------- AddTax /* Add sales tax to an amount. */ store x; store taxrate 5; /* in procent */ x*(1+taxrate/100); ---8<------------- RemoveTax /* Remove sales tax from an amount. */ store x; store taxrate 5; /* in procent */ x/(1+taxrate/100); ---8<------------- Stat /* Show the contents of the statistical registers */ store x; store x "num;sum;sos;ave;std;var;"; say "Number = %x0%\nSum = %x1%\nSOS = %x2%\n" "Average = %x3%\nStd. deviation = %x4%\nVariance = %x5%\n"; x; ---8<------------- Round /* Round last result to a specified number of decimals */ store x; ask "Enter number of decimals"; round ans; round ( x * 10^ans ) / 10^ans; ---8<------------- DMS /* Convert last result in decimal degrees to DMS (degrees, minutes, seconds) */ store x; store x0 abs x; store x1 trunc x0; /* degrees */ store x2 trunc( 60 * (x0-x1) ); /* minutes */ store x3 round( 3600 * (x0-x1) - 60*x2 ); /* seconds */ x3 >= 60; jz done; store x3 x3-60; store x2 x2+1; label done; say "%x0% decimal degrees are\n" "%x1% degrees,\n%x2% minutes, and\n%x3% seconds.\n"; x; ---8<------------- DD /* Convert DMS (degrees, minutes, seconds) to decimal degrees */ ask "Degrees"; store x1; ask "Minutes"; store x2; ask "Seconds"; store x3; abs x3 / 3600 + abs x2 / 60 + abs x1; ---8<------------- Gcd /* Compute the greatest common divisor of two numbers. * For example, the gcd of 42 and 18 is 6. */ ask "First number"; store x1 abs trunc ans; ask "Second number"; store x2 abs trunc ans; x1 < x2; jz loop; store x0 x1; /* Swap x1 and x2 */ store x1 x2; store x2 x0; label loop; store x0 trunc ( x1 / x2 ); store x0 x1 - x0*x2; store x1 x2; store x2 x0; x0 = 0; jz loop; x1; ---8<------------- NextPrime /* Compute the next prime number starting at the last result */ store x0 trunc abs ans; x0 <= 2; jz l0; 2 + ( x0 = 2 ); end; label l0; store x0 x0 + ( x0 < 2 )*(2-x0); store x0 x0 - 1 + ( x0 % 2 ); label l1; store x1 3; store x0 x0+2; label l2; (x0 % x1) & ( x1*x1 < x0 ); jz l3; store x1 x1+2; jmp l2; label l3; x1*x1 > x0; jz l1; x0; ---8<------------- AdndGenerator /* Generate AD&D characters stats */ store x; sub stat; store x1; sub stat; store x2; sub stat; store x3; sub stat; store x4; sub stat; store x5; sub stat; store x6; say "Character:\n" "%x1%\tSTR\n%x2%\tDEX\n%x3%\tCON\n" "%x4%\tINT\n%x5%\tWIS\n%x6%\tCHR\n"; x; end; label stat; sub d6; store x0; sub d6; store x0 x0+ans; sub d6; x0+ans; return; label d6; trunc(rand*6)+1; return; ---8<------------- DayOfWeek /* Calculates on which day of the week a given date falls. * The last result is used as the input date. * The date is written YYYYMMDD, i.e., 20030104 for January 4, 2003. * Works for any date between January 1, 1700 and December 31, 2199. * Dates are assumed to be in Gregorian calendar. */ store x; (x < 17000101 ) | ( x > 21991231 ); jnz error; store xy x \ 10000; store xm (x % 10000) \ 100; store xd x % 100; ( xm < 1 ) | ( xm > 12 ) | ( xd < 1 ) | ( xd > 31 ); jnz error; store x "1;4;4;0;2;5;0;3;6;1;4;6;"; store xr xy % 100; store xr xr + xr \ 4; index x xm-1; store xr xr + ans + xd; ( ( xy % 4 = 0 ) & ( ( xy % 100 <> 0 ) | ( xy % 400 = 0 ) ) ) & ( xm <= 2 ); jz l0; store xr xr-1; label l0; store x "4;2;0;6;4;"; index x (xy \ 100) - 17; store xr ( xr + ans ) % 7; xr = 0; jz l1; say "Saturday"; jmp done; label l1; xr = 1; jz l2; say "Sunday"; jmp done; label l2; xr = 2; jz l3; say "Monday"; jmp done; label l3; xr = 3; jz l4; say "Tuesday"; jmp done; label l4; xr = 4; jz l5; say "Wednesday"; jmp done; label l5; xr = 5; jz l6; say "Thursday"; jmp done; label l6; say "Friday"; label done; x; end; label error; say "Sorry, '%x%' is either not in the correct date " "format (YYYYMMDD) or it is a date outside my range " "(year 1700 through 2199)."; x; end; ---8<------------- DecisionMaker /* Unsure about what to do? This program will help you! */ store x; trunc(rand*6)+1; store xr; xr = 1; jz l1; string "Yes"; jmp done; label l1; xr = 2; jz l2; string "No"; jmp done; label l2; xr = 3; jz l3; string "Maybe"; jmp done; label l3; xr = 4; jz l4; string "Never"; jmp done; label l4; xr = 5; jz l5; string "Forget it"; jmp done; label l5; string "Go for it"; label done; say "Your decision : $string$"; x; ---8<-------------