By GokiSoft.com| 16:35 10/12/2021|
Lập Trình C

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

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 sort(Student studentList[3]);
void displayStudent(Student studentList[3]);
void findStudentByAge(Student studentList[3]);
void findStudentByProvince(Student studentList[3]);
void saveFile(Student studentList[3]);

int main(int argc, char *argv[]) {
	//Cau 1: Khai bao
	Student studentList[3];
	
	//Cau 2: Dung khung menu chuong trinh
	char c;
	do {
		showMenu();
		fflush(stdin); fflush(stdout);
		scanf("%c", &c);
		
		switch(c) {
			case '1': {
				inputStudent(studentList);
				break;
			}
			case '2': {
				sort(studentList);
				displayStudent(studentList);
				break;
			}
			case '3': {
				findStudentByAge(studentList);
				break;
			}
			case '4': {
				findStudentByProvince(studentList);
				break;
			}
			case '5': {
				saveFile(studentList);
				break;
			}
			case '6':
			case 'n':
			case 'N': {
				printf("\nThoat chuong trinh!!!");
				break;
			}
			default: {
				printf("\nNhap sai!!!");
				break;
			}
		}
	} while(c != '6' && c != 'n' && c!= 'N');
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap du lieu");
	printf("\n2. Sap xep & hien thi");
	printf("\n3. Tim kiem sinh vien co tuoi nho nhat");
	printf("\n4. Tim kiem theo tinh");
	printf("\n5. Luu file student.txt");
	//Khi nguoi dung nhap ky tu 6 | n | N -> thoat chuong trinh
	printf("\n6. Thoat");
	printf("\nChon: ");
}

void inputStudent(Student studentList[3]) {
	int i;
	printf("\nNHAP THONG TIN SINH VIEN");
	for(i=0;i<3;i++) {
		printf("\nNhap ten cua sinh vien: ");
		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 sort(Student studentList[3]) {
	int i, j, cmp;
	for(i=0;i<2;i++) {
		for(j=i+1;j<3;j++) {
			//i -> A, j -> B => cmp = -1
			//i -> B, j -> A => cmp = 1
			cmp = strcmp(studentList[i].name, studentList[j].name);
			if(cmp > 0) {
				//wrap vi tri
				Student tmp = studentList[i];
				studentList[i] = studentList[j];
				studentList[j] = tmp;
			}
		}
	}
}

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

void findStudentByAge(Student studentList[3]) {
	int i, index = 0;
	for(i=1;i<3;i++) {
		if(studentList[i].year < studentList[index].year) {
			index = i;
		}
	}
	
	//Ket thuc tim kiem -> index -> chua thong tin sinh vien co tuoi min
	printf("\n(min) Ho ten: %s\nTinh thanh: %s\nNam sinh: %d", studentList[index].name, 
		studentList[index].province, studentList[index].year);
}

void findStudentByProvince(Student studentList[3]) {
	int i, cmp;
	char s[30];
	
	printf("\nNhap tinh can tim kiem: ");
	fflush(stdin); fflush(stdout);
	gets(s);
	
	for(i=0;i<3;i++) {
		cmp = strcmp(studentList[i].province, s);
		if(cmp == 0) {
			printf("\nHo ten: %s\nTinh thanh: %s\nNam sinh: %d", studentList[i].name, 
				studentList[i].province, studentList[i].year);
		}
	}
}

void saveFile(Student studentList[3]) {
	int i;
	//B1. Mo ket noi
	FILE *fp;
	fp = fopen("student.txt", "w");
	
	//B2. Luu file
	if(fp == NULL) {
		printf("\nFile error!!!");
	} else {
		for(i=0;i<3;i++) {
			//Luu moi thang sinh vien -> dong -> cac du lieu name, province, year:
			//Luu theo cau truc sau: name = %s, provinces = %s, year = %d
			fprintf(fp, "name = %s, provinces = %s, year = %d\n", studentList[i].name, 
				studentList[i].province, studentList[i].year);
		}
	}
	
	//B3. Dong ket noi
	fclose(fp);
}




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