namespace Matrix
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.textBoxMatrixB = new System.Windows.Forms.TextBox();
this.textBoxMatrixA = new System.Windows.Forms.TextBox();
this.groupBoxA = new System.Windows.Forms.GroupBox();
this.groupBoxB = new System.Windows.Forms.GroupBox();
this.groupBoxResult = new System.Windows.Forms.GroupBox();
this.textBoxResult = new System.Windows.Forms.TextBox();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButtonAdd = new System.Windows.Forms.ToolStripButton();
this.toolStripButtonSubtract = new System.Windows.Forms.ToolStripButton();
this.toolStripButtonMultiply = new System.Windows.Forms.ToolStripButton();
this.groupBoxA.SuspendLayout();
this.groupBoxB.SuspendLayout();
this.groupBoxResult.SuspendLayout();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// textBoxMatrixB
//
this.textBoxMatrixB.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBoxMatrixB.Location = new System.Drawing.Point(10, 23);
this.textBoxMatrixB.Multiline = true;
this.textBoxMatrixB.Name = "textBoxMatrixB";
this.textBoxMatrixB.Size = new System.Drawing.Size(180, 167);
this.textBoxMatrixB.TabIndex = 0;
//
// textBoxMatrixA
//
this.textBoxMatrixA.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBoxMatrixA.Location = new System.Drawing.Point(10, 23);
this.textBoxMatrixA.Multiline = true;
this.textBoxMatrixA.Name = "textBoxMatrixA";
this.textBoxMatrixA.Size = new System.Drawing.Size(180, 167);
this.textBoxMatrixA.TabIndex = 0;
//
// groupBoxA
//
this.groupBoxA.Controls.Add(this.textBoxMatrixA);
this.groupBoxA.Location = new System.Drawing.Point(13, 8);
this.groupBoxA.Name = "groupBoxA";
this.groupBoxA.Padding = new System.Windows.Forms.Padding(10);
this.groupBoxA.Size = new System.Drawing.Size(200, 200);
this.groupBoxA.TabIndex = 1;
this.groupBoxA.TabStop = false;
this.groupBoxA.Text = "Matrix A:";
//
// groupBoxB
//
this.groupBoxB.Controls.Add(this.textBoxMatrixB);
this.groupBoxB.Location = new System.Drawing.Point(13, 214);
this.groupBoxB.Name = "groupBoxB";
this.groupBoxB.Padding = new System.Windows.Forms.Padding(10);
this.groupBoxB.Size = new System.Drawing.Size(200, 200);
this.groupBoxB.TabIndex = 2;
this.groupBoxB.TabStop = false;
this.groupBoxB.Text = "Matrix B:";
//
// groupBoxResult
//
this.groupBoxResult.Controls.Add(this.textBoxResult);
this.groupBoxResult.Controls.Add(this.toolStrip1);
this.groupBoxResult.Location = new System.Drawing.Point(219, 8);
this.groupBoxResult.Name = "groupBoxResult";
this.groupBoxResult.Padding = new System.Windows.Forms.Padding(10);
this.groupBoxResult.Size = new System.Drawing.Size(372, 406);
this.groupBoxResult.TabIndex = 3;
this.groupBoxResult.TabStop = false;
this.groupBoxResult.Text = "Result:";
//
// textBoxResult
//
this.textBoxResult.Dock = System.Windows.Forms.DockStyle.Top;
this.textBoxResult.Location = new System.Drawing.Point(10, 50);
this.textBoxResult.Multiline = true;
this.textBoxResult.Name = "textBoxResult";
this.textBoxResult.ReadOnly = true;
this.textBoxResult.Size = new System.Drawing.Size(352, 345);
this.textBoxResult.TabIndex = 1;
//
// toolStrip1
//
this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButtonAdd,
this.toolStripButtonSubtract,
this.toolStripButtonMultiply});
this.toolStrip1.Location = new System.Drawing.Point(10, 23);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(352, 27);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButtonAdd
//
this.toolStripButtonAdd.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonAdd.Image")));
this.toolStripButtonAdd.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.toolStripButtonAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButtonAdd.Name = "toolStripButtonAdd";
this.toolStripButtonAdd.Size = new System.Drawing.Size(53, 24);
this.toolStripButtonAdd.Text = "Add";
this.toolStripButtonAdd.ToolTipText = "Add";
this.toolStripButtonAdd.Click += new System.EventHandler(this.toolStripButtonAdd_Click);
//
// toolStripButtonSubtract
//
this.toolStripButtonSubtract.Image = global::Matrix.Properties.Resources.SubtractIcon;
this.toolStripButtonSubtract.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.toolStripButtonSubtract.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButtonSubtract.Name = "toolStripButtonSubtract";
this.toolStripButtonSubtract.Size = new System.Drawing.Size(75, 24);
this.toolStripButtonSubtract.Text = "Subtract";
this.toolStripButtonSubtract.ToolTipText = "Subtract";
this.toolStripButtonSubtract.Click += new System.EventHandler(this.toolStripButtonSubtract_Click);
//
// toolStripButtonMultiply
//
this.toolStripButtonMultiply.Image = global::Matrix.Properties.Resources.MultiplyIcon;
this.toolStripButtonMultiply.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.toolStripButtonMultiply.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButtonMultiply.Name = "toolStripButtonMultiply";
this.toolStripButtonMultiply.Size = new System.Drawing.Size(75, 24);
this.toolStripButtonMultiply.Text = "Multiply";
this.toolStripButtonMultiply.ToolTipText = "Multiply";
this.toolStripButtonMultiply.Click += new System.EventHandler(this.toolStripButtonMultiply_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(603, 427);
this.Controls.Add(this.groupBoxResult);
this.Controls.Add(this.groupBoxB);
this.Controls.Add(this.groupBoxA);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Padding = new System.Windows.Forms.Padding(10, 5, 10, 5);
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Matrix";
this.groupBoxA.ResumeLayout(false);
this.groupBoxA.PerformLayout();
this.groupBoxB.ResumeLayout(false);
this.groupBoxB.PerformLayout();
this.groupBoxResult.ResumeLayout(false);
this.groupBoxResult.PerformLayout();
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TextBox textBoxMatrixB;
private System.Windows.Forms.TextBox textBoxMatrixA;
private System.Windows.Forms.GroupBox groupBoxA;
private System.Windows.Forms.GroupBox groupBoxB;
private System.Windows.Forms.GroupBox groupBoxResult;
private System.Windows.Forms.TextBox textBoxResult;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButtonAdd;
private System.Windows.Forms.ToolStripButton toolStripButtonSubtract;
private System.Windows.Forms.ToolStripButton toolStripButtonMultiply;
}
}
Form1.cs
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;
namespace Matrix
{
public partial class Form1 : Form
{
private enum MatrixStatusCode
{
OK,
Empty,
NotEqual
}
public Form1()
{
InitializeComponent();
}
private void toolStripButtonAdd_Click(object sender, EventArgs e)
{
object[,] matrixA = textBoxMatrixA.Text.ToMatrix();
object[,] matrixB = textBoxMatrixB.Text.ToMatrix();
MatrixStatusCode status = CheckMatrix(matrixA, matrixB);
textBoxResult.Text = "Matrix A + Matrix B" + Environment.NewLine + Environment.NewLine + DisplayStatusCode(status);
if (status == MatrixStatusCode.OK)
{
double[,] matrixResult = new double[matrixA.GetLength(0), matrixA.GetLength(1)];
for (int i = 0; i < matrixA.GetLength(0); i++)
{
for (int j = 0; j < matrixA.GetLength(1); j++)
{
matrixResult[i, j] = Convert.ToDouble(matrixA[i, j]) + Convert.ToDouble(matrixB[i, j]);
}
}
textBoxResult.Text = "Matrix A + Matrix B" + Environment.NewLine + Environment.NewLine + DisplayResult(matrixResult);
}
}
private void toolStripButtonSubtract_Click(object sender, EventArgs e)
{
object[,] matrixA = textBoxMatrixA.Text.ToMatrix();
object[,] matrixB = textBoxMatrixB.Text.ToMatrix();
MatrixStatusCode status = CheckMatrix(matrixA, matrixB);
textBoxResult.Text = "Matrix A - Matrix B" + Environment.NewLine + Environment.NewLine + DisplayStatusCode(status);
if (status == MatrixStatusCode.OK)
{
double[,] matrixResult = new double[matrixA.GetLength(0), matrixA.GetLength(1)];
for (int i = 0; i < matrixA.GetLength(0); i++)
{
for (int j = 0; j < matrixA.GetLength(1); j++)
{
matrixResult[i, j] = Convert.ToDouble(matrixA[i, j]) - Convert.ToDouble(matrixB[i, j]);
}
}
textBoxResult.Text = "Matrix A - Matrix B" + Environment.NewLine + Environment.NewLine + DisplayResult(matrixResult);
}
}
private void toolStripButtonMultiply_Click(object sender, EventArgs e)
{
object[,] matrixA = textBoxMatrixA.Text.ToMatrix();
object[,] matrixB = textBoxMatrixB.Text.ToMatrix();
MatrixStatusCode status = CheckMatrixForMultiply(matrixA, matrixB);
textBoxResult.Text = "Matrix A x Matrix B" + Environment.NewLine + Environment.NewLine + DisplayStatusCode(status);
if (status == MatrixStatusCode.OK)
{
double[,] matrixResult = new double[matrixA.GetLength(0), matrixB.GetLength(1)];
for (int i = 0; i < matrixA.GetLength(0); i++)
{
for (int j = 0; j < matrixB.GetLength(1); j++)
{
for (int k = 0; k < matrixA.GetLength(1); k++)
matrixResult[i, j] += Convert.ToDouble(matrixA[i, k]) * Convert.ToDouble(matrixB[k, j]);
}
}
textBoxResult.Text = "Matrix A x Matrix B" + Environment.NewLine + Environment.NewLine + DisplayResult(matrixResult);
}
}
private MatrixStatusCode CheckMatrix(object[,] matrixA, object[,] matrixB)
{
MatrixStatusCode result;
if (matrixA != null && matrixB != null)
{
if (matrixA.GetLength(0) == matrixB.GetLength(0) && matrixA.GetLength(1) == matrixB.GetLength(1))
{
result = MatrixStatusCode.OK;
}
else
{
result = MatrixStatusCode.NotEqual;
}
}
else
{
result = MatrixStatusCode.Empty;
}
return result;
}
private MatrixStatusCode CheckMatrixForMultiply(object[,] matrixA, object[,] matrixB)
{
MatrixStatusCode result;
if (matrixA != null && matrixB != null)
{
if (matrixA.GetLength(0) == matrixB.GetLength(1) && matrixA.GetLength(1) == matrixB.GetLength(0))
{
result = MatrixStatusCode.OK;
}
else
{
result = MatrixStatusCode.NotEqual;
}
}
else
{
result = MatrixStatusCode.Empty;
}
return result;
}
private string DisplayStatusCode(MatrixStatusCode status)
{
string result;
switch (status)
{
default:
result = string.Empty;
break;
case MatrixStatusCode.Empty:
result = "ERROR: Some matrix not has data.";
break;
case MatrixStatusCode.NotEqual:
result = "ERROR: They're not the same size.";
break;
}
return result;
}
private string DisplayResult(double[,] metrix)
{
string result = string.Empty;
for (int i = 0; i < metrix.GetLength(0); i++)
{
for (int j = 0; j < metrix.GetLength(1); j++)
{
result += string.Format("{0, 5}", metrix[i, j].ToString());
if (j < metrix.GetLength(1) - 1)
result += ", ";
}
result += Environment.NewLine;
}
return result;
}
}
}
ExtensionMethods.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Matrix
{
public static class ExtensionMethods
{
public static object[,] ToMatrix(this string str)
{
object[,] metrix = null;
if (str != string.Empty)
{
string[] rs = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string[] rows = (from r in rs where r != string.Empty select r).ToArray();
int x = (from r in rows select r.Split((" ,:|/").ToCharArray()).Length).Max();
int y = rows.Length;
metrix = new object[y, x];
for (int i = 0; i < rows.Length; i++)
{
string[] data = rows[i].Split(',');
for (int j = 0; j < data.Length; j++)
{
metrix[i, j] = data[j];
}
}
}
return metrix;
}
}
}