You are on page 1of 3

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 MaHoaHoanVi
{
public partial class Form1 : Form
{
int cot = 11;
public Form1()
{
InitializeComponent();
}

private void btMaHoa_Click(object sender, EventArgs e)


{
//kiem tra du lieu dau vao
if (tbMaHoa.Text.Length == 0)
{
MessageBox.Show("Ban phai nhap du lieu can ma hoa");
tbMaHoa.Focus();
return;
}

//chuyen du lieu vao ma tran co so cot duoc quy dinh


int hang = (int)Math.Ceiling((Decimal)tbMaHoa.Text.Length /cot);
char[,] maTran = new char[cot, hang];
for (int h = 0; h < hang; h++)
for (int c = 0; c < cot; c++)
{
if (h * cot + c < tbMaHoa.Text.Length)
maTran[c, h] = tbMaHoa.Text[h * cot + c];
else
maTran[c, h] = '~';
}
//dao hoan vi cac cot
int[] cesar = new int[cot];
for(int i=0;i<cot;i++)
{
cesar[i] = i;
}
int[] cesarMH = new int[cot];
int hsk = int.Parse(tbHeSo.Text);
for (int i = 0; i < cot; i++)
cesarMH[i] = cesar[(i + hsk) % cot];
//xuat du lieu the tung cot va hien thi
string ketQua = "";
for (int i = 0; i < cot; i++)
for(int h=0;h<hang;h++)
{
if (! String.IsNullOrEmpty(maTran[cesarMH[i], h].ToString()))
{
ketQua += maTran[cesarMH[i], h];
}

}
tbKQMaHoa.Text = ketQua;

private void btGiaiMa_Click(object sender, EventArgs e)


{
//kiem tra giu lieu
if (tbKQMaHoa.Text.Length == 0)
{
MessageBox.Show("Ban phai nhap du lieu can ma hoa");
tbMaHoa.Focus();
return;
}
//chuyendu lieu can giai ma vao ma tran theo cot

int hang = (int)Math.Ceiling((Decimal)tbKQMaHoa.Text.Length / cot);


char[,] maTran = new char[cot, hang];
for (int c = 0; c < cot; c++)
for (int h = 0; h < hang; h++)
{
if (h * cot + c < tbKQMaHoa.Text.Length)
maTran[c, h] = tbKQMaHoa.Text[c*hang + h];
else
maTran[c, h] = '~';
}
//dao hoan vi cac cot
int[] cesar = new int[cot];
for (int i = 0; i < cot; i++)
{
cesar[i] = i;
}
int[] cesarGM = new int[cot];
int hsk = int.Parse(tbHeSo.Text);
for (int i = 0; i < cot; i++)
{
if (i - hsk >= 0)
cesarGM[i] = cesar[(i - hsk) % cot];
else
cesarGM[i] = cesar[(i + cot - hsk) % cot];
}
//doc ket qua theo hang va hien thi
string ketQua = "";
for (int h = 0; h <hang; h++)
for (int i = 0; i < cot; i++)
{
if (!String.IsNullOrEmpty(maTran[cesarGM[i], h].ToString()))
{
ketQua += maTran[cesarGM[i], h];
}

}
tbKetQuaGM.Text = ketQua.Replace("~", "");
}
}
}

You might also like