



















Preview text:
Sinh viên using CTDLGT_BT1; using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BT1_CTDLGT { internal class MangSinhVien {
private List ListSinhVien = null; public MangSinhVien() { ListSinhVien = new List(); } public void Nhap () { SinhVien sv = new SinhVien(); do { sv.Nhap();
if (KTMaSoSinhVien(sv.GetMaSo(), sv) == true) {
Console.WriteLine("Vui long nhap lai vi ma so sinh vien trung lap !"); Console.WriteLine(); }
} while (KTMaSoSinhVien(sv.GetMaSo(), sv) == true); sv.XepLoai(); ListSinhVien.Add(sv); Console.WriteLine(); }
public bool KTMaSoSinhVien(string ms, SinhVien sv) {
foreach(SinhVien a in ListSinhVien) { if(a.GetMaSo()==ms) { return true; } } return false; } public int SoLuongSinhVien() { int Count = 0; if (ListSinhVien != null) { Count = ListSinhVien.Count; } return Count; } public void Xuat(List listSV) {
if (listSV != null && listSV.Count > 0) {
foreach (SinhVien sv in listSV) { sv.XepLoai(); sv.Xuat(); Console.WriteLine(); } } } public List getListSinhVien() { return ListSinhVien; } } } using System;
using System.Collections.Generic; using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Permissions; using System.Text; using System.Threading.Tasks; namespace CTDLGT_BT1 { class SinhVien { private string maSo; private string hoTen; private string chuyenNganh; private int namSinh; private float diemTB; private string loai; public SinhVien() { }
public SinhVien(string maSo, string hoTen, string chuyenNganh, int namSinh, float diemTB) { this.maSo = maSo; this.hoTen = hoTen;
this.chuyenNganh = chuyenNganh; this.namSinh = namSinh; this.diemTB = diemTB; }
public SinhVien(SinhVien sv) { }
public void SetMaSo(string ms) { this.maSo = ms; } public string GetMaSo() { return this.maSo; }
public void SetHoTen(string ht) { this.hoTen = ht; } public string GetHoTen() { return this.hoTen; }
public void SetChuyenNganh(string cn) { this.chuyenNganh = cn; }
public string GetchuyenNganh() { return this.chuyenNganh; }
public void SetNamSinh(int ns) { this.namSinh = ns; } public int GetNamSinh() { return this.namSinh; }
public void SetDiemTB(float dtb) { this.diemTB = dtb; } public float GetDiemTB() { return this.diemTB; } public void SetLoai(string l) { this.loai = l; } public string GetLoai() { return this.loai; } public void XepLoai() { if (diemTB < 5) { loai = "kem"; }
else if(diemTB>=5&&diemTB<7) { loai = "Trung binh"; }
else if(diemTB>=7&&diemTB<8) { loai = "Kha"; } else { loai = "Gioi"; } } public void Nhap() { Console.Write("Ma so: ");
this.maSo = Console.ReadLine(); Console.Write("Ho ten: ");
this.hoTen = Console.ReadLine();
Console.Write("Chuyen Nganh: ");
this.chuyenNganh= Console.ReadLine(); Console.Write("Nam sinh: ");
this.namSinh = int.Parse(Console.ReadLine());
if(KiemTraNamSinh(this.namSinh)==true) {
Console.WriteLine("Nam sinh: true"); } else {
Console.WriteLine("Nam sinh: false"); }
Console.Write("Diem trung binh: ");
this.diemTB=float.Parse(Console.ReadLine());
if (KiemTraDiemTB(diemTB)==true) {
Console.WriteLine("diem tb: true"); } else {
Console.WriteLine("diem tb: false"); } } public void Xuat() {
Console.Write(" | Ma so: "+maSo);
Console.Write(" | Ho ten: "+hoTen);
Console.Write(" | Chuyen Nganh: "+chuyenNganh);
Console.Write(" | Nam sinh: "+namSinh);
Console.Write(" | Diem trung binh: "+diemTB);
Console.Write(" | Loai: "+loai); }
public bool KiemTraNamSinh(int ns) {
if(DateTime.Now.Year-ns<17||DateTime.Now.Year-ns>70) { return false; } return true; }
public bool KiemTraDiemTB(float dtb) { if(dtb>10||diemTB<0) { return false; } return true; } } } using CTDLGT_BT1; using System;
using System.Collections.Generic; using System.Linq;
using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace BT1_CTDLGT { internal class Program { static void TestSinhVien() { SinhVien svA=new SinhVien(); svA.Nhap();
Console.WriteLine("Thong tin sinh vien A:"); svA.XepLoai(); svA.Xuat();
SinhVien svB = new SinhVien("18DH001", "Lam Thanh Ngoc", "CNPM", 2000, 7F); }
static void TestMangSinhVien() {
MangSinhVien dssv = new MangSinhVien(); dssv.Nhap();
Console.WriteLine("Danh sach sinh vien");
dssv.Xuat(dssv.getListSinhVien()); }
static void Main(string[] args) {
MangSinhVien dssv = new MangSinhVien();
Console.Write("Nhap so luong sinh vien muon them vao danh sach: ");
int sl = int.Parse(Console.ReadLine()); for(int i=1;i<=sl;i++) {
Console.WriteLine("sinh vien thu {0}:",i); dssv.Nhap(); }
Console.WriteLine("\nDanh sach sinh vien: ");
dssv.Xuat(dssv.getListSinhVien()); Console.ReadKey(); } } } Bài int array using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace CTDLGT_BT2 { internal class IntArray { private int[] arr; public void SetArr(int[] a) { this.arr = a; } public int[] GetArr() { return this.arr; } public int this[int index] { set => arr[index] = value; get => arr[index]; } public IntArray() { } public IntArray(int k) { arr = new int[k]; Random r = new Random();
for (int i = 0; i < k; i++) { arr[i] = r.Next(1, 200); } } public IntArray(int[] a) { this.arr = new int[a.Length];
for (int i = 0; i < a.Length; i++) { this.arr[i] = a[i]; } } public IntArray(IntArray obj) {
this.arr = new int[obj.arr.Length];
for (int i = 0; i < obj.arr.Length; i++) { this.arr[i] = obj.arr[i]; } } public bool KiemTraKT(int n) {
if (n > 0 && n <= 200) { return true; } return false; } public void Nhap() {
for (int i = 0; i < arr.Length; i++) { Console.Write($"a[{i}]= ");
arr[i] = int.Parse(Console.ReadLine()); } } public void Xuat() {
for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " "); } Console.WriteLine(); } public int TimTuanTu(int x) {
for (int i = 0; i < arr.Length; i++) { if (arr[i] == x) { return i; } } return -1; } public int TimNhiPhan(int x) { int l = 0; int r = arr.Length - 1; while (l <= r) { int m = (l + r) / 2; if (arr[m] == x) { return m; } else if (x > arr[m]) { l = m + 1; } else { r = m - 1; } }
Console.WriteLine("Khong xuat hien !"); return -1; }
public void Swap(ref int a, ref int b) { int tam = a; a = b; b = tam; } public void InterchangeSort() {
for (int i = 0; i < arr.Length - 1; i++) {
for (int j = i + 1; j < arr.Length; j++) { if (arr[i] > arr[j]) { Swap(ref arr[i], ref arr[j]); } } } } public void BubbleSort() {
for (int i = 0; i < arr.Length - 1; i++) {
for (int j = 0; j < arr.Length - 1 - i; j++) { if (arr[j] > arr[j + 1]) {
Swap(ref arr[j], ref arr[j + 1]); } } } } public void SelectionSort() {
for (int i = 0; i < arr.Length - 1; i++) { int min = i;
for (int j = i + 1; j < arr.Length; j++) { if (arr[j] < arr[min]) { min = j; } }
Swap(ref arr[i], ref arr[min]); } } public void InsertionSort() { int n = arr.Length;
for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1;
while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
public void QuickSort(int left, int right) { if (left < right) {
int pivot = Partition(left, right); if (pivot > 1) { QuickSort(left, pivot - 1); } if (pivot + 1 < right) { QuickSort(pivot + 1, right); } } }
private int Partition(int left, int right) { int pivot = arr[left]; while (true) { while (arr[left] < pivot) { left++; } while (arr[right] > pivot) { right--; } if (left < right) {
if (arr[left] == arr[right]) return right; int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } else { return right; } } } public void ShellSort() { int i, j, pos, temp; pos = 3; while (pos > 0) { for (i = 0; i { j = i; temp =arr[i];
while ((j >= pos) && (arr[j - pos] > temp)) { arr[j] = arr[j - pos]; j = j - pos; } arr[j] = temp; } if (pos / 2 != 0) pos = pos / 2; else if (pos == 1) pos = 0; else pos = 1; } } public void ShakerSort() { int l = 0; int r = arr.Length - 1; int k = 0; while (l < r) {
for (int i = l; i < r; i++) { if (arr[i] > arr[i + 1]) {
Swap( ref arr[i],ref arr[i + 1]); k = i; } } r = k;
for (int i = r; i > l; i--) { if (arr[i] < arr[i - 1]) {
Swap( ref arr[i],ref arr[i - 1]); k = i; } } l = k; } }
public int[] MergeSort(int[]arr) { int[] left; int[] right;
int[] result = new int[arr.Length]; if (arr.Length <= 1) return arr;
int midPoint = arr.Length / 2; left = new int[midPoint]; if (arr.Length % 2 == 0) right = new int[midPoint]; else
right = new int[midPoint + 1];
for (int i = 0; i < midPoint; i++) left[i] = arr[i]; int x = 0;
for (int i = midPoint; i < arr.Length; i++) { right[x] = arr[i]; x++; } left = MergeSort(left); right = MergeSort(right); result = Merge(left, right); return result; }
private int[] Merge(int[] left, int[] right) {
int resultLength = right.Length + left.Length;
int[] result = new int[resultLength]; //
int indexLeft = 0, indexRight = 0, indexResult = 0;
while (indexLeft < left.Length || indexRight < right.Length) {
if (indexLeft < left.Length && indexRight < right.Length) {
if (left[indexLeft] <= right[indexRight]) {
result[indexResult] = left[indexLeft]; indexLeft++; indexResult++; } else {
result[indexResult] = right[indexRight]; indexRight++; indexResult++; } }
else if (indexLeft < left.Length) {
result[indexResult] = left[indexLeft]; indexLeft++; indexResult++; }
else if (indexRight < right.Length) {
result[indexResult] = right[indexRight]; indexRight++; indexResult++; } } return result; } } } using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CTDLGT_BT2 { internal class Program {
static void Main(string[] args) { //interchang sort
Console.OutputEncoding = Encoding.Unicode;
Console.WriteLine("Mảng chưa được xắp xếp"); IntArray a = new IntArray(7); a.Xuat(); a.InterchangeSort();
Console.WriteLine("Mảng sau khi được xắp xếp bằng Interchange Sort: "); a.Xuat(); Console.WriteLine(); //bubble sort
Console.WriteLine("Mảng chưa được xắp xếp"); ; IntArray b =new IntArray(7); b.Xuat(); b.BubbleSort();
Console.WriteLine("Mảng sau khi được xắp xếp bằng Bubble Sort: "); b.Xuat(); Console.WriteLine(); //selection sort
Console.WriteLine("Mảng chưa được xắp xếp"); IntArray c = new IntArray(7); c.Xuat(); c.SelectionSort();
Console.WriteLine("Mảng sau khi được xắp xếp bằng Seclection Sort: "); c.Xuat(); Console.WriteLine(); //Isertion sort
Console.WriteLine("Mảng chưa được xắp xếp"); IntArray d = new IntArray(7); d.Xuat();