int main() { //Sales_data just_isbn_book("123456789-D"); //Does not compile.We are forced to give at least isbn and title Sales_data just_isbn_title_book("123456789-D","TitleBook"); print(cout , just_isbn_title_book) ; cout << endl; Sales_data full_book("123456789-F","EffectiveC++", 3, 38.8); //test usage of 2nd constructor.OK it compiles print(cout , full_book) ; cout << endl; cout << "\tAfter testing the 2 constructors, we read form the file supplied in the command line " << endl; Sales_data total(cin); // if( !total.isbn().empty() ) { Sales_data trans; // variable to hold data for the next transaction while( read(cin, trans) ) { if ( total.isbn() == trans.isbn() ) // are we still getting data for the same ISBN total.combine(trans); else { // or we just started processing a new ISBN ? print(cout , total ) ; cout<<endl; //if new isbn, first output what we got so far for previews isbn total = trans ; //and start to process the next book(s) with the new ISBN } } print( cout , total ) ; cout<<endl; // when reading stops, do not forget to print the last transaction(s) } else std::cerr << "No data?!" << endl; // Reading failed? there was no input. Notify the user return 0; }
int main(int argc, char *argv[]) { if(argc != 3) { cerr << "Wrong input!" << endl; return -1; } ifstream input(argv[1]); // open the file of sales transactions ofstream output(argv[2]); // open the output file Sales_data total; // variable to hold the running sum if (read(input, total)) // read the first transaction { Sales_data trans; // variable to hold data for the next transaction while(read(input, trans)) // read the remaining transactions { if(total.isbn() == trans.isbn()) // check isbns total.combine(trans); // update the running total else { print(output, total) << endl; // print the results total = trans; // process the next book } } print(output, total) << endl; // print the last transaction } else { cerr << "No data?!" << endl; // no input } return 0; }
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) { Sales_data sum = lhs; sum.combine(rhs); return sum; }
int main(int argc, char const *argv[]) { Sales_data total; if(read(std::cin, total)) { Sales_data trans; while(read(std::cin, trans)) { if(total.bookNo == trans.bookNo) { total.combine(trans); } else { print(std::cout, total) << std::endl; total = trans; } } print(std::cout, total) << std::endl; } else { std::cerr << "No data" << std::endl; } return 0; }
// The transaction-processing program // Mostly copied from 7.3.cpp with some modifications int main() { // variable to hold data for the next transaction Sales_data total; // read the first transaction and ensure that there are // data to process if (read(cin, total)) { // variable to hold the running sum Sales_data trans; while (read(cin, trans)) { // if we're still processing the same book if (total.isbn() == trans.isbn()) { // update the running total total.combine(trans); } else { // print results for the previous book print(cout, total) << endl; // Total now refers to the next book total = trans; } } // print the last transaction print(cout, total) << endl; } else { // no input! warn the user cerr << "No data?!" << endl; return -1; // indicate failure } return 0; }
int main ( int argc, char *argv[] ) { //Sales_data total; Sales_data total = Sales_data(); Sales_data out = Sales_data("outnum", 8, 8.5); print(cout, out) << endl;; if(read(cin,total)) { Sales_data trans = Sales_data(""); while (read(cin,trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } else { cerr << "No data?!" << endl; } return 0; } /* ---------- end of function main ---------- */
int main(int argc, char* argv[]){ if( argc < 2){ cerr << "arguments are not enough" << endl; exit(1); } string fileName = argv[1]; ifstream input(fileName); if(input){ Sales_data total; if(read(input, total)){ Sales_data trans; while(read(input, trans)){ if(total.isbn() == trans.isbn()) total.combine(trans); else{ print(cout, total) << endl; total = trans; } } print(cout, total) << endl; }else{ cerr << "No data?!" << endl; } } }
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) { Sales_data sum = lhs; // copy data members from lhs into sum sum.combine(rhs); // add data members from rhs into sum return sum; }
int main(int argc, char **argv) { ifstream input(argv[1]); ofstream output(argv[2], ofstream::app); Sales_data total; if (read(input, total)) { Sales_data trans; while (read(input, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(output, total) << endl; total = trans; } } print(output, total) << endl; } else { cerr << "No data?!" << endl; } return 0; }
int main(){ Sales_data total; if(read(cin,total)){ Sales_data trans; while(read(cin,trans)){ if(total.isbn() == trans.isbn()){ total.combine(trans); }else{ print(cout,total) << endl; total = trans; } } print(cout,total) << endl; }else{ cerr << "No data ?! " << endl; } }
int main() { Sales_data total; // variable to hold the running sum if (read(cin, total)) { // read the first transaction Sales_data trans; // variable to hold data for the next transaction while(read(cin, trans)) // read the remaining transactions { if (total.isbn() == trans.isbn()) // check the isbns { total.combine(trans); // update the running total } else { print(cout, total) << endl; // print the results total = trans; // process the next book } } print(cout, total) << endl; // print the last transaction } else { // there was no input cerr << "No data?!" << endl; // notify the user } return 0; }
int main(int argc, char const *argv[]) { Sales_data total; std::ifstream in(argv[1]); if (read(in, total)) { Sales_data trans; while (read(in, trans)) { if (total.isbn() == trans.isbn()) { total.combine(trans); } else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } else { cerr << "No data" << endl; } return 0; }
void func(string infileName, string outfielName) { ifstream infile(infileName); ofstream outfile(outfielName, ostream::app); if (!infile.is_open()) { cerr << "error in open file: " << infileName; exit(1); } Sales_data total; if (read(infile, total)) { Sales_data trans; while (read(infile, trans)) { if (total.isbn() == trans.isbn()) { total.combine(trans); } else { print(outfile, total) << endl; total = trans; } } print(outfile, total) << endl; } else { cerr << "no data?" << endl; } }
int main() { Sales_data total; if (read(std::cin, total)) { //double price; //if (std::cin >> total.bookNo >> total.units_sold >> price) { // total.revenue = total.units_sold * price; Sales_data trans; while (read(std::cin, trans)) { //while (std::cin >> trans.bookNo >> trans.units_sold >> price) { // trans.revenue = trans.units_sold * price; if (total.isbn() == trans.isbn()) { total.combine(trans); } else { print(std::cout, total) << std::endl; //std::cout << total.bookNo << " " // << total.units_sold << " " // << total.revenue << std::endl; total = trans; // Use default copy constructor } } print(std::cout, total) << std::endl; //std::cout << total.bookNo << " " // << total.units_sold << " " // << total.revenue << std::endl; } else { std::cerr << "No data!" << std::endl; return -1; } return 0; }
int main(){ Sales_data total; if(cin >> total.bookNo >> total.units_sold >> total.revenue){ Sales_data trans; while(cin >> trans.bookNo >> trans.units_sold >> trans.revenue){ if(trans.isbn() == total.isbn()){ total.combine(trans); }else{ cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl; total = trans; } } cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl; }else{
Sales_data add (const Sales_data& lsh, const Sales_data& rsh) { Sales_data total = lsh; total.bookNo = lsh.isbn(); total.units_sold = lsh.units_sold + rsh.units_sold; total.revenue = lsh.revenue + rsh.revenue; return total; }
int main() { Sales_data total; if(read(cin, total)){ Sales_data trans; while(read(cin, trans)){ if(total.isbn()== trans.isbn()) total.combine(trans); else{ print(cout, total)<<endl; total= trans; } } print(cout, total)<<endl; } else{ std::cerr<<"No data!" <<endl; return -1; } return 0; }
void ReportForSalesData8_08(const std::string& filein, const std::string& fileout) { std::ifstream ifs(filein); if (!ifs) { std::cerr << "open file \"" << filein << "\" error." << std::endl; } std::ofstream ofs(fileout, std::ofstream::app); if (!ofs) { std::cerr << "open file \"" << fileout << "\" error." << std::endl; } Sales_data total; if (read(ifs, total)) //输入为文件流ifs { Sales_data trans; while (read(ifs, trans)) { if (total.isbn() == trans.isbn()) { total.combine(trans); } else { print(ofs, total) << std::endl; //输出到一个文件ofs流 total = trans; } } print(ofs, total) << std::endl; } else { std::cerr << "No data." << std::endl; } ifs.close(); }
int main() { Sales_data total; if (read(cin, total)) { Sales_data trans; while (read(cin, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total.bookNo = trans.bookNo; total.units_sold = trans.units_sold; total.revenue = trans.revenue; } } print(cout, total) << endl; } else { cerr << "No data?!" << endl; return -1; } return 0; }
int main(int argc, char *argv[]) { ifstream ifst(argv[1]); ofstream ofst(argv[2], ofstream::app); Sales_data total; if (read(ifst, total)) { Sales_data trans; while (read(ifst, trans)) { if (total.isbn() == trans.isbn()) { total.combine(trans); } else { print(ofst, total) << endl; total = trans; } } print(ofst, total) << endl; } else { std::cerr << "No Data?!" << endl; } return 0; }
int main() { Sales_data total; if (read(cin,total)) { Sales_data tram; //如果读入第一条记录 while (read(cin, tram)) //读入第二条记录 { if (total.isbn == tram.isbn) //如果第二条记录的isbn号等于第一条,结合 { total.conbine(tram); } else{ total = tram; //第一条记录等于第二条记录,再比较 } } print(cout, tram); //输出最后一条记录 } else { cout << "no data?"; } }
int main(int argc, char *argv[]) { if (argc != 2) { cerr << "Usage: \"Exercise 8.6\" filename" << endl; return 0; } ofstream output(argv[1], ofstream::app); Sales_data total; if (read(cin, total)) { Sales_data trans; while (read(cin, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(output, total) << endl; total = trans; } } print(output, total) << endl; // print the last transaction } else cerr << "No data?!" << endl; return 0; }
int main(int argc, char * argv[]) { Sales_data just_isbn_book("123456789-D"); //test usage of partial constructor.OK it compiles print(cout , just_isbn_book) ; cout << endl; Sales_data full_book("123456789-F", 3, 38.8, "Effective C++"); //test usage of full constructor.OK it compiles print(cout , full_book) ; cout << endl; cout << "\tAfter testing the 2 constructors, we read form the file supplied in the command line " << endl; if(argc == 1) //input file name passed with redirection < filename { Sales_data total(cin); //using the constructor for initialization: reading input e.g. from a file, or from console if( !total.isbn().empty() ) { Sales_data trans; // variable to hold data for the next transaction while( read(cin, trans) ) { if ( total.isbn() == trans.isbn() ) total.combine(trans); else { print(cout , total ) ; cout<<endl; total = trans ; } } print( cout, total ) ; cout<<endl; } else std::cerr << "No data?!" << endl; } else if (argc == 2) // input file passed as command line arg { ifstream input_file(argv[1]); Sales_data total; if (read(input_file, total)) { // read the first transaction Sales_data trans; // variable to hold data for the next transaction while(read(input_file, trans)) { // read the remaining transactions if (total.isbn() == trans.isbn()) // check isbn s total.combine(trans); // update the running total else { print(cout, total) << endl; // print the results total = trans; // process the next book } } print(cout, total) << endl; // print the last transaction } else // there was no input cerr << "No data?!" << endl; } return 0; }
int main() { Sales_data total; double totalPrice; if( cin >> total.bookNo >> total.units_sold >> totalPrice ){ Sales_data trans; double transPrice; total.CalcRevenue(totalPrice); while( cin >> trans.bookNo >> trans.units_sold >> transPrice ){ trans.CalcRevenue(transPrice); if( total.bookNo == trans.bookNo ){ total.AddData(trans); }else{ total.Print(); total.SetData(trans); } } total.Print(); return 0; }else{
bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs) { return lhs.isbn() < rhs.isbn(); }
bool compareIsbn(Sales_data &s1, Sales_data &s2) { return s1.isbn() < s2.isbn(); }
bool eqOp(const Sales_data &lhs, const Sales_data &rhs) { return lhs.isbn() == rhs.isbn(); }
size_t hasher(const Sales_data &sd) { return hash<string>() (sd.isbn()); }
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) { Sales_data total = lhs; total.combine(rhs); return total; }
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) { Sales_data sum = lhs; // Use default copy constructor sum.combine(rhs); return sum; }