Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: new to java help
|
Posted: Nov 7, 2002 5:23 PM
|
|
How's this? I made a few improvements over what you requested: You can type in the values and operator in any old order. It has a graphical interface instead of console interface. The calculator class works a little like the old HP calculators, using the reverse Polish notation approach.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
class Calculator
{
private Stack stack = new Stack();
public void Push( double a )
{
stack.Push( a );
}
public double Pop()
{
return (double)stack.Pop();
}
/// Todo: make this a property called "Value" or "Result"...
public double Peek()
{
return (double)stack.Peek();
}
public double Add()
{
Push( Pop() + Pop() );
return Peek();
}
public double Sub()
{
// The one at the top is subtracted from the next one down.
// (Pop() - Pop()) would get the wrong result.
Push( - Pop() + Pop() );
return Peek();
}
public double Mult()
{
Push( Pop() * Pop() );
return Peek();
}
public double Div()
{
double divisor = Pop();
Push( Pop() / divisor );
return Peek();
}
public double Mod()
{
// Not relevant for floating point, so convert to int:
int divisor = (int)Pop();
Push( (int)Pop() % divisor );
return Peek();
}
bool IsNum( string s )
{
// try converting it...
try
{
float.Parse(s);
return true;
}
catch( Exception e )
{
return false;
}
}
public double Calc( string s )
{
String []parts = s.Split();
char op = ' ';
foreach( string part in parts )
{
if( IsNum(part) )
Push( double.Parse( part ) );
else
op = part[0];
}
switch( op )
{
case '+': return Add();
case '-': return Sub();
case '*': return Mult();
case '/': return Div();
case '%': return Mod();
default: return Peek();
}
}
}
public class Form1 : System.Windows.Forms.Form
{
private Calculator calculator = new Calculator();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// Required designer variable.
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// Clean up any resources being used.
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(152, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Enter Your Calculation";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Enter";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "1 + 1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(184, 86);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1,
this.label1});
this.Name = "Form1";
this.Text = "Calculator";
this.ResumeLayout(false);
}
#endregion
/// The main entry point for the application.
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = calculator.Calc( textBox1.Text ).ToString();
}
}
}
|
|