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

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

Quản lý sinh viên - Assignment - 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 name[30];
	char province[30];
	int year;
} Student;

void showMenu();
void inputStudent(Student studentList[3]);
void sortByYear(Student *p);
void sortByAlpha(Student *p);
void display(Student *p);
void searchMin(Student studentList[3]);
void searchProvince(Student studentList[3]);

int main(int argc, char *argv[]) {
	Student studentList[3];
	
	int choose;
	
	do {
		showMenu();
		scanf("%d", &choose);
		
		switch(choose) {
			case 1: {
				inputStudent(studentList);
				break;
			}
			case 2: {
				sortByAlpha(studentList);
				display(studentList);
				break;
			}
			case 3: {
				searchMin(studentList);
				break;
			}
			case 4: {
				searchProvince(studentList);
				break;
			}
			case 5: {
				printf("\nComming soon");
				break;
			}
			case 6: {
				printf("\nThoat chuong trinh");
				break;
			}
			default: {
				printf("\nNhap sai!!!");
				break;
			}
		}
	} while(choose != 6);
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap du lieu");
	printf("\n2. Sap xep & hien thi");
	printf("\n3. Tim sv co tuoi min");
	printf("\n4. Tim sv theo tinh");
	printf("\n5. Luu file student.txt");
	printf("\n6. Thoat");
	printf("\nChon: ");
}

void inputStudent(Student studentList[3]) {
	int i;
	for(i=0;i<3;i++) {
		printf("\nNhap ten cua sinh vien (%d): ", i + 1);
		fflush(stdin); fflush(stdout);
		gets(studentList[i].name);
		printf("\nNhap tinh: ");
		fflush(stdin); fflush(stdout);
		gets(studentList[i].province);
		printf("\nNhap nam sinh: ");
		scanf("%d", &studentList[i].year);
	}
}

void sortByYear(Student *p) {
	int i, j;
	for(i=0;i<2;i++) {
		for(j=i+1;j<3;j++) {
			if(p[i].year > p[j].year) {
				Student tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
}

void sortByAlpha(Student *p) {
	int i, j;
	for(i=0;i<2;i++) {
		for(j=i+1;j<3;j++) {
			//cmp = 0 => trung nhau, 1 => (Z-A), -1 => (A-Z)
			//I: TRAN VAN A, J: NGUYEN VAN B -> J XEP TRUOC I && I -> TACH -> A, J -> TACH -> B => SO SANH
			//I XEP TRC J
			//THONG THUONG -> DON GIAN -> FIRST_NAME & LAST_NAME
			int cmp = strcmp(p[i].name, p[j].name);
			if(cmp > 0) {
				Student tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
}

void display(Student *p) {
	int i;
	for(i=0;i<3;i++) {
		printf("\nSinh vien %d", i + 1);
		printf("\nHo ten: %s\nTinh thanh: %s\nNam sinh: %d", p[i].name, p[i].province, p[i].year);
	}
}

void searchMin(Student studentList[3]) {
	/** C1: */
//	int i, maxYear = studentList[0].year;
//	
//	for(i=1;i<3;i++) {
//		if(studentList[i].year > maxYear) {
//			maxYear = studentList[i].year;
//		}
//	}
//	
//	for(i=0;i<3;i++) {
//		if(studentList[i].year == maxYear) {
//			printf("\nHo ten: %s, Tinh thanh: %s, Nam sinh: %d", 
//				studentList[i].name, 
//				studentList[i].province, 
//				studentList[i].year);
//		}
//	}
	//C2: sinh vien se dc sap xep theo thu tu tang dan ve nam sinh
	sortByYear(studentList);
	int i, maxYear = studentList[2].year;
	for(i=2;i>=0;i--) {
		if(studentList[i].year != maxYear) {
			break;
		}
		printf("\nHo ten: %s, Tinh thanh: %s, Nam sinh: %d", 
			studentList[i].name, 
			studentList[i].province, 
			studentList[i].year);
	}
}

void searchProvince(Student studentList[3]) {
	int i;
	char province[50];
	printf("\nNhap tinh can tim kiem: ");
	fflush(stdin); fflush(stdout);
	gets(province);
	
	for(i=0;i<3;i++) {
		//i>province: Ha Noi, province: Ha Nam => co phan biet chu Hoa & chu thuong.
		int cmp = strcmp(studentList[i].province, province);
		if(cmp == 0) {
			printf("\nHo ten: %s, Tinh thanh: %s, Nam sinh: %d", 
				studentList[i].name, 
				studentList[i].province, 
				studentList[i].year);
		}
	}
}




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 đó