C#的winform矩阵简单运算


程序截图



关键代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace MatrixMultiplication {
public partial class MainForm : Form {
public MainForm() {
InitializeComponent(); } int[,] A = new int[100,100];
int[,] B = new int[100,100];
int[,] AB = new int[100,100];
int[,] C = new int[100,100];
int[,] D = new int[100,100];
int A_row = 0,A_col = 0;
int B_row = 0,B_col = 0;
int C_row = 0,C_col = 0;
int D_row = 0,D_col = 0;
int AB_row = 0,AB_col = 0; public void readMatrix(TextBox other,char towhere){
if(towhere == 'A') {
string[] arr = new string[other.Lines.Length];
for(int i = 0; i < other.Lines.Length; i++) {
arr[i] = other.Lines[i];
}
string[] tem = arr[0].Split();
A_col = tem.Length;
A_row = other.Lines.Length;
for(int i = 0; i < A_row; i++) {
string[] temp = arr[i].Split();
for(int j = 0; j < A_col; j++) {
A[i,j] = Convert.ToInt32(temp[j]);
}
}
}
else if(towhere == 'B') {
string[] arr = new string[other.Lines.Length];
for(int i = 0; i < other.Lines.Length; i++) {
arr[i] = other.Lines[i];
}
string[] tem = arr[0].Split();
B_col = tem.Length;
B_row = other.Lines.Length;
for(int i = 0; i < B_row; i++) {
string[] temp = arr[i].Split();
for(int j = 0; j < B_col; j++) {
B[i,j] = Convert.ToInt32(temp[j]);
}
}
}
else if(towhere == 'C') {
string[] arr = new string[other.Lines.Length];
for(int i = 0; i < other.Lines.Length; i++) {
arr[i] = other.Lines[i];
}
string[] tem = arr[0].Split();
C_col = tem.Length;
C_row = other.Lines.Length;
for(int i = 0; i < C_row; i++) {
string[] temp = arr[i].Split();
for(int j = 0; j < C_col; j++) {
C[i,j] = Convert.ToInt32(temp[j]);
}
}
}
} public void compute(char Char) {
if(Char == '*') {
AB_row = A_row;
AB_col = B_col;
for(int i = 0; i < AB_row; i++) {
for(int j = 0; j < AB_col; j++) {
AB[i,j] = 0;
for(int k = 0; k < A_col; k++) {
AB[i,j] += A[i,k] * B[k,j];
}
}
}
}
else if(Char == '+') {
AB_row = A_row;
AB_col = A_col;
for(int i = 0; i < AB_row; i++) {
for(int j = 0; j < AB_col; j++) {
AB[i,j] = A[i,j] + B[i,j];
}
} }
else if(Char == '-') {
AB_row = A_row;
AB_col = A_col;
for(int i = 0; i < AB_row; i++) {
for(int j = 0; j < AB_col; j++) {
AB[i,j] = A[i,j] - B[i,j];
}
} }
} private void button1_Click(object sender,EventArgs e) {
if(BoxA.Text == "" || BoxB.Text == "") {
MessageBox.Show("请输入数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
readMatrix(BoxA,'A');
readMatrix(BoxB,'B');
if((A_row != B_col) || (A_col != B_row)) {
MessageBox.Show("行列规则不符合","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
BoxAB.Text = "";
compute('*');
for(int i = 0; i < AB_row; i++) {
for(int j = 0; j < AB_col; j++) {
BoxAB.Text += AB[i,j].ToString();
BoxAB.Text += " ";
}
BoxAB.Text += "\r\n";
}
} private void btnClear_Click(object sender,EventArgs e) {
A_row = 0; A_col = 0;
B_row = 0; B_col = 0;
AB_row = 0; AB_col = 0;
BoxA.Text = "";
BoxB.Text = "";
BoxAB.Text = "";
} private void Add_Click(object sender,EventArgs e) {
if(BoxA.Text == "" || BoxB.Text == "") {
MessageBox.Show("请输入数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
readMatrix(BoxA,'A');
readMatrix(BoxB,'B');
if((A_row != B_row) || (A_col != B_col)) {
MessageBox.Show("行列规则不符合","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
BoxAB.Text = "";
compute('+');
for(int i = 0; i < AB_row; i++) {
for(int j = 0; j < AB_col; j++) {
BoxAB.Text += AB[i,j].ToString();
BoxAB.Text += " ";
}
BoxAB.Text += "\r\n";
}
} private void Minus_Click(object sender,EventArgs e) {
if(BoxA.Text == "" || BoxB.Text == "") {
MessageBox.Show("请输入数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
readMatrix(BoxA,'A');
readMatrix(BoxB,'B');
if((A_row != B_row) || (A_col != B_col)) {
MessageBox.Show("行列规则不符合","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
BoxAB.Text = "";
compute('-');
for(int i = 0; i < AB_row; i++) {
for(int j = 0; j < AB_col; j++) {
BoxAB.Text += AB[i,j].ToString();
BoxAB.Text += " ";
}
BoxAB.Text += "\r\n";
}
} private void Trspos_Click(object sender,EventArgs e) {
if(BoxC.Text == "") {
MessageBox.Show("请输入数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
readMatrix(BoxC,'C');
#region 转置算法
D_row = C_col;
D_col = C_row;
for(int i = 0; i < C_row; i++) {
for(int j = 0; j < C_col; j++) {
D[j,i] = C[i,j];
}
}
#endregion
BoxD.Text = "";
for(int i = 0; i < D_row; i++) {
for(int j = 0; j < D_col; j++) {
BoxD.Text += D[i,j].ToString();
BoxD.Text += " ";
}
BoxD.Text += "\r\n";
}
} private void MatrixReturn_Click(object sender,EventArgs e) {
if(BoxC.Text == "") {
MessageBox.Show("请输入数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
readMatrix(BoxC,'C');
if(C_row!=C_col){
MessageBox.Show("该矩阵没有逆矩阵","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
#region 创建double类型二维数组作为结果
double y = 1.0;
double tem,x;
double[,] Return = new double[C_row,C_col*2];
#endregion
#region 求逆算法
for(int i = 0; i < C_row; i++) {
for(int j = 0; j < (2 * C_col); j++) {
if(j < C_row)
Return[i,j] = Convert.ToDouble(C[i,j]);
else if(j == C_row + i)
Return[i,j] = 1.0;
else
Return[i,j] = 0.0; }
}
for(int i = 0; i < C_row; i++) {
for(int k = 0; k < C_row; k++) {
if(k != i) {
tem = Return[k,i] / Return[i,i];
for(int j = 0; j < (2 * C_row); j++) {
x = Return[i,j] * tem;
Return[k,j] -= x;
}
}
}
}
for(int i = 0; i < C_row; i++) {
tem = Return[i,i];
for(int j = 0; j < (2 * C_row); j++) {
Return[i,j] /= tem;
}
}
for(int i = 0; i < C_row; i++) {
y *= Return[i,i];
}
#endregion if(y == 0) {
MessageBox.Show("该矩阵没有逆矩阵","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
} BoxD.Text = "";
for(int i = 0; i < C_row; i++) {
for(int j = 0; j < C_row; j++) {
BoxD.Text += Return[i,j + C_row].ToString("f2");
BoxD.Text += " ";
}
BoxD.Text += "\r\n";
} } private void btnClear2_Click(object sender,EventArgs e) {
BoxC.Text = "";
BoxD.Text = "";
C_row = 0;C_col = 0;
D_row = 0;D_col = 0;
} }
}

完整工程

度盘下载

C#的winform矩阵简单运算的更多相关文章

  1. ACM 16进制的简单运算

    16进制的简单运算 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 现在给你一个16进制的加减法的表达式,要求用8进制输出表达式的结果.   输入 第一行输入一个正整 ...

  2. 1、面向对象以及winform的简单运用(开篇)

    面向对象概述: 要学习好面向对象,我们应该从三个问题入手: 1.什么是面向对象? 2.为什么要面向对象? 3.该怎么面向对象? 面向对象,首先要有一个对象,那么对象是什么呢? 对象的定义是人们要进行研 ...

  3. NYOJ--244--16进制的简单运算(C++控制输入输出)

    16进制的简单运算 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 现在给你一个16进制的加减法的表达式,要求用8进制输出表达式的结果.   输入 第一行输入一个正整 ...

  4. HDU 2276 Kiki & Little Kiki 2(矩阵位运算)

    Kiki & Little Kiki 2 转载自:点这里 [题目链接]Kiki & Little Kiki 2 [题目类型]矩阵位运算 &题意: 一排灯,开关状态已知,每过一秒 ...

  5. 斐波那契数列F(n)【n超大时的(矩阵加速运算) 模板】

    hihocoder #1143 : 骨牌覆盖问题·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个 ...

  6. Redis 的简单运算

    Redis 的简单运算 命令 说明 备注 incr key 在原字段上加 1 只能对整数操作 incrby key increment 在原字段上加上整数 (increment) 只能对整数操作 de ...

  7. nyoj 244-16进制的简单运算 (scanf("%x%c%x", &a, &b, &c); printf("%o", a ± b))

    244-16进制的简单运算 内存限制:64MB 时间限制:1000ms 特判: No 通过数:12 提交数:13 难度:1 题目描述: 现在给你一个16进制的加减法的表达式,要求用8进制输出表达式的结 ...

  8. Matlab矩阵学习三 矩阵的运算

    Matlab矩阵的运算 一.矩阵的加减 在matlab中,矩阵的加减和数的加减符号一样,都是"+"和”-“,不同的是两个进行运算的矩阵维度必须相同  二.数乘  三.乘法 矩阵乘法 ...

  9. 快速电路仿真器(FastSPICE)中的高性能矩阵向量运算实现

    今年10-11月份参加了EDA2020(第二届)集成电路EDA设计精英挑战赛,通过了初赛,并参加了总决赛,最后拿了一个三等奖,虽然成绩不是很好,但是想把自己做的分享一下,我所做的题目是概伦电子出的F题 ...

随机推荐

  1. CentOS下通过rdesktop连接Windows远程桌面

    众所周知,微软的Windows提供了一种远程桌面系统(Remote Desktop),该服务的默认端口是3389,可使用户远程登录进行系统管理或作为终端服务器运行各种应用软件. 而要连接Windows ...

  2. RedHat安装VMwareTools出现解压压缩包时无法打开文件的现象

    出现这种情况的原因是因为解压命令没有加—C参数,使用的命令为:tat -xvzf VMware Tools: 正确的解压命令应该是: tar -xvzf VMware Tools -C /opt,加上 ...

  3. C#正则表达式语法规则详解

    正则表达式通常包含字母文本(Literaltext)和元字符(metacharacter) 字母文本指的是普通文本如"abcde"可匹配字符串中任何包含"abcde&qu ...

  4. PHP 按二维数组的键值排序

    /** * 按二维数组的键值排序 * @param unknown $array 二维数组 * @param unknown $key 二维数组的键值 * @param string $order 升 ...

  5. 自定义Attribute 服务端校验 客户端校验

    MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...

  6. php的命名规范

    1.类 类名每一个单词首字母大写,如类名StudentCourse. 2.常量 常量名所有字母大写,单词间用下划线分隔,如常量名NULL.TRUE.FALSE.ROOT_PATH等. 3.变量 为了保 ...

  7. 在CentOS下安装WebBench进行web 性能测试

    Webbench是有名的网站压力测试工具 编译安装:1. wget http://www.sfr-fresh.com/unix/privat/webbench-1.5.tar.gz2. tar zxv ...

  8. DoNet屌丝学Android(一)——Android开发准备工作 & No HelloWord & (真机)调试

    先乱扯淡一下吧,本人一.net屌丝,手持Android 4.2.2手机,Win7 x64本本,闲来无聊学习一下Android的开发,至于要开发啥玩意目前没有什么想法,就是想学学,搞不好是三分热度也有可 ...

  9. mariadb主从复制架构学习笔记

    复制功用: 数据分布 负载均衡:读操作,适用于读密集型的应用 备份 高可用和故障切换 MySQL升级测试 在从服务器上有两个线程: I/O线程:从master请求二进制日志信息,并保存至中继日志 SQ ...

  10. spark streaming kafka1.4.1中的低阶api createDirectStream使用总结

    转载:http://blog.csdn.net/ligt0610/article/details/47311771 由于目前每天需要从kafka中消费20亿条左右的消息,集群压力有点大,会导致job不 ...