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
| #include<iostream> #include<string> #include<fstream> using namespace std; fstream shu; void init(){ shu.open("book.dat", ios::in | ios::out | ios::binary);
if (shu.fail()){ shu.open("book.dat",ios::out | ios::binary); shu.close(); shu.open("book.dat", ios::in | ios::out | ios::binary); if (shu.is_open()){ cout << "创建文件成功"<< endl; } else{ cout << "打开文件失败" << endl;
exit(0); }
} } class Book{
public: string isbn; string name; double price; unsigned long nums; Book(){ } Book(string isbn, string name, double price, unsigned long nums) :isbn(isbn), name(name), price(price), nums(nums){ } void print(){ printf("ISBN码: %-5s 书名:《%-5s》 价格:%-3.lf 元 库存: %lu 本\n", isbn, name, price, nums); } void rxiuprice(double price_){ price = price_; } void rxiunums(unsigned long num){ nums = num; } }; int gethang(){ int num = 0; shu.seekg(0, ios::end); num = shu.tellg() / sizeof(Book); return num; } class XS{
public: static unsigned long totalbooks; void addbook(){ string isbn; string name; double price; unsigned long nums; cout << "输入书籍的信息\n" << endl;; cout << "ISBN码:"; cin >> isbn; cout << "书名:"; cin >> name; cout << "价格:"; cin >> price; cout << "库存:"; cin >> nums; cout << endl; Book book(isbn, name, price, nums); totalbooks++; shu.seekp((totalbooks - 1)*sizeof(Book), ios::beg); shu.write((char*)&book, sizeof(Book)); shu.flush(); cout << "size = " << totalbooks << endl; cout << "添加成功" << endl; } void deletebook(){ cout << "请输入书名或者isbn码:"; string name; cin >> name; fstream shu1("temp.dat", ios::out | ios::binary|ios::trunc); shu.seekg(0, ios::end); totalbooks = shu.tellg() / sizeof(Book); shu.seekg(0); for (int i = 0; i<totalbooks; ++i){ Book book; shu.read((char*)&book, sizeof(Book)); if (name == book.isbn || name == book.name){ continue; } else{ shu1.write((char*)&book, sizeof(Book)); } } shu1.close(); shu.close(); fstream shu2("temp.dat"); ofstream shu3("book.dat", ios::in | ios::binary|ios::trunc); for (int i = 0; i<totalbooks-1; ++i){ Book book; shu2.read((char*)&book, sizeof(Book)); shu3.write((char*)&book, sizeof(Book)); } shu3.close(); shu2.close(); system("del temp.dat"); init(); cout << "删除成功" << endl; --totalbooks; } void deletel_all_book(){ shu.close(); system("del book.dat"); init(); cout << "删除成功" << endl; totalbooks = 0; } void show_one_book(){ cout << "请输入书名:"; string name; cin >> name;
shu.seekg(0, ios::end); totalbooks = shu.tellg() / sizeof(Book); shu.seekg(0); for (int i = 0; i<totalbooks; ++i){ Book book; shu.read((char*)&book, sizeof(Book)); if (book.name == name){ book.print(); } } } void showallbook(){ shu.seekg(0, ios::end); cout << "书本数 " << shu.tellg() / sizeof(Book) << endl; totalbooks = shu.tellg() / sizeof(Book); shu.seekg(0); for (int i = 0; i<totalbooks; ++i){ Book book; shu.read((char*)&book, sizeof(Book)); book.print(); } } void cinbisn(){ shu.seekg(0, ios::end); int size = shu.tellg() / sizeof(Book); cout << "size=" << size << endl; string bisn; cout << "输入ISBN码:"; cin >> bisn; Book book; shu.seekg(0); int i = 0; for ( i= 0; i < size;++i) { shu.read((char*)&book, sizeof(Book)); if (book.isbn == bisn){ break; } } cout << "i=" << i << "book is " << book.isbn << "name =" << book.name << endl; cout << "1.修改单价\n2.修改库存" << endl; int cina; cin >> cina; if (cina == 1){ double prince = 0; cout << "修改单价为:"; cin >> prince; book.rxiuprice(prince); shu.seekp(sizeof(Book)*i, ios::beg); shu.write((char*)&book, sizeof(Book)); shu.flush(); cout << "修改单价成功" << endl; } else if (cina == 2){ unsigned long nums; cout << "修改库存为:"; cin >> nums; book.rxiunums(nums); shu.seekp(sizeof(Book)*i, ios::beg); shu.write((char*)&book, sizeof(Book)); shu.flush(); cout << "修改库存成功" << endl; } else { cout << "输入错误不发生修改" << endl; } } }; unsigned long XS::totalbooks = 0;
int main(){ init();
XS xs; shu.seekg(0, ios::end); xs.totalbooks = shu.tellg() / sizeof(Book); cout << "size= " << xs.totalbooks << endl; int ch; while (true) { cout << "-----------------------菜单------------------------|" << endl; cout << "| 1.添加书本 |" << endl; cout << "| 2.删除书本 |" << endl; cout << "| 3.修改书籍 |" << endl; cout << "| 4.查找书籍 |" << endl; cout << "| 5.显示所有书籍 |" << endl; cout << "| 6.删除所有书籍 |" << endl; cout << "| 7.保存并退出程序 | " << endl; cout << "|--------------------------------------------------|" << endl; cout << "输入选项:"; cin >> ch; switch (ch) { case 1: xs.addbook(); cout << "书本数:" << xs.totalbooks << endl; break; case 2: xs.deletebook(); cout << "书本数: " << xs.totalbooks << endl; break; case 3: xs.cinbisn(); break; case 4: xs.show_one_book(); break; case 5: xs.showallbook(); break; case 6: xs.deletel_all_book(); cout << "书本数: " << xs.totalbooks << endl; break; case 7: shu.flush(); shu.close(); return 0; default: break; } } shu.close(); return 0; }
|