Skip to content

Instantly share code, notes, and snippets.

@DungGramer
Created September 4, 2020 19:34
Show Gist options
  • Save DungGramer/7e4e821cd4f9a40126d152b906cc750b to your computer and use it in GitHub Desktop.
Save DungGramer/7e4e821cd4f9a40126d152b906cc750b to your computer and use it in GitHub Desktop.
Tạo class Sinh vien, tính điểm trung bình, sắp xếp theo điểm trung bình, xuất điểm trung bình > 7, xoá 1 vị trí bất kỳ
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define foreach(length) for(int i = 0; i < length; ++i)
class SinhVien {
public:
string maSV;
string hoTen;
string lop;
float tin1, tin2, tin3, tin4, diemTB;
public:
SinhVien() {
maSV = "";
hoTen = "";
lop = "";
tin1 = 0;
tin2 = 0;
tin3 = 0;
tin4 = 0;
diemTB = 0;
}
void tinhTB() {
diemTB = (tin1 + tin2 + tin3 + tin4) / 4;
}
float getDiemTB() {
return diemTB;
}
void nhap() {
cout<<"Nhap ma SV: "; cin.ignore(); getline(cin, maSV);
cout<<"Nhap ho ten: "; getline(cin, hoTen);
cout<<"Nhap lop: "; getline(cin, lop);
cout<<"Nhap diem Tin 1 - 2 - 3 - 4: ";
cin>>tin1>>tin2>>tin3>>tin4;
tinhTB();
}
void in() {
cout<<"Ma SV: "<<maSV<<" | ";
cout<<"Ho ten: "<<hoTen<<" | ";
cout<<"Lop: "<<lop<<" | ";
cout<<"Tin 1: "<<tin1<<" | ";
cout<<"Tin 2: "<<tin2<<" | ";
cout<<"Tin 3: "<<tin3<<" | ";
cout<<"Tin 4: "<<tin4<<" | ";
cout<<"Diem trung binh: "<<diemTB<<"\n";
};
};
void nhapLop(SinhVien sv[], int n) {
foreach(n) {
cout<<"Nhap thu sinh thu "<<i + 1<<endl;
sv[i].nhap();
}
}
void inLop(SinhVien sv[], int n) {
foreach(n) sv[i].in();
}
void swap(SinhVien *a, SinhVien *b) {
SinhVien temp;
temp = *a;
*a = *b;
*b = temp;
}
void bubleSort(SinhVien sv[], int length) {
int i, j;
for(int i = 0; i < length - 1; i++) {
for(int j = 0; j < length - i - 1; j++) {
if(sv[j].getDiemTB() > sv[j + 1].getDiemTB()) {
swap(&sv[j], &sv[j + 1]);
}
}
}
}
void lonHon7(SinhVien sv[], int length) {
foreach(length) {
if(sv[i].getDiemTB() > 7) {
sv[i].in();
}
}
}
void xoaPhanTu(SinhVien sv[], int &length, int pos) {
if(pos < 0) pos = 0;
if(pos >= length) pos = length - 1;
for(int i = pos; i < length - 1; i++) {
sv[i] = sv[i + 1];
}
length--;
}
int main() {
SinhVien sv[30];
int n;
cout<<"Nhap si so cua lop: "; cin>>n;
nhapLop(sv, n);
inLop(sv, n);
cout<<"\nSap xep theo diem trung binh: \n";
bubleSort(sv, n);
inLop(sv, n);
cout<<"\nDiem trung binh > 7: \n";
lonHon7(sv, n);
int key;
cout<<"\nNhap vi tri muon xoa: "; cin>>key;
cout<<"\nMang sau khi xoa: \n";
xoaPhanTu(sv, n, key - 1);
inLop(sv, n);
cout<<endl; system("pause"); return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment