0
Reply

Method invocation from class and use in the main program

techn no

techn no

Jul 25 2006 5:34 AM
1.7k

The basic operation for this stack visualizer are as followed:
push,pop,Isempty,peek,clear

Currently, i am having problem to display the code in the main form when the method is being called. What I want to achieve is to simulate the basic operation of 'push' and 'pop' functions.

When the push button is being invoked the code from the Array stack class /  linkedlist stack class has to be displayed and the lines of codes for the push or pop operations from that particular class has to be shown as the codes are being step over

For example if I am calling the push operation, the method of the class for this push operation has to be called and the lines of the codes has to be displayed. It is very similar to stepping over the codes line by line.

What i want to achieve is something like this? If will be very appreciated if any programming gurus could provide some valuable guidance or perhaps assist me checking the logic of the codes.

For this program i am using linkedlist as well as array implementation. Another thing i wish to knw is to clear the items of the stack and rest the counter to 0 once the method clear is being called.

My code is as followed:

Main form

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Stackproj
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmStack : System.Windows.Forms.Form
{


/// <summary>
/// Required designer variable.
/// </summary>

private int counter = 0;
private Stack stack = new Stack();
private Stack [] stackArray = new Stack [10];
//private Stack StackArray = new Stack();
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnPush;
private System.Windows.Forms.Button btnPop;
private System.Windows.Forms.Button btnPeek;
private System.Windows.Forms.Button btnSize;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tbxItem;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lblCounter;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton rbtnArray;
private System.Windows.Forms.RadioButton rbtnLinkedList;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.TextBox tbxOutput;
private System.Windows.Forms.Button btnStep;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.CheckBox cbxShow;
private System.Windows.Forms.Label label3;
public string status;

public frmStack()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.btnPush = new System.Windows.Forms.Button();
this.btnPop = new System.Windows.Forms.Button();
this.btnPeek = new System.Windows.Forms.Button();
this.btnSize = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.tbxItem = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.lblCounter = new System.Windows.Forms.Label();
this.btnReset = new System.Windows.Forms.Button();
this.rbtnArray = new System.Windows.Forms.RadioButton();
this.rbtnLinkedList = new System.Windows.Forms.RadioButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnGo = new System.Windows.Forms.Button();
this.tbxOutput = new System.Windows.Forms.TextBox();
this.btnStep = new System.Windows.Forms.Button();
this.cbxShow = new System.Windows.Forms.CheckBox();
this.label3 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.btnPush);
this.panel1.Controls.Add(this.btnPop);
this.panel1.Controls.Add(this.btnPeek);
this.panel1.Controls.Add(this.btnSize);
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.listBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.tbxItem);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.lblCounter);
this.panel1.Controls.Add(this.btnReset);
this.panel1.Cursor = System.Windows.Forms.Cursors.Hand;
this.panel1.Location = new System.Drawing.Point(24, 24);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(344, 360);
this.panel1.TabIndex = 13;
//
// btnPush
//
this.btnPush.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnPush.Location = new System.Drawing.Point(-1, 0);
this.btnPush.Name = "btnPush";
this.btnPush.Size = new System.Drawing.Size(72, 24);
this.btnPush.TabIndex = 5;
this.btnPush.Text = "Push";
this.btnPush.Click += new System.EventHandler(this.btnPush_Click_1);
//
// btnPop
//
this.btnPop.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnPop.Location = new System.Drawing.Point(63, 0);
this.btnPop.Name = "btnPop";
this.btnPop.Size = new System.Drawing.Size(72, 24);
this.btnPop.TabIndex = 3;
this.btnPop.Text = "Pop";
this.btnPop.Click += new System.EventHandler(this.btnPop_Click_1);
//
// btnPeek
//
this.btnPeek.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnPeek.Location = new System.Drawing.Point(135, 0);
this.btnPeek.Name = "btnPeek";
this.btnPeek.Size = new System.Drawing.Size(72, 24);
this.btnPeek.TabIndex = 5;
this.btnPeek.Text = "Peek";
this.btnPeek.Click += new System.EventHandler(this.btnPeek_Click_1);
//
// btnSize
//
this.btnSize.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnSize.Location = new System.Drawing.Point(199, 0);
this.btnSize.Name = "btnSize";
this.btnSize.Size = new System.Drawing.Size(72, 24);
this.btnSize.TabIndex = 2;
this.btnSize.Text = "Size";
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button2.Location = new System.Drawing.Point(271, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 13;
this.button2.Text = "Exit";
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(0, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(344, 212);
this.listBox1.TabIndex = 0;
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(16, 272);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 9;
this.label1.Text = "Enter a value:";
//
// tbxItem
//
this.tbxItem.Location = new System.Drawing.Point(104, 272);
this.tbxItem.Name = "tbxItem";
this.tbxItem.TabIndex = 1;
this.tbxItem.Text = "";
//
// label2
//
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(16, 312);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 11;
this.label2.Text = "Counter:";
//
// lblCounter
//
this.lblCounter.Location = new System.Drawing.Point(112, 312);
this.lblCounter.Name = "lblCounter";
this.lblCounter.Size = new System.Drawing.Size(88, 23);
this.lblCounter.TabIndex = 4;
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(232, 272);
this.btnReset.Name = "btnReset";
this.btnReset.TabIndex = 20;
this.btnReset.Text = "Reset";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// rbtnArray
//
this.rbtnArray.Checked = true;
this.rbtnArray.Location = new System.Drawing.Point(424, 72);
this.rbtnArray.Name = "rbtnArray";
this.rbtnArray.TabIndex = 14;
this.rbtnArray.TabStop = true;
this.rbtnArray.Text = "Array";
//
// rbtnLinkedList
//
this.rbtnLinkedList.Location = new System.Drawing.Point(424, 112);
this.rbtnLinkedList.Name = "rbtnLinkedList";
this.rbtnLinkedList.TabIndex = 15;
this.rbtnLinkedList.Text = "LinkedList";
//
// groupBox1
//
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(400, 48);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(144, 104);
this.groupBox1.TabIndex = 16;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Implementation";
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(440, 168);
this.btnGo.Name = "btnGo";
this.btnGo.TabIndex = 17;
this.btnGo.Text = "Go";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// tbxOutput
//
this.tbxOutput.Location = new System.Drawing.Point(400, 256);
this.tbxOutput.Multiline = true;
this.tbxOutput.Name = "tbxOutput";
this.tbxOutput.Size = new System.Drawing.Size(184, 128);
this.tbxOutput.TabIndex = 18;
this.tbxOutput.Text = "";
//
// btnStep
//
this.btnStep.Location = new System.Drawing.Point(600, 360);
this.btnStep.Name = "btnStep";
this.btnStep.TabIndex = 19;
this.btnStep.Text = "Step";
this.btnStep.Click += new System.EventHandler(this.btnStep_Click);
//
// cbxShow
//
this.cbxShow.Location = new System.Drawing.Point(400, 224);
this.cbxShow.Name = "cbxShow";
this.cbxShow.Size = new System.Drawing.Size(56, 24);
this.cbxShow.TabIndex = 20;
this.cbxShow.Text = "Show";
//
// label3
//
this.label3.Location = new System.Drawing.Point(472, 232);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 23);
this.label3.TabIndex = 21;
this.label3.Text = "Pseudo Code";
//
// frmStack
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(696, 405);
this.Controls.Add(this.label3);
this.Controls.Add(this.cbxShow);
this.Controls.Add(this.btnStep);
this.Controls.Add(this.tbxOutput);
this.Controls.Add(this.btnGo);
this.Controls.Add(this.rbtnLinkedList);
this.Controls.Add(this.rbtnArray);
this.Controls.Add(this.panel1);
this.Controls.Add(this.groupBox1);
this.Name = "frmStack";
this.Text = "Pseudo Code";
this.Load += new System.EventHandler(this.frmStack_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new frmStack());
}

Array StackVisualize

private void VisualizeStackArray(Stack sa)
{
Stack theStack = new Stack();

while(!sa.IsEmpty())
{
object o = (object)sa.Pop();
theStack.Push(o);
this.listBox1.Items.Add("|__" + stackArray.ToString() + "__|");
this.listBox1.Items.Add("|___|");
}

for(int i = 0 ; i < stackArray.Length ; i++)
{
this.listBox1.Items.Insert(0,"");
}

this.listBox1.SelectedIndex = this.listBox1.Items.Count-1;
while(!theStack.IsEmpty())
{
sa.Push(theStack.Pop());
}

}

Linkedlist StackVisualize

private void VisualizeStack(Stack s)
{
Stack t = new Stack();
object o;
this.listBox1.Items.Clear();

while(!s.IsEmpty())
{
o = s.Pop();
t.Push(o);
this.listBox1.Items.Add("|__" + o.ToString() + "__|");
this.listBox1.Items.Add("|_____|");

}
// ArrayList stacklist = new ArrayList();
// foreach(Stack st in stacklist)

for(int i=0;i<20;i++)
{
//stacklist.Add(tbxItem.Text);
this.listBox1.Items.Insert(0,"");
}
this.listBox1.SelectedIndex = this.listBox1.Items.Count-1;
while(!t.IsEmpty())
{
s.Push(t.Pop());
}
}

private void frmStack_Load(object sender, System.EventArgs e)
{
// tabPage1.Text = "Push";
// tabPage2.Text = "Pop";
// tabPage3.Text = "Peek";
// tabPage4.Text = "Size";

}

private void btnPush_Click_1(object sender, System.EventArgs e)
{
if(rbtnArray.Checked)
{
if(tbxItem.Text.Trim() != String.Empty)
{
Stack s = new Stack();
s.Push(tbxItem.Text.Trim());
listBox1.Items.Insert(0,tbxItem.Text.Trim());
counter++;
}
else
{
MessageBox.Show("Stack is empty", "Stack", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}


// if(tbxItem.Text == null)
// {
// MessageBox.Show("Stack is empty", "Stack", MessageBoxButtons.OK,
// MessageBoxIcon.Information);
// }
//
// Stack s = new Stack();
// stack.Push(tbxItem.Text.Trim());
// listBox1.Items.Insert(0,tbxItem.Text.Trim());
// counter++;

this.lblCounter.Text = counter.ToString();
//this.VisualizeStackArray(stackArray);


// if(s.IsEmpty())
// {
// MessageBox.Show("Stack is empty");
// }
// else
// {
// for(int i = 0; i < s.Size; i++)
// {
// listBox1.Items.Insert(0,tbxItem.Text.Trim());
// counter++;
//
// if(i > s.Size)
// {
// MessageBox.Show("Stack is full");
// }
//
// }
// }
}

if(rbtnLinkedList.Checked)
{

//if(tbxItem.Text.Trim() != nul)
if(tbxItem.Text.Trim() != String.Empty)
{
stack.Push(tbxItem.Text.Trim());
//listBox1.Items.Insert(0,"Pushed : " + tbxItem.Text.Trim());
listBox1.Items.Insert(0,tbxItem.Text.Trim());
counter++;
}

else
{
MessageBox.Show("Stack is empty","Stack",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}


//stack.Push(tbxItem.Text);
//this.lblCounter.Text = String.Format("The size of stack is: (0) Item", this.stack.Size.ToString());
this.lblCounter.Text = counter.ToString();
this.VisualizeStack(stack);

// stack.Clear();
// this.lblCounter.Text = counter.ToString();
// counter = 0;
}

}

private void Clear()
{
// stack.Clear();
// listBox1.Items.Clear();
// this.lblCounter.Text = counter.ToString();
}

private void btnPop_Click_1(object sender, System.EventArgs e)
{
try
{
MessageBox.Show("Element popped is: " + stack.Pop().ToString(),"Stack");
listBox1.Items.Remove(tbxItem.Text);
}

catch
{
MessageBox.Show("There are no more elements","Stack");
return;
}

//stack.Push(tbxItem.Text);
//string s = (string)stack.Pop(this.tbxItem.Text);
counter--;
this.lblCounter.Text = counter.ToString();
this.VisualizeStack(stack);

}

private void btnPeek_Click_1(object sender, System.EventArgs e)
{
try
{
MessageBox.Show("Element peeked is:" + stack.Peek().ToString(),"Stacks");
}
catch(Exception ex)
{
MessageBox.Show("Error:" + ex.Message,"Stacks",
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

private void panel1_Paint_1(object sender, System.Windows.Forms.PaintEventArgs e)
{
this.btnPush.Text = "Push";
}

private void button2_Click(object sender, System.EventArgs e)
{
Close();
// Application.Exit();
}

// private void axShockwaveFlash1_Enter(object sender, System.EventArgs e)
// {
// //Application.StartupPath + "\\Untitled-2.swf";
// }

private void button2_Click_1(object sender, System.EventArgs e)
{
if(MessageBox.Show("Do you really wish to exit your application?", "Stacks",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
Application.Exit();
}

private void btnGo_Click(object sender, System.EventArgs e)
{
// if(rbtnLinkedList.Checked || rbtnArray.Checked == false)
// {
// MessageBox.Show("Please select an option", "Warning",
// MessageBoxButtons.OK, MessageBoxIcon.Warning);
// }

if(rbtnArray.Checked)
{
MessageBox.Show("You have selected ArrayImplementation", "Stacks");
}
else
{
MessageBox.Show("You have selected LinkedListImplementation", "Stacks");
}
}

private void btnStep_Click(object sender, System.EventArgs e)
{

}

private void btnReset_Click(object sender, System.EventArgs e)
{
Stack s = new Stack();
for(int i = 0 ; i < stack.Size; i++)
{
listBox1.Items.Clear();
counter = 0;
}
stack.Clear();
this.lblCounter.Text = counter.ToString();
}
}
}

LinkedList Node class

using System;

namespace Stackproj
{
public class Node
{

private object data;
private Node link;

public Node (object d, Node l)
{
data = d;
link = l;
}

public object Data
{
get
{
return data;
}
set
{
data = value;
}
}
public Node Link
{
get
{
return link;
}
set
{
link = value;
}
}

}
}

LinkedList stack class

using System;

namespace Stackproj
{
/// <summary>
/// Summary description for Stack.
/// </summary>
public class Stack
{
private Node top;
private int size;

public Stack()// initialize constructor
{
top = null; // indicate empty stack
size = 0;
}
public int Size
{
get
{return size;}
}

// insert an object to the stack
public void Push(object item)
{
// create a node containing item
Node n = new Node(item, null);

// Link node to the first node
n.Link = top;

// Link head to the new node
top = n;

// Increment size
size++;
}

public object Pop()
{
object temp = null;

if(top == null)
{
throw new InvalidOperationException("Can't pop from empty stack");
}

if(top != null)
{
temp = top.Data; // store data of the first node
top = top.Link; // remove the first node
size--; // decrement the size
}
return temp; // return temp to the main program
//else
//{return null;}

}
public object Peek()
{
object temp = null;
if(top != null)
{
temp = top.Data;// store data in the first node
}
return temp; // return temp to the main program
}
public bool IsEmpty()
{
if(top == null)

{return true;}
else
{return false;}
}

public void Clear()
{
return;
}

}
}

ArrayStack class

using System;

namespace PostFix
{
/// <summary>
/// Summary description for StackArray.
/// </summary>
public class StackArray // region stackArray
{
// Attributes

private const int MAX_SIZE = 10; // initialize a fixed size to store the objects
private object [] stackArray; // store max size into the stackArray
private int top; // initialize a variable top to indicate how much of the array is being used

// Constructor

public StackArray(int s)
{
s = MAX_SIZE; // assign max size to s
stackArray = new object[MAX_SIZE]; // create new object and store maxsize into stackArray
top = -1; // initialize top to -1 to indicate an empty stack
}

// Methods

public void Push(object item)
{
if(top < MAX_SIZE -1) // check for stack overflow
{
throw new InvalidOperationException("Stack is full"); // throw user-defined exception if stack is full
}
top++; // increment top
stackArray[top]=item; // add item to stackArray

}

public void Pop()
{
object temp = null;
if(top >= 0) // check for stack underflow
{
throw new InvalidOperationException("Stack is empty"); //throw user-defined exception is stack is empty
}
else
{
object o = stackArray[top]; // remove object in array
}
top--; // decrement top
//stackArray[top];
}

public void Peek()
{
if(IsEmpty())
{
throw new InvalidOperationException("Empty stacks");
}
// else
// {
// return stackArray[top];
// }
}

public bool IsEmpty()
{
if(top < 0)
{return true;}
else
{return false;}
}

public bool IsFull()
{
return (top == MAX_SIZE-1);
}
} // end of region
}