By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Open Source C# Programmable Calculator

P: n/a
Folks:

I've posted the full source code to C# Programmable Calculator at my
website:

http://www.personalmicrocosms.com/html/cspcalc.html

C# Programmable Calculator is a Reverse Polish Notation (RPN) calculator
with custom buttons programmable in C#.

Enjoy!

Eric Bergman-Terrell
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Eric Terrell wrote:
Folks:

I've posted the full source code to C# Programmable Calculator at my
website:

http://www.personalmicrocosms.com/html/cspcalc.html

C# Programmable Calculator is a Reverse Polish Notation (RPN) calculator
with custom buttons programmable in C#.


You might be interested in boo ( http://boo.codehaus.org/ ), which would
let people use the calculator who do not have the .NET SDK installed.
You would just need to include the boo dlls with your application. You
can use the boo compiler, plus the scripts are easier to write than C#.

//see booc for an example of using boo compiler from C#
import Boo.Lang.Compiler
import Boo.Lang.Compiler.IO
import Boo.Lang.Compiler.Pipelines
compiler = BooCompiler()
compiler.Parameters.Input.Add(StringInput("<script >",
sourcetext))
compiler.Parameters.Pipeline = CompileToMemory()
result = compiler.Run()
if len(result.Errors):
print "errors compiling"
assembly = result.GeneratedAssembly
// result.GeneratedAssemblyEntryPoint.Invoke(null, (null,))
I converted Funcs.cs to boo using the C# to boo converter for SharpDevelop.
namespace NS

import System
import System.Collections
import System.IO
import CSPCUtil
import ICSPCAPI
import System.Drawing
import System.Text
import System.Windows.Forms
import System.Diagnostics

class Functions(IAPIUser):
private static _globals as (Object) = array(Object, 1024)

private static _iapi as IAPI

def constructor():
pass

def SaveAPI(iapi as IAPI):
Functions.iapi = iapi

[Button('Main')]
static def FloatingPoint():
_iapi.DisplayFormat = ''

[Button('Main')]
static def FixedPoint(digits as int):
if digits >= 0 and digits < 100:
_iapi.DisplayFormat = 'N' + digits
[Button('Math', '|x|')]
static def AbsoluteValue(x as double) as double:
return Math.Abs(x)

private static def CalcFib(x as int, memo as (long)) as long:
result as long
if x == 1 or x == 2:
result = 1
else:
if memo[x - 2] == 0:
memo[x - 2] = CalcFib(x - 2, memo)

if memo[x - 1] == 0:
memo[x - 1] = CalcFib(x - 1, memo)

result = memo[x - 2] + memo[x - 1]

return result

[Button('Math')]
static def Fib(x as int) as long:
return CalcFib(x, array(long, x))

[Button('Trig')]
static def Sin(x as double) as double:
return Math.Sin(x)

[Button('Trig')]
static def Cos(x as double) as double:
return Math.Cos(x)

[Button('Trig')]
static def Tan(x as double) as double:
return Math.Tan(x)

[Button('Trig')]
static def ASin(x as double) as double:
return Math.Asin(x)

[Button('Trig')]
static def ACos(x as double) as double:
return Math.Acos(x)

[Button('Trig')]
static def ATan(x as double) as double:
return Math.Atan(x)

[Button('Trig')]
static def Cosh(x as double) as double:
return Math.Cosh(x)

[Button('Trig')]
static def Sinh(x as double) as double:
return Math.Sinh(x)

[Button('Trig')]
static def Tanh(x as double) as double:
return Math.Tanh(x)

[Button('Trig')]
static def ACosh(x as double) as double:
return Math.Log(x + Math.Sqrt(x + 1) * Math.Sqrt(x - 1))

[Button('Trig')]
static def ASinh(x as double) as double:
return Math.Log(x + Math.Sqrt(1 + x * x))

[Button('Trig')]
static def ATanh(x as double) as double:
return (Math.Log(1 + x) - Math.Log(1 - x)) / 2

[Button('Math', 'n!')]
static def Factorial(n as int) as long:
result as long = 1
checked:
if n > 1:
result = n * Factorial(n - 1)

return result

[Button('Main', '%')]
static def Percent(x as double) as double:
return x / 100

[Button('Main')]
static def SciNot(n as double, PowerOf10 as double) as double:
return n * Math.Pow(10, PowerOf10)

[Button('Math', 'Pi')]
static def Pi() as double:
return Math.PI

[Button('Memory')]
static def Store(o as Object, index as int):
_globals[index] = o

[Button('Memory')]
def Recall(index as int) as Object:
return _globals[index]

[Button('Angles')]
static def Radians(degrees as double) as double:
return degrees * Math.PI / 180

[Button('Angles')]
static def Degrees(radians as double) as double:
return radians * 180 / Math.PI

[Button('Angles')]
static def DMS2Decimal(Degrees as double, Minutes as double, Seconds as
double) as double:
return Degrees + Minutes / 60 + Seconds / 3600

[Button('Angles')]
static def AngleBetweenLocations(Lat1Degrees as double, Lon1Degrees as
double, Lat2Degrees as double, Lon2Degrees as double) as double:
Lat1Radians as double = Radians(Lat1Degrees)
Lon1Radians as double = Radians(Lon1Degrees)
Lat2Radians as double = Radians(Lat2Degrees)
Lon2Radians as double = Radians(Lon2Degrees)
a as double = Lon1Radians - Lon2Radians
if a < 0:
a = -a

if a > Math.PI:
a = 2 * Math.PI - a

return Math.Acos(Math.Sin(Lat2Radians) * Math.Sin(Lat1Radians) +
Math.Cos(Lat2Radians) * Math.Cos(Lat1Radians) * Math.Cos(a))

[Button('Angles')]
static def DistanceBetweenLocations(Lat1Degrees as double, Lon1Degrees
as double, Lat2Degrees as double, Lon2Degrees as double) as double:
Angle as double = AngleBetweenLocations(Lat1Degrees, Lon1Degrees,
Lat2Degrees, Lon2Degrees)
Circumference as double = 24830
return Circumference * Angle / (2 * Math.PI)

[Button('Math')]
static def LN(x as double) as double:
return Math.Log(x)

[Button('Math', 'e^X')]
static def eToX(x as double) as double:
return Math.Pow(Math.E, x)

[Button('Math')]
static def e() as double:
return Math.E

[Button('Math')]
static def Log(x as double) as double:
return Math.Log(x) / Math.Log(10)

[Button('Math', '10^X')]
static def TenToX(x as double) as double:
return Math.Pow(10, x)

[Button('Math')]
static def FindFraction(Fraction as double, NumberOfTries as int) as
String:
Prefix as String = ''
if Fraction < 0:
Prefix = '-'

Fraction = Math.Abs(Fraction)
Numerator as int = 0
Denominator as int = 1
BestNumerator as int = 0
BestDenominator as int = 0
Error as double = 0
i as int = 0
while i < NumberOfTries:
TmpFraction as double = cast(double, Numerator) / cast(double,
Denominator)
TmpError as double = Math.Abs(TmpFraction - Fraction)
if i == 0 or TmpError < Error:
Error = TmpError
BestNumerator = Numerator
BestDenominator = Denominator

if TmpFraction < Fraction:
++Numerator
else:
++Denominator

++i

return Prefix + BestNumerator.ToString() + '/' +
BestDenominator.ToString() + '=' + cast(double, BestNumerator) /
cast(double, BestDenominator)

[Button('Math')]
static def Random() as double:
r as Random = Random()
return cast(double, r.Next()) / cast(double, int.MaxValue)

[Button('Main')]
static def IntegerPart(d as double) as double:
sign as int = Math.Sign(d)
return (sign * Math.Floor(Math.Abs(d)))

[Button('Main')]
static def FractionalPart(d as double) as double:
return d - IntegerPart(d)

[Button('Main')]
static def Round(d as double) as double:
return Math.Round(d)

[Button('Main')]
static def Floor(d as double) as double:
return Math.Floor(d)

[Button('Main')]
static def Ceiling(d as double) as double:
return Math.Ceiling(d)

private static def FormatMoney(amount as double) as String:
return amount.ToString('C')

[Button('Finance')]
static def Amortize(principal as double, interestRatePercent as double,
lengthOfLoanInYears as int):
html as StringBuilder = StringBuilder()
monthlyPrincipalPayment as double
try:
monthlyPrincipalPayment = cast(double, Util.Prompt(_iapi.TheMainForm,
'Extra Monthly Principal Payment', 'Extra monthly principal payment:',
'0.00', typeof(double)))
except exception as Exception:
monthlyPrincipalPayment = 0

html.Append('<HTML>')
J as double = interestRatePercent / (12 * 100)
N as int = lengthOfLoanInYears * 12
D as double = 1 - Math.Pow(1 + J, -N)
M as double = principal * (J / D)
T as double = M * N
html.Append('<BODY>')
html.Append('Details:<BR/><BR/>')
html.Append('<TABLE border=\'1\'>')
html.Append('<TR><TD>Principal</TD><TD>' + FormatMoney(principal) +
'</TD></TR>')
html.Append('<TR><TD>Term in Years</TD><TD>' + lengthOfLoanInYears +
'</TD></TR>')
html.Append('<TR><TD>Interest Rate</TD><TD>' + interestRatePercent +
'%</TD></TR>')
html.Append('<TR><TD>Monthly Normal Payment</TD><TD>' + FormatMoney(M)
+ '</TD></TR>')
html.Append('<TR><TD>Monthly Extra Principal Payment</TD><TD>' +
FormatMoney(monthlyPrincipalPayment) + '</TD></TR>')
html.Append('<TR><TD>Monthly Total Payment</TD><TD>' + FormatMoney(M +
monthlyPrincipalPayment) + '</TD></TR>')
html.Append('<TR><TD>Total Payments</TD><TD>' + FormatMoney(T) +
'</TD></TR>')
html.Append('</TABLE><BR/>')
html.Append('Schedule:<BR/><BR/>')
html.Append('<TABLE border=\'1\'>')
html.Append('<THEAD>')
html.Append('<TH>Month</TH>')
html.Append('<TH>Principal</TH>')
html.Append('<TH>Interest</TH>')
html.Append('<TH>Balance</TH>')
html.Append('</THEAD>')
totalMonths as int = 0
Month as int = 1
while Month <= N and principal > 0:
H as double = principal * J
C as double = M - H
newPrincipal as double = principal - C - monthlyPrincipalPayment
Q as double = newPrincipal
html.Append('<TR>')
html.Append('<TD>' + Month + '</TD>')
html.Append('<TD>' + FormatMoney(C) + '</TD>')
html.Append('<TD>' + FormatMoney(H) + '</TD>')
html.Append('<TD>' + FormatMoney(Q) + '</TD>')
html.Append('</TR>')
principal = Q
++totalMonths
++Month

html.Append('</TABLE>')
if monthlyPrincipalPayment > 0:
savedMonths as int = N - totalMonths
html.Append('<P>')
html.Append('Your monthly prepayment of ' +
FormatMoney(monthlyPrincipalPayment) + ' reduced the term of your loan
by ' + savedMonths + ' months or ' + (savedMonths / 12).ToString('N') +
' years.')
html.Append('</P>')

html.Append('</BODY>')
html.Append('</HTML>')
Util.DisplayBrowser(_iapi.TheMainForm, 'USA Amortization Schedule',
html.ToString(), true)

[Button('Dates & Times')]
def Now() as date:
return DateTime.Now

[Button('Dates & Times')]
def Today() as date:
return DateTime.Today

[Button('Dates & Times')]
def ToUniversalTime(dateTime as date) as date:
return dateTime.ToUniversalTime()

[Button('Dates & Times')]
def ToLocalTime(dateTime as date) as date:
return dateTime.ToLocalTime()

[Button('Dates & Times')]
def GetDate() as object:
return Util.GetDate(_iapi.TheMainForm, 'Select Date', DateTime.Today)

[Button('Dates & Times')]
def GetDateRange() as object:
return Util.GetDateRange(_iapi.TheMainForm, 'Select Date',
DateTime.Today, 365)

[Button('Dates & Times')]
def AddDays(date as date, days as int) as date:
return date.AddDays(days)

[Button('Dates & Times')]
def SubtractDays(date as date, days as int) as date:
return date.AddDays(-days)

[Button('Dates & Times')]
def SubtractDates(date1 as date, date2 as date) as TimeSpan:
return date1.Subtract(date2)

[assembly: Tab('Main', 1)]
[assembly: Tab('Math', 2)]
[assembly: Tab('Trig', 3)]
[assembly: Tab('Angles', 4)]
[assembly: Tab('Memory', 5)]
[assembly: Tab('Finance', 6)]
[assembly: Tab('Dates & Times', 7)]
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.