Chuong trinh sap xep tren mang
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication5
{
public partial class Form1 : Form
{
public int[] a;
public int spt = 0;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (button3.Enabled == false)
button3.Enabled = true;
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
bool ok;
ok = int.TryParse(textBox1.Text, out gt);
if (!ok)
{
MessageBox.Show("gia tri phai la so nguyen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return;
}
// them phan tu
spt++;
Array.Resize<int>(ref a, spt);
a[spt - 1] = gt;
button2.Enabled = true;
button1.Enabled = true;
// vo hieu hoa cac nut neu chua chon dieu kien
if (radioButton1.Checked == false && radioButton2.Checked == false)
button3.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
// xoa du kieu cu
textBox2.Clear();
// in mang
for (int i = 0; i < spt; i++)
textBox2.Text = textBox2.Text + a[i].ToString() + "";
}
private void button3_Click(object sender, EventArgs e)
{
// sap xep gian dan
if (radioButton2.Checked == true)
{
for(int i=0;i<=a.Length-2;i++)
for(int j=i+1; j<=a.Length-1;j++)
if (a[i] < a[j])
{
tg = a[i];
a[i] = a[j];
a[j] = tg;
}
}
// sap xep tang dan
if (radioButton1.Checked == true)
{
for(int i=0;i<=a.Length-2;i++)
for(int j=i+1;j<=a.Length-1;j++)
if (a[i] > a[j])
{
tg = a[i];
a[i] = a[j];
a[j] = tg;
}
}
// in mang da sap xep
textBox3.Clear();
for(int i=0;i<=a.Length-1;i++)
{
textBox3.Text=textBox3.Text+a[i].ToString()+"";
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// cho phep nut sap xep hoat dong khi chon nut sap xep
if (button3.Enabled == false)
button3.Enabled = true;
}
}
}
Xây dựng chương trình thêm bớt phần tử mảng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
// khai bao cac bien dung chung
public int[] a;
public int spt;
public Form1()
{
InitializeComponent();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
button1.Enabled = true;
button2.Enabled = true;
}
private void label6_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// kiem tra tinh hop le khi nhap du lieu
bool ok;
ok = int.TryParse(textBox1.Text, out gt);
if (!ok)
{
MessageBox.Show("gia tri phai la so nguyen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return;
}
// them phan tu
spt++;
Array.Resize<int>(ref a, spt);
a[spt - 1] = gt;
// cho phep cac doi tuong hoat dong
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Clear();
for (int i = 0; i < spt; i++)
textBox2.Text = textBox2.Text + a[i].ToString() + "";
}
private void button3_Click(object sender, EventArgs e)
{
// kiem tra hop le vi tri khi xoa
bool ok;
int vt;
ok = int.TryParse(textBox3.Text, out vt);
if (!ok)
{
MessageBox.Show("phai nhap vi tri xoa", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox3.Focus();
return;
}
if (vt < 0 || vt > spt)
{
MessageBox.Show("vi tri phai tu 1 den" + spt.ToString(),
"thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox4.Focus();
return;
}
// xoa phan tu
if (vt == spt)
{
spt--;
Array.Resize<int>(ref a, spt);
}
else
{
for (int i = vt - 1; i < spt - 1; i++)
a[i] = a[i + 1];
spt--;
Array.Resize<int>(ref a, spt);
}
// xoa noi dung cac doi tuong khi da xoa het mang
if (spt == 0)
{
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox2.Clear();
textBox1.Clear();
return;
}
// in mang da xoa
textBox4.Clear();
for (int i = 0; i < spt; i++)
textBox4.Text = textBox4.Text + a[i].ToString() + "";
}
private void button4_Click(object sender, EventArgs e)
{
// kiem tra hop le vi tri chen
bool ok;
int vt;
ok = int.TryParse(textBox5.Text, out vt);
if (!ok)
{
MessageBox.Show("phai nhap vi tri chen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox5.Focus();
return;
}
if (vt < 0 || vt > spt)
{
MessageBox.Show("vi tri chen phai tu 1 den" + spt.ToString(), "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox5.Focus();
return;
}
//kiem tra tinh hop le gia tri chen
ok = int.TryParse(textBox6.Text, out gt);
if (!ok)
{
MessageBox.Show("phai nhap gia tri chen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox6.Focus();
return;
}
// chen phan tu
spt++;
Array.Resize<int>(ref a, spt);
for (int i = spt - 1; i > vt - 1; i--)
{
a[i] = a[i - 1];
}
a[vt - 1] = gt;
// in mang da chen
textBox7.Clear();
for (int i = 0; i < spt; i++)
textBox7.Text = textBox7.Text + a[i].ToString() + "";
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication7
{
public partial class Form1 : Form
{
public int[] a;
public int spt = 0;
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
bool ok;
ok = int.TryParse(textBox1.Text, out gt);
if (!ok)
{
MessageBox.Show("gia tri nhap phai la so nguyen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return;
}
// them phan tu vao mang
spt++;
Array.Resize<int>(ref a, spt);
a[spt - 1] = gt;
button2.Enabled = true;
groupBox2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Clear();
for (int i = 0; i < spt; i++)
textBox2.Text = textBox2.Text + a[i].ToString() + "";
}
private void button3_Click(object sender, EventArgs e)
{
int dem;
// tinh tong gia tri cac phan tu
tong = 0;
for (int i = 0; i < spt; i++)
{
tong += a[i];
}
textBox3.Text = tong.ToString();
// tinh tong so chan
tong = 0;
for (int i = 0; i < spt; i++)
{
if (a[i] % 2 == 0 && a[i] != 0)
tong += a[i];
}
textBox4.Text = tong.ToString();
// tinh tong so le
tong = 0;
for (int i = 0; i < spt; i++)
{
if (a[i] % 2 != 0)
tong += a[i];
}
textBox5.Text = tong.ToString();
//tinh trung binh cong so le
tong = 0;
dem = 0;
for (int i = 0; i < spt - 1; i++)
{
if (a[i] % 2 != 0)
{
tong += a[i];
dem++;
}
}
if (dem != 0)
textBox7.Text = (tong / dem).ToString();
else
textBox7.Text = "khong co so le";
// tinh trung binh cong so chan
tong = 0;
dem = 0;
for (int i = 0; i < spt; i++)
{
if (a[i] % 2 == 0 && a[i] != 0)
{
tong += a[i];
dem++;
}
}
if (dem != 0)
textBox6.Text = (tong / dem).ToString();
else
textBox6.Text = "khong co so chan";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
public int[] a;
public int spt=0;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
bool ok;
ok = int.TryParse(textBox1.Text, out gt);
if (!ok)
{
MessageBox.Show("gia tri nhap phai la so nguyen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return;
}
// them phan tu vao mang
spt++;
Array.Resize<int>(ref a, spt);
a[spt - 1] = gt;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Clear();
// in mang
for (int i = 0; i < spt; i++)
textBox2.Text = textBox2.Text + a[i].ToString() + "";
}
}
}
Xây dựng chương trình tìm kiếm trên mảng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication8
{
public partial class Form1 : Form
{
public int[] a;
public int spt = 0;
public Form1()
{
InitializeComponent();
comboBox1.Items.Add("=");
comboBox1.Items.Add("<");
comboBox1.Items.Add("<=");
comboBox1.Items.Add(">");
comboBox1.Items.Add(">=");
comboBox1.Items.Add("!=");
comboBox1.Items.Add("Min");
comboBox1.Items.Add("Max");
comboBox1.Items.Add("[]");
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
bool ok;
ok = int.TryParse(textBox1.Text, out gt);
if (!ok)
{
MessageBox.Show("gia tri nhap phai la so nguyen", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return;
}
// them phan tu vao amng
spt++;
Array.Resize<int>(ref a, spt);
a[spt - 1] = gt;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Clear();
// in phan tu
for (int i = 0; i < spt; i++)
textBox2.Text = textBox2.Text + a[i].ToString() + "";
}
private void button3_Click(object sender, EventArgs e)
{
bool ok;
// kiem tra hop le dieu kien tim kiem
if (comboBox1.Text.Trim().Length == 0)
{
MessageBox.Show("phai nhap dieu kien tim kiem", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
comboBox1.Focus();
return;
}
// kiem tra hop le gia tri tim kiem
ok = int.TryParse(textBox3.Text, out gt);
if (!ok)
{
MessageBox.Show("phai nhap gia tri tim kiem", "thong bao",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox3.Focus();
return;
}
// in ket qua
textBox4.Clear();
switch (comboBox1.SelectedIndex)
{
case 0:// tim voi dien kien =
for (int i = 0; i < a.Length; i++)
{
if (a[i] == int.Parse(textBox3.Text))
textBox4.Text = textBox4.Text + a[i] + "";
}
break;
case 1:// tim voi dieu kien <
for (int i = 0; i < a.Length; i++)
{
if (a[i] < int.Parse(textBox3.Text))
textBox4.Text = textBox4.Text + a[i] + "";
}
break;
case 2:// tim voi <=
for (int i = 0; i < a.Length; i++)
{
if (a[i] <= int.Parse(textBox3.Text))
textBox4.Text = textBox4.Text + a[i] + "";
}
break;
case 3:// tim voi >
for (int i = 0; i < a.Length; i++)
{
if (a[i] > int.Parse(textBox3.Text))
textBox4.Text = textBox4.Text + a[i] + "";
}
break;
case 4:// tim voi >=
for(int i=0;i<a.Length;i++)
{
if(a[i]>=int.Parse(textBox3.Text))
textBox4.Text=textBox4.Text+a[i]+"";
}
break;
case 5:// tim voi !=
for (int i = 0; i < a.Length; i++)
{
if (a[i] != int.Parse(textBox3.Text))
textBox4.Text = textBox4.Text + a[i] + "";
}
break;
case 6: // tim voi gia tri nho nhat
int min=a[0];
for (int i = 0; i < a.Length; i++)
{
if (a[i] < min)
min = a[i];
}
textBox4.Text = min.ToString();
break;
case 7: // tim voi gia tri lon nhat
int max = a[0];
for (int i = 0; i < a.Length; i++)
{
if (a[i] > max)
max = a[i];
}
textBox4.Text = max.ToString();
break;
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
}
}
Bạn đang đọc truyện trên: AzTruyen.Top