473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Open Source C# Programmable Calculator

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
1 6911
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Raptor | last post by:
I'm a complete Javascript n00b, using a snippet I found on the web. I'll probably be buying a Javascript book. What's the authoritative on-line resource for Javascript, like php.net is for PHP? ...
0
by: Eric Bergman-Terrell | last post by:
If you like the C# programming language and RPN (HP style) calculators, you'll love C# Programmable Calculator. Take a look at it at: http://www.personalmicrocosms.com/html/cspcalc.html ...
4
by: Ricardo Correia | last post by:
Hello, Does anyone know how to open an application from a windows service?? For example, I have a windows service running and using a timer or something else I want to open Windows Calculator!...
3
by: w2000 | last post by:
Greeting all, I am studying Javascript to custom my yahoo estore, I need to open a custom sized window in a new browser , I tried the following script on 3 different PCs running XP pro. IE 6,...
15
by: may_2005 | last post by:
Hello, I am working on a piece of (open source) hardware, which needs a certain amount of (open source) embedded C code developed for the hardware to perform a useful task. I am looking for...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
2
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, My application knows how to open a word doucment and replace strings, and then print it, but it does it only when Microsoft Word is installed, since I've few computers that don't have...
23
by: andrew | last post by:
Hi, I have been taken to task about the length of lines in the source html of: http://www.slrn.sourceforge.org which, as I commonly do, has no line breaks at all in the source html. My...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.