期中報告作業
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
Operator operator1 = new Operator();
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue >= 96 && e.KeyValue <= 105)
{
operator1.choose(textBox1, (char)(e.KeyValue - 48));
}
else if (e.KeyValue >= 48 && e.KeyValue <= 57)
{
operator1.choose(textBox1, (char)e.KeyValue);
}
else
{
switch (e.KeyValue)
{
case 107:
operator1.choose(textBox1, '+');
break;
case 109:
operator1.choose(textBox1, '-');
break;
case 106:
operator1.choose(textBox1, '*');
break;
case 111:
operator1.choose(textBox1, '/');
break;
case 8:
operator1.choose(textBox1, 'B');
break;
case 13:
operator1.choose(textBox1, '=');
break;
case 27:
operator1.choose(textBox1, 'C');
break;
case 110:
operator1.choose(textBox1, '.');
break;
case 46:
operator1.choose(textBox1, 'D');
break;
default:
Debug.Print("keycode = " + (e.KeyValue).ToString());
break;
}
}
}
private void button20_Click(object sender, EventArgs e)
{
char ch = ((Button)sender).Text[0];
if (((Button)sender).Text == "CE")
ch = 'D';
else if (((Button)sender).Text == "sqrt")
ch = '@';
operator1.choose(textBox1, ch);
button5.Focus();
}
}
public class Operator
{
private double num = 0.0, ans = 0.0;
private char preOp;
private int opConter = 0;
private bool preIsOperator = true;
private bool preIskEquals = true;
private bool preIsDot = false;
private string Run(char ch)
{
switch (ch)
{
case '+':
ans = ans + num;
break;
case '-':
ans = ans - num;
break;
case '*':
ans = ans * num;
break;
case '/':
ans = ans / num;
break;
}
opConter = 0;
return ans.ToString();
}
public void choose(TextBox textBox1, char ch)
{
switch (ch)
{
case '+':
case '-':
case '*':
case '/':
if (!preIsOperator)
{
if (preOp == '\0') preOp = ch;
if (opConter > 0)
textBox1.Text = Run(preOp);
opConter++;
preIsOperator = true;
}
preIskEquals = false;
preIsDot = false;
preOp = ch;
break;
case '=':
textBox1.Text = Run(preOp);
preIskEquals = true;
preIsOperator = true;
preIsDot = false;
break;
case '@':
if (preIskEquals)
{
ans = Math.Sqrt(ans);
textBox1.Text = ans.ToString();
}
else
{
num = Math.Sqrt(num);
textBox1.Text = num.ToString();
}
break;
case 'D': // CE
textBox1.Text = "0";
num = double.Parse(textBox1.Text);
break;
case 'B': // BackSpace
if (textBox1.Text[textBox1.Text.Length - 1] == '.') preIsDot = false;
if (textBox1.Text.Length > 1)
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
else
textBox1.Text = "0";
num = double.Parse(textBox1.Text);
break;
case 'C':
textBox1.Text = "0";
num = ans = 0.0;
preIskEquals = true;
preIsOperator = true;
preIsDot = false;
break;
case '.':
if (!preIsDot)
{
textBox1.Text += ".";
preIsOperator = false;
preIsDot = true;
}
break;
default:
if (preIsOperator) textBox1.Text = "";
textBox1.Text += ch.ToString();
if (preIskEquals)
{
ans = double.Parse(textBox1.Text);
num = 0.0;
}
else
num = double.Parse(textBox1.Text);
preIsOperator = false;
break;
}
Debug.Print("ans = " + ans.ToString() + ", num = " + num.ToString() + ", preOp = " + preOp.ToString() + ", ch = '" + ch.ToString() + "'\r\n");
}
}
}
沒有留言:
張貼留言