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

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

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

5

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

Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2020-10-03 02:00:03



//Assignment StudentInformation 2(checked)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct student_st{
	char fullname[30],rollno[10];
	 int year; 
	 float mark;
}Student;

void Menu(){
	printf("\n1.Input data of students.");
	printf("\n2.Sort, display details information and statistic of all students.");
	printf("\n3.Find the student have year of birth.");
	printf("\n4.Save to binary file student.dat.");
	printf("\n5.Exit.");
}

void inputstudentdata(Student *p){
	int i;
	for(i=0;i<3;i++){
		printf("\nEnter data of student %d:",i+1);
		fflush(stdin);fflush(stdout);
		printf("\nFull name: ");
		gets(p[i].fullname);
		fflush(stdin);fflush(stdout);
		printf("\nRoll number: ");
		gets(p[i].rollno);
		fflush(stdin);fflush(stdout);
		printf("\nYear of birth: ");
		scanf("%d",&p[i].year);
		fflush(stdin);fflush(stdout);
		printf("\nMark(/10): ");
		scanf("%f",&p[i].mark);
		fflush(stdin);fflush(stdout);
	}
}

void displaystudentdata(Student *p){
	int i,j;
	for(i=0;i<2;i++){
		for(j=i+1;j<3;j++){
			if(p[i].mark<p[j].mark){
				Student temp=p[i];
				p[i]=p[j];
				p[j]=temp;
			}
		}
	}
	printf("\n%-5s||%-30s||%-15s||%-5s||%-10s||%-15s||","No","Name","Roll Number","Year","Mark","Status");
	for(i=0;i<3;i++){
		if(p[i].mark>=7.5){
			printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||DISTINCTION    ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		}
		else if(p[i].mark>=6.0){
			printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||CREDIT         ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		}
		else if(p[i].mark>=4.0){
			printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||PASS           ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		}
		else if(p[i].mark<4.0){
			printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||FAIL           ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		}
	}
	printf("\n");
	int count=0;
	for(i=0;i<3;i++){
		if(p[i].mark>=7.5){
			count++;
		}
	}
	if(count!=0)
	printf("\nDISTINCTION has %d student",count);
	count=0;
	for(i=0;i<3;i++){
		if(6.0<=p[i].mark && p[i].mark<7.5){
			count++;
		}
	}
	if(count!=0)
	printf("\nCREDIT has %d student",count);
	count=0;
	for(i=0;i<3;i++){
		if(4.0<=p[i].mark && p[i].mark<6.0){
			count++;
		}
	}
	if(count!=0)
	printf("\nPASS has %d student",count);
	count=0;
	for(i=0;i<3;i++){
		if(p[i].mark<4.0){
			count++;
		}
	}
	if(count!=0)
	printf("\nFAIL has %d student",count);
	printf("\n");
}

void yearsearchstudent(Student *p){
	int a;
	printf("\nEnter year for search: ");
	scanf("%d",&a);
	int count=0;
	int i;
	for(i=0;i<3;i++){
		if(p[i].year==a){
			count++;
		}
	}
	if(count!=0){
		printf("\n%-5s||%-30s||%-15s||%-5s||%-10s||%-15s||","No","Name","Roll Number","Year","Mark","Status");
	}
	count=0;
	for(i=0;i<3;i++){
		if(p[i].year==a){
			count++;
			if(p[i].mark>=7.5){
			    printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||DISTINCTION    ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		    }
		    else if(p[i].mark>=6.0){
			    printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||CREDIT         ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		    }
		    else if(p[i].mark>=4.0){
			    printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||PASS           ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		    }
		    else if(p[i].mark<4.0){
			    printf("\n00%d  ||%-30s||%-15s||%-5d||%-10g||FAIL           ||",i+1,p[i].fullname,p[i].rollno,p[i].year,p[i].mark);
		}
		}
	}
	if(count==0){
		printf("\nThere are no student have this year of birth\n");
	}
	printf("\n");
}

void savefile(Student *p){
	FILE *fp;
	fp = fopen("student2.dat","wb");
	fwrite(p, sizeof(Student), 3, fp);
	fclose(fp);
}

int main(){
	Student studentList[3];
	int choose;
	while(1){
		Menu();
		printf("\nChoose your selection: ");
		scanf("%d",&choose);
		switch(choose){
			case 1:
				inputstudentdata(studentList);
			break;
			case 2:
				displaystudentdata(studentList);
			break;
			case 3:
				yearsearchstudent(studentList);
			break;
			case 4:
				savefile(studentList);
				printf("\nSuccessfully saved!!\n");
			break;
			case 5:
				printf("\nGoodbyeeeeee\n");
				exit(0);
			break;
			default:
				printf("\nWrong command!!!\n");
			break;	
		}
	}
}



Đặng Trần Nhật Minh [T2008A]
Đặng Trần Nhật Minh

2020-10-02 09:57:10



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

int n = 3;

typedef struct student_st {
	
	char name[30], rollno[15];
	int year;
	float mark;
	char status[15];
	
} SV;

void swap(SV &a, SV &b) {
	
	SV temp = a;
	a = b;
	b = temp;
	
}

void inputStudent(SV *a, int n) {
	
	for (int i = 0; i < n; i++) {
		
		printf("\nEnter data of student No.%d:", i + 1);
					
		printf("\nFull Name: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].name);
					
		printf("\nRoll No: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].rollno);
					
		printf("\nYear of birth: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &a[i].year);
		
		printf("\nMark(/10): ");
		fflush(stdin); fflush(stdout);
		scanf("%f", &a[i].mark);
								
	}
	
}

void sortStudent(SV *t, int n) {
	
	int index_MAX, cD = 0, cC = 0, cP = 0, cF = 0;
	
	for (int i = 0; i < n; i++) {
		
		if (t[i].mark >= 7.5) strcpy(t[i].status, "DISTINCTION"), cD++;
		else if (t[i].mark >= 6.0) strcpy(t[i].status, "CREDIT"), cC++;
		else if (t[i].mark >= 4.0) strcpy(t[i].status, "PASS"), cP++;
		else if (t[i].mark < 4.0) strcpy(t[i].status, "FAILED"), cF++;
		
	}
	
	for (int i = 0; i < n - 1; i++) {
					
		index_MAX = i;
				
		for (int j = i + 1; j < n; j++)
			if (t[index_MAX].mark < t[j].mark) index_MAX = j;
				
		swap(t[index_MAX], t[i]);
					
	}
	
	printf("\n|No.|Name                          |Roll Number    |Year |Mark |Status         |"); //|3|30|15|5|5|15
	
	for (int i = 0; i < n; i++)
		printf("\n|00%d|%-30s|%-15s|%-5d|%-.2f |%-15s|", i + 1, t[i].name, t[i].rollno, t[i].year, t[i].mark, t[i].status);
		
		
	if (cD > 0) printf("\nDISTINCTION has %d students.", cD);
	if (cC > 0) printf("\nCREDIT has %d students.", cC);
	if (cP > 0) printf("\nPASSED has %d students.", cP);
	if (cF > 0) printf("\nFAILED has %d students.", cF);
	
}

void searchAge(SV *a, int n) {
	
	printf("\nEnter year for search: ");
	int sy; scanf("%d", &sy);
	
	bool check = false;
	
	for (int i = 0; i < n; i++)
		if (a[i].year == sy) printf("\nName: %s", a[i].name), check = true;
		
	if (!check) printf("\nThere are no student have this year of birth.");
	
}

void outputStudent(SV *a, int n) {
	
	FILE *fp;
	
	fp = fopen("student.dat", "wb");
	
	fwrite(a, sizeof(SV), n, fp);
	
	fclose(fp);
	
}

int main() {

	SV studentList[n];
	
	int sel;
	
	while (true) {
		
		printf("\n==========Menu=========");
		printf("\n1. Input data of students.");
		printf("\n2. Sort, display details information and statistic of all students.");
		printf("\n3. Find the  student have year of birth.");
		printf("\n4. Save to binary file studen.dat.");
		printf("\n5. Exit.");	
		printf("\n========================");
		printf("\nNhap lua chon cua ban: "); 
		fflush(stdin); fflush(stdout);
		scanf("%d", &sel);
		
		switch (sel) {
			
			case 1:
				
				inputStudent(studentList, n);
				
				break;
				
			case 2:
				
				sortStudent(studentList, n);
				
				break;
				
			case 3:
				
				searchAge(studentList, n);
				
				break;
				
			case 4:
				
				outputStudent(studentList, n);
				
				break;
				
			case 5:
				
				printf("EXIT!");
				
				exit(0);
				
			default:
				
				printf("\nNHAP SAI!");
				
				break;
			
		}
		
	}
	
	return 0;

}