回复 楼主 飞鹰JACKEY
仓促写了一个,按照我的理解应该能满足你的要求,如有不妥还望指教。不知道如何发图,所以没办法发UI图片上来,就看代码吧。批量加运算时,先打开两个批量文件输入数据保存好,然后点击“批量加运算结果”按钮,就会计算结果并保存到"BatchResult.txt"文件并用记事本打开。
程序代码:
//Counter.cs
using System;
using System.Collections.Generic;
namespace BatchCalculator
{
public class Counter
{
public static double Add(double value0, double value1)
{
return value0 + value1;
}
public static List<double> BatchAdd(List<double> values0, List<double> values1)
{
var result = new List<double>();
if (values0.Count != values1.Count)
{
if (values0.Count > values1.Count)
values0 = values0.GetRange(0, values1.Count);
else
values1 = values1.GetRange(0, values0.Count);
}
for (var i = 0; i < values0.Count; i++)
result.Add(values0[i] + values1[i]);
return result;
}
public static double Dec(double value0, double value1)
{
return value0 - value1;
}
public static double Multiply(double value0, double value1)
{
return value0 * value1;
}
public static double Divide(double value0, double value1)
{
return value0 / value1;
}
public static double Sqrt(double value0)
{
return Math.Sqrt(value0);
}
}
}
//FormMain.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using using System.Text;
using System.Windows.Forms;
namespace BatchCalculator
{
public partial class FormMain : Form
{
#region 常量
private const string InvalidNumber = "很抱歉,您输入的数值超过了有效范围!";
#endregion
#region 全局字段
private bool _calculateCompleted;
private List<double> _numbers;
private List<string> _numberStrings;
private List<string> _operates;
private string _startupPath;
#endregion
#region 构造函数
public FormMain()
{
InitializeComponent();
Init();
}
#endregion
#region 控件事件
void ButCalculator_Click(object sender, EventArgs e)
{
var but = (Button) sender;
var command = but.Text;
switch (command)
{
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
{
if (_calculateCompleted)
{
_calculateCompleted = false;
_numberStrings.Clear();
}
_numberStrings.Add(command);
TbResult.Text = string.Concat(_numberStrings);
}
break;
case "Clear":
{
ClearInnerData();
TbResult.Clear();
}
break;
case "*":
case "/":
case "+":
case "-":
{
if (_numberStrings.Count == 0) break;
double number;
if (!double.TryParse(string.Concat(_numberStrings), out number))
{
MessageBox.Show(InvalidNumber, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
_numberStrings.Clear();
break;
}
_operates.Add(command);
_numbers.Add(number);
_numberStrings.Clear();
}
break;
case "=":
{
if (_numberStrings.Count > 0)
_numbers.Add(double.Parse(string.Concat(_numberStrings)));
CalculateProcess();
}
break;
}
}
void ButEditBatchDataFile_Click(object sender, EventArgs e)
{
var but = (Button) sender;
var targetFile = but.Tag.ToString();
if (targetFile == "BatchResult.txt")
{
var numberArray0 = GetNumberArrayFromFile(string.Concat(_startupPath, "第一组数.txt"));
var numberArray1 = GetNumberArrayFromFile(string.Concat(_startupPath, "第二组数.txt"));
try
{
var batchAddResultArray = Counter.BatchAdd(numberArray0, numberArray1);
var sw = new StreamWriter(targetFile, false);
foreach (var number in batchAddResultArray)
sw.WriteLine(number);
sw.Close();
}
catch (Exception)
{
MessageBox.Show(InvalidNumber, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
var filePath = string.Concat(_startupPath, targetFile);
if (!File.Exists(filePath))
File.CreateText(filePath);
Process.Start("notepad.exe", string.Concat(_startupPath, but.Tag.ToString()));
}
#endregion
#region 私有方法
private void CalculateProcess()
{
if (_numbers.Count == 0) return;
var number = _numbers[0];
for (var i = 0; i < _operates.Count; i++)
{
var nextOperateNumber = _numbers[i + 1];
try
{
switch (_operates[i])
{
case "+":
number = Counter.Add(number, nextOperateNumber);
break;
case "-":
number = Counter.Dec(number, nextOperateNumber);
break;
case "*":
number = Counter.Multiply(number, nextOperateNumber);
break;
case "/":
number = Counter.Divide(number, nextOperateNumber);
break;
}
}
catch
{
MessageBox.Show(InvalidNumber, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
}
TbResult.Text = number.ToString(CultureInfo.InvariantCulture);
ClearInnerData();
foreach (var c in number.ToString(CultureInfo.InvariantCulture))
_numberStrings.Add(c.ToString(CultureInfo.InvariantCulture));
_calculateCompleted = true;
}
private void ClearInnerData()
{
_numberStrings.Clear();
_numbers.Clear();
_operates.Clear();
}
private void Init()
{
_startupPath = string.Concat(Application.StartupPath, Path.DirectorySeparatorChar);
_numberStrings = new List<string>();
_numbers = new List<double>();
_operates = new List<string>();
foreach (Button but in GbBatchData.Controls)
but.Click += ButEditBatchDataFile_Click;
foreach (var control in Controls)
{
if (!(control is Button)) continue;
var but = (Button) control;
but.Click += ButCalculator_Click;
}
}
#endregion
#region 静态私有方法
private static List<double> GetNumberArrayFromFile(string filePath)
{
var result = new List<double>();
var sr = new StreamReader(filePath, Encoding.Default);
while (true)
{
var line = sr.ReadLine();
if (string.IsNullOrWhiteSpace(line)) break;
double Number;
if (!double.TryParse(line.Trim(' '), out Number))
continue;
result.Add(Number);
}
return result;
}
#endregion
}
}
//FormMain.Designer.cs
namespace BatchCalculator
{
partial class FormMain
{
private components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.GbBatchData = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.TbResult = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.GbBatchData.SuspendLayout();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(6, 22);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(131, 23);
this.button1.TabIndex = 0;
this.button1.Tag = "第一组数.txt";
this.button1.Text = "编辑批量文件一(&A)";
this.button1.UseVisualStyleBackColor = true;
this.button2.Location = new System.Drawing.Point(6, 51);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(131, 23);
this.button2.TabIndex = 1;
this.button2.Tag = "第二组数.txt";
this.button2.Text = "编辑批量文件二(&B)";
this.button2.UseVisualStyleBackColor = true;
this.GbBatchData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.GbBatchData.Controls.Add(this.button3);
this.GbBatchData.Controls.Add(this.button1);
this.GbBatchData.Controls.Add(this.button2);
this.GbBatchData.Location = new System.Drawing.Point(12, 65);
this.GbBatchData.Name = "GbBatchData";
this.GbBatchData.Size = new System.Drawing.Size(143, 107);
this.GbBatchData.TabIndex = 2;
this.GbBatchData.TabStop = false;
this.GbBatchData.Text = "批数据文件";
this.button3.Location = new System.Drawing.Point(6, 79);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(131, 23);
this.button3.TabIndex = 2;
this.button3.Tag = "BatchResult.txt";
this.button3.Text = "批量加运算结果(&C)";
this.button3.UseVisualStyleBackColor = true;
this.TbResult.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.TbResult.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.TbResult.Location = new System.Drawing.Point(18, 12);
this.TbResult.Name = "TbResult";
this.TbResult.ReadOnly = true;
this.TbResult.Size = new System.Drawing.Size(365, 30);
this.TbResult.TabIndex = 3;
this.TbResult.Text = "0";
this.TbResult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.button4.Location = new System.Drawing.Point(169, 58);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(49, 23);
this.button4.TabIndex = 4;
this.button4.Tag = "";
this.button4.Text = "7";
this.button4.UseVisualStyleBackColor = true;
this.button5.Location = new System.Drawing.Point(224, 58);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(49, 23);
this.button5.TabIndex = 5;
this.button5.Text = "8";
this.button5.UseVisualStyleBackColor = true;
this.button6.Location = new System.Drawing.Point(279, 58);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(49, 23);
this.button6.TabIndex = 6;
this.button6.Text = "9";
this.button6.UseVisualStyleBackColor = true;
this.button7.Location = new System.Drawing.Point(334, 58);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(49, 23);
this.button7.TabIndex = 7;
this.button7.Text = "Clear";
this.button7.UseVisualStyleBackColor = true;
this.button8.Location = new System.Drawing.Point(334, 87);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(49, 23);
this.button8.TabIndex = 11;
this.button8.Text = "*";
this.button8.UseVisualStyleBackColor = true;
this.button9.Location = new System.Drawing.Point(279, 87);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(49, 23);
this.button9.TabIndex = 10;
this.button9.Text = "6";
this.button9.UseVisualStyleBackColor = true;
this.button10.Location = new System.Drawing.Point(224, 87);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(49, 23);
this.button10.TabIndex = 9;
this.button10.Text = "5";
this.button10.UseVisualStyleBackColor = true;
this.button11.Location = new System.Drawing.Point(169, 87);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(49, 23);
this.button11.TabIndex = 8;
this.button11.Text = "4";
this.button11.UseVisualStyleBackColor = true;
this.button12.Location = new System.Drawing.Point(334, 116);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(49, 23);
this.button12.TabIndex = 15;
this.button12.Text = "/";
this.button12.UseVisualStyleBackColor = true;
this.button13.Location = new System.Drawing.Point(279, 116);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(49, 23);
this.button13.TabIndex = 14;
this.button13.Text = "3";
this.button13.UseVisualStyleBackColor = true;
this.button14.Location = new System.Drawing.Point(224, 116);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(49, 23);
this.button14.TabIndex = 13;
this.button14.Text = "2";
this.button14.UseVisualStyleBackColor = true;
this.button15.Location = new System.Drawing.Point(169, 116);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(49, 23);
this.button15.TabIndex = 12;
this.button15.Text = "1";
this.button15.UseVisualStyleBackColor = true;
this.button16.Location = new System.Drawing.Point(334, 144);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(49, 23);
this.button16.TabIndex = 19;
this.button16.Text = "=";
this.button16.UseVisualStyleBackColor = true;
this.button17.Location = new System.Drawing.Point(279, 144);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(49, 23);
this.button17.TabIndex = 18;
this.button17.Text = "-";
this.button17.UseVisualStyleBackColor = true;
this.button18.Location = new System.Drawing.Point(224, 144);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(49, 23);
this.button18.TabIndex = 17;
this.button18.Text = "+";
this.button18.UseVisualStyleBackColor = true;
this.button19.Location = new System.Drawing.Point(169, 144);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(49, 23);
this.button19.TabIndex = 16;
this.button19.Text = "0";
this.button19.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(395, 182);
this.Controls.Add(this.button16);
this.Controls.Add(this.button17);
this.Controls.Add(this.button18);
this.Controls.Add(this.button19);
this.Controls.Add(this.button12);
this.Controls.Add(this.button13);
this.Controls.Add(this.button14);
this.Controls.Add(this.button15);
this.Controls.Add(this.button8);
this.Controls.Add(this.button9);
this.Controls.Add(this.button10);
this.Controls.Add(this.button11);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.TbResult);
this.Controls.Add(this.GbBatchData);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FormMain";
this.ShowIcon = false;
this.Text = "Batch Calculator by ";
this.GbBatchData.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.GroupBox GbBatchData;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox TbResult;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
}
}
[
本帖最后由 mmxo 于 2012-11-9 17:22 编辑 ]