Lấy dữ liệu từ file sản phẩm để in ra hoá đơn

Mình đang làm bài tập giải thuật c++ bằng stack
Mình có 2 phần là sản phẩm và hóa đơn.
Mình muốn lấy thông tin đã có trong file sản phẩm để in ra trong phần hóa đơn thì phải làm sao vậy mọi người, dưới đây là code của mình:

/*
quan ly hoa don
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

//======================================================Quan ao ==============================
//cau truc san pham
struct SanPham{
	int MaSP;
	char tenSP[25];
	char nuocsx[25];
    float size;
    float giasp;
};

//cau truc node
struct NodeSP{
	SanPham SPinfo;
	struct NodeSP *SPnext;
};

//cau truc stack
struct stackSP{
	NodeSP *topsp;
};
stackSP Ssp;

//khoi tao stack
void initSP(stackSP &Ssp){
	Ssp.topsp = NULL;
}

// kiem tra ngan xep rong
int isEmptysp(stackSP Ssp){
	if(Ssp.topsp == NULL)
		return 1;
	else 
		return 0;
}

//tao 1 node moi
NodeSP *get_nodesp(SanPham &sp){
	NodeSP *p= new NodeSP;
	p->SPinfo= sp;
	p->SPnext=NULL;
	return p;
}


//them phhan tu dau vao stack
void SPpush(stackSP &Ssp, NodeSP *p){
	if(Ssp.topsp == NULL){
		Ssp.topsp = p;
	}
	else{
		p->SPnext = Ssp.topsp;
		Ssp.topsp = p;
	}
}

//nhap tt san pham
void nhapttsp(SanPham &sp)
{
	cout<<"Nhap ma san pham: ";cin>>sp.MaSP;
	cin.ignore();
	cout<<"Nhap ten san pham: ";
	gets(sp.tenSP);
	cout<<"Nhap nuoc san xuat: ";
	gets(sp.nuocsx);
	cout<<"Nhap size san pham: ";
	cin>>sp.size;
	cout<<"Nhap gia san pham: ";
	cin>>sp.giasp;
	
}

//hien thong tin san pham
void hienttsp(SanPham sp){
	cout<<setw(20)<<sp.MaSP;
	cout<<setw(20)<<sp.tenSP;
	cout<<setw(20)<<sp.nuocsx;
	cout<<setw(20)<<sp.size;
	cout<<setw(20)<<sp.giasp<<endl;
}

//nhap danh sach san pham
void nhapdssp(stackSP &Ssp){
	NodeSP *p;
	int n,i;
	SanPham sp;
	cout<<"Nhap so luong san pham: ";
	cin>>n;
	for( int i = 0 ; i < n ; i++){
		cout<<"\nNhap thong tin sp: "<<i+1;
		cout<<endl;
		
		nhapttsp(sp);
		p = get_nodesp(sp);
		SPpush(Ssp,p);
	}
}

//hien danh sach san pham
void hiendssp(stackSP Ssp){
	cout<<endl;
	NodeSP *p;
	cout<<setw(20)<<"Ma SP";
	cout<<setw(20)<<"Ten SP";
	cout<<setw(20)<<"Nuoc san xuat";
	cout<<setw(20)<<"Size";
	cout<<setw(20)<<"Gia SP"<<endl;
	for ( p = Ssp.topsp ; p != NULL ; p = p -> SPnext){
		hienttsp(p -> SPinfo);
	}
}

//luu file san pham
void luufilesp(stackSP &Ssp) {
	char tenf[20];
    NodeSP *x;
    x=Ssp.topsp;
    SanPham sp;
    if (strcmp(tenf, "SanPham.txt") != 0) {
        cout<<"\nNhap ten file can nhap : ";
        cin>>tenf;
        strcat(tenf, ".txt");
    }
    FILE *f;
    f = fopen(tenf, "ab");
    if (x == NULL) {
        cout<<"\nDanh sach chua co gia tri, nhap truoc khi luu file!";
    }
    if (f == NULL) {
        cout<<"\nLoi khi mo tep";
        exit(1);
    } else {
        while (x != NULL) {
            fwrite(&x->SPinfo, sizeof(x->SPinfo), 1, f);
            x = x->SPnext;
        }

        fclose(f);
        cout<<"\nDa luu vao file: "<<tenf;
    }
}

//doc file san pham
void docfilesp(stackSP &Ssp) {
    FILE *f;
    char tenf[20];
    initSP(Ssp);
    if (strcmp(tenf, "SanPham.txt") != 0) {
        cout<<"\nNhap ten file : ";
        fflush(stdin);
        cin>>tenf;
        strcat(tenf, ".txt");
    }
    NodeSP *p;
    SanPham sp;
    f = fopen(tenf, "rb");
    if (f == NULL) {
        cout<<"\nMo file bi loi!";
    } else {
        while (fread(&sp, sizeof(sp), 1, f) == 1) {
            p = new NodeSP;
            p->SPinfo = sp;
            p->SPnext = NULL;
            SPpush(Ssp, p);
        }
        fclose(f);
    }
}

//======================================================Hoa don ==============================
//cau truc hoa don
struct HoaDon{
	int MaHD;
	char ngayLap[10];
	char thangLap[10];
	char namLap[10];
	float tongTien;
	int slsp;
	stackSP Ssp;
	SanPham sp;
	//stackNV snv;

};

//khoi tao node 
struct NodeHD{
	HoaDon HDinfo;
	struct NodeHD *HDnext;
};

//cau truc stack
struct stackHD{
	NodeHD *HDtop;
};
stackHD shd;

//khoi tao stack
void initHD(stackHD &shd){
	shd.HDtop = NULL;
}

//kiem tra ngan xep rong
int isEmptyhd(stackHD shd){
	if(shd.HDtop == NULL)
		return 1;
	else 
		return 0;
	
}

//tao 1 node moi
NodeHD *get_nodehd(HoaDon &hd){
	NodeHD *p = new NodeHD;
	p->HDinfo = hd;
	p->HDnext = NULL;
	return p;
}

//them phan tu dau vao stack
void HDpush(stackHD &shd, NodeHD *p){
	if(shd.HDtop == NULL ){
		shd.HDtop = p;
	}
	else
	{
		p->HDnext = shd.HDtop;
		shd.HDtop = p;
	}
}
//nhap tt hoa don
void nhaptthd(HoaDon &hd)
{
	cout<<"Nhap ma hoa don: ";cin>>hd.MaHD;
	cin.ignore();
	cout<<"Nhap ngay lap hoa don: ";
	gets(hd.ngayLap);
	cout<<"Nhap thang lap hoa don: ";
	gets(hd.thangLap);
	cout<<"Nhap nam lap hoa don: ";
	gets(hd.namLap);
	cout<<"Nhap so luong san pham: ";
	cin>>hd.slsp;
	cout<<"Nhap ma san pham: ";
	cin>>hd.sp.MaSP;
	hd.tongTien = hd.slsp * hd.sp.giasp;
}

//hien thong tin hoa don
void hientthd(HoaDon hd){
	cout<<setw(20)<<hd.MaHD;
	cout<<setw(20)<<hd.ngayLap;
	cout<<setw(20)<<hd.thangLap;
	cout<<setw(20)<<hd.namLap;
	cout<<setw(20)<<hd.slsp;
	cout<<setw(20)<<hd.sp.MaSP;
	cout<<setw(20)<<hd.sp.tenSP;
	cout<<setw(20)<<hd.sp.giasp;
	cout<<setw(20)<<hd.sp.size;
	cout<<setw(20)<<hd.tongTien<<endl;
}

//nhap danh sach hoa don
void nhapdshd(stackHD &shd){
	NodeHD *p;
	int n,i;
	HoaDon hd;
	cout<<"Nhap so luong hoa don: ";
	cin>>n;
	for( int i = 0 ; i < n ; i++){
		cout<<"\nNhap thong tin hd: "<<i+1;
		cout<<endl;
		
		nhaptthd(hd);
		p = get_nodehd(hd);
		HDpush(shd,p);
	}
}


//hien danh sach hoa don
void hiendshd(stackHD shd){
	cout<<endl;
	NodeHD *p;
	cout<<setw(20)<<"Ma HD";
	cout<<setw(20)<<"Ngay lap HD";
	cout<<setw(20)<<"Thang lap HD";
	cout<<setw(20)<<"Nam lap HD";
	cout<<setw(20)<<"So luong SP";
	cout<<setw(20)<<"Ma SP";
	cout<<setw(20)<<"Ten SP";
	cout<<setw(20)<<"Gia SP";
	cout<<setw(20)<<"Size SP";
	cout<<setw(20)<<"Tong tien"<<endl;
	for ( p = shd.HDtop ; p != NULL ; p = p -> HDnext){
		hientthd(p -> HDinfo);
		
	}
}


//luu file hoa don
void luufilehd(stackHD &shd) {
	char tenf[20];
    NodeHD *x;
    x=shd.HDtop;
    HoaDon hd;
    if (strcmp(tenf, "HoaDon.txt") != 0) {
        cout<<"\nNhap ten file can nhap : ";
        cin>>tenf;
        strcat(tenf, ".txt");
    }
    FILE *f;
    f = fopen(tenf, "ab");
    if (x == NULL) {
        cout<<"\nDanh sach chua co gia tri, nhap truoc khi luu file!";
    }
    if (f == NULL) {
        cout<<"\nLoi khi mo tep";
        exit(1);
    } else {
        while (x != NULL) {
            fwrite(&x->HDinfo, sizeof(x->HDinfo), 1, f);
            x = x->HDnext;
        }

        fclose(f);
        cout<<"\nDa luu vao file: "<<tenf;
    }
}

//doc file hoa don
void docfilehd(stackHD &shd) {
    FILE *f;
    char tenf[20];
    initHD(shd);
    if (strcmp(tenf, "HoaDon.txt") != 0) {
        cout<<"\nNhap ten file : ";
        fflush(stdin);
        cin>>tenf;
        strcat(tenf, ".txt");
    }
    NodeHD *p;
    HoaDon hd;
    f = fopen(tenf, "rb");
    if (f == NULL) {
        cout<<"\nMo file bi loi!";
    } else {
        while (fread(&hd, sizeof(hd), 1, f) == 1) {
            p = new NodeHD;
            p->HDinfo = hd;
            p->HDnext = NULL;
            HDpush(shd, p);
        }
        fclose(f);
    }
}

//menu san pham
void menuSP(){
	stackSP Ssp;
	NodeSP *p;
    SanPham sp;
    initSP(Ssp);
    int chon = 99;
    while (chon != 0){
        cout<<"====================================================================="<<endl;
        cout<<"                CHUONG TRINH QUAN LI  SAN PHAM BANG STACK                "<<endl;
        cout<<" 1.Nhap san pham vao danh sach "<<endl;
        cout<<" 2.Hien danh sach san pham"<<endl;
       
        cout<<" 0.Thoat khoi chuong trinh "<<endl;
        cout<<"Nhap lua chon cua ban: ";
        cin>>*&chon;
        switch (chon) {
            case 1:
                initSP(Ssp);
                nhapdssp(Ssp);
                 luufilesp(Ssp);
                cout<<"\n";
                break;
            case 2:
            	docfilesp(Ssp);
				cout<<"Danh sach San pham"<<endl;
				hiendssp(Ssp);
				cout<<"\n";
                break;
          
            case 0:
                break;
            default:
                cout<<"\nBan nhap sai, xin vui long nhap lai!";
                break;
        
}
}
}
//menu hoa don
void menuHD(){
	stackHD shd;
	NodeHD *p;
    HoaDon hd;
    initHD(shd);
    int chon = 99;
    while (chon != 0){
        cout<<"====================================================================="<<endl;
        cout<<"                CHUONG TRINH QUAN LI HOA DON BANG STACK                "<<endl;
        cout<<" 1.Nhap hoa don vao danh sach "<<endl;
        cout<<" 2.Hien danh sach hoa don"<<endl;
       
        cout<<" 0.Thoat khoi chuong trinh "<<endl;
        cout<<"Nhap lua chon cua ban: ";
        cin>>*&chon;
        switch (chon) {
            case 1:
                initHD(shd);
                nhapdshd(shd);
                luufilehd(shd);
                cout<<"\n";
                break;
            case 2:
				docfilesp(Ssp);
            	docfilehd(shd);
				cout<<"Danh sach hoa don"<<endl;
				hiendshd(shd);
				cout<<"\n";
                break;
          
            case 0:
                break;
            default:
                cout<<"\nBan nhap sai, xin vui long nhap lai!";
                break;
}
}
}

int main(){
	initSP(Ssp);
	initHD(shd);
	int k=99;
	while(k!=0){
		cout<<"1. Quan ly san pham"<<endl;
		cout<<"2. Quan ly hoa don"<<endl;
		cout<<"0. Thoat"<<endl;
		cin>>k;
		switch(k){
			case 1:
				menuSP();
				break;
			case 2:
				menuHD();
				break;
			case 0:
				break;
			default:
				cout<<"sai,lai"<<endl;
				break;
		}
	}
}

Cậu làm như docfilehd là được, nếu như tớ hiểu đúng ý cậu.
Mà ý cậu là gì nhỉ?

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?