Calculator with Stack.....
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class calc : Form
{
public calc()
{
InitializeComponent();
}
Stack<int> st = new Stack<int>();
private void button1_Click(object sender, EventArgs e)
{
string[] ar = new string[100];
string fg = textBox1.Text;
ar = fg.Split('+');
int total = 0;
for (int i = 0; i < ar.Length; i++)
{
st.Push(Convert.ToInt32(ar[i].ToString()));
listBox1.Items.Add(ar[i].ToString());
}
MessageBox.Show(st.Count.ToString());
for (int i = st.Count; i > 0; i--)
{
int s = st.Pop();
total = total + s;
}
MessageBox.Show(Convert.ToString(total));
}
}
}
Category: