By GokiSoft.com| 15:21 25/09/2020|
Lập Trình C

[Share Code] Hướng dẫn optimize function - struct trong C - Quản lý sinh viên - Lập trình C BT1935



Nội dung:
	- Pointer trong structure
	- Tách file trong project (function)
===================================================
Xây dựng chương trình quản lý sinh viên
Sinh viên gồm các thông tin:
	- Tên, tuổi, địa chỉ

1. Tạo mảng gồm 3 phần tử sinh viên
2. Viết menu chương trình
	1. Nhập thông tin sinh viên
	2. Hiển thị thông tin sinh viên
	3. Thoát


Cách 1

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int length = 3;

typedef struct Student_ST {
	char name[50], address[200];
	int age;
} Student;

void showMenu() {
	printf("\n1. Nhap thong tin sinh vien");
	printf("\n2. Hien thi thong tin sinh vien");
	printf("\n3. Thoat");
	printf("\nChon: ");
}

void input(Student *p) {
	int i;
	//Nhap du lieu
	for(i=0;i<length;i++) {
		printf("\nNhap sinh vien thu %d:", i + 1);
		printf("\nNhap ten: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].name);
		
		printf("\nNhap tuoi: ");
		fflush(stdin);fflush(stdout);
		scanf("%d", &p[i].age);
		
		printf("\nNhap dia chi: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].address);
	}
}

void display(Student *p) {
	int i;
	//Hien thi thong tin sinh vien
	for(i=0;i<length;i++) {
		printf("\n%d. Ten: %s, tuoi: %d, dia chi: %s", i+1, 
			p[i].name, p[i].age, p[i].address);	
	}
}

int main(int argc, char *argv[]) {
	Student studentList[3];
	int choose, i;
	
	do {
		showMenu();
		scanf("%d", &choose);
		
		switch(choose) {
			case 1:
				input(studentList);
				break;
			case 2:
				display(studentList);
				break;
			case 3:
				printf("\nGoodbye!!!");
				break;
			default:
				printf("\nNhap sai!!!");
				break;
		}
	} while(choose != 3);
	
	return 0;
}

Cách 2:

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct Student_ST {
	char name[50], address[200];
	int age;
} Student;

int main(int argc, char *argv[]) {
	Student studentList[3];
	int choose, i, length = 3;
	
	do {
		printf("\n1. Nhap thong tin sinh vien");
		printf("\n2. Hien thi thong tin sinh vien");
		printf("\n3. Thoat");
		printf("\nChon: ");
		scanf("%d", &choose);
		
		switch(choose) {
			case 1:
				//Nhap du lieu
				for(i=0;i<3;i++) {
					printf("\nNhap sinh vien thu %d:", i + 1);
					printf("\nNhap ten: ");
					fflush(stdin);fflush(stdout);
					gets(studentList[i].name);
					
					printf("\nNhap tuoi: ");
					fflush(stdin);fflush(stdout);
					scanf("%d", &studentList[i].age);
					
					printf("\nNhap dia chi: ");
					fflush(stdin);fflush(stdout);
					gets(studentList[i].address);
				}
				break;
			case 2:
				//Hien thi thong tin sinh vien
				for(i=0;i<3;i++) {
					printf("\n%d. Ten: %s, tuoi: %d, dia chi: %s", i+1, 
						studentList[i].name, studentList[i].age, studentList[i].address);	
				}
				break;
			case 3:
				printf("\nGoodbye!!!");
				break;
			default:
				printf("\nNhap sai!!!");
				break;
		}
	} while(choose != 3);
	
	return 0;
}

Cách 3:

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int length = 3;

typedef struct Student_ST {
	char name[50], address[200];
	int age;
} Student;

void showMenu();
void input(Student *p);
void display(Student *p);

int main(int argc, char *argv[]) {
	Student studentList[3];
	int choose, i;
	
	do {
		showMenu();
		scanf("%d", &choose);
		
		switch(choose) {
			case 1:
				input(studentList);
				break;
			case 2:
				display(studentList);
				break;
			case 3:
				printf("\nGoodbye!!!");
				break;
			default:
				printf("\nNhap sai!!!");
				break;
		}
	} while(choose != 3);
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap thong tin sinh vien");
	printf("\n2. Hien thi thong tin sinh vien");
	printf("\n3. Thoat");
	printf("\nChon: ");
}

void input(Student *p) {
	int i;
	//Nhap du lieu
	for(i=0;i<length;i++) {
		printf("\nNhap sinh vien thu %d:", i + 1);
		printf("\nNhap ten: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].name);
		
		printf("\nNhap tuoi: ");
		fflush(stdin);fflush(stdout);
		scanf("%d", &p[i].age);
		
		printf("\nNhap dia chi: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].address);
	}
}

void display(Student *p) {
	int i;
	//Hien thi thong tin sinh vien
	for(i=0;i<length;i++) {
		printf("\n%d. Ten: %s, tuoi: %d, dia chi: %s", i+1, 
			p[i].name, p[i].age, p[i].address);	
	}
}


Liên kết rút gọn:

https://gokisoft.com/1935

Bình luận

avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-28 03:41:57

Code Tham Khảo.



#include <stdio.h>
#include <stdlib.h>
#include "student.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	//Xu ly file
	//Mo ket noi File
	FILE *fp = fopen("vidu.txt", "a");
	
	//Ghi du lieu
	fprintf(fp, "Sinh vien Aptech\n");
	fputc('A', fp);
	
	//Dong ket noi
	fclose(fp);
	
	//Mo ket noi File
	fp = fopen("vidu.txt", "r");
	
	//Ghi du lieu
	char str[100];
	fgets(str, 100, fp);
	printf("\nstr: %s", str);
	
	//Dong ket noi
	fclose(fp);
	//Ket thuc xu ly file
	
	Student studentList[3];
	int choose, i;
	
	FILE *fp1;
	
	do {
		showMenu();
		scanf("%d", &choose);
		
		switch(choose) {
			case 1:
				input(studentList);
				break;
			case 2:
				display(studentList);
				break;
			case 3:
				printf("\nGoodbye!!!");
				break;
			case 4:
				//save file
				fp1 = fopen("student.dat", "wb");
				
				//save danh sach sinh vien vao file
				fwrite(studentList, sizeof(Student), 3, fp1);
				
				//dong ket noi
				fclose(fp1);
				break;
			case 5:
				//save file
				fp1 = fopen("student.dat", "rb");
				
				//save danh sach sinh vien vao file
				fread(studentList, sizeof(Student), 3, fp1);
				
				//dong ket noi
				fclose(fp1);
				break;
			default:
				printf("\nNhap sai!!!");
				break;
		}
	} while(choose != 3);
	
	return 0;
}



typedef struct Student_ST {
	char name[50], address[200];
	int age;
} Student;

void showMenu();
void input(Student *p);
void display(Student *p);



#include <stdio.h>
#include <stdlib.h>
#include "student.h"

int length = 3;

void showMenu() {
	printf("\n1. Nhap thong tin sinh vien");
	printf("\n2. Hien thi thong tin sinh vien");
	printf("\n3. Thoat");
	printf("\n4. Luu File");
	printf("\n5. Doc File");
	printf("\nChon: ");
}

void input(Student *p) {
	int i;
	//Nhap du lieu
	for(i=0;i<length;i++) {
		printf("\nNhap sinh vien thu %d:", i + 1);
		printf("\nNhap ten: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].name);
		
		printf("\nNhap tuoi: ");
		fflush(stdin);fflush(stdout);
		scanf("%d", &p[i].age);
		
		printf("\nNhap dia chi: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].address);
	}
}

void display(Student *p) {
	int i;
	//Hien thi thong tin sinh vien
	for(i=0;i<length;i++) {
		printf("\n%d. Ten: %s, tuoi: %d, dia chi: %s", i+1, 
			p[i].name, p[i].age, p[i].address);	
	}
}


avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-25 08:23:17

FILE


//Xu ly file
	//Mo ket noi File
	FILE *fp = fopen("vidu.txt", "a");
	
	//Ghi du lieu
	fprintf(fp, "Sinh vien Aptech\n");
	fputc('A', fp);
	
	//Dong ket noi
	fclose(fp);
	
	//Mo ket noi File
	fp = fopen("vidu.txt", "r");
	
	//Ghi du lieu
	char str[100];
	fgets(str, 100, fp);
	printf("\nstr: %s", str);
	
	//Dong ket noi
	fclose(fp);
	//Ket thuc xu ly file