[code]using System; using System.IO; using System.Collections; namespace DCT110X_DHSG { class CTime { private int hour, minute, second; public CTime() { hour = minute = second = 0; } public CTime(int h, int m, int s) { hour = h; minute = m; second = s; } public void Nhap() { do { Console.Write("nhap gio="); hour = Int32.Parse(Console.ReadLine()); Console.Write("nhap phut="); minute = Int32.Parse(Console.ReadLine()); Console.Write("nhap giay="); second = Int32.Parse(Console.ReadLine()); if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) Console.WriteLine("Nhap sai, hay nhap lai!"); }while(hour<0||hour>=24||minute<0||minute>=60||second<0||second>=60); } public void Xuat() { Console.WriteLine("{0}:{1}:{2}", hour, minute, second); } public int Hour { get { return hour; } set { hour = value; } } public int Minute { get { return minute; } set { minute = value; } } public int Second { get { return second; } set { second = value; } } public void Tang1Giay() { second++; if (second == 60) { minute++; second = 0; } if (minute == 60) { hour++; minute = 0; } if (hour == 24) hour = 0; } public void Giam1Giay() { second--; if (second == -1) { second = 59; minute--; } if (minute == -1) { minute = 59; hour--; } if (hour == -1) hour = 23; } public CTime CongThemGiay(int s) { /* CTime temp= new CTime(hour,minute,second); for (int i = 0; i < s; i++) temp.Tang1Giay(); return temp;*/ int Sum = DoiRaGiay()+s; return DoiRaThoiGian(Sum); } private int DoiRaGiay() { return hour * 3600 + minute * 60 + second; } private CTime DoiRaThoiGian(int s) { CTime temp = new CTime(); temp.hour = s / 3600; temp.minute = (s % 3600) / 60; temp.second = s % 60; return temp; } } class CToanHoc { public static int Tong(int a, int b) { return a + b; } public static double CanBacHai(int n) { return Math.Sqrt(n); } //private CToanHoc() { } public static double PI; public CToanHoc() { PI = 3.14159; } static CToanHoc() { PI = 3.14159; } }; class CMyArray { public int[] data; public CMyArray(int n) { data=new int[n]; for (int i = 0; i <data.Length; i++) data[i] = 0; } public void Input() { for(int i=0;i<data.Length;i++) { Console.Write("nhap phan tu thu {0}: ", i); data[i]=Int32.Parse(Console.ReadLine()); } } public void Print() { for(int i=0;i<data.Length;i++) Console.Write(" {0} ",data[i]); } public int this[int i] //indexer { get { return data[i]; } set { data[i] = value; } } }; class CDiem { private int x, y; public CDiem() { x = y = 0; } public CDiem(int d) { x = y = d; } public CDiem(int x, int y) { this.x = x; this.y = y; } public void Nhap() { Console.Write("nhap toa do x: "); x = Convert.ToInt32(Console.ReadLine()); Console.Write("nhap toa do y: "); y = Convert.ToInt32(Console.ReadLine()); } public void Xuat() { Console.Write("({0},{1})", x, y); } public void DiChuyen(int a, int b) { x += a; y += b; } public void Len() { y++; } public void Xuong() { y--; } public void Trai() { x--; } public void Phai() { x++; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } }; class CHinhTron { private CDiem tam; int r;//Ban kinh public CHinhTron() { tam = new CDiem(); //tam.X = tam.Y = 0; r = 1; } public CHinhTron(int x, int y, int r) { tam = new CDiem(x,y); //tam.X = x; tam.Y = y; this.r = r; } public int R { get { return r; } set { r = value;} } public void Nhap() { tam.Nhap(); Console.Write("Nhap ban kinh: "); r = Convert.ToInt32(Console.ReadLine()); } public void Xuat() { Console.Write("["); tam.Xuat(); Console.WriteLine(",{0}]",r); } public double DienTich() { return Math.PI * r * r; } public double ChuVi() { return Math.PI * r * 2; } }; class CTru { private CHinhTron day; private int h;//Chieu cao public CTru() { day = new CHinhTron(); h = 1; } public CTru(int x, int y, int r, int h) { day = new CHinhTron(x, y, r); this.h = h; } public void Nhap() { day.Nhap(); Console.Write("Nhap chieu cao: "); h = Convert.ToInt32(Console.ReadLine()); } public void Xuat() { day.Xuat(); Console.WriteLine("H = {0}",h); } public double DienTichXungQuanh() { return day.ChuVi() * h; } public double DienTichToanPhan() { return day.ChuVi() * h +day.DienTich()*2; } public double TheTich() { return day.DienTich() * h; } }; class SinhVien { private string maSV, hoTen, lop; private float diem1, diem2, diem3; public SinhVien() { } public string MASV { get { return maSV; } set { maSV = value; } } public string HOTEN { get { return hoTen; } set { hoTen = value; } } public string LOP { get { return lop; } set { lop = value; } } public float DIEM1 { get { return diem1; } set { diem1 = value; } } public float DIEM2 { get { return diem2; } set { diem2 = value; } } public float DIEM3 { get { return diem3; } set { diem3 = value; } } public void Nhap() { Console.Write("Nhap ma sinh vien:"); maSV = Console.ReadLine(); Console.Write("Nhap ho ten:"); hoTen = Console.ReadLine(); Console.Write("Nhap lop"); lop = Console.ReadLine(); Console.Write("Nhap diem mon 1:"); diem1 = float.Parse(Console.ReadLine()); Console.Write("Nhap diem mon 2:"); diem2 = float.Parse(Console.ReadLine()); Console.Write("Nhap diem mon 3:"); diem3 = Convert.ToSingle(Console.ReadLine()); } public float TinhDTB() { return (diem1 + diem2 + diem3) / 3; } public string XepLoai() { string loai; if (TinhDTB() >= 8.5) loai = "Gioi"; else if (TinhDTB() >= 7) loai = "Kha"; else if (TinhDTB() >= 5.5) loai = "Trung binh"; else loai = "Yeu kem"; return loai; } public void Xuat() { Console.WriteLine("{0,-10}{1,-25}{2,-7}{3,4} {4,4} {5,4} {6:0.00} {7,- 10}",maSV,hoTen,lop,diem1,diem2,diem3,TinhDTB(),XepLoai()); } }; class DanhSachSinhVien { private SinhVien[] dssv; public DanhSachSinhVien() { } public SinhVien this[int i] { get { return dssv[i]; } set { dssv[i] = value; } } public void Nhap() { int n; Console.Write("Nhap so sinh vien: "); n = int.Parse(Console.ReadLine()); dssv = new SinhVien[n]; for (int i = 0; i < dssv.Length; i++) { dssv[i] = new SinhVien(); Console.WriteLine("Nhap thong tin SV thu {0}:",i); dssv[i].Nhap(); } } public void Xuat() { Console.WriteLine("Ma so Ho ten Lop Diem1 Diem2 Diem3 DTB Loai"); for (int i = 0; i < dssv.Length; i++) dssv[i].Xuat(); } public void GhiFile(string filename) { StreamWriter writer = new StreamWriter(filename); writer.WriteLine(dssv.Length); for (int i = 0; i < dssv.Length; i++) { writer.WriteLine(dssv[i].MASV); writer.WriteLine(dssv[i].HOTEN); writer.WriteLine(dssv[i].LOP); writer.WriteLine(dssv[i].DIEM1); writer.WriteLine(dssv[i].DIEM2); writer.WriteLine(dssv[i].DIEM3); } writer.Close(); } public void DocFile(string filename) { StreamReader reader = new StreamReader(filename); int n = int.Parse(reader.ReadLine()); dssv = new SinhVien[n]; for (int i = 0; i < dssv.Length; i++) { dssv[i] = new SinhVien(); dssv[i].MASV = reader.ReadLine(); dssv[i].HOTEN = reader.ReadLine(); dssv[i].LOP = reader.ReadLine(); dssv[i].DIEM1 = float.Parse(reader.ReadLine()); dssv[i].DIEM2 = float.Parse(reader.ReadLine()); dssv[i].DIEM3 = float.Parse(reader.ReadLine()); } reader.Close(); } public void Them1SinhVien() { Array.Resize<SinhVien>(ref dssv, dssv.Length + 1); dssv[dssv.Length - 1] = new SinhVien(); dssv[dssv.Length - 1].Nhap(); } public int TimKiemSinhVien(string MaSo) { int pos = -1; for(int i=0;i<dssv.Length;i++) if (MaSo == dssv[i].MASV) { pos = i; break; } return pos; } public bool Xoa1SinhVien(string MaSo) { int pos = TimKiemSinhVien(MaSo); if (pos < 0) return false; else { for (int i = pos; i < dssv.Length - 1; i++) dssv[i] = dssv[i + 1]; Array.Resize<SinhVien>(ref dssv,dssv.Length-1); return true; } } public bool SuaDiemSinhVien(string MaSo) { int pos = TimKiemSinhVien(MaSo); if (pos < 0) return false; else { Console.Write("Nhap diem 1 (moi):"); dssv[pos].DIEM1 = int.Parse(Console.ReadLine()); Console.Write("Nhap diem 2 (moi):"); dssv[pos].DIEM2 = int.Parse(Console.ReadLine()); Console.Write("Nhap diem 3 (moi):"); dssv[pos].DIEM3 = int.Parse(Console.ReadLine()); return true; } } }; class Program { static void Main(string[] args) { DanhSachSinhVien ds = new DanhSachSinhVien(); ds.DocFile("D:\\LTHDT.txt"); string MaSo; char ch; do{ Console.WriteLine("Ban hay chon 1 chuc nang:"); Console.WriteLine("1. Them 1 sinh vien."); Console.WriteLine("2. Tim theo ma sinh vien."); Console.WriteLine("3. Xoa sinh vien."); Console.WriteLine("4. Cap nhat thong tin sinh vien."); Console.WriteLine("5. In danh sach sinh vien."); Console.WriteLine("6. Sap xep."); Console.WriteLine("0. Thoat chuong trinh."); ch = Convert.ToChar(Console.ReadLine()); switch (ch) { case '1': ds.Them1SinhVien(); break; case '2': Console.Write("Nhap ma so can tim: "); MaSo = Console.ReadLine(); int pos=ds.TimKiemSinhVien(MaSo); if (pos >= 0) { Console.WriteLine("Tim thay!!!"); ds[pos].Xuat(); } else Console.WriteLine("Khong Tim thay!!!"); break; case '3': Console.Write("Nhap ma so can xoa: "); MaSo = Console.ReadLine(); if (ds.Xoa1SinhVien(MaSo)) Console.WriteLine("Xoa thanh cong!"); else Console.WriteLine("Khong co sinh vien nay!"); break; case '4': Console.Write("Nhap ma so can sua diem: "); MaSo = Console.ReadLine(); if (ds.SuaDiemSinhVien(MaSo)) Console.WriteLine("Da sua thanh cong!"); else Console.WriteLine("Khong co sinh vien nay!"); break; case '5': ds.Xuat(); break; case '6': break; } }while(ch!='0'); ds.GhiFile(@"D:\LTHDT.txt"); } } } [/code]
|