01/10/2018, 16:59

Tại sao mình sử dụng hàm istream và ostream thì void TinhSoThangDaGui() không cho ra kết quả của biến

Đề bài: Có 2 loại sổ tiết kiệm có kỳ hạn và không có kỳ hạn, thông tin cơ bản của mỗi loại gồm: CMND, họ tên, ngày lập sổ, tiền gửi, lãi suất

  • Đối với sổ có kỳ hạn, cần lưu thêm thông tin “kỳ hạn” (gửi bao nhiêu tháng)
  • Công thức tính số tiền lãi (với n là số tháng gửi)
  • Đối với sổ không kỳ hạn : Tiền lãi = tiền gửi * lãi suất * n
  • Đối với sổ có kỳ hạn
    • Tiền lãi = tiền gửi * lãi suất * n, nếu n >= kỳ hạn
    • Ngược lại, tiền lãi bằng 0
  • Yêu cầu:
  • Cài đặt lớp để thể hiện các loại sổ tiết kiệm trên để:
    • Quản lý thông tin sổ tiết kiệm
    • Tính số tiền lãi của sổ cho đến ngày hiện tại (lấy từ hệ thống)
  • Hãy cài đặt lớp NganHang, quản lý danh sách sổ tiết kiệm để thực hiện các chức năng sau
  • Nhập, xuất danh sách sổ tiết kiệm (sử dụng vector)
  • Tính tổng tiền lãi hàng phải trả
    Hỏi: Tại sao khi mình sử dụng 2 hàm istream và ostream ở file SoTietKiem.h thì hàm void TinhSoThangDaGui() (lấy ngày từ hệ thống để tính ra số tháng gửi) không cho ra kết quả của biến “sothanggui” mà trong khi hàm đó vẫn được duyệt qua nhỉ?
    Mình sử dụng kỹ thuật chia tách file nhé
// main.cpp

#include "NganHang.h"

int main()
{
	NganHang x;
	cin >> x;
	cout << x;
	float tongtienlai = x.TinhTongTienLai();
	cout << "

TONG TIEN LAI NGAN HANG PHAI TRA LA: " << (size_t)tongtienlai << endl;
	system("pause");
	return 0;
}

// Ngay1.h

#pragma once
#include <iostream>

using namespace std;

class Ngay
{
	private:
		int ngay;
		int thang;
		int nam;
	public:
		friend istream& operator >>(istream &, Ngay &);
		friend ostream& operator <<(ostream &, Ngay);
		int Getter_Ngay();
		void Setter_Ngay(int);
		int Getter_Thang();
		void Setter_Thang(int);
		int Getter_Nam();
		void Setter_Nam(int);
		Ngay();
		~Ngay();
};

// Ngay1.cpp

#include "Ngay1.h"

istream& operator >>(istream &is, Ngay &d)
{
	cout << "Nhap ngay: ";
	is >> d.ngay;
	cout << "Nhap thang: ";
	is >> d.thang;
	cout << "Nhap nam: ";
	is >> d.nam;
	return is;
}

ostream& operator <<(ostream &os, Ngay d)
{
	os << d.ngay << "/" << d.thang << "/" << d.nam;
	return os;
}

int Ngay::Getter_Ngay()
{
	return ngay;
}

void Ngay::Setter_Ngay(int d)
{
	ngay = d;
}

int Ngay::Getter_Thang()
{
	return thang;
}

void Ngay::Setter_Thang(int m)
{
	thang = m;
}

int Ngay::Getter_Nam()
{
	return nam;
}

void Ngay::Setter_Nam(int y)
{
	nam = y;
}

Ngay::Ngay()
{
}

Ngay::~Ngay()
{
}

// SoTietKiem.h

#include "Ngay1.h"
#pragma once
#include <string>
#include <time.h>

class SoTietKiem
{
	protected:
		string cmnd;
		string hoten;
		float sotiengui;
		Ngay ngaylapso;
		float laisuat;
		float sothanggui;
	public:
		void SoTienGui();
		friend istream& operator >>(istream &, SoTietKiem &);
		friend ostream& operator <<(ostream &, SoTietKiem);
		void TinhSoThangDaGui();
		string Getter_CMND();
		void Setter_CMND(string);
		SoTietKiem();
		~SoTietKiem();
};

// SoTietKiem.cpp

#include "SoTietKiem.h"

istream& operator >>(istream &is, SoTietKiem &x)
{
	cout << "Nhap ho ten: ";
	fflush(stdin);
	getline(is, x.hoten);
	cout << "Nhap ngay lap the:
";
	is >> x.ngaylapso;
	cout << "Nhap so tien gui: ";
	is >> x.sotiengui;
	do{
		cout << "Nhap lai suat: ";
		is >> x.laisuat;
		if(x.laisuat <= 0)
			cout << "
Lai suat khong hop le
";
	}while(x.laisuat <= 0);
	return is;
}

void SoTietKiem::TinhSoThangDaGui()
{
	time_t t = time(0);
	struct tm *now = localtime(&t);
	int ngayht = now -> tm_mday;
	int thanght = now -> tm_mon + 1;
	int namht = now -> tm_year + 1900;
	
	int mangngay[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if(namht % 400 == 0 || (namht % 4 == 0 && namht % 100 != 0))
		mangngay[1] = 29;
		
	int ngaygui = ngaylapso.Getter_Ngay();
	int thanggui = ngaylapso.Getter_Thang();
	int namgui = ngaylapso.Getter_Nam();
	
	sothanggui = 12 * (namht - namgui);
	
	if(thanght > thanggui)
		sothanggui += thanght - thanggui;
	else if(thanght < thanggui)
		sothanggui -= thanggui - thanght;
		
	if(ngayht > ngaygui)
		sothanggui += (float)(ngayht - ngaygui) / mangngay[ngayht - 1];
	else if(ngayht < ngaygui)
	{
		sothanggui--;
		sothanggui += (float)(mangngay[thanght - 1] - (ngaygui - ngayht)) / mangngay[thanght - 1];
	}
	cout << "tai sao dong nay in ra duoc nhung sothanggui thi khong gan duoc ket qua?";
}

ostream& operator <<(ostream &os, SoTietKiem x)
{
	os << "CMND: " << x.cmnd << endl;
	os << "Ho ten: " << x.hoten << endl;
	os << "Ngay lap the: " << x.ngaylapso << endl;
	os << "So tien gui: " << (size_t)x.sotiengui << endl;
	os << "Lai suat: " << x.laisuat << " (%)" << endl;
	os << "So thang gui: " << x.sothanggui << endl;
	return os;
}

string SoTietKiem::Getter_CMND()
{
	return cmnd;
}

void SoTietKiem::Setter_CMND(string cm)
{
	cmnd = cm;
}
SoTietKiem::SoTietKiem()
{
}

SoTietKiem::~SoTietKiem()
{
}

// SoCoKyHan.h

#include "SoTietKiem.h"
#pragma once

class SoCoKyHan : public SoTietKiem
{
	private:
		int kyhangui;
	public:
		friend istream& operator >>(istream &, SoCoKyHan &);
		friend ostream& operator <<(ostream &, SoCoKyHan);
		float TinhTienLai();
		SoCoKyHan();
		~SoCoKyHan();
	protected:
};

// SoCoKyHan.cpp

#include "SoCoKyHan.h"

istream& operator >>(istream &is, SoCoKyHan &x)
{
	// goi lai operator nhap cua cha
	SoTietKiem *cha = static_cast<SoTietKiem*>(&x);
	is >> *cha;
	do{
		cout << "Nhap ky han gui: ";
		is >> x.kyhangui;
		if(x.kyhangui <= 0)
			cout << "
Ky han gui khong hop le
";
	}while(x.kyhangui <= 0);
	return is;
} 

ostream& operator <<(ostream &os, SoCoKyHan x)
{
	// goi lai operator xuat cua cha
	SoTietKiem cha = static_cast<SoTietKiem>(x);
	os << cha;
	cout << "Ky han gui: " << x.kyhangui << endl;
	return os;
}

float SoCoKyHan::TinhTienLai()
{
	if(sothanggui >= kyhangui)
		return sotiengui * laisuat / 100 * sothanggui;
	return 0;
}

SoCoKyHan::SoCoKyHan()

{
}

SoCoKyHan::~SoCoKyHan()
{
}

// SoKhongKyHan.h

#include "SoTietKiem.h"
#pragma once

class SoKhongKyHan : public SoTietKiem
{
	public:
		float TinhTienLai();
		SoKhongKyHan();
		~SoKhongKyHan();
};

// SoKhongKyHan.cpp

#include "SoKhongKyHan.h"

float SoKhongKyHan::TinhTienLai()
{
	return sotiengui * laisuat / 100 * sothanggui;
}

SoKhongKyHan::SoKhongKyHan()
{
}

SoKhongKyHan::~SoKhongKyHan()
{
}

// NganHang.h

#include "SoCoKyHan.h"
#include "SoKhongKyHan.h"
#pragma once
#include <vector>

class NganHang
{
	private:
		vector<SoCoKyHan> ListSoCoKyHan;
		vector<SoKhongKyHan> ListSoKhongKyHan;
	public:
		friend istream& operator >>(istream &, NganHang &);
		friend ostream& operator <<(ostream &, NganHang);
		float TinhTongTienLai();
		NganHang();
		~NganHang();
};

// NganHang.cpp

#include "NganHang.h"
#include <stdlib.h>

istream& operator >>(istream &is, NganHang &x)
{
	while(1)
	{
		system("cls");
		cout << "
======== NGAN HANG ========
		         
1. Nhap so khong ky han
		         
2. Nhap so co ky han
		         
0. Thoat (in danh sach)
		         
==========================";
		int chon;
		do{
			cout << "
Nhap lua chon: ";
			is >> chon;
		}while(chon < 0 || chon > 2);
		bool CheckTrung;
		string cm;
		if(chon != 0)
		{
			do{
				cout << "Nhap CMND: ";
				fflush(stdin);
				getline(is, cm);
				CheckTrung = true;
				int size_socokyhan = x.ListSoCoKyHan.size();
				for(int i = 0; i < size_socokyhan; i++)
					if(x.ListSoCoKyHan[i].Getter_CMND() == cm)
					{
						CheckTrung = false;
						break;
					}
				if(CheckTrung == true)
				{
					int size_sokhongkyhan = x.ListSoKhongKyHan.size();
					for(int i = 0; i < size_sokhongkyhan; i++)
					if(x.ListSoKhongKyHan[i].Getter_CMND() == cm)
					{
						CheckTrung = false;
						break;
					}
				}
				if(CheckTrung == false)
					cout << "CMND da bi trung
";
			}while(CheckTrung == false);
		}
		if(1 == chon)
		{
			SoKhongKyHan a;
			is >> a;
			a.Setter_CMND(cm);
			x.ListSoKhongKyHan.push_back(a);				
		}
		else if(2 == chon)
		{
			SoCoKyHan a;
			is >> a;
			a.Setter_CMND(cm);
			x.ListSoCoKyHan.push_back(a);	
		}
		else if(0 == chon)
		{
			system("cls");
			break;
		}
		else
		{
			cout << "
LUA CHON KHONG HOP LE
";
			system("pause");
		}
	}
	return is;
}

ostream& operator <<(ostream &os, NganHang x)
{
	int size_sokhongkyhan = x.ListSoKhongKyHan.size();
	os << "

===== SO KHONG KY HAN =====
";
	for(int i = 0; i < size_sokhongkyhan; i ++)
	{
		os << "
So khong ky han thu " << i + 1 << endl;
		os << x.ListSoKhongKyHan[i];
	}
	int size_socokyhan = x.ListSoCoKyHan.size();
	os << "

=====   SO CO KY HAN   =====
";
	for(int i = 0; i < size_socokyhan; i ++)
	{
		os << "
So co ky han thu " << i + 1 << endl;
		os << x.ListSoCoKyHan[i];
	}
	return os;
}

float NganHang::TinhTongTienLai()
{
	float tong = 0;
	int size_sokhongkyhan = ListSoKhongKyHan.size();
	for(int i = 0; i < size_sokhongkyhan; i ++)
		tong += ListSoKhongKyHan[i].TinhTienLai();
	int size_socokyhan = ListSoCoKyHan.size();
	for(int i = 0; i < size_socokyhan; i ++)
		tong += ListSoCoKyHan[i].TinhTienLai();
	return tong;
}

NganHang::NganHang()
{
}

NganHang::~NganHang()
{
}
Bài liên quan
0