第一周作业 1959110 张立桐
#include"Book.h" #include<iostream> #include<iomanip> #include<fstream> #include<string> using namespace std; Book::Book() { bookID = ""; bookName = ""; author = ""; publish = ""; price = 0; } Book::Book(string bi, string bn, string au, string pu, double pr) { bookID = bi; bookName = bn; author = au; publish = pu; price = pr; } void Book::display() { cout << "书号:" << bookID << "\t"; cout << " 书名: " << bookName << "\t"; cout << " 作者: " << author << "\t"; cout << " 出版社: " << publish << "\t"; cout << " 价格: " << price << "\t"; } #include<iostream> #include<string> using namespace std; class Book { public: Book(); Book(string bi, string bh, string au, string pu, double pt);//构造函数 void display();//显示函数 private: string bookID;//编号 string bookName;//书名 string author;//作者 string publish;//出版社 double price;//价格 }; #include "Buyer.h" #include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; Buyer::Buyer() { buyerID = ""; name = ""; address = ""; } Buyer::Buyer(string id, string na, string ad) { name = na; buyerID = id; address = ad; } int getLine() { ifstream in("Buyer.txt"); string line; int n = 0; while (getline(in, line)) { n++; } in.close(); return n; } void Buyer::display() { cout << " 买家信息: "; cout << "ID :" << buyerID << "\t"; cout << " 姓名: " << name << "\t"; cout << " 地址: " << address << "\n"; } void People::display() { Buyer::display(); cout << " 该买家是普通人,无折扣率。" << "\n"; } void Member::display() { Buyer::display(); cout << " 该买家是会员,折扣率为 " << discount_member * 10 << " 折! \n"; } void Vip::display() { Buyer::display(); cout << " 该买家是贵宾,折扣率为 " << discount_vip * 10 << "折! \n"; } #pragma once #include <string> using namespace std; // 购书者类 class Buyer { public: Buyer(); Buyer(string id, string na, string ad); // 构造函数 virtual void display(); // 显示函数(虚函数) protected: string buyerID; // 买家ID string name; // 姓名 string address; // 地址 }; // 普通人类 class People : public Buyer { public: People(string id, string na, string ad) :Buyer(id, na, ad) { } void display(); }; // 会员类 class Member : public Buyer { public: Member(string id, string na, string ad, double dc) :Buyer(id, na, ad) { discount_member = dc; } void display(); private: double discount_member; // 折扣率 }; // 贵宾类 class Vip : public Buyer { public: Vip(string id, string na, string ad, double dc) :Buyer(id, na, ad) { discount_vip = dc; } void display(); private: double discount_vip; // 折扣率 }; #include <iostream> #include "Receipt.h" using namespace std; // 函数原型 void main_menu(); // 主菜单 void add_menu(); // 添加发票操作完成后显示的菜单 void delete_menu(); // 删除发票操作完成后显示的菜单 void select_menu(); // 查找发票操作完成后显示的菜单 void print_menu(); // 显示所有发票信息操作完成后显示的菜单 int main() { main_menu(); return 0; } // 主菜单 void main_menu() { Receipt re; cout << "---------------- 网上购书的结账系统----------------" << endl; cout << " 1. 发票信息录入 " << endl; cout << " 2. 发票信息删除 " << endl; cout << " 3. 根据发票号查询发票信息 " << endl; cout << " 4. 根据买家ID 查询发票信息 " << endl; cout << " 5. 打印所有发票信息 " << endl; cout << " 0. 退出 " << endl; cout << "----------------------------------------------------------" << endl; cout << " 请输入1-4 中的任意数字(0 退出):"; int index = -1; cin >> index; switch (index) { case 0: break; case 1: { re.addReceipt(); add_menu(); } case 2: { re.deleteReceipt(); delete_menu(); } case 3: { re.selectReceiptByRI(); select_menu(); } case 4: { re.selectReceiptByBI(); select_menu(); } case 5: { re.print(); print_menu(); } } } // 添加发票操作完成后显示的菜单 void add_menu() { Receipt re; cout << "---------------- 网上购书的结账系统----------------" << endl; cout << " 1. 发票信息再录入 " << endl; cout << " 2. 返回上级目录 " << endl; cout << " 0. 退出 " << endl; cout << "----------------------------------------------------------" << endl; cout << " 请输入1-2 中的任意数字(0 退出):"; int index = -1; cin >> index; switch (index) { case 0: break; case 1: { re.addReceipt(); add_menu(); } case 2: main_menu(); } } // 删除发票操作完成后显示的菜单 void delete_menu() { Receipt re; cout << "---------------- 网上购书的结账系统----------------" << endl; cout << " 1. 发票信息再删除 " << endl; cout << " 2. 返回上级目录 " << endl; cout << " 0. 退出 " << endl; cout << "----------------------------------------------------------" << endl; cout << " 请输入1-2 中的任意数字(0 退出):"; int index = -1; cin >> index; switch (index) { case 0: break; case 1: { re.deleteReceipt(); delete_menu(); } case 2: main_menu(); } } //根据订单号查找发票操作完成后显示的菜单 void select_menu() { Receipt re; cout << "---------------- 网上购书的结账系统----------------" << endl; cout << " 1. 根据发票号查询发票信息 " << endl; cout << " 2. 根据买家ID 查询发票信息 " << endl; cout << " 3. 返回上级目录 " << endl; cout << " 0. 退出 " << endl; cout << "----------------------------------------------------------" << endl; cout << " 请输入1-2 中的任意数字(0 退出):"; int index = -1; cin >> index; switch (index) { case 0: break; case 1: { re.selectReceiptByRI(); select_menu(); } case 2: { re.selectReceiptByBI(); select_menu(); } case 4: main_menu(); } } // 显示所有发票信息操作完成后显示的菜单 void print_menu() { Receipt re; cout << "---------------- 网上购书的结账系统----------------" << endl; cout << " 1. 返回上级目录 " << endl; cout << " 0. 退出 " << endl; cout << "----------------------------------------------------------" << endl; cout << " 请输入1-2 中的任意数字(0 退出):"; int index = -1; cin >> index; switch (index) { case 0: break; case 1: main_menu(); } } #include "Order.h" #include <iostream> #include <iomanip> #include <fstream> #include <string> Order::Order() { receiptID = 0; buyerID = ""; } // 构造函数 Order::Order(int re, string bu) { re = receiptID; bu = buyerID; } // 获取Order.txt 中的行数 int Order::getLine() { ifstream in("Order.txt"); string line; int n = 0; while (getline(in, line)) { n++; } in.close(); return n; } // 从Order.txt 中读取数据放入Receipt 对象数组中 void Order::readOrder(Order o[]) { ifstream infile("Order.txt", ios::in | ios::binary); if (!infile) { cerr << " open error" << endl; exit(1); } int n = getLine(); for (int i = 0; i < n; i++) { // 只读取存放在数组中但不对其进行相关操作 string re, bu; infile >> re >> bu; int re2 = atoi(re.c_str()); o[i].receiptID = re2; o[i].buyerID = bu; } infile.close(); } // 判断Order.txt 中是否存在该订单号,若存在返回该订单的行号,不存在返回-1 int Order::Judge(int receiptID) { Order order[50]; readOrder(order); // 调用Read() 函数,获取数据,以便等下进行相关数据的判断 int n = getLine(); for (int i = 0; i < n; i++) { if (receiptID == order[i].receiptID) { return i; // 如果存在,返回其下标 break; } } return -1; // 否则,返回-1 } // 添加订单 void Order::addOrder(int re, string bu) { ofstream outfile("Order.txt", ios::app); if (!outfile) { cerr << " open error" << endl; exit(1); } outfile << re << "\t"; outfile << bu << "\n"; outfile.close(); } // 删除订单 void Order::deleteOrder(int receiptID) { Order order[50]; readOrder(order); // 定义一个l 来接收Judge() 的返回值,等下用来判断该num是否存在 int l = Judge(receiptID); // 如果l 不等于-1 , 表示要删除的订单信息存在 if (l != -1) { int n = getLine(); ofstream outfile("Order.txt"); if (!outfile) { cerr << " open error" << endl; exit(1); } for (int i = 0; i < n; i++) { // 把下标不等于l (即除了要删的订单信息外)其余的数据重新写入磁盘保存 if (i != l) { outfile << order[i].receiptID << "\t"; outfile << order[i].buyerID << endl; } } outfile.close(); } } #pragma once #include <iostream> using namespace std; class Order { public: Order(); Order(int re, string bu); // 构造函数 int getLine(); // 获取Order.txt 中的行数 void readOrder(Order o[]); // 从Order.txt 中读取数据放入Receipt 对象数组中 int Judge(int receiptID); // 判断Order.txt 中是否存在该订单号,若存在返回该订单的行号,不存在返回 - 1 void addOrder(int receiptID, string buyerID); // 添加订单信息 void deleteOrder(int receiptID); // 删除订单信息 private: int receiptID; // 发票编号 string buyerID; // 会员编号 }; #include "Receipt.h" #include <iostream> #include <iomanip> #include <fstream> #include <string> #include "Book.h" #include "Buyer.h" #include "Order.h" using namespace std; const int MAX = 50; Receipt::Receipt() { receiptID = 0; pay = 0; // 支付总金额 } // 构造函数 Receipt::Receipt(int re, string bo, int nu) { receiptID = re; bookID = bo; num = nu; } // 获取Book.txt 的行数 int Receipt::getBookLine() { ifstream in("Book.txt"); string line; int n = 0; while (getline(in, line)) { n++; } in.close(); return n; } // 从Book.txt 中读取数据放入数组中 void Receipt::readBook(string bookID[], string bookName[], string author[], string publish[], string price[]) { ifstream infile("Book.txt", ios::in); if (!infile) { cerr << " open error" << endl; exit(1); } for (int i = 0; i < 5; i++) { infile >> bookID[i] >> bookName[i] >> author[i] >> publish[i] >> price[i]; // 只读取存放在数组中但不对其进行相关操作 } infile.close(); } // 获取Buyer.txt 的行数 int Receipt::getBuyerLine() { ifstream in("Buyer.txt"); string line; int n = 0; while (getline(in, line)) { n++; } in.close(); return n; } // 从Buyer.txt 中读取数据放入数组中 void Receipt::readBuyer(string buyerID[], string name[], string address[], string type[]) { ifstream infile("Buyer.txt", ios::in | ios::binary); if (!infile) { cerr << " open error" << endl; exit(1); } for (int i = 0; i < 5; i++) { infile >> buyerID[i] >> name[i] >> address[i] >> type[i]; // 只读取存放在数组中但不对其进行相关操作 } infile.close(); } // 从Order.txt 中,读取数据,放入数组中 void Receipt::readOrder(string receiptId[], string buyerIDs[]) { ifstream infile("Order.txt", ios::in | ios::binary); if (!infile) { cerr << " open error" << endl; exit(1); } Order o; int n = o.getLine(); for (int i = 0; i < n; i++) // 只读取存放在数组中但不对其进行相关操作 { infile >> receiptId[i] >> buyerIDs[i]; } infile.close(); } // 获取Receipt.txt 的行数 int Receipt::getReceiptLine() { ifstream in("Receipt.txt"); string line; int n = 0; while (getline(in, line)) { n++; } in.close(); return n; } // 从Receipt.txt 中读取数据放入Receipt 对象数组中 void Receipt::readReceipt(Receipt receipt[]) { ifstream infile("Receipt.txt", ios::in | ios::binary); if (!infile) { cerr << " open error" << endl; exit(1); } int n = getReceiptLine(); for (int i = 0; i < n; i++) // 只读取存放在数组中但不对其进行相关操作 { string re, bu, bo, nu; infile >> re >> bo >> nu; int re2 = atoi(re.c_str()); receipt[i].receiptID = re2; receipt[i].bookID = bo; int nu2 = atoi(nu.c_str()); receipt[i].num = nu2; } infile.close(); } // 判断Receipt 中是否存在用户要求的发票编号,如果存在,返回行号数组类 returnLs Receipt::Judge(int receiptid) { Receipt receipt[MAX]; returnLs ls; readReceipt(receipt); // 调用Read() 函数,获取数据,以便等下进行相关数据的判断 int j = 0; for (int i = 0; i < 10; i++) { int ri = receipt[i].receiptID; if (receiptid == ri) { ls.l[j] = i; j++; } } return ls; // 如果存在,返回其下标数组的类 } // 添加发票 void Receipt::addReceipt() { string buyerID; string bookID; int num = 0; // 从Receipt.txt 中读取数据放入Receipt 对象数组中 Receipt receipt[MAX]; readReceipt(receipt); int l = getReceiptLine(); // 获取行数 receiptID = receipt[l+1].receiptID; // 将发票号的初值赋为Receipt.txt 中最后一行的receiptID 的值 ofstream outfile("Receipt.txt", ios::app); if (!outfile) { cerr << " open error" << endl; exit(1); } // 获取买家ID cout << " 请输入买家ID :"; cin >> buyerID; // 将买家ID 和订单编号写入Order.txt 中 Order order; order.addOrder(receiptID, buyerID); // 将订单编号和书籍编号及数量存入Receipt.txt 中 cout << " 请输入购买书籍编号及数量,当书籍编号为0时,停止录入" << endl; while (bookID != "0") { int i = 0; cin >> bookID; // 当用户输入0时跳出循环,停止录入 if (bookID == "0") break; cin >> num; outfile << receiptID+1 << "\t"; outfile << bookID << "\t"; outfile << num << endl; receiptID++; } // 关闭文件 outfile.close(); cout << " 添加发票成功! " << endl; } void Receipt::deleteReceipt() { // 从Receipt.txt 中读取数据放入Receipt 对象数组中 Receipt receipt[MAX]; readReceipt(receipt); cout << " 请输入你要删除的订单的发票编号:"; cin >> receiptID; // 删除Order.txt 中用户输入订单号的那行数据 Order o; o.deleteOrder(receiptID); // 获取用户所需订单号在Receipt.txt 所在的行 returnLs ls = Judge(receiptID); if (ls.l[0] >= 0) { int n = getReceiptLine(); ofstream outfile("Receipt.txt"); if (!outfile) { cerr << " open error" << endl; exit(1); } int j = 0; for (int i = 0; i < n; i++) { // 把下标不等于ls[] 中的订单信息(即除了要删的订单信息外)其余的数据重新写入磁盘保存 if (i != ls.l[j]) { outfile << receipt[i].receiptID << "\t"; outfile << receipt[i].bookID << "\t"; outfile << receipt[i].num << endl; } else { j++; } } outfile.close(); cout << " 删除成功!" << endl; } else cout << " 该数据库没有此订单!" << endl; } // 根据订单号查找发票 void Receipt::selectReceiptByRI() { // 读取Receipt.txt 中的内容 Receipt receipt[MAX]; readReceipt(receipt); // 读取Book.txt 中的内容 string bookID[MAX], bookName[MAX], author[MAX], publish[MAX], price[MAX]; readBook(bookID, bookName, author, publish, price); // 读取Buyer.txt 中的内容 string buyerID[MAX], name[MAX], address[MAX], type[MAX]; readBuyer(buyerID, name, address, type); // 读取Order.txt 中的内容 string receiptId[MAX], buyerIDs[MAX]; readOrder(receiptId, buyerIDs); cout << " 请输入所需查找发票的编号: "; int ri = 0; cin >> ri; double discount = 1; int x = 0; Order o; int n = o.getLine(); // 循环Order.txt 中的内容,匹配查找订单号与循环内容中的订单号相同,从而获得对应订单的买家ID for (int i = 0; i < n; i++) { int rei = atoi(receiptId[i].c_str()); if (ri == rei) { x++; cout << endl << " 发票编号: " << ri << endl; // 循环Buyer.txt 中的内容, 匹配查找订单号的买家ID 与循环体中的买家ID 相同, 输出买家信息 int n1 = getBuyerLine(); for (int k = 0; k < n1; k++) { if (buyerIDs[i] == buyerID[k]) { // 判断买家身份,获取折扣 if (type[k] == "0") { People p(buyerID[k], name[k], address[k]); p.display(); } else if (type[k] == "1") { Member m(buyerID[k], name[k], address[k], 0.9); m.display(); discount = 0.9; } else if (type[k] == "2") { Vip v(buyerID[k], name[k], address[k], 0.8); v.display(); discount = 0.8; } } } } } returnLs ls = Judge(ri); if (ls.l[0] >= 0) { int n = getReceiptLine(); // 循环Receipt.txt 中的内容,判断查找的发票号与循环的发票号是否相同,循环结束后输出该订单的总金额 for (int j = 0; j < n; j++) { // 相同时,获取它的书籍编号 if (ri == receipt[j].receiptID) { int n2 = getBookLine(); // 循环书籍的内容,判断上面获取的书籍编号与循环的书籍编号是否相同 for (int m = 0; m < n2; m++) { // 相同,输出书籍信息,计算总金额 if (receipt[j].bookID == bookID[m]) { double pri = atof(price[m].c_str()); Book b(bookID[m], bookName[m], author[m], publish[m], pri); b.display(); cout << " 数量: " << receipt[j].num << endl << endl; pay = pay + pri * receipt[j].num; } } } } cout << " 总金额: " << pay * discount << endl; cout << "***********************************************************" << endl; } if (x == 0) { cout << endl << " 不存在该订单! " << endl; cout << "***********************************************************" << endl; } } // 根据买家ID 查找发票 void Receipt::selectReceiptByBI() { // 读取Receipt.txt 中的内容 Receipt receipt[MAX]; readReceipt(receipt); // 读取Book.txt 中的内容 string bookID[MAX], bookName[MAX], author[MAX], publish[MAX], price[MAX]; readBook(bookID, bookName, author, publish, price); // 读取Buyer.txt 中的内容 string buyerID[MAX], name[MAX], address[MAX], type[MAX]; readBuyer(buyerID, name, address, type); // 读取Order.txt 中的内容 string receiptId[MAX], buyerIDs[MAX]; readOrder(receiptId, buyerIDs); cout << " 请输入所需查找发票的卖家ID : "; string bi = ""; cin >> bi; double discount = 1; int x = 0; Order o; int n2 = o.getLine(); int n = atoi(receiptId[n2 - 1].c_str()); // 循环Order 中的内容,判断用户输入的买家ID 是否与循环体中的内容是否相同 for (int i = 0; i < n; i++) { // 相同时,输出发票号,卖家信息,书籍信息,总价 if (bi == buyerIDs[i]) { x++; //1. 输出发票号 cout << endl << " 发票号为: " << receiptId[i] << endl; //2. 输出买家信息 // 循环Buyer.txt ,判断在Order.txt 中的买家ID 是否与用户输入的买家ID 相同 int n2 = getBuyerLine(); for (int k = 0; k < n2; k++) { // 如果相同输出买家信息 if (buyerID[k] == bi) { if (type[k] == "0") { People p(buyerID[k], name[k], address[k]); p.display(); } else if (type[k] == "1") { Member m(buyerID[k], name[k], address[k], 0.9); m.display(); discount = 0.9; } else if (type[k] == "2") { Vip v(buyerID[k], name[k], address[k], 0.8); v.display(); discount = 0.8; } } } //3. 输出书籍信息 int n = getReceiptLine(); // 循环Receipt.txt, 判断循环体中的订单号与上述循环是否相同 for (int j = 0; j < n; j++) { int reci = atoi(receiptId[i].c_str()); if (receipt[j].receiptID == reci) { int n3 = getBookLine(); // 循环Book.txt, 判断Receipt.txt 中的书籍编号与循环体中书籍编号是否相同 for (int m = 0; m < n3; m++) { // 相同则输出书籍信息 if (receipt[j].bookID == bookID[m]) { double pri = atof(price[m].c_str()); Book b(bookID[m], bookName[m], author[m], publish[m], pri); b.display(); cout << " 数量: " << receipt[j].num << endl << endl; pay = pay + pri * receipt[j].num; } } } } cout << " 总金额: " << pay * discount << endl; cout << "***********************************************************" << endl; } } if (x == 0) { cout << endl << " 该买家没有订单! " << endl; cout << "***********************************************************" << endl; } } // 显示所有发票 void Receipt::print() { // 读取Receipt.txt 中的内容 Receipt receipt[MAX]; readReceipt(receipt); // 读取Book.txt 中的内容 string bookID[MAX], bookName[MAX], author[MAX], publish[MAX], price[MAX]; readBook(bookID, bookName, author, publish, price); // 读取Buyer.txt 中的内容 string buyerID[MAX], name[MAX], address[MAX], type[MAX]; readBuyer(buyerID, name, address, type); // 读取Order.txt 中的内容 string receiptId[MAX], buyerIDs[MAX]; readOrder(receiptId, buyerIDs); double discount = 1; Order o; int n2 = o.getLine(); int n = atoi(receiptId[n2 - 1].c_str()); for (int z = 0; z < n; z++) { // 判断Order.txt 中是否存在内容 if (n2 <= 0) { cout << " 不存在任何发票! " << endl; break; } else { // 循环Order 中的内容,判断订单号是否存在 for (int i = 0; i < n2; i++) { int rei = atoi(receiptId[i].c_str()); // 如果存在,输出订单号,输出买家信息,输出书本,输出订单总价 if (rei == z) { //1. 输出订单号 cout << endl << " 发票号为: " << rei << endl; //2. 输出买家信息 // 循环Buyer.txt ,判断在Order.txt 中的买家ID 是否与循环体中的买家ID 相同 int n2 = getBuyerLine(); for (int k = 0; k < n2; k++) { // 如果相同输出买家信息 if (buyerIDs[i] == buyerID[k]) { if (type[k] == "0") { People p(buyerID[k], name[k], address[k]); p.display(); } else if (type[k] == "1") { Member m(buyerID[k], name[k], address[k], 0.9); m.display(); discount = 0.9; } else if (type[k] == "2") { Vip v(buyerID[k], name[k], address[k], 0.8); v.display(); discount = 0.8; } } } //3. 输出书籍信息 int n = getReceiptLine(); // 循环Receipt.txt, 判断循环体中的订单号与上述是否相同 for (int j = 0; j < n; j++) { if (receipt[j].receiptID == z) { int n3 = getBookLine(); // 循环Book.txt, 判断Receipt.txt 中的书籍编号与循环体中书籍编号是否相同 for (int m = 0; m < n3; m++) { // 相同则输出书籍信息 if (receipt[j].bookID == bookID[m]) { double pri = atof(price[m].c_str()); Book b(bookID[m], bookName[m], author[m], publish[m], pri); b.display(); cout << " 数量: " << receipt[j].num << endl << endl; pay = pay + pri * receipt[j].num; } } } } cout << " 总金额: " << pay * discount << endl; cout << "***********************************************************" << endl; } } } } } #pragma once #include <iostream> #include <string> using namespace std; // 行号类,存放一个int 数组 class returnLs { public: int l[50]; }; // 发票类 class Receipt { public: Receipt(); Receipt(int re, string bo, int nu); // 构造函数 int getBookLine(); // 获取Book.txt 中的行数 void readBook(string bookID[], string bookName[], string author[], string publish[], string price[]); // 从Book.txt 中读取数据放入数组中 int getBuyerLine(); // 获取Buyer.txt 的行数 void readBuyer(string buyerID[], string name[], string address[], string type[]);// 从Buyer.txt 中读取数据放入数组中 void readOrder(string receiptID[], string buyerIDs[]); // 从Order.txt 中,读取数据,放入数组中 int getReceiptLine(); // 获取Receipt.txt 的行数 void readReceipt(Receipt receipt[]); // 从Receipt.txt 中读取数据放入Receipt 对象数组中 returnLs Judge(int receiptID); // 判断Receipt.txt 中是否存在用户要求的发票编号,如果存在,返回行号数组类 void addReceipt(); // 添加发票 void deleteReceipt(); // 删除发票 void selectReceiptByRI(); // 根据订单号查找发票 void selectReceiptByBI(); // 更具买家ID 查找发票 void print(); // 打印发票 private: int receiptID; // 发票编号 string bookID; // 图书编号 int num; // 购书数量 double pay; // 支付总金额 };



本次作业中所使用的代码来自于其他同学的以前的代码,最开始的功能是很简单的,只具备简单的增删查改功能,后来经过本次的优化,新增购买书籍功能,从开始的图书管理系统变成一个购买查询系统,并且还进行了会员价格以及非会员价格的分类计算,系统更加完善。而还在页面布局上做了一些优化,更加简洁美观。
浙公网安备 33010602011771号