Skip to content

Instantly share code, notes, and snippets.

@scorta
Created October 31, 2017 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scorta/b13569c3e8373a3ea3720fa2ce0c4539 to your computer and use it in GitHub Desktop.
Save scorta/b13569c3e8373a3ea3720fa2ce0c4539 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <iomanip>
#include "json.hpp"
using namespace std;
using json = nlohmann::json;
struct SV
{
json sv;
bool operator<(const SV &b) const {
if (this->sv["name"] < b.sv["name"])
return true;
else if (this->sv["name"] == b.sv["name"])
if (this->sv["family name"] < b.sv["family name"])
return true;
return false;
}
};
// Cho hai danh sách sinh viên theo định dạng JSON như sau
json sv1 = R"(
{
"SV": [
{"id": 1,
"name": "E",
"family name": "Nguyen Van",
"grade": 6.0
},
{"id": 3,
"name": "A",
"family name": "Nguyen Thi",
"grade": 9.5
},
{"id": 5,
"name": "C",
"family name": "Phung Thanh",
"grade": 8.5
}
]
}
)"_json;
json sv2 = R"(
{
"SV": [
{"id": 2,
"name": "D",
"family name": "Nguyen Van",
"grade": 8
},
{"id": 4,
"name": "C",
"family name": "Nguyen Thi",
"grade": 5.5
},
{"id": 6,
"name": "B",
"family name": "Phung Thanh",
"grade": 6.5
}
]
}
)"_json;
void ImportSV(const int &n, const json &json_data, vector<SV> &sv_list){
SV tmp;
for(int i = 0; i < n; ++i){
tmp.sv = json_data["SV"][i];
sv_list.push_back(tmp);
}
sort(sv_list.begin(), sv_list.end());
}
void PrintSV(const SV &sv_){
// printf("%2s\n | %4s | %15s | %f\n", sv_.sv["id"], sv_.sv["name"], sv_.sv["family name"], sv_.sv["grade"]);
cout << sv_.sv["id"] << setw(4) << "| " << sv_.sv["name"] << setw(4) << "| " << sv_.sv["family name"] << setw(6) << "| " << sv_.sv["grade"] << endl;
}
int main() {
// Hãy sắp xếp hai danh sách theo tên sinh viên, nếu trùng tên thì sắp xếp theo họ
// Hợp nhất hai danh sách và xuất kết quả
// Đếm số lượng sinh viên giỏi (grade>=8.5), khá (grade >= 6.5 và < 8.5),
// và trung bình (grade >= 5.5 và < 6.5)
int n_sv1 = 3, n_sv2 = 3; //number of students in each lists
vector<SV> sv_list1, sv_list2, sv_list (n_sv1 + n_sv2);
unsigned int sv_gioi = 0, sv_kha = 0, sv_tb = 0;
//Import and Sort
ImportSV(n_sv1, sv1, sv_list1);
ImportSV(n_sv2, sv2, sv_list2);
//Merge
merge(sv_list1.begin(), sv_list1.end(), sv_list2.begin(),sv_list2.end(), sv_list.begin());
//Print the list and count
printf("Danh sach sinh vien:\n");
printf("ID | Name | Family Name | Grade\n");
for(int i = 0; i < n_sv1 + n_sv2; ++i){
PrintSV(sv_list[i]);
// cout << sv_list[i].sv << endl;
if(sv_list[i].sv["grade"] >= 8.5)
sv_gioi++;
else if(sv_list[i].sv["grade"] >= 6.5)
sv_kha++;
else if(sv_list[i].sv["grade"] >= 5.5)
sv_tb++;
}
printf("So luong sinh vien:\n - Gioi: %d\n - Kha: %d\n - Trung Binh: %d\n", sv_gioi, sv_kha, sv_tb);
return 0;
// * Hãy cố gắng giải quyết vấn đề theo cách đơn giản nhất
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment