/
myfun.cpp
271 lines (257 loc) · 5.76 KB
/
myfun.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include<fstream>
#include<iostream>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<stdio.h>
#include"myfun.h"
#include"book.h"
//extern const int NUM1;
//extern const int NUM2;
const int CMD_COLS = 80;
const int CMD_LINES = 25;
using namespace std;
void SetScreenGrid()
{
char sysSetBuf[80];
//cout<<sysSetBuf<<"mode con cols = "<<CMD_COLS<<",lines = "<<CMD_LINES<<endl;
sprintf(sysSetBuf,"mode con cols=%d lines=%d",CMD_COLS,CMD_LINES);
system(sysSetBuf);
cout<<"SetScreenGrid"<<endl;
return ;
}
void SetSysCaption()
{
system("title BookManager");
cout<<"SetSysCaption"<<endl;
return ;
}
void MainLoop()
{
ShowWelcome();
while(true)
{
ClearScreen();
ShowWelcome();
ShowRootMenu();
switch(GetSelect())
{
case 1:
ClearScreen();
GuideInput();
break;
case 2:
ClearScreen();
ViewData();
break;
case 3:
ClearScreen();
DeleteBookFromFile();
break;
}
}
return ;
}
void ClearScreen()
{
system("cls");
cout<<"ClearScreen"<<endl;
return ;
}
void ShowWelcome()
{
for(int i = 0;i < 7;i++)
cout<<endl;
cout.width(40);
cout<<"************"<<endl;
cout.width(40);
cout<<"图书管理系统"<<endl;
cout.width(40);
cout<<"************"<<endl;
cout<<"ShowWelcome"<<endl;
return ;
}
void ShowRootMenu()
{
cout.width(40);
cout<<"请选择功能"<<endl;
cout<<endl;
cout.width(38);
cout<<" 1 添加新书"<<endl;
cout<<endl;
cout.width(38);
cout<<"2 浏览全书"<<endl;
cout<<endl;
cout.width(38);
cout<<"3 删除图书"<<endl;
cout<<"ShowRootMenu"<<endl;
return ;
}
int GetSelect()
{
char buf[256];
gets(buf);
return atoi(buf);
cout<<"GetSelect"<<endl;
return 0;
}
void WaitUser()
{
//int iInputPage = 0;
cout<<"Enter---返回菜单"<<endl;
cout<<"q-------退出"<<endl;
char buf[256];
gets(buf);
if(buf[0] == 'q')
exit(0);
else
ShowRootMenu();
return ;
}
void GuideInput()
{
char inName[NUM1];
char inIsbn[NUM1];
char inPrice[NUM2];
char inAuthor[NUM2];
cout<<"请输入书名:"<<endl;
gets(inName);
cout<<"请输入ISBN: "<<endl;
gets(inIsbn);
cout<<"请输入定价:"<<endl;
gets(inPrice);
cout<<"请输入作者:"<<endl;
gets(inAuthor);
cout<<"GuideInput"<<endl;
CBook book(inName, inIsbn, inPrice, inAuthor);
book.WriteData();
cout<<"Write Finish!"<<endl;
WaitUser();
return ;
}
long GetFileLength(ifstream &ifs)
{
long tmppos;
long respos;
tmppos = ifs.tellg();
ifs.seekg(0, ios::end);
respos = ifs.tellg();
ifs.seekg(tmppos, ios::beg);
return respos;
}
void ViewData(int iSelectPage )
{
int iPage = 0;
int iCurPage = 0;
int iDataCount = 0;
char inName[NUM1];
char inIsbn[NUM1];
char inPrice[NUM2];
char inAuthor[NUM2];
bool bIndex = false;
int iFileLength;
iCurPage = iSelectPage;
ifstream ifile;
ifile.open("book.dat",ios::binary);
iFileLength = GetFileLength(ifile);
iDataCount = iFileLength/(NUM1 + NUM1 + NUM2 + NUM2);
if(iDataCount >= 1)
bIndex = true;
iPage = iDataCount / 20 + 1;
ClearScreen();
cout<<"共有记录:"<<iDataCount<<" ";
cout<<"共有页数:"<<iPage<<" ";
cout<<"当前页数:"<<iCurPage<<" ";
cout<<"1---显示下一页"<<endl;
cout<<"2---返回"<<endl;
cout.width(5);
cout<<"Index";
cout.width(22);
cout<<"Name";
cout.width(22);
cout<<"Isbn";
cout.width(15);
cout<<"Price";
cout.width(15);
cout<<"Author";
cout<<endl;
try
{
ifile.seekg((iCurPage - 1) * 20 * (NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
if(!ifile.fail())
{
for(int i = 1;i < 21;i++)
{
memset(inName, 0, 128);
memset(inIsbn, 0, 128);
memset(inPrice, 0, 50);
memset(inAuthor, 0, 50);
if(bIndex)
{
cout.width(3);
cout<<((iCurPage-1) * 20 +i);
}
ifile.read(inName, NUM1);
cout<<setw(24)<<inName;
ifile.read(inIsbn, NUM1);
cout<<setw(24)<<inIsbn;
ifile.read(inPrice, NUM2);
cout<<setw(12)<<inPrice;
ifile.read(inAuthor, NUM2);
cout<<setw(12)<<inAuthor;
cout<<endl;
if(ifile.tellg() < 0)
bIndex = false;
else
bIndex = true;
}
}
}
catch(...)
{
cout<<"throw file exception"<<endl;
throw "file error occurred";
ifile.close();
}
if(iCurPage < iPage)
{
iCurPage = iCurPage + 1;
WaitView(iCurPage, iPage);
}
else
{
WaitView(iCurPage, iPage);
}
ifile.close();
return ;
}
void WaitView(int nextPage, int iPage)
{
if(nextPage == iPage)
{
cout<<"已到最后一页,按‘1’键回到主菜单"<<endl;
if(GetSelect() == 1)
MainLoop();
}
else
{
cout<<"按‘1’键回到主菜单,‘2’浏览下一页"<<endl;
if(GetSelect() == 1)
MainLoop();
else
ViewData(nextPage);
}
return ;
}
void DeleteBookFromFile()
{
int iDelCount;
cout<<"Input Delete Index:"<<endl;
cin>>iDelCount;
CBook tmpbook;
tmpbook.DeleteData(iDelCount);
cout<<"Delete Finish"<<endl;
WaitUser();
cout<<"DeleteBookFromFile"<<endl;
return ;
}