?? item.h
字號:
#include <stdio.h>
#include <memory.h>
#define MAX_MONEY 100000000// 存入的最大金額
#define LENGTH 41 //一個記錄長
class Item{
public:
int account;//帳號,9位
int password;//密碼,6位
double deposit;//剩余金額,17位
char type;//存款類型,u代表活期,f代表定期
int month;//月份,3位
//成員函數
Item(){ memset(this,0,sizeof(this));};//構造函數
void Fin(int, FILE*);//輸入是position和輸入文件名 從文件讀入相應記錄
void Fout(int, FILE*);//輸入是postion和輸出文件名 寫入文件的相應記錄
bool AddMoney(double, double);//輸入是金額和利率 存錢,如果儲蓄金額超過MAX_MONEY,返回false
bool DrawMoney(double, double);//輸入的是取款金額和利率, 取錢,如果不夠則不取,返回false
bool ChangePass(int);//輸入的是密碼, 如果改變不成功則返回false,此函數中不包括密碼驗證
void Display();//輸出帳號,剩余金額,存款類型
bool CheckPass(int);//輸入是帳號名 用來驗證身份
bool ChangeType(char);//輸入是u或者f
char GetType();//返回類型
//內部用來計算利息的
void CountInterest(double);//輸入是利率
//高級管理者的成員函數
void DisplayPass();//可以看到用戶的密碼
bool operator < (Item &);
bool operator > (Item &);
Item & operator = (Item &);
bool operator == (Item &);
};
void Item::Fin(int pos, FILE* In)
{
fseek(In,pos*LENGTH,0);//LENGTH為一個記錄長
fscanf(In,"%d %d %lf %c %d\n", &account, &password, &deposit, &type, &month);
return;
}
void Item::Fout(int pos, FILE* Out)
{
int whole=(int)deposit;
if(whole>deposit)
whole-=1;
int frac=(deposit-whole)*10000000;
if(frac&10 >=5){//處理double型不精確導致的錯誤,四舍五入
frac/=10;
frac++;
}
else
frac/=10;
fseek(Out, pos*LENGTH,0);
fprintf(Out, "%09d %06d %09d.%06d %c %03d\n" ,account ,password ,whole ,frac ,type ,month);
return;
}
bool Item::AddMoney(double plus, double rate)
{
CountInterest(rate);
if(deposit+plus>MAX_MONEY)
return false;
else{
deposit+=plus;
return true;
}
}
bool Item::DrawMoney(double minus, double rate)//輸入的是取款金額, 取錢,如果不夠則不取,返回false
{
CountInterest(rate);
if(deposit-minus<0)
return false;
else{
deposit-=minus;
return true;
}
}
bool Item::ChangePass(int newpass)
{
password=newpass;
return true;
}
void Item::Display()
{
printf("account=%d deposit=%.2lf type=%c month=%d\n",account, deposit, type, month);
return;
}
bool Item::CheckPass(int pass)//輸入是帳號名 用來驗證身份
{
if(pass==password)
return true;
else
return false;
}
bool Item::ChangeType(char t)//輸入是u或者f
{
if(t=='u' || t=='r'){
type=t;
return true;
}
else
return false;
}
char Item::GetType()//返回類型
{
return type;
}
//內部用來計算利息的
void Item::CountInterest(double rate)
{
double plus=deposit*month*rate;
if(deposit+plus>MAX_MONEY)
deposit=MAX_MONEY;
else
deposit+=plus;
month=0;
return;
}
//高級管理者的成員函數
void Item::DisplayPass()//可以看到用戶的密碼
{
printf("account=%d password=%d",account, password);
return;
}
//重載運算符
bool Item::operator < (Item & a)
{
if(account < a.account)
return true;
else
return false;
}
bool Item::operator > (Item & a)
{
if(account > a.account)
return true;
else
return false;
}
Item & Item::operator = (Item & a)
{
account=a.account;
password=a.password;
deposit=a.deposit;
type=a.type;
month=a.month;
return *this;
}
bool Item::operator == (Item & a)
{
if(account==a.account)
return true;
else
return false;
}
/******************************************************/
class Index
{
public:
int pos;//文件中的位置
int account;//用戶的賬號
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -