编译环境:windows xp / vc7.0
//--------------------------------------------stdafx.h-----------------------------------------------------------------------
#pragma once
#include <iostream>
#include <tchar.h>
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
//-----------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------MyObject.h---------------------------------------------------
#include "stdafx.h"
#include "MyObject.h"
char* GetSubString(const char* szString, char* szBuffer, int nStart, int nEnd)
{
char* temp = szBuffer;
for (int j = nStart; j != nEnd; j++)
*szBuffer++ = szString[j];
return temp;
}
string space(int n)
{
string temp("");
for (int i = 0; i < n; i++) temp = temp + " ";
return temp;
}
//------------------------------------------------------------------------------------------------------------------
//--------------------------------------------MyObject.cpp------------------------------------------------------------
#include "stdafx.h"
#include "MyObject.h"
char* GetSubString(const char* szString, char* szBuffer, int nStart, int nEnd)
{
char* temp = szBuffer;
for (int j = nStart; j != nEnd; j++)
*szBuffer++ = szString[j];
return temp;
}
string space(int n)
{
string temp("");
for (int i = 0; i < n; i++) temp = temp + " ";
return temp;
}
date::date(int year, int month, int day)
{
m_year = year >= 0 ?year:0;
m_month = month >= 1 && month <= 12 ?month:1;
//1,3,5,7,8,10,12 有31天
//4,6,9,11 有30
//2 平年只有28天,润年有29天
switch (m_month)
{
case 2:
m_day = day >= 1 && day <= 28+leapyear()?day:1;
break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
m_day = day >= 1 && day <= 31?day:1;
break;
case 4:case 6:case 9:case 11:
m_day = day >= 1 && day <= 30?day:1;
break;
}
}
date::date(char* szString)
{
char buffer[3];
memset(buffer,0,3);
m_year = atoi(GetSubString(szString,buffer,0,2));
m_month = atoi(GetSubString(szString,buffer,2,4));
m_day = atoi(GetSubString(szString,buffer,4,6));
m_year = m_year >= 0 ?m_year:0;
m_month = m_month >= 1 && m_month <= 12 ?m_month:1;
//1,3,5,7,8,10,12 有31天
//4,6,9,11 有30
//2 平年只有28天,润年有29天
switch (m_month)
{
case 2:
m_day = m_day >= 1 && m_day <= 28+leapyear()?m_day:1;
break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
m_day = m_day >= 1 && m_day <= 31?m_day:1;
break;
case 4:case 6:case 9:case 11:
m_day = m_day >= 1 && m_day <= 30?m_day:1;
break;
}
}
/*date::date(string str)
{
char *buf=new char[str.length()];
char b[3];
memset(b,0,3);
m_year = m_year >= 0 ?m_year:0;
m_month = m_month >= 1 && m_month <= 12 ?m_month:1;
//1,3,5,7,8,10,12 有31天
//4,6,9,11 有30
//2 平年只有28天,润年有29天
switch (m_month)
{
case 2:
m_day = m_day >= 1 && m_day <= 28+leapyear()?m_day:1;
break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
m_day = m_day >= 1 && m_day <= 31?m_day:1;
break;
case 4:case 6:case 9:case 11:
m_day = m_day >= 1 && m_day <= 30?m_day:1;
break;
}
delete []buf;
}*/
string date::tostring()
{
string temp("");
char buf[3]="/0";
wsprintf(buf,"%d",m_year);
if (m_year < 10) temp += '0';
temp += buf;
wsprintf(buf,"%d",m_month);
if (m_month < 10) temp += '0';
temp += buf;
wsprintf(buf,"%d",m_day);
if (m_day < 10) temp += '0';
temp += buf;
return temp;
}
date date::GetToday()
{
SYSTEMTIME st;
GetLocalTime(&st);
return date(st.wYear%100,st.wMonth,st.wDay);
}
date& operator +(date& dt, const int& i)
{
int max;
for (int j = i; j != 0; j--)
{
switch(dt.m_month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
max = 31;
break;
case 4:case 6:case 9:case 11:
max = 30;
break;
case 2:
max = 28+dt.leapyear();
break;
}
if (dt.m_day < max) ++dt.m_day;
else
{
dt.m_day = 1;
if (dt.m_month < 12) ++dt.m_month;
else
{
dt.m_month = 1;
++dt.m_year;
}
}
}
return dt;
}
std::ostream& operator << (std::ostream& out, const date& object)
{
if (object.m_year < 10) cout << '0';
cout << object.m_year <<'/';
if (object.m_month < 10) cout << '0';
cout << object.m_month <<'/';
if (object.m_day < 10) cout << '0';
cout << object.m_day;
return out;
}
books::books(const string& szFileName)
{
FileName = szFileName;
dataread();
}
books::~books()
{
datawrite();
}
bool books::dataread()
{
std::fstream fin;
fin.open(FileName.c_str(),std::ios_base::in);
if (!fin.is_open()) return true;
fin.seekg(0,std::ios::beg);
char buf[20]={"/0"};
int i=0;
while(!fin.eof())
{
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
id.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
book_name.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
author.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
book_concern.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
if (buf[0] == '1') flag.push_back(1);
else flag.push_back(0);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
b_date.push_back(date(buf));
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
g_date.push_back(date(buf));
}
fin.close();
del(static_cast<int>(id.size()));
return true;
}
bool books::datawrite()
{
std::fstream out;
DeleteFile(FileName.c_str());
out.open(FileName.c_str(),std::ios_base::out | std::ios_base::app);
out.seekg(0,std::ios::beg);
int i = 0;
char buffer[2]="/0";
while (i != id.size())
{
out.put(static_cast<int>(id[i].length()));
out.write(id[i].c_str(),static_cast<int>(id[i].length()));
out.put(static_cast<int>(book_name[i].length()));
out.write(book_name[i].c_str(),static_cast<int>(book_name[i].length()));
out.put(static_cast<int>(author[i].length()));
out.write(author[i].c_str(),static_cast<int>(author[i].length()));
out.put(static_cast<int>(book_concern[i].length()));
out.write(book_concern[i].c_str(),static_cast<int>(book_concern[i].length()));
out.put(static_cast<int>(1));
wsprintf(buffer,"%d",flag[i]);
out.write(buffer,1);
out.put(6);
out.write(b_date[i].tostring().c_str(),6);
out.put(6);
out.write(g_date[i].tostring().c_str(),6);
i++;
}
out.close();
return true;
}
int books::find_book(const string& tid, const string& name, const string& concern)
{
for (int i = 0; i != id.size(); i++)
{
if (id[i] == tid) return i;
}
for (int i = 0; i != book_name.size(); i++)
{
if (book_name[i] == name) return i;
}
for (int i = 0; i != book_concern.size(); i++)
{
if (book_concern[i] == concern) return i;
}
return -2;//-2表示没找到
}
bool books::add(const string& tid, const string& name, const string& tauthor,
const string& concern, const int& state, const string& bdate, const string& gdate)
{
int y,m,d;
id.push_back(tid);
book_name.push_back(name);
author.push_back(tauthor);
book_concern.push_back(concern);
flag.push_back(state);
y = atoi(bdate.substr(0,2).c_str());
m = atoi(bdate.substr(2,2).c_str());
d = atoi(bdate.substr(4,2).c_str());
b_date.push_back(date(y,m,d));
y = atoi(gdate.substr(0,2).c_str());
m = atoi(gdate.substr(2,2).c_str());
d = atoi(gdate.substr(4,2).c_str());
g_date.push_back(date(y,m,d));
return true;
}
bool books::del(int n)
{
if (n >= 1 && n <= (int)id.size())
{
id[n-1] = id[id.size()-1];
book_concern[n-1] = book_concern[book_concern.size()-1];
author[n-1] = author[author.size()-1];
book_name[n-1] = book_name[book_name.size()-1];
flag[n-1] = flag[flag.size()-1];
b_date[n-1] = b_date[b_date.size()-1];
g_date[n-1] = g_date[g_date.size()-1];
id.pop_back();
book_concern.pop_back();
author.pop_back();
book_name.pop_back();
flag.pop_back();
b_date.pop_back();
g_date.pop_back();
return true;
}
else return false;
}
bool books::give(const string& tid)
{
int id_index = find_book(tid);
if ( id_index == -2) return false;
else
{
if (flag[id_index] == 0)
{
flag[id_index] = 1;
b_date[id_index] = date::GetToday();
g_date[id_index] = date::GetToday()+60;
return true;
}
else return false;
}
}
bool books::back(const string& tid)
{
//id_index == -2表示该记录不存在
int id_index = find_book(tid);
if (id_index == -2) return false;
else
{
if (flag[id_index] == 1)
{
flag[id_index] = 0;
b_date[id_index] = date();
g_date[id_index] = date();
return true;
}
else return false;
}
}
bool books::modi(const string& tid, const string& name, const string& tauthor,
const string& concern, const int& state, const string& bdate, const string& gdate)
{
int id_index = find_book(tid);
if (id_index == -2) return false;
else
{
book_name[id_index] = name;
author[id_index] = tauthor;
book_concern[id_index] = concern;
flag[id_index] = state;
b_date[id_index] = date(const_cast<char*>(bdate.c_str()));
g_date[id_index] = date(const_cast<char*>(gdate.c_str()));
return true;
}
}
menu::menu(const books& object)
{
L1: banner();
char nCode = '0';
cout << "/n/t请输入菜单项:";
while(cin >> nCode)
{
cin.ignore();
switch (nCode)
{
case '1':
list(object);
break;
case '2':
borrow(const_cast<books&>(object));
break;
case '3':
give_back(const_cast<books&>(object));
break;
case '4':
find_book(const_cast<books&>(object));
break;
case '5':
remove(const_cast<books&>(object));
break;
case '6':
append(const_cast<books&>(object));
break;
case '7':
modification(const_cast<books&>(object));
break;
case '8':
flush(const_cast<books&>(object));
break;
case '0':
return;
default:
system("cls");
goto L1;
}
}
}
void menu::banner(void)
{
cout << "-------------------------------- 图书管理系统 --------------------------------" << std::endl;
cout << " 1.浏览 2.借阅 3.还书 4.查找 5.删除 6.添加 7.修改 8.刷新 0.退出" << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
bool menu::list(const books& object, int nSelect)
{
//nSelect == -2表示没找到
//nSelect == -1表示所有记录
//nSelect == other表示找到的记录
system("cls");
banner();
int i=0;
cout << "编号";
cout << space(6);
cout << "书名";
cout << space(16);
cout << "作者";
cout << space(8);
cout << "出版社";
cout << space(6);
cout << "状态";
cout << space(2);
cout << "借出日期";
cout << space(2);
cout << "归还日期";
cout << std::endl;
if (nSelect == -1)
{
//显示所有记录
while(i != object.id.size())
{
cout << object.id[i];
cout << space(10-(int)object.id[i].length());
cout << object.book_name[i];
cout << space(20-(int)object.book_name[i].length());
cout << object.author[i];
cout << space(12-(int)object.author[i].length());
cout << object.book_concern[i];
cout << space(14-(int)object.book_concern[i].length());
cout << object.flag[i];
cout << space(3);
cout << object.b_date[i];
cout << space(2);
cout << object.g_date[i];
cout << endl;
i++;
}
}
else if(nSelect != -2)
{
//显示查找到的记录
cout << object.id[nSelect];
cout << space(10-(int)object.id[nSelect].length());
cout << object.book_name[nSelect];
cout << space(20-(int)object.book_name[nSelect].length());
cout << object.author[nSelect];
cout << space(12-(int)object.author[nSelect].length());
cout << object.book_concern[i];
cout << space(14-(int)object.book_concern[nSelect].length());
cout << object.flag[nSelect];
cout << space(3);
cout << object.b_date[nSelect];
cout << space(2);
cout << object.g_date[nSelect];
cout << endl;
}
return true;
}
int menu::find_book(books& object)
{
int nCode = 0;
string name;
string id;
string concern;
cout << "/n/t请输入查询条件!" << endl;
cout << "/t编号:";
getline(cin, id);
cout << "/t书名:";
getline(cin, name);
cout << "/t出版社:";
getline(cin, concern);
int i = object.find_book(id,name,concern);
if (i == -2) cout << "/n/t没查找符合条件的记录...!/n/t按4键重新输入查询条件...!" << endl << '/t';
else list(const_cast<const books&>(object),i);
return 0;
}
bool menu::append(books& object)
{
string id;
string name;
string author;
string concern;
string bdate;
string gdate;
int flag;
cout << "/n/t------- 追加记录! -------";
cout << "/n/t编号:";
getline(cin,id);
id = id.empty() ? "NULL":id;
cout << "/t书名:";
getline(cin,name);
name = name.empty() ? "NULL":name;
cout << "/t作者:";
getline(cin,author);
author = author.empty() ? "NULL":author;
cout << "/t出版社:";
getline(cin,concern);
concern = concern.empty() ? "NULL":concern;
cout << "/t状态:";
cin >> flag;
flag = flag != 0 ?1:0;
fflush(stdin);
cout << "/t借阅日期(格式:050318表示2005年3月18日):";
getline(cin,bdate);
bdate = bdate.length() < 6 ? "000000":bdate;
cout << "/t归还日期(格式:050318表示2005年3月18日):";
getline(cin,gdate);
gdate = gdate.length() < 6 ? "000000":bdate;
object.add(id,name,author,concern,flag,bdate,gdate);
cout << "/n/t按6键继续追加记录...!" << endl << '/t';
return true;
}
bool menu::remove(books& object)
{
string nCode = "";
cout << "/n/t请输入要删除的编号:";
cin >> nCode;
if (object.del(object.find_book(nCode)+1))
{
cout << "/n/t删除成功...!按5键继续,任意键返回...!";
return true;
}
else
{
cout << "/n/t删除失败...!按5键继续,任意键返回...!";
return false;
}
}
bool menu::borrow(books& object)
{
string nCode = "";
cout << "/n/t请输入要借阅的书籍编号:";
cin >> nCode;
if (object.give(nCode))
{
cout << "/n/t借阅成功...!还书期限为:" << object.g_date[object.find_book(nCode)] << endl;
return true;
}
else cout << "/n/t该书不存在或者已被借阅...!任意键返回...!";
return false;
}
bool menu::give_back(books& object)
{
string nCode = "";
cout << "/n/t请输入要还的书籍编号:";
cin >> nCode;
if (object.back(nCode))
{
cout << "/n/t还书已被成功记录...!" << endl;
return true;
}
else cout << "/n/t该书不存在或者还没被借阅...!任意键返回...!";
return false;
}
bool menu::modification(books& object)
{
string id;
string name;
string author;
string concern;
string bdate;
string gdate;
int flag;
cout << "/n/t------- 修改加录...! -------";
cout << "/n/t编号:";
getline(cin,id);
id = id.empty() ? "NULL":id;
cout << "/t书名:";
getline(cin,name);
name = name.empty() ? "NULL":name;
cout << "/t作者:";
getline(cin,author);
author = author.empty() ? "NULL":author;
cout << "/t出版社:";
getline(cin,concern);
concern = concern.empty() ? "NULL":concern;
cout << "/t状态:";
cin >> flag;
flag = flag != 0 ?1:0;
fflush(stdin);
cout << "/t借阅日期(格式:050318表示2005年3月18日):";
getline(cin, bdate);
bdate = bdate.length() < 6 ? "000000":bdate; //格式错误以000000提交
cout << "/t归还日期(格式:050318表示2005年3月18日):";
getline(cin, gdate);
gdate = gdate.length() < 6 ? "000000":gdate; //格式错误以000000提交
if (object.modi(id,name,author,concern,flag,bdate,gdate)) cout << "/n/t修改记录成功...!";
else cout << "/n/t修改记录失败...!原因:记录不存在";
cout << "/n/t按7键继续修改记录...!" << endl << '/t';
return true;
}
bool menu::flush(books& object)
{
object.datawrite();
list(object);
return true;
}
date::date(int year, int month, int day)
{
m_year = year >= 0 ?year:0;
m_month = month >= 1 && month <= 12 ?month:1;
//1,3,5,7,8,10,12 有31天
//4,6,9,11 有30
//2 平年只有28天,润年有29天
switch (m_month)
{
case 2:
m_day = day >= 1 && day <= 28+leapyear()?day:1;
break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
m_day = day >= 1 && day <= 31?day:1;
break;
case 4:case 6:case 9:case 11:
m_day = day >= 1 && day <= 30?day:1;
break;
}
}
date::date(char* szString)
{
char buffer[3];
memset(buffer,0,3);
m_year = atoi(GetSubString(szString,buffer,0,2));
m_month = atoi(GetSubString(szString,buffer,2,4));
m_day = atoi(GetSubString(szString,buffer,4,6));
m_year = m_year >= 0 ?m_year:0;
m_month = m_month >= 1 && m_month <= 12 ?m_month:1;
//1,3,5,7,8,10,12 有31天
//4,6,9,11 有30
//2 平年只有28天,润年有29天
switch (m_month)
{
case 2:
m_day = m_day >= 1 && m_day <= 28+leapyear()?m_day:1;
break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
m_day = m_day >= 1 && m_day <= 31?m_day:1;
break;
case 4:case 6:case 9:case 11:
m_day = m_day >= 1 && m_day <= 30?m_day:1;
break;
}
}
/*date::date(string str)
{
char *buf=new char[str.length()];
char b[3];
memset(b,0,3);
m_year = m_year >= 0 ?m_year:0;
m_month = m_month >= 1 && m_month <= 12 ?m_month:1;
//1,3,5,7,8,10,12 有31天
//4,6,9,11 有30
//2 平年只有28天,润年有29天
switch (m_month)
{
case 2:
m_day = m_day >= 1 && m_day <= 28+leapyear()?m_day:1;
break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
m_day = m_day >= 1 && m_day <= 31?m_day:1;
break;
case 4:case 6:case 9:case 11:
m_day = m_day >= 1 && m_day <= 30?m_day:1;
break;
}
delete []buf;
}*/
string date::tostring()
{
string temp("");
char buf[3]="/0";
wsprintf(buf,"%d",m_year);
if (m_year < 10) temp += '0';
temp += buf;
wsprintf(buf,"%d",m_month);
if (m_month < 10) temp += '0';
temp += buf;
wsprintf(buf,"%d",m_day);
if (m_day < 10) temp += '0';
temp += buf;
return temp;
}
date date::GetToday()
{
SYSTEMTIME st;
GetLocalTime(&st);
return date(st.wYear%100,st.wMonth,st.wDay);
}
date& operator +(date& dt, const int& i)
{
int max;
for (int j = i; j != 0; j--)
{
switch(dt.m_month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
max = 31;
break;
case 4:case 6:case 9:case 11:
max = 30;
break;
case 2:
max = 28+dt.leapyear();
break;
}
if (dt.m_day < max) ++dt.m_day;
else
{
dt.m_day = 1;
if (dt.m_month < 12) ++dt.m_month;
else
{
dt.m_month = 1;
++dt.m_year;
}
}
}
return dt;
}
std::ostream& operator << (std::ostream& out, const date& object)
{
if (object.m_year < 10) cout << '0';
cout << object.m_year <<'/';
if (object.m_month < 10) cout << '0';
cout << object.m_month <<'/';
if (object.m_day < 10) cout << '0';
cout << object.m_day;
return out;
}
books::books(const string& szFileName)
{
FileName = szFileName;
dataread();
}
books::~books()
{
datawrite();
}
bool books::dataread()
{
std::fstream fin;
fin.open(FileName.c_str(),std::ios_base::in);
if (!fin.is_open()) return true;
fin.seekg(0,std::ios::beg);
char buf[20]={"/0"};
int i=0;
while(!fin.eof())
{
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
id.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
book_name.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
author.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
book_concern.push_back(buf);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
if (buf[0] == '1') flag.push_back(1);
else flag.push_back(0);
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
b_date.push_back(date(buf));
memset(buf,0,20);
fin.read (buf,static_cast<int>(fin.get()));
g_date.push_back(date(buf));
}
fin.close();
del(static_cast<int>(id.size()));
return true;
}
bool books::datawrite()
{
std::fstream out;
DeleteFile(FileName.c_str());
out.open(FileName.c_str(),std::ios_base::out | std::ios_base::app);
out.seekg(0,std::ios::beg);
int i = 0;
char buffer[2]="/0";
while (i != id.size())
{
out.put(static_cast<int>(id[i].length()));
out.write(id[i].c_str(),static_cast<int>(id[i].length()));
out.put(static_cast<int>(book_name[i].length()));
out.write(book_name[i].c_str(),static_cast<int>(book_name[i].length()));
out.put(static_cast<int>(author[i].length()));
out.write(author[i].c_str(),static_cast<int>(author[i].length()));
out.put(static_cast<int>(book_concern[i].length()));
out.write(book_concern[i].c_str(),static_cast<int>(book_concern[i].length()));
out.put(static_cast<int>(1));
wsprintf(buffer,"%d",flag[i]);
out.write(buffer,1);
out.put(6);
out.write(b_date[i].tostring().c_str(),6);
out.put(6);
out.write(g_date[i].tostring().c_str(),6);
i++;
}
out.close();
return true;
}
int books::find_book(const string& tid, const string& name, const string& concern)
{
for (int i = 0; i != id.size(); i++)
{
if (id[i] == tid) return i;
}
for (int i = 0; i != book_name.size(); i++)
{
if (book_name[i] == name) return i;
}
for (int i = 0; i != book_concern.size(); i++)
{
if (book_concern[i] == concern) return i;
}
return -2;//-2表示没找到
}
bool books::add(const string& tid, const string& name, const string& tauthor,
const string& concern, const int& state, const string& bdate, const string& gdate)
{
int y,m,d;
id.push_back(tid);
book_name.push_back(name);
author.push_back(tauthor);
book_concern.push_back(concern);
flag.push_back(state);
y = atoi(bdate.substr(0,2).c_str());
m = atoi(bdate.substr(2,2).c_str());
d = atoi(bdate.substr(4,2).c_str());
b_date.push_back(date(y,m,d));
y = atoi(gdate.substr(0,2).c_str());
m = atoi(gdate.substr(2,2).c_str());
d = atoi(gdate.substr(4,2).c_str());
g_date.push_back(date(y,m,d));
return true;
}
bool books::del(int n)
{
if (n >= 1 && n <= (int)id.size())
{
id[n-1] = id[id.size()-1];
book_concern[n-1] = book_concern[book_concern.size()-1];
author[n-1] = author[author.size()-1];
book_name[n-1] = book_name[book_name.size()-1];
flag[n-1] = flag[flag.size()-1];
b_date[n-1] = b_date[b_date.size()-1];
g_date[n-1] = g_date[g_date.size()-1];
id.pop_back();
book_concern.pop_back();
author.pop_back();
book_name.pop_back();
flag.pop_back();
b_date.pop_back();
g_date.pop_back();
return true;
}
else return false;
}
bool books::give(const string& tid)
{
int id_index = find_book(tid);
if ( id_index == -2) return false;
else
{
if (flag[id_index] == 0)
{
flag[id_index] = 1;
b_date[id_index] = date::GetToday();
g_date[id_index] = date::GetToday()+60;
return true;
}
else return false;
}
}
bool books::back(const string& tid)
{
//id_index == -2表示该记录不存在
int id_index = find_book(tid);
if (id_index == -2) return false;
else
{
if (flag[id_index] == 1)
{
flag[id_index] = 0;
b_date[id_index] = date();
g_date[id_index] = date();
return true;
}
else return false;
}
}
bool books::modi(const string& tid, const string& name, const string& tauthor,
const string& concern, const int& state, const string& bdate, const string& gdate)
{
int id_index = find_book(tid);
if (id_index == -2) return false;
else
{
book_name[id_index] = name;
author[id_index] = tauthor;
book_concern[id_index] = concern;
flag[id_index] = state;
b_date[id_index] = date(const_cast<char*>(bdate.c_str()));
g_date[id_index] = date(const_cast<char*>(gdate.c_str()));
return true;
}
}
menu::menu(const books& object)
{
L1: banner();
char nCode = '0';
cout << "/n/t请输入菜单项:";
while(cin >> nCode)
{
cin.ignore();
switch (nCode)
{
case '1':
list(object);
break;
case '2':
borrow(const_cast<books&>(object));
break;
case '3':
give_back(const_cast<books&>(object));
break;
case '4':
find_book(const_cast<books&>(object));
break;
case '5':
remove(const_cast<books&>(object));
break;
case '6':
append(const_cast<books&>(object));
break;
case '7':
modification(const_cast<books&>(object));
break;
case '8':
flush(const_cast<books&>(object));
break;
case '0':
return;
default:
system("cls");
goto L1;
}
}
}
void menu::banner(void)
{
cout << "-------------------------------- 图书管理系统 --------------------------------" << std::endl;
cout << " 1.浏览 2.借阅 3.还书 4.查找 5.删除 6.添加 7.修改 8.刷新 0.退出" << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
bool menu::list(const books& object, int nSelect)
{
//nSelect == -2表示没找到
//nSelect == -1表示所有记录
//nSelect == other表示找到的记录
system("cls");
banner();
int i=0;
cout << "编号";
cout << space(6);
cout << "书名";
cout << space(16);
cout << "作者";
cout << space(8);
cout << "出版社";
cout << space(6);
cout << "状态";
cout << space(2);
cout << "借出日期";
cout << space(2);
cout << "归还日期";
cout << std::endl;
if (nSelect == -1)
{
//显示所有记录
while(i != object.id.size())
{
cout << object.id[i];
cout << space(10-(int)object.id[i].length());
cout << object.book_name[i];
cout << space(20-(int)object.book_name[i].length());
cout << object.author[i];
cout << space(12-(int)object.author[i].length());
cout << object.book_concern[i];
cout << space(14-(int)object.book_concern[i].length());
cout << object.flag[i];
cout << space(3);
cout << object.b_date[i];
cout << space(2);
cout << object.g_date[i];
cout << endl;
i++;
}
}
else if(nSelect != -2)
{
//显示查找到的记录
cout << object.id[nSelect];
cout << space(10-(int)object.id[nSelect].length());
cout << object.book_name[nSelect];
cout << space(20-(int)object.book_name[nSelect].length());
cout << object.author[nSelect];
cout << space(12-(int)object.author[nSelect].length());
cout << object.book_concern[i];
cout << space(14-(int)object.book_concern[nSelect].length());
cout << object.flag[nSelect];
cout << space(3);
cout << object.b_date[nSelect];
cout << space(2);
cout << object.g_date[nSelect];
cout << endl;
}
return true;
}
int menu::find_book(books& object)
{
int nCode = 0;
string name;
string id;
string concern;
cout << "/n/t请输入查询条件!" << endl;
cout << "/t编号:";
getline(cin, id);
cout << "/t书名:";
getline(cin, name);
cout << "/t出版社:";
getline(cin, concern);
int i = object.find_book(id,name,concern);
if (i == -2) cout << "/n/t没查找符合条件的记录...!/n/t按4键重新输入查询条件...!" << endl << '/t';
else list(const_cast<const books&>(object),i);
return 0;
}
bool menu::append(books& object)
{
string id;
string name;
string author;
string concern;
string bdate;
string gdate;
int flag;
cout << "/n/t------- 追加记录! -------";
cout << "/n/t编号:";
getline(cin,id);
id = id.empty() ? "NULL":id;
cout << "/t书名:";
getline(cin,name);
name = name.empty() ? "NULL":name;
cout << "/t作者:";
getline(cin,author);
author = author.empty() ? "NULL":author;
cout << "/t出版社:";
getline(cin,concern);
concern = concern.empty() ? "NULL":concern;
cout << "/t状态:";
cin >> flag;
flag = flag != 0 ?1:0;
fflush(stdin);
cout << "/t借阅日期(格式:050318表示2005年3月18日):";
getline(cin,bdate);
bdate = bdate.length() < 6 ? "000000":bdate;
cout << "/t归还日期(格式:050318表示2005年3月18日):";
getline(cin,gdate);
gdate = gdate.length() < 6 ? "000000":bdate;
object.add(id,name,author,concern,flag,bdate,gdate);
cout << "/n/t按6键继续追加记录...!" << endl << '/t';
return true;
}
bool menu::remove(books& object)
{
string nCode = "";
cout << "/n/t请输入要删除的编号:";
cin >> nCode;
if (object.del(object.find_book(nCode)+1))
{
cout << "/n/t删除成功...!按5键继续,任意键返回...!";
return true;
}
else
{
cout << "/n/t删除失败...!按5键继续,任意键返回...!";
return false;
}
}
bool menu::borrow(books& object)
{
string nCode = "";
cout << "/n/t请输入要借阅的书籍编号:";
cin >> nCode;
if (object.give(nCode))
{
cout << "/n/t借阅成功...!还书期限为:" << object.g_date[object.find_book(nCode)] << endl;
return true;
}
else cout << "/n/t该书不存在或者已被借阅...!任意键返回...!";
return false;
}
bool menu::give_back(books& object)
{
string nCode = "";
cout << "/n/t请输入要还的书籍编号:";
cin >> nCode;
if (object.back(nCode))
{
cout << "/n/t还书已被成功记录...!" << endl;
return true;
}
else cout << "/n/t该书不存在或者还没被借阅...!任意键返回...!";
return false;
}
bool menu::modification(books& object)
{
string id;
string name;
string author;
string concern;
string bdate;
string gdate;
int flag;
cout << "/n/t------- 修改加录...! -------";
cout << "/n/t编号:";
getline(cin,id);
id = id.empty() ? "NULL":id;
cout << "/t书名:";
getline(cin,name);
name = name.empty() ? "NULL":name;
cout << "/t作者:";
getline(cin,author);
author = author.empty() ? "NULL":author;
cout << "/t出版社:";
getline(cin,concern);
concern = concern.empty() ? "NULL":concern;
cout << "/t状态:";
cin >> flag;
flag = flag != 0 ?1:0;
fflush(stdin);
cout << "/t借阅日期(格式:050318表示2005年3月18日):";
getline(cin, bdate);
bdate = bdate.length() < 6 ? "000000":bdate; //格式错误以000000提交
cout << "/t归还日期(格式:050318表示2005年3月18日):";
getline(cin, gdate);
gdate = gdate.length() < 6 ? "000000":gdate; //格式错误以000000提交
if (object.modi(id,name,author,concern,flag,bdate,gdate)) cout << "/n/t修改记录成功...!";
else cout << "/n/t修改记录失败...!原因:记录不存在";
cout << "/n/t按7键继续修改记录...!" << endl << '/t';
return true;
}
bool menu::flush(books& object)
{
object.datawrite();
list(object);
return true;
}
//-----------------------------------------------------------------------------------------------------------------------------------
测试代码:
#include "stdafx.h"
#include "MyObject.h"
int main(int argc, _TCHAR* argv[])
{
//books a;
//menu m(a);
books a("data.dat");
menu m(a);
return 0;
}