Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 25th, 2015, 5:55 AM   #1
StarTrekCafe
Programmer
 
Join Date: Jul 2013
Posts: 48
Rep Power: 0 StarTrekCafe is on a distinguished road
c # wpf stack over flow issue

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/?Link...52&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();
    }
  }
}

Last edited by pritaeas; Dec 28th, 2015 at 2:32 AM. Reason: Added code tags.
StarTrekCafe is offline   Reply With Quote
Old Dec 28th, 2015, 2:32 AM   #2
pritaeas
mod_pritaeas
 
Join Date: May 2009
Location: The Netherlands
Posts: 251
Rep Power: 8 pritaeas is on a distinguished road
Re: c # wpf stack over flow issue

> points the exception to line 266

What line is 266 in the above snippet?
pritaeas is offline   Reply With Quote
Old Jan 4th, 2016, 4:53 AM   #3
StarTrekCafe
Programmer
 
Join Date: Jul 2013
Posts: 48
Rep Power: 0 StarTrekCafe is on a distinguished road
hi. well id fix and rewrite the program. but got an error, only one, and says cannot implicitly convert double to string, can you help me out and point me in the rightRe: c # wpf stack over flow issue

hi. can you help me out. have written the application, and a lot more simpler, but got one error. cannot implicityly convert double to string. can you help me out or point me in the right direction. never seen this error before. and visual studio 2015, the error window, is more helpful, says the description, and the line number. very useful. so will paste the code below. can you help. marvin. private void btnEqual_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(Result.Text))
return;

try {
Result.Text = Evaluate(Result.Text);
}
catch(DivideByZeroException e) {
Result.Text = "Error: divide by zero detected!"
}

}
Quote:
Originally Posted by pritaeas View Post
> points the exception to line 266

What line is 266 in the above snippet?
StarTrekCafe is offline   Reply With Quote
Old Jan 8th, 2016, 5:56 AM   #4
pritaeas
mod_pritaeas
 
Join Date: May 2009
Location: The Netherlands
Posts: 251
Rep Power: 8 pritaeas is on a distinguished road
Re: c # wpf stack over flow issue

What code is triggering the conversion error? The code you pasted does not appear to be the cause of the exception. What does Evaluate do exactly?

I usually don't say this, but since this forum is not active at all, you may want to ask your questions over at DaniWeb. There's a lot more activity and you may get answers much sooner there.
pritaeas is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stack / Queue / Link List Implementation For Help.... jovenne C++ 1 Jul 27th, 2011 1:20 AM
problem with my stack program on array implementations kaye21 C 1 Apr 27th, 2010 1:28 AM
data flow diagrams and context diagrams SweetFlvr Software Design and Algorithms 0 Nov 16th, 2009 8:36 PM
Postfix expressions using a stack SkyRunnr C 6 Jan 19th, 2009 10:19 PM
Flow Control Issue BlazingWolf C# 1 Feb 1st, 2007 6:00 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC