Bài Tập 1 - Lập Trình Hướng Đối Tượng
Thứ Bảy, 13 tháng 10, 2018
/
No Comments
#include<iostream> #include<string.h> using namespace std; class ThiSinh{ private: char Maso[32], Hoten[32], Ketqua[9]; float DiemTB, DiemLT, DiemTH; public: ThiSinh(); char* getMaso(){ return Maso; } char* getHoten(){ return Hoten; } char* getKetqua(){ return Ketqua; } float getDiemTB(){ return DiemTB; } float getDiemLT(){ return DiemLT; } float getDiemTH(){ return DiemTH; } void Nhap(); void Xuat(); }; class MangThiSinh{ private: ThiSinh mang[100]; int n; public: void NhapMang(); void XuatMang(); void LietKe_ThiSinh_DiemTH_Va_LT_Tren7(); float TrungBinhCong_DiemThiSinh_List(); void Search_with_code(); void XapXep_TheoTen(); }; int main(){ // Mang Thi Sinh MangThiSinh mang_ts; //Bat dau nhap mang thi sinh cout<<"\n============== NHAP MANG ===============\n"<<endl; mang_ts.NhapMang(); //Xuat Mang Da Nhap cout<<"\n============== XUAT MANG ===============\n"<<endl; mang_ts.XuatMang(); //Liet Ke Thi sinh co diem LT & TH tren 7 cout<<"\n============== Tren 7 ===============\n"<<endl; mang_ts.LietKe_ThiSinh_DiemTH_Va_LT_Tren7(); //Tinh Tong Diem Trong List cout<<"\n============== TONG MANG ===============\n"<<endl; cout<<"Tong Mang = "<<mang_ts.TrungBinhCong_DiemThiSinh_List()<<endl; // Tim Kiem bang ho ten hoac bang ma so cout<<"\n============== Search MANG ===============\n"<<endl; mang_ts.Search_with_code(); //xap xiep theo ten cout<<"\n============== XAP XEP MANG ==============="<<endl; mang_ts.XapXep_TheoTen(); mang_ts.XuatMang(); return 0; } ThiSinh::ThiSinh(){ DiemLT = DiemTB = DiemTH = 0.0; strcpy(Hoten," "); strcpy(Maso," "); strcpy(Ketqua," "); } void ThiSinh::Nhap(){ cin.ignore(); cout<<"Nhap Ten Thi Sinh: "; gets(Hoten); cout<<"Nhap Ma So Thi Sinh: "; gets(Maso); cout<<"Nhap Diem Ly Thuyet: "; cin>>DiemLT; while(DiemLT > 10 || DiemLT < 0){ cout<<"Diem Phai > 0 Va < 10"<<endl; cout<<"Nhap Diem Ly Thuyet: "; cin>>DiemLT; } cout<<"Nhap Diem Thuc Hanh: "; cin>>DiemTH; while(DiemTH > 10 || DiemTH < 0){ cout<<"Diem Phai > 0 Va < 10"<<endl; cout<<"Nhap Diem Thuc Hanh: "; cin>>DiemTH; } if(((getDiemLT() + getDiemTH()*2)/3) >= 5) strcpy(Ketqua,"DAT"); else strcpy(Ketqua,"KHONG DAT"); DiemTB = (DiemLT + (DiemTH*2))/3; } void ThiSinh::Xuat(){ cout<<"Ten Thi Sinh: \t\t"<<getHoten()<<endl; cout<<"Ma So Thi Sinh: \t"<<getMaso()<<endl; cout<<"Diem Ly Thuyet: \t"<<getDiemLT()<<endl; cout<<"Diem Thuc Hanh: \t"<<getDiemTH()<<endl; cout<<"Diem TB: \t\t"<<getDiemTB()<<endl; cout<<"Ket Qua: \t\t"<<getKetqua()<<endl; } //---------------------------------------------------- void MangThiSinh::NhapMang(){ cout<<"Nhap So Luong Thi Sinh: "; cin>>n; for(int i=0; i<n; i++){ cout<<"Nhap Thi Sinh Thu [" << i+1<<"] / ["<<n<<"]"<<endl; mang[i].Nhap(); cout<<endl; } } void MangThiSinh::XuatMang(){ for(int i=0; i<n; i++){ cout<<"Thi Sinh Thu [" << i+1<<"] / ["<<n<<"]"<<endl; mang[i].Xuat(); cout<<endl; } } void MangThiSinh::LietKe_ThiSinh_DiemTH_Va_LT_Tren7(){ int a = 0; for(int i=0; i<n; i++) if(mang[i].getDiemLT()>7 && mang[i].getDiemTH()>7){ mang[i].Xuat(); cout<<endl; a+= 1; } if(a == 0) cout<<"Khong co thi sinh LT & TH > 7"<<endl; } float MangThiSinh::TrungBinhCong_DiemThiSinh_List(){ float result = 0; for(int i=0; i<n; i++) result+= mang[i].getDiemTB(); return result; } void MangThiSinh::Search_with_code(){ // int b; // do{ // // cout<<"==== MENU ===="<<endl; // cout<<"0. Exit\t1. Tim Tiep"<<endl; // cout<<"Ban Muon Chon: "; cin>>b; // // char x[32]; bool a = true; // cout<<"Nhap Ma So Hoac Ten Thi Sinh Can Tim: "; fflush(stdin); gets(x); // // for(int i=0; i<n; i++){ // if((strcmp(mang[i].getMaso(),x) == 0) || (strcmp(mang[i].getHoten(),x) == 0)){ // cout<<"Da Tim Thay Thi Sinh ["<<x<<"]"<<endl; // mang[i].Xuat(); // } // else a = false; // } // if(a == false) cout<<"Khong tim thay ["<<x<<"]"<<endl; // }while(b!=0); char x[32]; bool a = true; cout<<"Nhap Ma So Hoac Ten Thi Sinh Can Tim: "; fflush(stdin); gets(x); for(int i=0; i<n; i++){ if((strcmp(mang[i].getMaso(),x) == 0) || (strcmp(mang[i].getHoten(),x) == 0)){ cout<<"Da Tim Thay Thi Sinh ["<<x<<"]"<<endl; mang[i].Xuat(); } else a = false; } if(a == false) cout<<"Khong tim thay ["<<x<<"]"<<endl; } void MangThiSinh::XapXep_TheoTen(){ ThiSinh temp; for(int i=0; i<n-1; i++) for(int j=i+1; j<n; j++){ if(strcmp(mang[i].getHoten(),mang[j].getHoten()) > 0){ temp = mang[i]; mang[i] = mang[j]; mang[j] = temp; } } }
#include<iostream> #include<conio.h> #include<string.h> #include<stdio.h> using namespace std; class ThiSinh{ private: char Maso[12], Hoten[32], KetQua[12]; float PointLT, PointTH, PointTB; public: ThiSinh(); char* getMaso(){ return Maso; } char* getHoten(){ return Hoten; } char* getKetQua(){ return KetQua; } float getPointLT(){ return PointLT; } float getPointTH(){ return PointTH; } float getPointTB(){ return PointTB; } void Nhap(); void Xuat(); float TinhTB(); void XepLoai(); }; class Array_ThiSinh{ private: ThiSinh arr[100]; int n; public: void Nhap(); void Arr_Xuat(); void LietKe_PointTH_PointLT_Tren_7(); float PointTBC_List(); void Search_Code(); void XapXepTenTangDan(); }; int main(){ Array_ThiSinh arr_ts; cout<<"Nhap Thong Tin Cho Cac Thi Sinh"<<endl; arr_ts.Nhap(); cout<<"======================================="<<endl; cout<<"Xuat Thong Tin Thi Sinh Da Nhap"<<endl; arr_ts.Arr_Xuat(); cout<<"======================================="<<endl; cout<<"Thi Sinh Co Diem TH Va LT Lon Hon 7"<<endl; arr_ts.LietKe_PointTH_PointLT_Tren_7(); cout<<"======================================="<<endl; cout<<"Point TBC Of List"<<endl; cout<<"Result = "<<arr_ts.PointTBC_List(); cout<<"\n\n======================================="<<endl; cout<<"Tim Thi Sinh Bang Ma So\n"<<endl; arr_ts.Search_Code(); cout<<"\n\n======================================="<<endl; cout<<"Xap Xep Ten Tang Dan"<<endl; arr_ts.XapXepTenTangDan(); arr_ts.Arr_Xuat(); _getch(); return 0; } ThiSinh::ThiSinh(){ PointLT = PointTB = PointTH = 0; strcpy(Maso,""); strcpy(Hoten,""); strcpy(KetQua,""); } void ThiSinh::Nhap(){ cin.ignore(); cout<<"Nhap Ten Thi Sinh: "; gets(Hoten); fflush(stdin); cout<<"Nhap Ma So Thi Sinh: "; gets(Maso); fflush(stdin); cout<<"Nhap Diem Ly Thuyet: "; cin>>PointLT; cout<<"Nhap Diem Thuc Hanh: "; cin>>PointTH; } float ThiSinh::TinhTB(){ PointTB = (PointLT + PointTH*2) / 3; return PointTB; } void ThiSinh::Xuat(){ cout<<"Ten: \t\t" << getHoten()<<endl; cout<<"Maso: \t\t"<<getMaso()<<endl; cout<<"PointLT= \t"<<getPointLT()<<endl; cout<<"PointTH= \t"<<getPointTH()<<endl; cout<<"PointTB= \t"<<getPointTB()<<endl; cout<<"Xep Loai: \t"<<getKetQua()<<endl; } void ThiSinh::XepLoai(){ if(getPointTB() >= 5) strcpy(KetQua,"DAT"); else strcpy(KetQua,"KHONG DAT"); } //---------------------------------------------------------- void Array_ThiSinh::Nhap(){ cout<<"Nhap So Luong Phan Tu: "; cin>>n; for(int i=0; i<n; i++){ cout<<"Nhap Thi Sinh Thu "<<i+1<<endl; arr[i].Nhap(); arr[i].TinhTB(); arr[i].XepLoai(); cout<<"\n"; } } void Array_ThiSinh::Arr_Xuat(){ for(int i=0; i<n; i++){ cout<<"Thi Sinh Thu: "<<i+1<<endl; arr[i].Xuat(); cout<<"\n"; } } void Array_ThiSinh::LietKe_PointTH_PointLT_Tren_7(){ for(int i=0; i<n; i++){ if(arr[i].getPointLT() > 7 && arr[i].getPointTH() >7) arr[i].Xuat(); cout<<"\n"; } } float Array_ThiSinh::PointTBC_List(){ float TB; for(int i=0; i<n; i++){ TB+= arr[i].getPointTB(); } return TB; } void Array_ThiSinh::Search_Code(){ char x[12]; bool a = true; cout<<("Nhap Ma So Can Tim: "); fflush(stdin); gets(x); for(int i=0; i<n; i++){ if(strcmp(arr[i].getMaso(),x) == 0){ cout<<"\nDa Tim Thay ["<<x<<"]"<<endl; arr[i].Xuat(); } //else a = false; } //if(a == false) cout<<"Khong Tim Thay ["<<x<<"]"<<endl; } void Array_ThiSinh::XapXepTenTangDan(){ ThiSinh t; for(int i=0;i<n-1;i++){ for(int j=i+1; j<n;j++){ if(strcmp(arr[i].getHoten(),arr[j].getHoten()) > 0){ t= arr[i]; arr[i]= arr[j]; arr[j]= t; } } } }
#include <conio.h> #include <stdio.h> #include <math.h> #include <iomanip> #include <iostream> using namespace std; class SoPhuc { private: float Thuc, Ao; public: SoPhuc(); // Constructor mac dinh SoPhuc(float R,float I); SoPhuc (const SoPhuc & Z); // Constructor sao chep SoPhuc (float R); // Constructor chuyen doi void Nhap(); //Nhap so phuc void Xuat(); // Hien thi so phuc //float getThuc(); //float getAo(); //void setThuc(int t); //void setAo(int a); // Cac tinh toan SoPhuc operator + (SoPhuc Z); //SoPhuc operator - (SoPhuc Z); //SoPhuc operator * (SoPhuc Z); //SoPhuc operator / (SoPhuc Z); //Ham tinh toan SoPhuc Cong(SoPhuc Z); //SoPhuc Tru(SoPhuc Z); //SoPhuc Nhan(SoPhuc Z); //SoPhuc Chia(SoPhuc Z); //int LonHon(SoPhuc Z); //int NhoHon(SoPhuc Z); //int Bang(SoPhuc Z); // Cac tinh toan so sanh ////int operator == (SoPhuc Z); //int operator != (SoPhuc Z); //int operator > (SoPhuc Z); //int operator >= (SoPhuc Z); //int operator < (SoPhuc Z); //int operator <= (SoPhuc Z); float Module(); // Giá tri tuyet doi cua so phuc //Cac ham kiem tra //int ThuanAo(); //int ThuanThuc(); }; class MangSoPhuc { private: SoPhuc a[100]; int n; public: void Nhap(); void Xuat(); //..cac phuong thuc cn lai }; int main() { MangSoPhuc x; //Nhap danh sach cout<<"Nhap thong tin cac So phuc trong Mang: "; x.Nhap(); cout<<"Thong tin cac So phuc trong Mang: "; x.Xuat(); //..Them code getch(); return 1; } SoPhuc::SoPhuc() { Thuc = 0.0; Ao = 0.0; } SoPhuc::SoPhuc(float R,float I) { Thuc = R; Ao = I; } SoPhuc::SoPhuc(const SoPhuc & Z) //Ham dung sao chep { Thuc = Z.Thuc; Ao = Z.Ao; } SoPhuc::SoPhuc(float R) { Thuc = R; Ao = 0.0; } void SoPhuc::Nhap() { cout<<"\n Nhap phan thuc "; cin>>Thuc; cout<<"\n Nhap phan ao "; cin>>Ao; } void SoPhuc::Xuat() { cout<<"(" << Thuc << " + " << Ao << "i)"; } /* Cho hai so phuc z = a + bi và w = c + di. Phep Cong: z+w = (a +c) + (b+d)i Phep Tru: z-w = (a +c) - (b+d)i Phep Nhan: z.w = (a * c - b * d) + (a * d + b * c)i Phep Chia: z/w = [(a * c + b * d) / (c * c + d * d)] + (b * c - a * d)i */ SoPhuc SoPhuc::Cong(SoPhuc Z) { SoPhuc Tmp; Tmp.Thuc = Thuc + Z.Thuc; Tmp.Ao = Ao + Z.Ao; return Tmp; } float SoPhuc::Module() { return sqrt(Thuc*Thuc+Ao*Ao); } //CAC PHUONG THUC THUOC LOP MangSoPhuc void MangSoPhuc::Nhap() { cout <<"\n Nhap n : "; cin >> n; for (int i=0;i<n;i++) { cout<<"Nhap a["<<i<<"]: "; a[i].Nhap(); } } void MangSoPhuc::Xuat() { for (int i=0;i<n;i++) { a[i].Xuat(); cout << " "; } }
-- 1. EXEC SP_ATTACH_DB 'QUANLYSACH', 'Z:\QLBS.mdf','Z:\QLBS_log.ldf' USE QUANLYSACH 2. CREATE TABLE [dbo].[CHAMCONGNV]( [MaNV] [char](5) NOT NULL, [HoLot] [nvarchar](25) NULL, [TenNV] [nvarchar](10) NULL, [Phai] [nvarchar](3) NULL, [NgaySinh] [date] NULL, [DiaChi] [nvarchar](40) NULL, CONSTRAINT [PK_CHAMCONGNV] PRIMARY KEY CLUSTERED ( [MaNV] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] 3. ALTER TABLE CHAMCONGNV ADD NGAYCONG INT ALTER TABLE CHAMCONGNV ADD DONGIAGIACONG INT select * from CHAMCONGNV 4. ALTER TABLE CHAMCONGNV DROP COLUMN HOLOT ALTER TABLE CHAMCONGNV DROP COLUMN TENNV ALTER TABLE CHAMCONGNV DROP COLUMN PHAI ALTER TABLE CHAMCONGNV DROP COLUMN NGAYSINH ALTER TABLE CHAMCONGNV DROP COLUMN DIACHI 5. ALTER TABLE HOADON ADD NGAYLAPHD CHAR(10) 6. ALTER TABLE HOADON ALTER COLUMN NGAYLAPHD DATETIME 7. ALTER DATABASE QUANLYSACH MODIFY NAME= QLSACH 8. EXEC SP_RENAME 'CHAMCONGNV', 'BANGLUONG' 9. DROP TABLE [BANGLUONG] 10. -KHÔNG ĐƯỢC VÌ CÒN RÀNG BUỘC KHÓA CHÍNH, MUỐN XÓA PHẢI XÓA KHÓA CHÍNH BẰNG LỆNH ALTER TABLE HOADON DROP CONSTRAINT FK_CTHD_Hoadon DROP TABLE [HOADON]
int n, s= 0; printf("Nhap vao 1 so: "); scanf("%i", &n); for(int i=2;i<=n;i++){ if(n%i == 0){ s= s+ (n/i); } } if(s == n) printf("=> %i La So Hoan Hao!",n); else printf("=> %i Khong La So hoan Hao!",n);