Chương trình c bài tập đơn giản

29 6 0
Chương trình c   bài tập đơn giản

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Ngôn ngữ C khá đơn giản, chỉ khoảng hơn 80 từ khóa và hơn mười mấy kiểu dữ liệu được dựng sẵn. Tuy nhiên, ngôn ngữ C có ý nghĩa to lớn khi nó thực thi những khái niệm lập trình hiện đại. C bao gồm tất cả những hỗ trợ cho cấu trúc, thành phần component, lập trình hướng đối tượng. Những tính chất đó hiện diện trong một ngôn ngữ lập trình hiện đại. Hơn nữa ngôn ngữ C được xây dựng trên nền tảng hai ngôn ngữ mạnh nhất là C++ và Java.

Bài tập C# 2008 Bài tập C# 2008 ComboBOX .2 ListBox Đồng hồ đếm ngược Xâu kí tự Dùng ToolBar Thực đơn Mảng liệu 10 Truyền giá trị qua FORM 12 Chuyển giá trị nhờ DialogueResult 13 Chuyển tham số thủ tục .14 Mơ tả lớp, xử lí xâu 16 SQL SERVER 2005 18 Cơ sở liệu 20 Làm việc với sở liệu 21 Đưa đữ liệu lên DataGridView 22 Bổ sung dòng liệu 24 Xóa dịng liệu đánh dấu bảng liệu 27 ComboBOX using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace bai_COMBOBOX { public partial class chinh : Form { public chinh() { InitializeComponent(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.textBox1.Text = "Đã chọn "; this.textBox1.Text += this.comboBox1.SelectedItem.ToString(); } private void button2_Click(object sender, EventArgs e) { this.textBox1.ResetText(); } private void chinh_Load(object sender, EventArgs e) { // property cua Combobox da co chi tiet this.comboBox1.Items.Add("chi tiet 4"); this.comboBox1.Items.Add("chi tiet 5"); } } } ListBox using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace baitap_listbox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.textBox1.Text = "Đã chọn "; this.textBox1.Text += this.listBox1.SelectedItem.ToString(); } private void button2_Click(object sender, EventArgs e) { this.textBox1.ResetText(); } private void Form1_Load(object sender, EventArgs e) { } } } Đồng hồ đếm ngược using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace Baitap_Dongho { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int i = 10; private void button1_Click(object sender, EventArgs e) { this.textBox1.Text = "Hiện trạng : "; if (this.radioButton1.Checked) this.textBox1.Text += " OK"; else this.textBox1.Text += " NOT"; } private void timer1_Tick(object sender, EventArgs e) { this.label3.Text = i.ToString(); i ; if (i < 0) this.timer1.Enabled = false; } private void label1_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { this.timer1.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { Application.Exit(); } } } Xâu kí tự using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace btap_xau { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string s1 = this.textBox1.Text; string s2 = this.textBox2.Text; // so sánh, phân biệt chữ hoa Nếu không phân biệt Compare (s1,s2, true); // so sánh từ trái sang phải, theo AI int kq = string.Compare(s1, s2); // đánh giá kết this.textBox3.Text = "xâu thứ "; if (kq == -1) this.textBox3.Text += ""; this.textBox3.Text += " xâu thứ hai"; } private void button2_Click(object sender, EventArgs e) { string s1 = this.textBox1.Text; string s2 = this.textBox2.Text; // nối hai xâu this.textBox3.Text = string.Concat(s1, s2); } private void button5_Click(object sender, EventArgs e) { this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; this.textBox1.Focus(); } private void button3_Click(object sender, EventArgs e) { string s1 = this.textBox1.Text; string s2 = this.textBox2.Text; // cho biết vị trí xuất s2 s1 lưu ý xâu từ vị trí int i = s1.IndexOf(s2); if (i >= 0) { this.textBox3.Text = "Xâu xuất xâu vị trí i = "; this.textBox3.Text += i; // thay xâu s2 xâu s3 string s3 = "THAY THẾ"; this.textBox3.Text += "\n\r\n Thay s2 s3 mới"; this.textBox3.Text += "\n\r\n kết " + s1.Replace(s2, s3); } else this.textBox3.Text = " xâu không chứa xâu 2"; } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { string s1 = this.textBox1.Text; string s2 = this.textBox2.Text; // chèn s2 vào s1 Vị trí chèn sau số this.textBox3.Text = "Chèn s2 vào s1, TRƯỚC kí tự đầu tiên\n\r\n"; this.textBox3.Text += s1.Insert(s1.IndexOf("4"), s2); } } } Dùng ToolBar using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace baitap_Toolbar { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void toolStripTextBox1_Click(object sender, EventArgs e) { //ứng với Một Form Mot = new Form2(); Mot.ShowDialog(); } private void toolStripTextBox2_Click(object sender, EventArgs e) { //ứng với Hai } private void Form1_Load(object sender, EventArgs e) { } } } using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace baitap_Toolbar { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } } } Thực đơn using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace baitap_thucdon { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void closeToolStripMenuItem_Click(object sender, EventArgs e) { //Mục 1.2 thực đơn Chức } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { } private void openToolStripMenuItem_Click(object sender, EventArgs e) { //mục thực đơn 1.1 Form muc11 = new Form2(); muc11.ShowDialog(); } private void mục21ToolStripMenuItem_Click(object sender, EventArgs e) { //Mục 2.1 thực đơn Chức } private void mục22ToolStripMenuItem_Click(object sender, EventArgs e) { // mục 2.2 thực đơn Chức } private void Form1_Load(object sender, EventArgs e) { } } } using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace baitap_thucdon { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } private void Form2_Load(object sender, EventArgs e) { } } } Mảng liệu using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace baitap_Mang_dlieu { public partial class Form1 : Form { // khai bao bien du lieu const int m = 3; const int n = 4; int spt = 0; int[] a = { 1, 2, 3, }; // : int [] a = new int[4] {1,2,3,4}; int [,] b = new int [m, n]; public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { // xóa textbox form this.textBox1.Text = ""; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { // in ma tran chieu this.textBox1.Text = "Mảng chiều a :\n\r\n"; for (int i = 0; i < a.Length; i++) this.textBox1.Text += a[i] + " "; // in mảng xếp tăng dần Array.Sort(a, 0, n); 10 using using using using using using using System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace Btap_truyen_thamso { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Bổ sung CLASS, tức trình public class Trao { public void Hoanvi(ref int a, ref int b) //truyen theo biến { int w = a; a = b; b = w; } public void Hv(int a, int b) // truyền theo giá trị { int c = a; a = b; b = c; // kết thúc, A, B không thay đổi } } // hết mô tả CLASS private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { int a = int.Parse(this.textBox1.Text); int b = int.Parse(this.textBox2.Text); Trao i = new Trao(); //đến class Trao i.Hoanvi(ref a, ref b); this.textBox1.Text = a.ToString(); this.textBox2.Text = b.ToString(); } private void button2_Click(object sender, EventArgs e) { this.textBox1.ResetText(); this.textBox2.ResetText(); } private void button3_Click(object sender, EventArgs e) { int a = int.Parse(this.textBox1.Text); int b = int.Parse(this.textBox2.Text); Trao i = new Trao(); //đến class Trao i.Hv(a, b); // A, B không thay đổi this.textBox1.Text = a.ToString(); this.textBox2.Text = b.ToString(); } } } 15 Mơ tả lớp, xử lí xâu using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace lopXAU { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // mô tả CLASS FORM public class Xau { // - thuoc tinh string gthieu = "Xử lí dịng văn bản"; // - method // in public string In() { return gthieu; } //lay ten từ hoten public string Ten(string hoten) { int i = hoten.LastIndexOf(" "); return hoten.Substring(i+1, hoten.Length - i-1); } // lay ho dem từ hoten public string Ho(string hoten) { int i = hoten.LastIndexOf(" "); return hoten.Substring(0, i); } // đếm số từ hoten public int Dem(string hoten) { const char Trong = ' '; string s = hoten.Trim(); 16 int i = s.Split(Trong).Length; // chuyển xâu thành mảng, đếm return i; } } // - hết mô tả CLASS private void Form1_Load(object sender, EventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { // giới thiệu Xau s = new Xau(); this.textBox2.Text = s.In(); } private void button2_Click(object sender, EventArgs e) { // trích tên Xau s = new Xau(); this.textBox2.Text = "tên : " + s.Ten(this.textBox1.Text); } private void button3_Click(object sender, EventArgs e) { // trích họ, đệm Xau s = new Xau(); this.textBox2.Text = "Họ : " + s.Ho(this.textBox1.Text); } private void button4_Click(object sender, EventArgs e) { Xau s = new Xau(); this.textBox2.Text = "Số từ họ tên : " + s.Dem (this.textBox1.Text); } private void button5_Click(object sender, EventArgs e) { Application.Exit(); } } } 17 SQL SERVER 2005 • • Chạy SQLServer2005_SSMSEE để tạo chương trình quản lí máy chủ SQL Server; File MSSQL2005_Setup cho phép tạo cấu hình Sau cài đặt chương trình Khởi động chương trình đưa người dùng đến cửa sổ tạo sở liệu Khi khởi động chương trình, xuất phần yêu cầu kết nối đến máy chủ : 18 Tạo them bảng NGUOI vào sở liệu 19 Cơ sở liệu 20 Làm việc với sở liệu using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient; // khai bao namespace baitap_csdl { public partial class Form1 : Form { // xâu kết nối string strConnectionString = "Data Source = \\SQLEXPRESS; Initial Catalog = Quanly; Integrated Security = True"; // Đối tượng kết nối SqlConnection conn = null; // Đối tượng đưa liệu vào bảng NGUOI SqlDataAdapter danguoi = null; // đối tượng hiển thị lên Form DataTable dtnguoi = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { // khởi động nối conn = new SqlConnection (strConnectionString); // tải liệu danguoi = new SqlDataAdapter("SELECT * FROM nguoi", conn); dtnguoi = new DataTable(); dtnguoi.Clear(); danguoi.Fill(dtnguoi); // Đưa lieu vào listBox this.listBox1.DataSource = dtnguoi; //this.listBox1.DisplayMember = "ten"; this.listBox1.ValueMember = "ten"; } catch (SqlException) { MessageBox.Show ("không tải !"); } 21 } private void button1_Click(object sender, EventArgs e) { // giải phóng dtnguoi.Dispose(); dtnguoi = null; //hủy kết nói conn = null; //thoát DialogResult Traloi; Traloi = MessageBox.Show("Thoát", "Trả lời", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (Traloi==DialogResult.OK) Application.Exit(); } } } Đưa đữ liệu lên DataGridView using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient; // khai bao namespace btap_datagridview { public partial class Form1 : Form { // xâu kết nối string strConnectionString = "Data Source = \\SQLEXPRESS; Initial Catalog = Quanly; Integrated Security = True"; // Đối tượng kết nối SqlConnection conn = null; // Đối tượng đưa liệu vào bảng NGHE SqlDataAdapter danghe = null; // đối tượng hiển thị lên Form DataTable dtnghe = null; 22 // Đối tượng đưa liệu vào bảng NGUOI SqlDataAdapter danguoi = null; // đối tượng hiển thị lên Form DataTable dtnguoi = null; //mô tả hàm Tải lại liệu void Tai() { try { // kết nối conn = new SqlConnection(strConnectionString); // tải dư lieu danghe = new SqlDataAdapter("SELECT * FROM nghe", conn); dtnghe = new DataTable(); dtnghe.Clear(); danghe.Fill(dtnghe); // đưa dư lieu lên COMBOBOX DataGridView // (nghenghiep.Columns["ten"] as DataGridViewComboBoxColumn).DataSource = dtnghe; // (nghenghiep.Columns["ten"] as DataGridViewComboBoxColumn).DisplayMember = "viec"; //(nghenghiep.Columns["ten"] as DataGridViewComboBoxColumn).ValueMember = "viec"; //đưa liệu vào dtnguoi danguoi = new SqlDataAdapter("SELECT * FROM nguoi", conn); dtnguoi = new DataTable(); dtnguoi.Clear(); danguoi.Fill(dtnguoi); // đưa liệu lên DataGridView nghenghiep.DataSource = dtnguoi; } catch (SqlException) { MessageBox.Show("Không tải bảng NGUOI"); } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { // kết nối conn = new SqlConnection(strConnectionString); // tải dư lieu danghe = new SqlDataAdapter("SELECT * FROM nghe", conn); dtnghe = new DataTable(); dtnghe.Clear(); danghe.Fill(dtnghe); // dư lieu vào DataGridView nghenghiep.DataSource = dtnghe; } catch (SqlException) { MessageBox.Show("không tải liệu"); } } private void nghenghiep_CellContentClick(object sender, DataGridViewCellEventArgs e) { 23 } private void button1_Click(object sender, EventArgs e) { // giải phóng dtnghe.Dispose(); dtnghe = null; dtnguoi.Dispose(); dtnguoi = null; //hủy kết nói conn = null; //thốt DialogResult Traloi; Traloi = MessageBox.Show("Thốt", "Trả lời", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (Traloi == DialogResult.OK) Application.Exit(); } private void button2_Click(object sender, EventArgs e) { Tai(); } } } Bổ sung dòng liệu using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient; // khai báo bổ sung namespace btap_insert_CSDL { public partial class Form1 : Form { // xâu kết nối string strConnectionString = "Data Source = \\SQLEXPRESS; Initial Catalog = Quanly; Integrated Security = True"; // Đối tượng kết nối SqlConnection conn = null; // Đối tượng đưa liệu vào bảng NGHE SqlDataAdapter da = null; // đối tượng hiển thị lên Form 24 DataTable dt = null; public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { // Thốt khỏi chương trình // giải phóng dt.Dispose(); dt = null; //hủy kết nối conn = null; //thoát DialogResult Traloi; Traloi = MessageBox.Show("Thoát", "Trả lời", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (Traloi == DialogResult.OK) Application.Exit(); } private void button2_Click(object sender, EventArgs e) { // Hiện liệu tải bảng liệu NGUOI try { // kết nối conn = new SqlConnection(strConnectionString); // tải dư lieu da = new SqlDataAdapter("SELECT * FROM nguoi", conn); dt = new DataTable(); dt.Clear(); da.Fill(dt); // dư lieu vào DataGridView hien.DataSource = dt; } catch (SqlException) { MessageBox.Show("không tải liệu NGUOI"); } // số dòng bảng liệu int r = this.hien.Rows.Count; string r2 = r.ToString(); this.textBox5.Text = r2; } private void button4_Click(object sender, EventArgs e) { //xóa vùng đệm 25 this.textBox1.ResetText(); this.textBox2.ResetText(); this.textBox3.ResetText(); this.textBox4.ResetText(); this.textBox5.ResetText(); // số liệu } private void button3_Click(object sender, EventArgs e) { // Khẳng định bổ sung liệu Mở kết nối conn = new SqlConnection(strConnectionString); // không cần, chạy để liệu conn.Open(); try { // Thực lệnh SqlCommand cau = new SqlCommand(); cau.Connection = conn; cau.CommandType = CommandType.Text; // viết câu INSERT INTO cau.CommandText = System.String.Concat ("INSERT INTO nguoi VALUES ("+ "'"+this.textBox1.Text.ToString()+"'," + this.textBox2.Text.ToString() + "," + "'" + this.textBox3.Text.ToString() + "'," + this.textBox4.Text.ToString() +")" ); MessageBox.Show(cau.CommandText); // khai thác câu lệnh cau.CommandType = CommandType.Text; cau.ExecuteNonQuery(); } catch(SqlException) { MessageBox.Show("Khơng bổ sung được"); } } } } 26 Xóa dòng liệu đánh dấu bảng liệu using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient; namespace btap_update_CSDL { public partial class Form1 : Form { // xâu kết nối string strConnectionString = "Data Source = \\SQLEXPRESS; Initial Catalog = Quanly; Integrated Security = True"; // Đối tượng kết nối SqlConnection conn = null; // Đối tượng đưa liệu vào bảng NGHE SqlDataAdapter da = null; // đối tượng hiển thị lên Form DataTable dt = null; //mo ta tai_nguoi void tai_nghe() { try { // kết nối conn = new SqlConnection(strConnectionString); // tải dư lieu da = new SqlDataAdapter("SELECT * FROM nghe", conn); dt = new DataTable(); dt.Clear(); 27 da.Fill(dt); // dư lieu vào DataGridView hien.DataSource = dt; } catch (SqlException) { MessageBox.Show("không tải liệu NGHE"); } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { tai_nghe(); } private void button2_Click(object sender, EventArgs e) { // tải bảng liệu NGUOI try { // kết nối conn = new SqlConnection(strConnectionString); // tải dư lieu da = new SqlDataAdapter("SELECT * FROM nguoi", conn); dt = new DataTable(); dt.Clear(); da.Fill(dt); // dư lieu vào DataGridView hien.DataSource = dt; } catch (SqlException) { MessageBox.Show("không tải liệu NGUOI"); } } private void button1_Click(object sender, EventArgs e) { tai_nghe(); } private void button3_Click(object sender, EventArgs e) { // giải phóng dt.Dispose(); dt = null; //hủy kết nối conn = null; //thoát DialogResult Traloi; Traloi = MessageBox.Show("Thoát", "Trả lời", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (Traloi == DialogResult.OK) Application.Exit(); } private void button4_Click(object sender, EventArgs e) { 28 // xóa liệu bảng NGHE conn.Open(); try { // thực câu lệnh SqlCommand cau = new SqlCommand(); cau.Connection = conn; cau.CommandType = CommandType.Text; // lấy thứ tự ghi int r = hien.CurrentCell.RowIndex; // tách tên người string ten1 = hien.Rows[r].Cells[0].Value.ToString(); // viết câu SQL cau.CommandText = System.String.Concat ("DELETE FROM nghe WHERE ten ='" + ten1 + "'"); cau.CommandType = CommandType.Text; MessageBox.Show(cau.CommandText); // khai thác câu lệnh SQL cau.ExecuteNonQuery(); // cập nhật lại bảng liệu tai_nghe(); MessageBox.Show("đã xóa"); } catch(SqlException) { MessageBox.Show("khơng xóa !"); } conn.Close(); } } } 29 ... tạo chương trình quản lí máy chủ SQL Server; File MSSQL2005_Setup cho phép tạo cấu hình Sau cài đặt chương trình Khởi động chương trình đưa người dùng đến cửa sổ tạo sở liệu Khi khởi động chương. .. InitializeComponent(); } // mô tả CLASS FORM public class Xau { // - thuoc tinh string gthieu = "Xử lí dịng văn bản"; // - method // in public string In() { return gthieu; } //lay ten... string Ten(string hoten) { int i = hoten.LastIndexOf(" "); return hoten.Substring(i+1, hoten.Length - i-1); } // lay ho dem từ hoten public string Ho(string hoten) { int i = hoten.LastIndexOf(" ");

Ngày đăng: 28/07/2022, 06:43

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan