By GokiSoft.com| 20:06 06/01/2022|
Lập Trình C

[Video] Quản lý sinh viên 2 - Assigment - Lập trình C - C2110L

Quản lý sinh viên 2 - Assigment - Lập trình C



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct Student_ST {
	char fullname[30];
	char rollNo[10];
	int year;
	float mark;
} Student;

void showMenu();
void input(Student *p);
void display(Student p[3]);
void searchByYear(Student p[3]);
void saveFile(Student *p);
void readFile(Student *p);
void saveTextFile(Student *p);

int main(int argc, char *argv[]) {
	//1) Khai bao mang sinh vien
	Student studentList[3];
	
	char choose;
	do {
		showMenu();
		fflush(stdin); fflush(stdout);
		scanf("%c", &choose);
		
		switch(choose) {
			case '1': {
				input(studentList);
				break;
			}
			case '2': {
				display(studentList);
				break;
			}
			case '3': {
				searchByYear(studentList);
				break;
			}
			case '4': {
				saveFile(studentList);
				break;
			}
			case '5': {
				readFile(studentList);
				break;
			}
			case '6': {
				saveTextFile(studentList);
				break;
			}
			case 'N':
			case 'n':
			case '7': {
				printf("\nThoat chuong trinh!!!");
				break;
			}
			default: {
				printf("\nNhap sai!!!");
				break;
			}
		}
	} while(choose != '7' && choose != 'N' && choose != 'n');
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap thong tin sinh vien");
	printf("\n2. Sap xep, hien thi, thong ke");
	printf("\n3. Tim kiem theo nam sinh");
	printf("\n4. Luu student.dat");
	printf("\n5. Doc student.dat");
	printf("\n6. Luu student.txt");
	printf("\n7. Thoat (7 | N | n)");
	printf("\nChon: ");
}

void input(Student *p) {
	int i;
	for(i=0;i<3;i++) {
		printf("\nNhap thong tin sinh vien thu %d", i + 1);
		printf("\nNhap ten: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].fullname);
		
		printf("\nNhap MSV: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].rollNo);
		
		printf("\nNhap nam sinh: ");
		scanf("%d", &p[i].year);
		
		printf("\nNhap diem: ");
		scanf("%f", &p[i].mark);
	}
}

void display(Student p[3]) {
	int i, j;
	
	//1) Sort sinh vien theo diem giam dan
	for(i=0;i<2;i++) {
		for(j=i+1;j<3;j++) {
			if(p[i].mark < p[j].mark) {
				//doi cho vi tri 2 phan tu
				Student tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
	
	//2) Hien thi thong tin sinh vien
	char status[30];
	
	printf("\n%3s|%15s|%15s|%5s|%5s|%15s|", "No", "Name", "Roll Number", "Year", "Mark", "Status");
	for(i=0;i<3;i++) {
		if(p[i].mark >= 7.5) {
			strcpy(status, "DISTINCTION");
		} else if(p[i].mark >= 6) {
			strcpy(status, "CREDIT");
		} else if(p[i].mark >= 4) {
			strcpy(status, "PASS");
		} else {
			strcpy(status, "FAIL");
		}
		
		printf("\n%3d|%15s|%15s|%5d|%5.2f|%15s|", i + 1, p[i].fullname, p[i].rollNo, p[i].year, p[i].mark, status);
	}
	
	//3) Bao cao thong ke
	//Su dung duoc cho duy nhat bai nay
	int dCount = 0, cCount = 0, pCount = 0, fCount = 0;
	for(i=0;i<3;i++) {
		if(p[i].mark >= 7.5) {
			dCount++;
		} else if(p[i].mark >= 6) {
			cCount++;
		} else if(p[i].mark >= 4) {
			pCount++;
		} else {
			fCount++;
		}
	}
	
	if(pCount > 0) {
		printf("\nDISTINCTION gom %d sinh vien", dCount);
	}
	if(cCount > 0) {
		printf("\nCREDIT gom %d sinh vien", cCount);
	}
	if(pCount > 0) {
		printf("\nPASS gom %d sinh vien", pCount);
	}
	if(fCount > 0) {
		printf("\nFAIL gom %d sinh vien", fCount);
	}
	
	//4) Thong ke so sinh vien theo nam sinh: 1999 -> 2 sinh vien, 2000 -> 1 sinh vien
	int count[3];
	//Khoi tao gia tri cho mang count
	for(i=0;i<3;i++) {
		count[i] = 1;
	}
	
	for(i=0;i<2;i++) {
		if(count[i] == 0) {
			continue;
		}
		
		for(j=i+1;j<3;j++) {
			if(count[j] == 0) {
				continue;
			}
			
			if(p[i].year == p[j].year) {
				count[i]++;
				count[j]--;
			}
		}
	}
	
	//Hien thi ket qua thong ke
	for(i=0;i<3;i++) {
		if(count[i] > 0) {
			printf("\nNam sinh %d gom %d sinh vien", p[i].year, count[i]);
		}
	}
}

void searchByYear(Student p[3]) {
	int year, i, count = 0;
	printf("\nNhap nam sinh can tim kiem: ");
	scanf("%d", &year);
	
	char status[30];
	
	for(i=0;i<3;i++) {
		if(p[i].year == year) {
			if(count == 0) {
				printf("\n%3s|%15s|%15s|%5s|%5s|%15s|", "No", "Name", "Roll Number", "Year", "Mark", "Status");
			}
			count++;
			
			//Hien thi
			if(p[i].mark >= 7.5) {
				strcpy(status, "DISTINCTION");
			} else if(p[i].mark >= 6) {
				strcpy(status, "CREDIT");
			} else if(p[i].mark >= 4) {
				strcpy(status, "PASS");
			} else {
				strcpy(status, "FAIL");
			}
			
			printf("\n%3d|%15s|%15s|%5d|%5.2f|%15s|", i + 1, p[i].fullname, p[i].rollNo, p[i].year, p[i].mark, status);
		}
	}
	if(count == 0) {
		printf("\nKhong tim thay sinh vien co nam sinh nay");
	}
}

void saveFile(Student *p) {
	FILE *fp;
	
	fp = fopen("student.dat", "wb");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		fwrite(p, sizeof(Student), 3, fp);
	}
	
	fclose(fp);
	
	printf("\nGhi file thanh cong");
}

void readFile(Student *p) {
	FILE *fp;
	
	fp = fopen("student.dat", "rb");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		fread(p, sizeof(Student), 3, fp);
	}
	
	fclose(fp);
	
	printf("\nDoc file thanh cong");
}

void saveTextFile(Student *p) {
	//Luu du lieu vao file text nhu nao
	//Moi sinh vien duoc luu theo dinh dang sao
	//Luu nhu option: 2
	FILE *fp;
	
	fp = fopen("E:\student.txt", "w");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		//Ghi du lieu vao file
		char status[30];
		int i;
	
		fprintf(fp, "\n%3s|%15s|%15s|%5s|%5s|%15s|", "No", "Name", "Roll Number", "Year", "Mark", "Status");
		for(i=0;i<3;i++) {
			if(p[i].mark >= 7.5) {
				strcpy(status, "DISTINCTION");
			} else if(p[i].mark >= 6) {
				strcpy(status, "CREDIT");
			} else if(p[i].mark >= 4) {
				strcpy(status, "PASS");
			} else {
				strcpy(status, "FAIL");
			}
			
			fprintf(fp, "\n%3d|%15s|%15s|%5d|%5.2f|%15s|", i + 1, p[i].fullname, p[i].rollNo, p[i].year, p[i].mark, status);
		}
	}
	
	fclose(fp);
	
	printf("\nGhi file thanh cong");
}


SỬA LẠI ĐỀ: CHO PHÉP NHẬP SỐ SINH VIÊN CẦN QUẢN LÝ



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct Student_ST {
	char fullname[30];
	char rollNo[10];
	int year;
	float mark;
} Student;

int N;

void showMenu();
void input(Student *p);
void display(Student *p);
void searchByYear(Student *p);
void saveFile(Student *p);
void readFile(Student *p);
void saveTextFile(Student *p);

int main(int argc, char *argv[]) {
	//1) Khai bao mang sinh vien
	printf("\nNhap so sinh vien can quan ly: ");
	scanf("%d", &N);
	
	Student studentList[N];
	
	char choose;
	do {
		showMenu();
		fflush(stdin); fflush(stdout);
		scanf("%c", &choose);
		
		switch(choose) {
			case '1': {
				input(studentList);
				break;
			}
			case '2': {
				display(studentList);
				break;
			}
			case '3': {
				searchByYear(studentList);
				break;
			}
			case '4': {
				saveFile(studentList);
				break;
			}
			case '5': {
				readFile(studentList);
				break;
			}
			case '6': {
				saveTextFile(studentList);
				break;
			}
			case 'N':
			case 'n':
			case '7': {
				printf("\nThoat chuong trinh!!!");
				break;
			}
			default: {
				printf("\nNhap sai!!!");
				break;
			}
		}
	} while(choose != '7' && choose != 'N' && choose != 'n');
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap thong tin sinh vien");
	printf("\n2. Sap xep, hien thi, thong ke");
	printf("\n3. Tim kiem theo nam sinh");
	printf("\n4. Luu student.dat");
	printf("\n5. Doc student.dat");
	printf("\n6. Luu student.txt");
	printf("\n7. Thoat (7 | N | n)");
	printf("\nChon: ");
}

void input(Student *p) {
	int i;
	for(i=0;i<N;i++) {
		printf("\nNhap thong tin sinh vien thu %d", i + 1);
		printf("\nNhap ten: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].fullname);
		
		printf("\nNhap MSV: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].rollNo);
		
		printf("\nNhap nam sinh: ");
		scanf("%d", &p[i].year);
		
		printf("\nNhap diem: ");
		scanf("%f", &p[i].mark);
	}
}

void display(Student *p) {
	int i, j;
	
	//1) Sort sinh vien theo diem giam dan
	for(i=0;i<N-1;i++) {
		for(j=i+1;j<N;j++) {
			if(p[i].mark < p[j].mark) {
				//doi cho vi tri 2 phan tu
				Student tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
	
	//2) Hien thi thong tin sinh vien
	char status[30];
	
	printf("\n%3s|%15s|%15s|%5s|%5s|%15s|", "No", "Name", "Roll Number", "Year", "Mark", "Status");
	for(i=0;i<N;i++) {
		if(p[i].mark >= 7.5) {
			strcpy(status, "DISTINCTION");
		} else if(p[i].mark >= 6) {
			strcpy(status, "CREDIT");
		} else if(p[i].mark >= 4) {
			strcpy(status, "PASS");
		} else {
			strcpy(status, "FAIL");
		}
		
		printf("\n%3d|%15s|%15s|%5d|%5.2f|%15s|", i + 1, p[i].fullname, p[i].rollNo, p[i].year, p[i].mark, status);
	}
	
	//3) Bao cao thong ke
	//Su dung duoc cho duy nhat bai nay
	int dCount = 0, cCount = 0, pCount = 0, fCount = 0;
	for(i=0;i<N;i++) {
		if(p[i].mark >= 7.5) {
			dCount++;
		} else if(p[i].mark >= 6) {
			cCount++;
		} else if(p[i].mark >= 4) {
			pCount++;
		} else {
			fCount++;
		}
	}
	
	if(pCount > 0) {
		printf("\nDISTINCTION gom %d sinh vien", dCount);
	}
	if(cCount > 0) {
		printf("\nCREDIT gom %d sinh vien", cCount);
	}
	if(pCount > 0) {
		printf("\nPASS gom %d sinh vien", pCount);
	}
	if(fCount > 0) {
		printf("\nFAIL gom %d sinh vien", fCount);
	}
	
	//4) Thong ke so sinh vien theo nam sinh: 1999 -> 2 sinh vien, 2000 -> 1 sinh vien
	int count[N];
	//Khoi tao gia tri cho mang count
	for(i=0;i<N;i++) {
		count[i] = 1;
	}
	
	for(i=0;i<N-1;i++) {
		if(count[i] == 0) {
			continue;
		}
		
		for(j=i+1;j<N;j++) {
			if(count[j] == 0) {
				continue;
			}
			
			if(p[i].year == p[j].year) {
				count[i]++;
				count[j]--;
			}
		}
	}
	
	//Hien thi ket qua thong ke
	for(i=0;i<N;i++) {
		if(count[i] > 0) {
			printf("\nNam sinh %d gom %d sinh vien", p[i].year, count[i]);
		}
	}
}

void searchByYear(Student *p) {
	int year, i, count = 0;
	printf("\nNhap nam sinh can tim kiem: ");
	scanf("%d", &year);
	
	char status[30];
	
	for(i=0;i<N;i++) {
		if(p[i].year == year) {
			if(count == 0) {
				printf("\n%3s|%15s|%15s|%5s|%5s|%15s|", "No", "Name", "Roll Number", "Year", "Mark", "Status");
			}
			count++;
			
			//Hien thi
			if(p[i].mark >= 7.5) {
				strcpy(status, "DISTINCTION");
			} else if(p[i].mark >= 6) {
				strcpy(status, "CREDIT");
			} else if(p[i].mark >= 4) {
				strcpy(status, "PASS");
			} else {
				strcpy(status, "FAIL");
			}
			
			printf("\n%3d|%15s|%15s|%5d|%5.2f|%15s|", i + 1, p[i].fullname, p[i].rollNo, p[i].year, p[i].mark, status);
		}
	}
	if(count == 0) {
		printf("\nKhong tim thay sinh vien co nam sinh nay");
	}
}

void saveFile(Student *p) {
	FILE *fp;
	
	fp = fopen("student.dat", "wb");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		fwrite(p, sizeof(Student), N, fp);
	}
	
	fclose(fp);
	
	printf("\nGhi file thanh cong");
}

void readFile(Student *p) {
	FILE *fp;
	
	fp = fopen("student.dat", "rb");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		fread(p, sizeof(Student), N, fp);
	}
	
	fclose(fp);
	
	printf("\nDoc file thanh cong");
}

void saveTextFile(Student *p) {
	//Luu du lieu vao file text nhu nao
	//Moi sinh vien duoc luu theo dinh dang sao
	//Luu nhu option: 2
	FILE *fp;
	
	fp = fopen("E:\student.txt", "w");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		//Ghi du lieu vao file
		char status[30];
		int i;
	
		fprintf(fp, "\n%3s|%15s|%15s|%5s|%5s|%15s|", "No", "Name", "Roll Number", "Year", "Mark", "Status");
		for(i=0;i<N;i++) {
			if(p[i].mark >= 7.5) {
				strcpy(status, "DISTINCTION");
			} else if(p[i].mark >= 6) {
				strcpy(status, "CREDIT");
			} else if(p[i].mark >= 4) {
				strcpy(status, "PASS");
			} else {
				strcpy(status, "FAIL");
			}
			
			fprintf(fp, "\n%3d|%15s|%15s|%5d|%5.2f|%15s|", i + 1, p[i].fullname, p[i].rollNo, p[i].year, p[i].mark, status);
		}
	}
	
	fclose(fp);
	
	printf("\nGhi file thanh cong");
}





Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó