Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1

    Help C++: Lập trình để khi tắt chương trình (Alt + F4) vẫn lưu dữ liệu nhập vào

    Hiện tại có lập trình 1 chương trình nhỏ trên Microsoft Visual C++ 6
    Sau khi nhập dữ liệu vào và dùng chức năng thoát ra thì dữ liệu sẽ lưu vào file.
    Nhưng nếu mà nhập dữ liệu mà thoát trực tiếp bằng cách tắt thẳng dấu X trên cửa sổ (dos) chương trình thì dữ liệu không lưu lại.
    Vậy em đăng đoạn code lên đây, mong các anh chị giúp đỡ em để khi thoát trực tiếp, dữ liệu vẫn lưu lại được.
    Em xin cám ơn.
    Mã:
    #include <stdio.h>
    #include <string.h>
    #include <iostream.h>
    
    struct HocSinh;
    struct MonHoc;
    struct DiemMonHoc;
    
    struct HocSinh 
    {
    	char maHS[9];
    	char tenHS[40];
    	char ngaySinh[10];
    	char gioiTinh[3];
    	float tongKetDiem;
    	DiemMonHoc* DanhSachDiemMonHoc;
    	DiemMonHoc* DiemMonHocCuoiCung;
    	HocSinh* next;
    };
    
    struct MonHoc 
    {
    	char maHS[9];
    	char tenHS[40];
    	MonHoc* next;
    };
    
    struct DiemMonHoc 
    {
    	float diemheso1;
    	float diemheso2;
    	float diemheso3;
    	float tbc;
    	MonHoc* s;
    	DiemMonHoc* next;
    };
    
    HocSinh* stList = NULL;
    HocSinh* stLast = NULL;
    
    MonHoc* danhsachMH = NULL;
    MonHoc* monHocCuoiCung = NULL;
    
    int soMH = 0;
    
    
    void MenuChuongTrinh()
    {
    	printf("			  CHUONG TRINH QUAN LY HOC SINH		
    ");
    	printf("                    ------------------MENU----------------
    ");
    	printf("                  0 |        Menu chuong trinh           |
    ");
    	printf("                  1 |        Nhap hoc sinh               |
    ");
    	printf("                  2 |        Nhap mon hoc                |
    ");
    	printf("                  3 |        Nhap diem                   |
    ");
    	printf("                  4 |        Xem danh sach hoc sinh      |
    ");
    	printf("                  5 |        Tra cuu thong tin hoc sinh  |
    ");
    	printf("                  6 |        Sua thong tin hoc sinh      |
    ");
    	printf("                  7 |        Xoa hoc sinh                |
    ");
    	printf("                  8 |        Thoat khoi chuong trinh     |
    ");
    	printf("                    --------------------------------------
    ");
    }
    
    HocSinh* TaoHocSinh(char maHS[9], char tenHS[40], char ngaySinh[10], char gioiTinh[3])
    {
    	HocSinh* st = new HocSinh();
    
    	strcpy(st->maHS, maHS);           
    	strcpy(st->tenHS, tenHS);
    	strcpy(st->ngaySinh, ngaySinh);
    	strcpy(st->gioiTinh, gioiTinh);
    
    	st->DiemMonHocCuoiCung = NULL;
    	st->DanhSachDiemMonHoc = NULL;
    	st->next = NULL;
    	st->tongKetDiem = 0;
    
    	return st;
    }
    
    
    void ThemHocSinh(HocSinh* st)
    {
    	if(stList == NULL)
    	{
    		stList = st;
    		stLast = st;
    	}
    	else
    	{
    		stLast->next = st;
    		stLast = st;
    	}
    }
    
    HocSinh* TonTaiHocSinh(char maHS[9])
    {
    	HocSinh* pPos = stList;
    	while(pPos != NULL)
    	{
    		if(strcmp(pPos->maHS, maHS) == 0)
    			return pPos;
    
    		pPos = pPos->next;
    	}
    
    	return NULL;
    }
    
    
    void NhapThongTinHocSinh()
    {
    	int n;
    	char maHS[9];
    	char tenHS[40];
    	char ngaySinh[10];
    	char gioiTinh[3];
    
    	HocSinh* st;
    
    	do
    	{
    		printf("\t\t\t       Ban co 2 lua chon 
    \t\t\t 1. Nhap HS       2. Tro lai
    ");
    		printf("Chon 1 hoac 2:  ");
    		scanf("%d", &n);
    		getchar();
    
    		if(n == 1)
    		{
    			do
    			{
    				printf("Nhap ma so hoc sinh    :      ");
    				gets(maHS);
    
    				st = TonTaiHocSinh(maHS);
    
    				if(st != NULL)
    					printf("Chu y: Ma so hoc sinh do ton tai!
    ");
    			}
    			while(st != NULL);
    
    			printf("Nhap ho ten hoc sinh   :      ");
    			gets(tenHS);
    			printf("Nhap vao ngay sinh     :      ");
    			gets(ngaySinh);
    
    			bool flag = true;
    			do
    			{
    				printf("Nhap gioi tinh (Nam/Nu):      ");
    				gets(gioiTinh);
    				if(strcmp(gioiTinh, "Nam") != 0 && strcmp(gioiTinh, "Nu") != 0)
    					printf("Loi: Nhap sai gioi tinh(Nam/Nu)!
    ");
    				else
    					flag = false;
    			}
    			while(flag);
    
    			st = TaoHocSinh(maHS, tenHS, ngaySinh, gioiTinh);
    
    			ThemHocSinh(st);
    
    		}
    	}
    	while(n != 2);
    }
    
    
    
    
    MonHoc* TaoMonHoc(char maHS[9], char tenHS[40])
    {
    	MonHoc* mh = new MonHoc();
    	strcpy(mh->maHS, maHS);
    	strcpy(mh->tenHS, tenHS);
    	mh->next = NULL;
    
    	return mh;
    }
    
    
    
    
    void ThemMonHoc(MonHoc* st)
    {
    	if(danhsachMH == NULL)
    	{
    		danhsachMH = st;
    		monHocCuoiCung = st;
    	}
    	else
    	{
    		monHocCuoiCung->next = st;
    		monHocCuoiCung = st;
    	}
    }
    
    
    
    MonHoc* TonTaiMonHoc(char maHS[9])
    {
    	MonHoc* pPos = danhsachMH;
    	while(pPos != NULL)
    	{
    		if(strcmp(pPos->maHS, maHS) == 0)
    			return pPos;
    
    		pPos = pPos->next;
    	}
    
    	return NULL;
    }
    
    
    void NhapMonHoc()
    {
    	int n;
    	char maHS[9];
    	char tenHS[40];
    
    	MonHoc* st;
    
    
    	do
    	{
    		printf("\t\t\t       Ban co 2 lua chon 
    \t\t\t 1. Nhap MH       2. Tro lai
    ");
    		printf("Chon 1 hoac 2:  ");
    		scanf("%d", &n);
    		getchar();
    
    		if(n == 1)
    		{
    			do
    			{
    				printf("Nhap ma so mon hoc     : ");
    				cin.getline(maHS, 9);
    
    				st = TonTaiMonHoc(maHS);
    
    				if(st != NULL)
    					printf("Chu y: Ma so mon hoc da ton tai!
    ");
    			}
    			while(st != NULL);
    
    			printf("Nhap ten mon hoc       : ");
    			cin.getline(tenHS, 40);
    
    			st = TaoMonHoc(maHS, tenHS);
    
    			ThemMonHoc(st);
    
    		}
    	}
    	while(n != 2);
    }
    
    
    DiemMonHoc* TaoDiemMonHoc(float d1, float d2, float d3, MonHoc* mh)
    {
    	DiemMonHoc* s = new DiemMonHoc();
    	s->diemheso1 = d1;
    	s->diemheso2 = d2;
    	s->diemheso3 = d3;
    	s->s = mh;
    	s->tbc = (d1 + d2 * 2 + d3 * 3) / 6;
    	s->next = NULL;
    
    	return s;
    }
    
    void ThemDiemMonHoc(HocSinh* st, DiemMonHoc* s)
    {
    	if(st->DanhSachDiemMonHoc == NULL)
    	{
    		st->DiemMonHocCuoiCung = s;
    		st->DanhSachDiemMonHoc = s;
    	}
    	else
    	{
    		st->DiemMonHocCuoiCung->next = s;
    		st->DiemMonHocCuoiCung = s;
    	}
    }
    
    
    void NhapDiemMonHoc()
    {
    	char maHS[9];
    	HocSinh* st;
    
    	do
    	{
    		printf("Ma so hoc sinh can nhap diem: ");
    		cin.getline(maHS, 9);
    
    		st = TonTaiHocSinh(maHS);
    	}
    	while(st == NULL);
    
    
    	float diemheso1 = 0;
    	float diemheso2 = 0;
    	float diemheso3 = 0;
    	float total = 0;
    	DiemMonHoc* DiemMonHoc;
    	MonHoc* mh = danhsachMH;
    
    	float sum = 0;
    	int n = 0; 
    	while(mh != NULL)
    	{
    		printf("
    Nhap diem mon: %s
    ", mh->tenHS);
    		printf("Diem 1: ");
    		scanf("%f", &diemheso1);
    		printf("Diem 2: ");
    		scanf("%f", &diemheso2);
    		printf("Diem 3: ");
    		scanf("%f", &diemheso3);
    		DiemMonHoc = TaoDiemMonHoc(diemheso1, diemheso2, diemheso3, mh);
    		ThemDiemMonHoc(st, DiemMonHoc);
    
    		sum += DiemMonHoc->tbc;
    		++n;
    
    		mh = mh->next;
    	}
    
    	printf("
    Da nhap diem xong!
    
    ");
    
    	st->tongKetDiem = sum / n;
    	
    }
    
    void InDanhSachHocSinh()
    {
    	printf("-------------DANH SACH HOC SINH------------
    
    ");
    	HocSinh* st = stList;
    	while(st != NULL)
    	{
    		printf("Ma hoc sinh   :\t\t%s 
    ", st->maHS);
    		printf("Ho ten        :\t\t%s 
    ", st->tenHS);
    		printf("Ngay sinh     :\t\t%s 
    ", st->ngaySinh);
    		printf("Gioi tinh     :\t\t%s 
    ", st->gioiTinh);
    		printf("Diem tong ket :\t\t%f 
    ", st->tongKetDiem);
    		printf("-------------------------------------------
    
    ");
    		st = st->next;
    	}
    
    }
    
    void TimKiemThongTin()
    {
    		char maHS[9];
    		HocSinh* st;
    			printf("
    Ma so HS can tim kiem: ");
    			cin.getline(maHS, 9);
    
    			st = TonTaiHocSinh(maHS);
    
    			if(st == NULL)
    				printf("
    Khong ton tai hoc sinh co ma so nhu tren!
    ");
    			else
    			{
    				printf("
    ------------------THONG TIN HOC SINH------------------
    
    ");
    				printf("Ma so         :\t\t%s
    ", st->maHS);
    				printf("Ho ten        :\t\t%s
    ", st->tenHS);
    				printf("Ngay sinh     :\t\t%s
    ", st->ngaySinh);
    				printf("Gioi tinh     :\t\t%s
    ", st->gioiTinh);
    				printf("Diem tong ket :\t\t%f
    ", st->tongKetDiem);
    
    
    				DiemMonHoc* s = st->DanhSachDiemMonHoc;
    				if(s != NULL)
    					printf("
    ------------------------BANG DIEM-----------------------
    
    ");
    				while(s != NULL)
    				{
    					printf("Ten MH  :\t\t%s
    ", s->s->tenHS);
    					printf("Ma so mon hoc :\t\t%s
    ", s->s->maHS);
    					printf("Diem he so 1  :\t\t%f
    ", s->diemheso1);
    					printf("Diem he so 2  :\t\t%f
    ", s->diemheso2);
    					printf("Diem he so 3  :\t\t%f
    ", s->diemheso3);
    					printf("Diem tong ket :\t\t%f
    ", s->tbc);
    					printf("--------------------------------------------------------
    ");
    					s = s->next;
    				}
    			}
    }
    
    
    void SuaThongTin()
    {
    	char maHS[9];
    
    	printf("Nhap ma so hoc sinh can sua: ");
    	cin.getline(maHS, 9);
    
    	HocSinh* st;
    	st = TonTaiHocSinh(maHS);
    	
    	if(st == NULL)
    		printf("
    Ma so hoc sinh khong ton tai!
    ");
    	else
    	{
    		char maHS[9];
    		char tenHS[40];
    		char ngaySinh[10];
    		char gioiTinh[3];
    		HocSinh* tmp;
    
    		printf("
    --------NHAP THONG TIN MOI CHO HOC SINH------
    
    ");
    		printf("An Enter neu ko muon sua phan thong tin dang thao tac!
    ");
    
    			do
    			{
    				printf("Nhap Ma so hoc sinh: ");
    				gets(maHS);
    
    				tmp = TonTaiHocSinh(maHS);
    
    				if(tmp != NULL)
    					printf("Loi: Ma so hoc sinh do ton tai!
    ");
    			}
    			while(tmp != NULL);
    
    			printf("Nhap ho ten            : ");
    			gets(tenHS);
    			printf("Nhap ngay sinh         : ");
    			gets(ngaySinh);
    
    			bool flag = true;
    			do
    			{
    			printf("Nhap gioi tinh <Nam/Nu>: ");
    				gets(gioiTinh);
    				if(strcmp(gioiTinh, "Nam") != 0 && strcmp(gioiTinh, "Nu") != 0 && strcmp(gioiTinh, "") != 0)
    					printf("Loi: Nhap sai gioi tinh(Nam/Nu)!
    ");
    				else
    					flag = false;
    			}
    			while(flag);
    
    			if(strcmp(maHS, "") != 0)
    			{
    				strcpy(st->maHS, maHS);
    			}
    
    			if(strcmp(tenHS, "") != 0)
    			{
    				strcpy(st->tenHS, tenHS);
    			}
    			if(strcmp(ngaySinh, "") != 0)
    			{
    				strcpy(st->ngaySinh, ngaySinh);
    			}
    			if(strcmp(gioiTinh, "") != 0)
    			{
    				strcpy(st->gioiTinh, gioiTinh);
    			}
    
    	}
    }
    
    
    void KiemTraThongTin(char maHS[9], HocSinh*& st, HocSinh*& pre)
    {
    	HocSinh* pPos = stList;
    	pre = NULL;
    	st = NULL;
    	while(pPos != NULL)
    	{
    		if(strcmp(pPos->maHS, maHS) == 0)
    		{
    			st = pPos;
    			break;
    		}
    
    		pre = pPos;
    		pPos = pPos->next;
    	}
    
    }
    
    void XoaHocSinh()
    {
    	char maHS[9];
    
    	printf("Nhap Ma so hoc sinh can xoa: ");
    	cin.getline(maHS, 9);
    
    	HocSinh* st;
    	HocSinh* pre;
    	HocSinh* pDel;
    	KiemTraThongTin(maHS, st, pre);
    	
    	if(st == NULL)
    		printf("
    Ma so hoc sinh khong ton tai!
    ");
    	else
    	{
    		pDel = st;
    
    		if(pre == NULL)
    		{
    			if(stLast == stList)
    			{
    				stLast = NULL;
    				stList = NULL;
    			}
    			else
    			{
    				stList = stList->next;
    			}
    
    		}
    		else
    		{
    			pre->next = st->next;
    		}
    
    		delete pDel;
    
    		printf("
    
    Da xoa hoc sinh!
    
    ");
    	}
    }
    
    
    void XoaDanhSach()
    {
    	HocSinh* st = stList;
    	HocSinh* stDel;
    	MonHoc* mh = danhsachMH;
    	MonHoc* mhDel;
    
    	while(st != NULL)
    	{
    		stDel = st;
    		
    		st = st->next;
    
    		delete stDel;
    	}
    
    	while(mh != NULL)
    	{
    		mhDel = mh;
    
    		mh = mh->next;
    
    		delete mhDel;
    	}
    
    	stList = stLast = NULL;
    	danhsachMH = monHocCuoiCung = NULL;
    }
    
    void LuuThongTin()
    {
    	FILE* f = fopen("MonHoc.data", "wb");
    	if(f != NULL)
    	{
    		MonHoc* mh = danhsachMH;
    
    		while(mh != NULL)
    		{
    			fwrite(mh->maHS, sizeof(mh->maHS), 1, f);
    			fwrite(mh->tenHS, sizeof(mh->tenHS), 1, f);
    			mh = mh->next;
    		}
    
    		fclose(f);
    	}
    	
    
    	f = fopen("HocSinh.data", "wb");
    
    	if(f != NULL)
    	{
    		HocSinh* st = stList;
    		DiemMonHoc* sc;
    
    		while(st != NULL)
    		{
    			fwrite(st->maHS, sizeof(st->maHS), 1, f);
    			fwrite(st->tenHS, sizeof(st->tenHS), 1, f);
    			fwrite(st->ngaySinh, sizeof(st->ngaySinh), 1, f);
    			fwrite(st->gioiTinh, sizeof(st->gioiTinh), 1, f);
    			fwrite(&(st->tongKetDiem), sizeof(st->tongKetDiem), 1, f);
    
    			sc = st->DanhSachDiemMonHoc;
    
    			while(sc != NULL)
    			{
    				fwrite(sc->s->maHS, sizeof(sc->s->maHS), 1, f);
    				fwrite(&(sc->diemheso1), sizeof(sc->diemheso1), 1, f);
    				fwrite(&(sc->diemheso2), sizeof(sc->diemheso2), 1, f);
    				fwrite(&(sc->diemheso3), sizeof(sc->diemheso3), 1, f);
    				sc = sc->next;
    			}
    			
    			st = st->next;
    		}
    
    		fclose(f);
    	}
    }
    
    void TaiThongTin()
    {
    	XoaDanhSach();
    
    	FILE* f = fopen("MonHoc.data", "rb");
    	int n = 0;
    	if(f != NULL)
    	{
    		char maHS[9];
    		char tenHS[40];
    		MonHoc* mh;
    
    		while(!feof(f))
    		{
    			if(!fread(maHS, sizeof(maHS), 1, f)) break;
    			if(!fread(tenHS, sizeof(tenHS), 1, f)) break;
    
    			mh = TaoMonHoc(maHS, tenHS);
    
    			ThemMonHoc(mh);
    			++n;
    		}
    
    		fclose(f);
    	}
    
    
    
    	f = fopen("HocSinh.data", "rb");
    
    	if(f != NULL)
    	{
    		char maHS[9];
    		char tenHS[40];
    		char ngaySinh[10];
    		char gioiTinh[3];
    		float diemheso1;
    		float diemheso2;
    		float diemheso3;
    		float tongKetDiem;
    		HocSinh* st;
    		DiemMonHoc* sc;
    		MonHoc* mh;
    
    		while(!feof(f))
    		{
    			if(!fread(maHS, sizeof(maHS), 1, f)) break;
    			if(!fread(tenHS, sizeof(tenHS), 1, f)) break;
    			if(!fread(ngaySinh, sizeof(ngaySinh), 1, f)) break;
    			if(!fread(gioiTinh, sizeof(gioiTinh), 1, f)) break;
    			if(!fread(&(tongKetDiem), sizeof(tongKetDiem), 1, f)) break;
    
    			st = TaoHocSinh(maHS, tenHS, ngaySinh, gioiTinh);
    			st->tongKetDiem = tongKetDiem;
    
    			ThemHocSinh(st);
    
    			for(int i = 0; i < n; ++i)
    			{
    				if(!fread(maHS, sizeof(maHS), 1, f)) break;
    				if(!fread(&diemheso1, sizeof(diemheso1), 1, f)) break;
    				if(!fread(&diemheso2, sizeof(diemheso2), 1, f)) break;
    				if(!fread(&diemheso3, sizeof(diemheso3), 1, f)) break;
    
    				mh = TonTaiMonHoc(maHS);
    
    				sc = TaoDiemMonHoc(diemheso1, diemheso2, diemheso3, mh);
    
    				ThemDiemMonHoc(st, sc);
    			}
    		}
    
    		fclose(f);
    	}
    }
    
    
    int main()
    {
    	TaiThongTin();
    
    	int n;
    	
    	MenuChuongTrinh();
    	
    	do
    	{
    		printf("                       Moi ban chon chuc nang <0-8>: ");
    		scanf("%d", &n);
    		getchar();
    
    		switch(n)
    		{
    		case 0:
    			MenuChuongTrinh();
    			break;
    		case 1:
    			NhapThongTinHocSinh();
    			break;
    		case 2:
    			NhapMonHoc();
    			break;
    		case 3:
    			NhapDiemMonHoc();
    			break;
    		case 4:
    			InDanhSachHocSinh();
    			break;
    		case 5:
    			TimKiemThongTin();
    			break;
    		case 6:
    			SuaThongTin();
    			break;
    		case 7:
    			XoaHocSinh();
    			break;
    		}
    	}
    	while(n != 8);
    
    	LuuThongTin();
    	XoaDanhSach();
    
    	return 0;
    }
    Các anh chị giúp em với nhé.

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã:
    #include <windows.h> BOOL __stdcall handler_routine(DWORD dwCtrlType) {  if (dwCtrlType == CTRL_CLOSE_EVENT || dwCtrlType == CTRL_C_EVENT) {    //xử lý khi bị nhấn [X], Alt+F4, ... ở đây    return TRUE;  }   return FALSE;} int main() {  SetConsoleCtrlHandler(handler_routine, TRUE);}

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi greigaz
    Mã:
    #include <windows.h> BOOL __stdcall handler_routine(DWORD dwCtrlType) {  if (dwCtrlType == CTRL_CLOSE_EVENT || dwCtrlType == CTRL_C_EVENT) {    //xử lý khi bị nhấn [X], Alt+F4, ... ở đây    return TRUE;  }   return FALSE;} int main() {  SetConsoleCtrlHandler(handler_routine, TRUE);}
    Em đã nhưng dữ liệu nó vẫn không được lưu lại anh ạ. Anh xem giúp em với nhé (y)

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •