0% found this document useful (0 votes)
8 views12 pages

Bài Thu Hoạch

Uploaded by

nmhung.7676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Bài Thu Hoạch

Uploaded by

nmhung.7676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100

// Cấu trúc dữ liệu cho sinh viên


struct Student {
char id[10];
char name[50];
char className[20];
float mathScore;
float physicsScore;
float chemistryScore;
float averageScore;
};

// Mảng chứa danh sách sinh viên


struct Student
students[MAX_STUDENTS];
int numStudents = 0;
// Hàm thêm sinh viên
void addStudent() {
if (numStudents < MAX_STUDENTS) {
struct Student newStudent;

printf("Nhap ma sinh vien: ");


scanf("%s", newStudent.id);
printf("Nhap ho va ten: ");
scanf("%s", newStudent.name);
printf("Nhap lop: ");
scanf("%s",
newStudent.className);
printf("Nhap diem toan: ");
scanf("%f",
&newStudent.mathScore);
printf("Nhap diem ly: ");
scanf("%f",
&newStudent.physicsScore);
printf("Nhap diem hoa: ");
scanf("%f",
&newStudent.chemistryScore);
// Tính điểm trung bình
newStudent.averageScore =
(newStudent.mathScore +
newStudent.physicsScore +
newStudent.chemistryScore) / 3.0;

students[numStudents++] =
newStudent;
printf("Them sinh vien thanh cong!\
n");
} else {
printf("Danh sach sinh vien da day.
Khong the them sinh vien moi.\n");
}
}

// Hàm sửa thông tin sinh viên


void editStudent() {
char searchId[10];
int found = 0;

printf("Nhap ma sinh vien can sua: ");


scanf("%s", searchId);
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].id, searchId)
== 0) {
printf("Nhap thong tin moi:\n");
printf("Nhap ho va ten: ");
scanf("%s", students[i].name);
printf("Nhap lop: ");
scanf("%s",
students[i].className);
printf("Nhap diem toan: ");
scanf("%f",
&students[i].mathScore);
printf("Nhap diem ly: ");
scanf("%f",
&students[i].physicsScore);
printf("Nhap diem hoa: ");
scanf("%f",
&students[i].chemistryScore);

// Tính lại điểm trung bình


students[i].averageScore =
(students[i].mathScore +
students[i].physicsScore +
students[i].chemistryScore) / 3.0;

printf("Sua sinh vien thanh


cong!\n");
found = 1;
break;
}
}

if (!found) {
printf("Khong tim thay sinh vien
voi ma %s.\n", searchId);
}
}

// Hàm xóa sinh viên


void deleteStudent() {
char searchId[10];
int found = 0;

printf("Nhap ma sinh vien can xoa: ");


scanf("%s", searchId);

for (int i = 0; i < numStudents; i++) {


if (strcmp(students[i].id, searchId)
== 0) {
for (int j = i; j < numStudents -
1; j++) {
students[j] = students[j + 1];
}
numStudents--;

printf("Xoa sinh vien thanh


cong!\n");
found = 1;
break;
}
}
if (!found) {
printf("Khong tim thay sinh vien voi ma
%s.\n", searchId);
}
}
// Hàm hiển thị thông tin sinh viên
void displayStudent(struct Student
student) {
printf("%-10s%-20s%-10s%-10.2f%-
10.2f%-10.2f%-10.2f\n",
student.id, student.name,
student.className,
student.mathScore,
student.physicsScore,
student.chemistryScore,
student.averageScore);
}

// Hàm hiển thị toàn bộ thông tin sinh


viên
void displayAllStudents() {
printf("%-10s%-20s%-10s%-10s%-10s
%-10s%-10s\n",
"Ma SV", "Ho va ten", "Lop",
"Toan", "Ly", "Hoa", "Diem TB");
for (int i = 0; i < numStudents; i++) {
displayStudent(students[i]);
}
}
// Hàm sắp xếp sinh viên theo điểm
trung bình
void sortByAverageScore(int ascending)
{
for (int i = 0; i < numStudents - 1; i+
+) {
for (int j = 0; j < numStudents - i -
1; j++) {
if ((ascending &&
students[j].averageScore > students[j +
1].averageScore) ||
(!ascending &&
students[j].averageScore < students[j +
1].averageScore)) {
struct Student temp =
students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("Sap xep thanh cong!\n");
}
// Hàm tìm kiếm sinh viên theo lớp
void searchByClass() {
char searchClass[20];
int found = 0;

printf("Nhap lop can tim kiem: ");


scanf("%s", searchClass);

printf("%-10s%-20s%-10s%-10s%-10s
%-10s%-10s\n",
"Ma SV", "Ho va ten", "Lop",
"Toan", "Ly", "Hoa", "Diem TB");

for (int i = 0; i < numStudents; i++) {


if (strcmp(students[i].className,
searchClass) == 0) {
displayStudent(students[i]);
found = 1;
}
}
if (!found) {
printf("Khong tim thay sinh vien
trong lop %s.\n", searchClass);
}
}

// Hàm tìm kiếm sinh viên theo điểm


trong khoảng
void searchByScoreRange(float
minScore, float maxScore) {
int found = 0;

printf("%-10s%-20s%-10s%-10s%-10s
%-10s%-10s\n",
"Ma SV", "Ho va ten", "Lop",
"Toan", "Ly", "Hoa", "Diem TB");

for (int i = 0; i < numStudents; i++) {


if (students[i].averageScore >=
minScore && students[i].averageScore
<= maxScore) {
displayStudent(students[i]);
found = 1;
}
}

if (!found) {
printf("Khong tim thay sinh vien
trong khoang diem tu %.2f den %.2f.\n",
minScore, maxScore);
}
}

int main() {
int choice;
float minScore, maxScore;

do {
printf("\n*** MENU ***\n");
printf("1. Them sinh vien\n");
printf("2. Sua thong tin sinh vien\
n");
printf("3. Xoa sinh vien\n");
printf("4. Hien thi danh sach sinh
vien\n");
printf("5. Sap xep sinh vien theo
diem trung binh (tang)\n");
printf("6. Sap xep sinh vien theo
diem trung binh (giam)\n");
printf("7. Tim kiem sinh vien theo
lop\n");
printf("8. Tim kiem sinh vien theo diem
trong khoang\n");

You might also like