[talk-c] help with stack over flow c # application

  • From: "Marvin Hunkin" <startrekcafe@xxxxxxxxx>
  • To: <talk-c@xxxxxxxxxxxxx>
  • Date: Sat, 26 Dec 2015 21:30:00 +1030

hi. a blind it student using the jaws for windows screen reader from
http://www.freedomscientific.com and doing certificate iv programming from
http://www.upskilled.edu.au. doing a simple calculator. and have run into a
stack over flow. have tried looking on the net, googled, and did read about
recursive functions, and also stack over flow, but could only find examples,
and fixes for c # console, or windows forms, nothing for universal apps or
xaml. so can any one help me out, point me to where i can find the answers,
and just fix this problem. this is the only problem. and points the
exception to line 266. can any one help me. any one in australia, or south
east asia, as similar or same time zone. marvin.

Ps:: pasting below.



View Detail

System.StackOverflowException {The runtime refused to evaluate the
expression at th

System.StackOverflowException was unhandled

HResult=-2147023895

InnerException:



<Page

x:Class="Calculator.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml";

xmlns:local="using:Calculator"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008";

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006";

mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<TextBlock HorizontalAlignment="Left" Margin="120,50,0,0"
TextWrapping="Wrap" Text="Simple Calculator"

VerticalAlignment="Top"/>

<TextBox x:Name="Result" IsReadOnly="true" Margin="10,75,0,0"

TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="340"/>

<Button Content="1" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="78,189,0,0" Width="42" x:Name="btn1" Click="btn1_Click"/>

<Button Content="2" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="125,189,0,0" Width="42" x:Name="btn2" Click="btn2_Click"/>

<Button Content="3" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="172,189,0,0" Width="42" x:Name="btn3" Click="btn3_Click"/>

<Button Content="4" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="78,152,0,0" Width="42" x:Name="btn4" Click="btn4_Click"/>

<Button Content="5" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="125,152,0,0" Width="42" x:Name="btn5" Click="btn5_Click"/>

<Button Content="6" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="172,152,0,0" Width="42" x:Name="btn6" Click="btn6_Click"/>

<Button Content="7" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="78,115,0,0" Width="42" x:Name="btn7" Click="btn7_Click"/>

<Button Content="8" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="125,115,0,0" Width="42" x:Name="btn8" Click="btn8_Click"/>

<Button Content="9" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="172,115,0,0" Width="42" x:Name="btn9" Click="btn9_Click"/>

<Button Content="0" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="125,226,0,0" Width="42" x:Name="btn0" Click="btn0_Click"/>

<Button Content="+" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="219,226,0,0" Width="42" x:Name="btnPlus" Click="btnPlus_Click"/>

<Button Content="-" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="219,189,0,0" Width="42" x:Name="btnMinus" Click="btnMinus_Click"/>

<Button Content="*" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="219,152,0,0" Width="42" x:Name="btnTimes" Click="btnTimes_Click"/>

<Button Content="/" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="219,115,0,0" Width="42" x:Name="btnDiv" Click="btnDiv_Click"/>

<Button Content="=" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="78,226,0,0" Width="42" x:Name="btnEqual" Click="btnEqual_Click"/>

<Button Content="C" HorizontalAlignment="Left" VerticalAlignment="Top"

Margin="172,226,0,0" Width="42" x:Name="btnClear" Click="btnClear_Click"/>

</Grid>

</Page>

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;



// The Blank Page item template is documented at
http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409



namespace Calculator

{

/// <summary>

/// An empty page that can be used on its own or navigated to within a
Frame.

/// </summary>

public sealed partial class MainPage : Page

{

public MainPage()

{

this.InitializeComponent();



Result.Text = 0.ToString();

}



// Function to add numbers and check for a empty char and empty string.



private void AddNumberToResult(double number)

{

if (char.IsNumber(Result.Text.Last()))

{

if (Result.Text.Length == 1 && Result.Text == "0")

{

Result.Text = string.Empty;

}

Result.Text += number;

}

else

{

if (number != 0)

{

Result.Text += number;

}

}

}



// Enum to add the different mathematical operations for this
application.



enum Operation { MINUS = 1, PLUS = 2, DIV = 3, TIMES = 4, NUMBER = 5 }



// Function to add the operation and the Result to this application.

// Tadd a substring to the Result of the text in this application.



private void AddOperationToResult(Operation operation)

{

if (Result.Text.Length == 1 && Result.Text == "0")

return;



if (!char.IsNumber(Result.Text.Last()))

{

Result.Text = Result.Text.Substring(0, Result.Text.Length - 1);

}



// Switch statement to choose the different mathematical operations
for this application.



switch (operation)

{

case Operation.MINUS:

Result.Text += "-";

break;

case Operation.PLUS:

Result.Text += "+";

break;

case Operation.DIV:

Result.Text += "/";

break;

case Operation.TIMES:

Result.Text += "*";

break;

}

}



// Add all the number functions to the text box and the mathematical
operations.

// Then assign the Result to using the mathematical operators, to
display the Result in the text box.

// Call the Add Number To Result function in the Number click events for
this application.



private void btn1_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(1);

}



private void btn2_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(2);

}



private void btn3_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(3);

}



private void btn4_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(4);

}



private void btn5_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(5);

}



private void btn6_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(6);

}



private void btn7_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(7);

}



private void btn8_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(8);

}



private void btn9_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(9);

}



private void btn0_Click(object sender, RoutedEventArgs e)

{

AddNumberToResult(0);

}



private void btnPlus_Click(object sender, RoutedEventArgs e)

{

AddOperationToResult(Operation.PLUS);

}



private void btnMinus_Click(object sender, RoutedEventArgs e)

{

AddOperationToResult(Operation.MINUS);

}



private void btnTimes_Click(object sender, RoutedEventArgs e)

{

AddOperationToResult(Operation.TIMES);

}



private void btnDiv_Click(object sender, RoutedEventArgs e)

{

AddOperationToResult(Operation.DIV);

}



// Function to use the Equals operators and mathematical operators for
this application.



#region Equal

private class Operand

{

public Operation operation = Operation.NUMBER; // Default constructor

public double value = 0;



public Operand left = null;

public Operand right = null;

}



//Get an expression from Result.Text and build a

// tree function.



private Operand BuildTreeOperand()

{

Operand tree = null;



string expression = Result.Text;

if (!char.IsNumber(expression.Last()))

{

expression = expression.Substring(0, expression.Length - 1);

}



string numberStr = string.Empty;

foreach (char c in expression.ToCharArray())

{

if (char.IsNumber(c) || c == '.' || numberStr == string.Empty && c
==

'-')

{

numberStr += c;

}

else

{

AddOperandToTree(ref tree, new Operand()

{

value = double.Parse(numberStr)

});

numberStr = string.Empty;



// Switch statement to show the different calculator operations
for this application.



Operation op = Operation.MINUS; // Default
condition.



switch (c)

{

case '-':

op = Operation.MINUS;

break;

case '+':

op = Operation.PLUS;

break;

case '/':

op = Operation.DIV;

break;

case '*':

op = Operation.TIMES;

break;

}

AddOperandToTree(ref tree, new Operand()

{

operation = op

});

}

}



// Last number.





AddOperandToTree(ref tree, new Operand()

{

value = double.Parse(numberStr)

});



return tree;

}



// This function will add the Operand tree and all associated objects
for this application.



private void AddOperandToTree(ref Operand tree, Operand elem)

{

if (tree == null)

{

tree = elem;

}

else

{

if (elem.operation < tree.operation)

{

Operand auxTree = tree;

tree = elem;

elem.left = tree;

}

else

{

AddOperandToTree(ref tree.right, elem); // Recursive function for
the Add Operand To Tree structure.//

}

}

}



// Function to calculate the Operand tree and all objects for this
application.



private double Calc(Operand tree)

{

if (tree.left == null && tree.right == null) // Selects the number.

{

return tree.value;

}

else// Select an operation.

{

double subResult = 0;

// Switch statement to choose the different operations for this
application.

switch (tree.operation)

{

case Operation.MINUS:

subResult = Calc(tree.left) - Calc(tree.right);

break; // Recursive function.

case Operation.PLUS:

subResult = Calc(tree.left) + Calc(tree.right);

break;

case Operation.DIV:

subResult = Calc(tree.left) / Calc(tree.right);

break;

case Operation.TIMES:

subResult = Calc(tree.left) * Calc(tree.right);

break;

}

return subResult;

}

}



// Function to have the Equals button, set empty string and then set up
the calculations and the Results in the text box.



private void btnEqual_Click(object sender, RoutedEventArgs e)

{

if (string.IsNullOrEmpty(Result.Text))

return;



// Build a new string tree from the Result.Text TextBox.



Operand tree = BuildTreeOperand();



// Evaluate tree to calculate final Result from the TextBox.



double value = Calc(tree);



Result.Text = value.ToString();

}



// Function to clear the text box and reset all values to 0 or null.



#endregion Equal



private void btnClear_Click(object sender, RoutedEventArgs e)

{

Result.Text = 0.ToString();

}

}

}



Other related posts:

  • » [talk-c] help with stack over flow c # application - Marvin Hunkin