K3ACNTT

1.Mảng mới

Void nhapmang(int[] m)

{Random x = new Random();

for (int i = 0; i < m.Length; i++)

            {

m[i] = x.Next(1, 101);

            }

        }

void hienmang(int[] m, Label lbl)

        {

            lblhienthi.Text = "";

            for (int i = 0; i < m.Length; i++)

                lblhienthi.Text += Convert.ToString(m[i] + "  ");

        }

int[] ma;

private void btnmangmoi        {

try

  {

                int gt;

gt = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("moi nhap chieu dai mang", "thong bao", "15", 100, 300));

                ma = new int[gt];

                nhapmang(ma);

                hienmang(ma, lblhienthi);

            }

            catch

            {

                MessageBox.Show("ban chua nhap du lieu", "thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

            }

}

------------------

2.Mảng Đảo

 void daydao(int[] m, Label lbl)

        {

            int i = 0, j = m.Length - 1, t;

            do

            {

                t = m[i];

                m[i] = m[j];

                m[j] = t;

                i++;

                j--;

            }

            while (i < m.Length / 2);

            hienmang(ma, lblketqua);

        }

private void btnmangdao

        {

            daydao(ma, lblketqua);

        }

-----------------

3.Tìm kiếm

int tim(int[] m, int x)

        {

            for (int i = 0; i < m.Length; i++)

                if (m[i] == x)

                    return (i);

            return (-1);

        }

 private void btntimkiem        {

            try

            {

                int x = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("nhap gia tri can tim kiem", "tim kiem", "0", 100, 300));

                int vt = tim(ma, x);

                if (vt != -1)

                {

                    MessageBox.Show("tim thay" + " " + x + " tai vi tri" + " " + vt, "thong bao");

                }

                else

                {

                    MessageBox.Show("khong tim thay" + x + "tai vi tri" + vt, "thong bao");

                }

            }

            catch

            {

                MessageBox.Show("ban chua nhap gia tri can tim", "thong bao");

            }

        }

-----------------

4.xóa ptử mang

private void btnxoa

        {

            try

            {

                int x = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("nhap gia tri can xoa", "xoa", "0", 100, 300));

                int vt = tim(ma, x);

                if (vt == -1)

                    MessageBox.Show("khong thay gia tri nao de xoa", "thong bao");

                while (vt != -1)

                {

                    for (int i = vt; i < ma.Length - 1; i++)

                        ma[i] = ma[i + 1];

                    Array.Resize(ref ma, ma.Length - 1);

                    vt = tim(ma, x);

                }

                hienmang(ma, lblketqua);

            }

            catch

            {

                MessageBox.Show("ban chua nhap du lieu", "thong bao");

            }

        }

- tim

- hienmang

Như ở câu trên

---------------

5.Liệt kê chẵn

void lkchan(int[] m)

        {

            string st = "";

            for (int i = 0; i < ma.Length; i++)

                if (ma[i] % 2 == 0)

                    st += ma[i] + " ";

            lblketqua.Text = st.ToString();

        }

private void btnlkchan        

{

lkchan(ma);}

-----------

6.Tổng Mảng

long tongmang(int[] m)

        {

            long t = 0;

            int i = 0;

            while (i < m.Length)

            {

                t += m[i++];

            }

            return t;

        }

private void btntongmang        {    lblketqua.Text = Convert.ToString(tongmang(ma));

        }

------------------

7.Liệt kê lẻ

void lkle(int[] m)

        {

            string st = "";

            for (int i = 0; i < ma.Length; i++)

                if (ma[i] % 2 == 1)

                    st += ma[i] + " ";

            lblketqua.Text = st.ToString();

        }

 private void btnlkle

{

lkle(ma);

        }

-------------

8.Đếm lẻ

void demle(int[] m)

        {

            int d = 0;

            for (int i = 0; i < ma.Length; i++)

                if (ma[i] % 2 == 1)

                    d++;

            lblketqua.Text = d.ToString();

        }

 private void btndemle

{

 demle(ma);

        }

---------------

9.Đếm chẵn

void demchan(int[] m)

        {

            int d = 0;

            for (int i = 0; i < ma.Length; i++)

                if (ma[i] % 2 == 0)

                    d++;

            lblketqua.Text = d.ToString();

        }

 private void btndemchan        {

 demchan(ma);

        }

------------

10.Tổng lẻ

private void btntongle       

{

long t=0;

for (int i = 0; i < ma.Length; i++)

{

if (ma[i] % 2 == 1)

t += ma[i];

            }        lblketqua.Text = Convert.ToString(t);

}

------------

11.Tổng chẵn

private void btntongchan        {

long t = 0;

for (int i = 0; i < ma.Length; i++)

            {

                if (ma[i] % 2 == 0)

                    t += ma[i];

            }

            lblketqua.Text = Convert.ToString(t);

}

---------------

12.Max

int max(int[] m)

{

try

{

int intmax = ma[0];

for (int i = 0; i < ma.Length; i++)

if (intmax < ma[i])                 intmax = ma[i];        return intmax;

            }

catch

            {             MessageBox.Show("mang rong!!!", "thong bao");       }return 0;

        }

private void btnmax       

{         lblketqua.Text = max(ma).ToString();

        }

-------------

13.Min

int min(int[] m)

{

try

{

int intmin = ma[0];

for (int i = 0; i < ma.Length; i++)

if (intmin > ma[i])                     intmin = ma[i];

return intmin;

            }

catch

            {

                MessageBox.Show("mang rong!!!", "thong bao");

            }

return 0;

        }

private void btnmin

{lblketqua.Text = min(ma).ToString();

        }

--------------

14.Liệt kê ng tố

private bool nto(int x)

        {

bool kt = true;

int i;

for (i = 2; i <= x / 2; i++)

if (x % i == 0)               kt = false;

return kt;

        }

void lkento(int[] m)

{

string st = "";

for (int i = 0; i < ma.Length; i++)

if (nto(ma[i]))                   st += ma[i] + " ";        lblketqua.Text = st.ToString();

        }

private void btnlkento       

{     

lkento(ma);

}

15.Đếm ng tố

private bool nto(int x)

        {

bool kt = true;

int i;

for (i = 2; i <= x / 2; i++)

if (x % i == 0)               kt = false;

return kt;

        }

void demnto(int[] m)

{

int d = 0;

  for (int i = 0; i < ma.Length; i++)

if (nto(ma[i]))

                d++;

 lblketqua.Text = d.ToString();

        }

private void btndemnto       

{          demnto(ma);

        }

--------------------------

16.Liệt kê chính phương

private bool cp(int x)

{

bool kt = false;

int i;

for (i = 2; i <= x; i++)

if (i * i == x)         kt = true;

return kt;

        }

void lkecp(int[] m)

{

string st = "";

for (int i = 0; i < ma.Length; i++)

if (cp(ma[i]))                  st += ma[i] + " ";

lblketqua.Text = st.ToString();

}

private void btnlkecp       

{           lkecp(ma);

        }

---------------

17.Đếm chính phương

private bool cp(int x)

{

bool kt = false;

int i;

for (i = 2; i <= x; i++)

if (i * i == x)

kt = true;

return kt;

        }

void demcp(int[] m)

{int d = 0;

for (int i = 0; i < ma.Length; i++)

if (cp(ma[i]))

d++;       lblketqua.Text = d.ToString();

        }

private void btndemcp       

{

demcp(ma);

        }

------------

18.Liệt kê hoàn hảo

private bool hh(int x)

{

bool kt = false;

int i, s = 0;

for (i = 1; i <= x; i++)

if (x % i == 0)          s = s + i;

if (s - x == x)

kt = true;

return kt;

        }

void lkehh(int[] m)

{

string st = "";

for (int i = 0; i < ma.Length; i++)

if (hh(ma[i]))

st += ma[i] + " ";

lblketqua.Text = st.ToString();

        }

private void btnlkehh       

{          lkehh(ma);

        }

-----------------

19.Đếm hoàn hảo

private bool hh(int x)

{

bool kt = false;

int i, s = 0;

for (i = 1; i <= x; i++)

if (x % i == 0)

s = s + i;

if (s - x == x)

kt = true;

return kt;

        }

void demhh(int[] m)

{

int d = 0;

for (int i = 0; i < ma.Length; i++)

if (hh(ma[i]))

      d++;

lblketqua.Text = d.ToString();

        }

private void btndemhh       

{           demhh(ma);

        }

-------------

20.Liệt kê fibo

private bool fibo(int x)

{

bool kt = false;

int f1 = 1, f2 = 1, f = f1 + f2;

while (f1 < x)

{

 f1 = f2;

 f2 = f;

f2 = f1 + f2;

if (f1 == x)                 kt = true;

   }

return kt;

    }

void lkefibo(int[] m)

{

string st = "";

for (int i = 0; i < ma.Length; i++)

if (fibo(ma[i]))

st += ma[i] + " ";

lblketqua.Text = st.ToString();

        }

private void btnlkefibo        {        lkefibo(ma);

        }

-----------

21.Đếm fibo

private bool fibo(int x)

{

bool kt = false;

int f1 = 1, f2 = 1, f = f1 + f2;

while (f1 < x)

   {

f1 = f2;

f2 = f;

f2 = f1 + f2;

if (f1 == x)

kt = true;

        }

return kt;

        }

void demfibo(int[] m)

{

int d = 0;

for (int i = 0; i < ma.Length; i++)

if (fibo(ma[i]))               d++;      lblketqua.Text = d.ToString();

        }

 private void btndemfibo        {          demfibo(ma);

        }

---------------------------

22.Nhị phân

private void nhiphan

        {

            int n, du, i, dem;

            string st;

            n = Convert.ToInt32(Interaction.InputBox("moi nhap sothong bao", "5", 100, 300));

            st = "";

            dem = 0;

            while (n != 0)

            {

                du = n % 2;

                st = (du).ToString() + st;

                dem = dem + 1;

                n = n / 2;

            }

            for (i = 0; i < 16 - dem; i++)

                st = "0" + st;

            txtkq.Text = st.ToString();

        }

-------------

23.Hecxa

private void hecxa

        {

            int n, du, i, dem;

            string st;

            n = Convert.ToInt32(Interaction.InputBox("moi nhap sothong bao", "5", 100, 300));

            st = "";

            dem = 0;

            while (n != 0)

            {

                du = n % 16;

                {

                    if (du <= 9)

                        st = (du).ToString() + st;

                    else

                        if (du > 9)

                            st = (char)(du + 55) + st;

                }

                dem++;

                n = n / 16;

            }

            for (i = 0; i < 4 - dem; i++)

                st = "0" + st;

            st = "$" + st;

            txtkq.Text = st.ToString();

        }

-----------

24.In Tam giác số

private void Thuchien        {

int i, j;

int n=Convert.ToInt32(Interaction.InputBox("NhậpThông báo","5",100,300));

int k=n;       for(i=1;i<=n;i++)

            {

                {

string;            for(int t=k--;t>=0;t--)

{                 st+=    "    ";

    }

string st1= "";                    for(j=i;j<=2*i-1;j++)                   {                       st1+=(j%10).ToString()+"  ";                   }                   string st2="";                 for (int m=j-2;m>=i;m--)                  {                        st2+=(m%10).ToString()+ "  ";                  }                 listBox1.Items.Add(st+st1+st2);

       }               listBox1.Items.Add( "  " );

        }

    }

private void Exit        {        this.Close();

        }

---------

25. Tam giác Sao

private void Tamgiacsao        {

int n = Convert.ToInt32(Interaction.InputBox("Nhap n", "thong bao", "5", 50, 100));

            for (int i = 0; i < n; i++)

            {

                string st = "";

                for (int j = 0; j <=i; j++)

                {

                    st += " * ";

                }

                    listBox1.Items.Add(st);

                }

                listBox1.Items.Add(" ");

            }

--------------

26.Tam giác sao ngược

 private void saonguoc       

{  int n = Convert.ToInt32(Interaction.InputBox("Nhap n", "thong bao", "5", 50, 100));

            for (int i = n; i >0; i--)

            {

                string st = "";

                for (int j = 0; j < i; j++)

                {

                    st += " * ";

                }

                    listBox1.Items.Add(st);

                }

                listBox1.Items.Add(" ");

            }

----------------------------

27. Quản lý sinh viên

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;

using System.Data.SqlClient;

namespace ketnoi

{

    public partial class FrmDataGridview : Form

    {

        public FrmDataGridview()

        {

            InitializeComponent();

        }

        private void btnThoat_Click(object sender, EventArgs e)

        {

            DialogResult r;

            r = MessageBox.Show("Ban co muon thoat khỏi chương trinh khong?","Thong bao",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

            if (r==DialogResult.Yes)

                Application.Exit();

        }

        private string conStr = "Data Source = (local);" +

  "Initial Catalog = khoa1;" +

  "persist security info = true;" +

  "User Id=sa; Password=; Connect Timeout =50";

        private SqlDataAdapter myDataAdapter;

        private DataSet myDataSet;

        private DataTable myTable;

        private SqlCommand myCommand;

        private SqlConnection myConnection;

        //  private SqlDataReader myDataReader;

        private void Display()

        {

            /*string SqlStr = "SELECT * FROM tblKhoaDaoTao inner join tblNganhDaoTao ON MaKhoa inner join tbSinhVien on MaNganh"; */

          Create Table  tblTam

          { TenKhoa varchar(10)

            TenNganh varchar(10) 

            HoTen Varchar(10)

            Lop varchar(10)

           }

        string SqlStr = "SELECT * FROM tblTam

            //Tao thong qua xau ket noi da mo

            myDataAdapter = new SqlDataAdapter(SqlStr, myConnection);

            myDataSet = new DataSet();

            myDataAdapter.Fill(myDataSet, " tblKhoaDaoTao ");

            myTable = myDataSet.Tables["tblKhoaDaoTao "];

            //Chuyen len luoi

            dataGridView1.DataSource = myTable;

            dataGridView1.AutoGenerateColumns = false;

        }

       private void FrmDataGridview_Load(object sender, EventArgs e)

        {

            myConnection = new SqlConnection(conStr);

            myConnection.Open();

            Display();

         }

         private void btnGhi_Click(object sender, EventArgs e)

        {

            string sSql;

            sSql = "Insert Into tblTam Values (N'" + txttenkhoa.Text + "',N'” + txttennganh.Text + "',N'" + txthoten.Text + "',N'" + txtlop.Text + "')";

            myCommand = new SqlCommand(sSql, myConnection);

            myCommand.ExecuteNonQuery();

            Display();

            // SetControls(true);

        }

        private void btnXoa_Click(object sender, EventArgs e)

        {

            string sSql;

            sSql = "Delete From tblTam " +

            "Where MaKhoa = N'" + txtMaKhoa.Text + "'";

            myCommand = new SqlCommand(sSql, myConnection);

            myCommand.ExecuteNonQuery();

            Display();

        }

        private void btnThemmoi_Click(object sender, EventArgs e)

        {  Drop DATABASE tblTam;

            txtMaKhoa.Text = "";

            txtTenKhoa.Text = "";

            txtMaKhoa.Focus();

        }

    }

}

private void Form1_Load(object sender, EventArgs e)

        {

           this.label1.BackColor = System.Drawing.Color.Transparent;

        }

public int i=5;

        private void timer1_Tick(object sender, EventArgs e)

        { //while(label1.Left<=this.Width)

            label1.Left += i;

            if (label1.Left >= this.Width - label1.Width || label1.Left <= 0)

                label1.Left = this.Form1_Load.Left;

               // i = -i;

        }

[email protected]

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: #lalala