📄 mainform.cs
字号:
namespace _24
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Threading;
using System.Windows.Forms;
public class MainForm : Form
{
public MainForm()
{
this.outputString = "";
this.operators = new string[] { "+", "-", "*", "/", "--", "-/" };
this.components = null;
this.InitializeComponent();
this.comboBox1.SelectedIndex = 0;
this.comboBox2.SelectedIndex = 0;
this.comboBox3.SelectedIndex = 0;
this.comboBox4.SelectedIndex = 0;
}
private bool calculate(ArrayList trace)
{
if ((trace.Count == 1) && (Math.Abs((double) (((TraceItem) trace[0]).number - 24)) < 1E-07))
{
this.outputString = ((TraceItem) trace[0]).expression;
return true;
}
for (int num1 = 0; num1 < trace.Count; num1++)
{
for (int num2 = num1 + 1; num2 < trace.Count; num2++)
{
TraceItem item1 = (TraceItem) trace[num1];
TraceItem item2 = (TraceItem) trace[num2];
double num3 = item1.number;
double num4 = item2.number;
ArrayList list1 = new ArrayList();
for (int num5 = 0; num5 < trace.Count; num5++)
{
if ((num5 != num1) && (num5 != num2))
{
TraceItem item3 = new TraceItem();
item3.number = ((TraceItem) trace[num5]).number;
item3.expression = ((TraceItem) trace[num5]).expression;
list1.Add(item3);
}
}
TraceItem item4 = new TraceItem();
list1.Add(item4);
item4.number = num3 + num4;
item4.expression = "(" + item1.expression + "+" + item2.expression + ")";
if (this.calculate(list1))
{
return true;
}
item4.number = num3 - num4;
item4.expression = "(" + item1.expression + "-" + item2.expression + ")";
if (this.calculate(list1))
{
return true;
}
item4.number = num3 * num4;
item4.expression = "(" + item1.expression + "*" + item2.expression + ")";
if (this.calculate(list1))
{
return true;
}
if (num4 != 0)
{
item4.number = num3 / num4;
item4.expression = "(" + item1.expression + "/" + item2.expression + ")";
if (this.calculate(list1))
{
return true;
}
}
item4.number = num4 - num3;
item4.expression = "(" + item2.expression + "-" + item1.expression + ")";
if (this.calculate(list1))
{
return true;
}
if (num3 != 0)
{
item4.number = num4 / num3;
item4.expression = "(" + item2.expression + "/" + item1.expression + ")";
if (this.calculate(list1))
{
return true;
}
}
}
}
return false;
}
private void CalculateAll()
{
base.Enabled = false;
for (int num1 = 0; num1 < 13; num1++)
{
for (int num2 = 0; num2 < 13; num2++)
{
for (int num3 = 0; num3 < 13; num3++)
{
for (int num4 = 0; num4 < 13; num4++)
{
this.comboBox1.SelectedIndex = num1;
this.comboBox2.SelectedIndex = num2;
this.comboBox3.SelectedIndex = num3;
this.comboBox4.SelectedIndex = num4;
this.computeHandler();
}
}
}
}
base.Enabled = true;
}
private void clearBtn_Click(object sender, EventArgs e)
{
this.resultLBox.Items.Clear();
}
private void computeBtn_Click(object sender, EventArgs e)
{
this.computeHandler();
}
private void computeHandler()
{
this.outputString = "";
double[] numArray1 = new double[] { this.GetInputNumber(this.comboBox1), this.GetInputNumber(this.comboBox2), this.GetInputNumber(this.comboBox3), this.GetInputNumber(this.comboBox4) };
ArrayList list1 = new ArrayList();
foreach (double num1 in numArray1)
{
TraceItem item1 = new TraceItem();
item1.number = num1;
item1.expression = num1.ToString();
list1.Add(item1);
}
if (!this.calculate(list1))
{
this.outputString = "No Solution";
}
this.outputString = string.Concat(new object[] { "(", numArray1[0], ", ", numArray1[1], ", ", numArray1[2], ", ", numArray1[3], ") --- ", this.outputString });
this.resultLBox.Items.Add(this.outputString);
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void dumpBtn_Click(object sender, EventArgs e)
{
if (this.saveFileDialog.ShowDialog() == DialogResult.OK)
{
StreamWriter writer1 = new StreamWriter(this.saveFileDialog.FileName);
foreach (string text1 in this.resultLBox.Items)
{
writer1.WriteLine(text1);
}
writer1.Close();
}
}
private void enumerateBtn_Click(object sender, EventArgs e)
{
ThreadStart start1 = new ThreadStart(this.CalculateAll);
new Thread(start1).Start();
}
private double GetInputNumber(ComboBox comboBox)
{
string text1 = comboBox.SelectedItem.ToString();
switch (text1)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
}
return Convert.ToDouble(text1);
}
private void InitializeComponent()
{
ResourceManager manager1 = new ResourceManager(typeof(MainForm));
this.computeBtn = new Button();
this.resultLBox = new ListBox();
this.enumerateBtn = new Button();
this.dumpBtn = new Button();
this.clearBtn = new Button();
this.saveFileDialog = new SaveFileDialog();
this.randomBtn = new Button();
this.comboBox1 = new ComboBox();
this.comboBox2 = new ComboBox();
this.comboBox3 = new ComboBox();
this.comboBox4 = new ComboBox();
base.SuspendLayout();
this.computeBtn.BackColor = Color.Navy;
this.computeBtn.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
this.computeBtn.ForeColor = Color.Yellow;
this.computeBtn.Location = new Point(8, 0x44);
this.computeBtn.Name = "computeBtn";
this.computeBtn.TabIndex = 4;
this.computeBtn.Text = "Compute";
this.computeBtn.Click += new EventHandler(this.computeBtn_Click);
this.resultLBox.BackColor = Color.GreenYellow;
this.resultLBox.Location = new Point(8, 100);
this.resultLBox.Name = "resultLBox";
this.resultLBox.Size = new Size(0xd8, 0xad);
this.resultLBox.TabIndex = 5;
this.enumerateBtn.BackColor = Color.Navy;
this.enumerateBtn.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
this.enumerateBtn.ForeColor = Color.Yellow;
this.enumerateBtn.Location = new Point(8, 280);
this.enumerateBtn.Name = "enumerateBtn";
this.enumerateBtn.Size = new Size(0x90, 0x17);
this.enumerateBtn.TabIndex = 6;
this.enumerateBtn.Text = "Get your cheat sheet";
this.enumerateBtn.Click += new EventHandler(this.enumerateBtn_Click);
this.dumpBtn.BackColor = Color.Navy;
this.dumpBtn.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
this.dumpBtn.ForeColor = Color.Yellow;
this.dumpBtn.Location = new Point(0xa8, 280);
this.dumpBtn.Name = "dumpBtn";
this.dumpBtn.Size = new Size(0x38, 0x17);
this.dumpBtn.TabIndex = 7;
this.dumpBtn.Text = "Dump";
this.dumpBtn.Click += new EventHandler(this.dumpBtn_Click);
this.clearBtn.BackColor = Color.Violet;
this.clearBtn.Location = new Point(0x98, 0x48);
this.clearBtn.Name = "clearBtn";
this.clearBtn.Size = new Size(0x48, 20);
this.clearBtn.TabIndex = 8;
this.clearBtn.Text = "Clear";
this.clearBtn.Click += new EventHandler(this.clearBtn_Click);
this.saveFileDialog.DefaultExt = "txt";
this.saveFileDialog.FileName = "result";
this.saveFileDialog.Filter = "Text files|*.txt";
this.randomBtn.BackColor = Color.PaleGoldenrod;
this.randomBtn.FlatStyle = FlatStyle.Popup;
this.randomBtn.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
this.randomBtn.ForeColor = Color.OrangeRed;
this.randomBtn.Location = new Point(8, 8);
this.randomBtn.Name = "randomBtn";
this.randomBtn.Size = new Size(0x90, 0x17);
this.randomBtn.TabIndex = 9;
this.randomBtn.Text = "Random Problem";
this.randomBtn.Click += new EventHandler(this.randomBtn_Click);
this.comboBox1.BackColor = Color.CornflowerBlue;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.Items.AddRange(new object[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" });
this.comboBox1.Location = new Point(8, 40);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new Size(0x30, 0x15);
this.comboBox1.TabIndex = 10;
this.comboBox2.BackColor = Color.CornflowerBlue;
this.comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox2.Items.AddRange(new object[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" });
this.comboBox2.Location = new Point(0x40, 40);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new Size(0x30, 0x15);
this.comboBox2.TabIndex = 11;
this.comboBox3.BackColor = Color.CornflowerBlue;
this.comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox3.Items.AddRange(new object[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" });
this.comboBox3.Location = new Point(120, 40);
this.comboBox3.Name = "comboBox3";
this.comboBox3.Size = new Size(0x30, 0x15);
this.comboBox3.TabIndex = 12;
this.comboBox4.BackColor = Color.CornflowerBlue;
this.comboBox4.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox4.Items.AddRange(new object[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" });
this.comboBox4.Location = new Point(0xb0, 40);
this.comboBox4.Name = "comboBox4";
this.comboBox4.Size = new Size(0x30, 0x15);
this.comboBox4.TabIndex = 13;
this.AutoScaleBaseSize = new Size(5, 13);
this.BackColor = Color.SpringGreen;
base.ClientSize = new Size(230, 0x134);
base.Controls.AddRange(new Control[] { this.comboBox4, this.comboBox3, this.comboBox2, this.comboBox1, this.randomBtn, this.clearBtn, this.dumpBtn, this.enumerateBtn, this.resultLBox, this.computeBtn });
base.FormBorderStyle=FormBorderStyle.Fixed3D;
// .FormBorderStyle = FormBorderStyle.Fixed3D;
base.Icon = (Icon) manager1.GetObject("$this.Icon");
base.MaximizeBox = false;
base.Name = "MainForm";
this.Text = "24 - V0.11";
base.ResumeLayout(false);
}
[STAThread]
private static void Main()
{
Application.Run(new MainForm());
}
private void randomBtn_Click(object sender, EventArgs e)
{
Random random1 = new Random();
this.comboBox1.SelectedIndex = random1.Next(0, 13);
this.comboBox2.SelectedIndex = random1.Next(0, 13);
this.comboBox3.SelectedIndex = random1.Next(0, 13);
this.comboBox4.SelectedIndex = random1.Next(0, 13);
}
private Button clearBtn;
private ComboBox comboBox1;
private ComboBox comboBox2;
private ComboBox comboBox3;
private ComboBox comboBox4;
private Container components;
private Button computeBtn;
private Button dumpBtn;
private Button enumerateBtn;
private string[] operators;
private string outputString;
private Button randomBtn;
private ListBox resultLBox;
private SaveFileDialog saveFileDialog;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -