patterncsharpModerate
To make a calculator from scratch
Viewed 0 times
scratchfrommakecalculator
Problem
As a personal challenge, I am trying to make a very basic calculator without using any CLR's integer and arithmetic operations, but to only use memory.
The idea was to do what CLR/OS does. i came up with a basic counter, Addition and multiplication, which works fine but multiplication takes long time to compute (if i use big number). Any advice for code improvement for better performance?
```
partial class FastCalculator
{
static LinkedList numbers = new LinkedList(new Char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
static Dictionary> fastNumbers = new Dictionary>();
static FastCalculator()
{
LinkedListNode zero = numbers.First;
while (zero != null)
{
fastNumbers[zero.Value] = zero;
zero = zero.Next;
}
}
//static void Main(string[] args)
//{
// try
// {
// System.Diagnostics.Debug.WriteLine(0 + " PlusOne is " + PlusOne("0"));
// System.Diagnostics.Debug.WriteLine(5 + " PlusOne is " + PlusOne("5"));
// System.Diagnostics.Debug.WriteLine(9 + " PlusOne is " + PlusOne("9"));
// System.Diagnostics.Debug.WriteLine(10 + " PlusOne is " + PlusOne("10"));
// System.Diagnostics.Debug.WriteLine(999 + " PlusOne is " + PlusOne("999"));
// System.Diagnostics.Debug.WriteLine(256 + " PlusOne is " + PlusOne("256"));
// System.Diagnostics.Debug.WriteLine("Multiply 999*256 =" + Multiply("999", "256"));
// }
// catch (Exception ex)
// {
// System.Diagnostics.Debug.WriteLine(ex.Message);
// }
//}
static string PlusOne(string num)
{
if (!string.IsNullOrEmpty(num))
{
LinkedList input = new LinkedList(num.ToCharArray());
if (input.Last.Value != numbers.Last.Value)// not 9
{
input.Last.Value = fastNumbers[input.Last.Value].Next.Value;
return LinkedListToString(input);
The idea was to do what CLR/OS does. i came up with a basic counter, Addition and multiplication, which works fine but multiplication takes long time to compute (if i use big number). Any advice for code improvement for better performance?
```
partial class FastCalculator
{
static LinkedList numbers = new LinkedList(new Char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
static Dictionary> fastNumbers = new Dictionary>();
static FastCalculator()
{
LinkedListNode zero = numbers.First;
while (zero != null)
{
fastNumbers[zero.Value] = zero;
zero = zero.Next;
}
}
//static void Main(string[] args)
//{
// try
// {
// System.Diagnostics.Debug.WriteLine(0 + " PlusOne is " + PlusOne("0"));
// System.Diagnostics.Debug.WriteLine(5 + " PlusOne is " + PlusOne("5"));
// System.Diagnostics.Debug.WriteLine(9 + " PlusOne is " + PlusOne("9"));
// System.Diagnostics.Debug.WriteLine(10 + " PlusOne is " + PlusOne("10"));
// System.Diagnostics.Debug.WriteLine(999 + " PlusOne is " + PlusOne("999"));
// System.Diagnostics.Debug.WriteLine(256 + " PlusOne is " + PlusOne("256"));
// System.Diagnostics.Debug.WriteLine("Multiply 999*256 =" + Multiply("999", "256"));
// }
// catch (Exception ex)
// {
// System.Diagnostics.Debug.WriteLine(ex.Message);
// }
//}
static string PlusOne(string num)
{
if (!string.IsNullOrEmpty(num))
{
LinkedList input = new LinkedList(num.ToCharArray());
if (input.Last.Value != numbers.Last.Value)// not 9
{
input.Last.Value = fastNumbers[input.Last.Value].Next.Value;
return LinkedListToString(input);
Solution
- Arithmetic is implemented in hardware, not by the OS or the CLR (except for things that need a large number of bits to represent - software is used to help with those).
- Binary is used by the hardware, not decimal.
- You can find fast arithmetic algorithms in Dasgupta, Papadimitriou and Vazirani (2006). I recommend this book, it's great.
Context
StackExchange Code Review Q#2334, answer score: 10
Revisions (0)
No revisions yet.