fork(3) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cctype>
  5. #include <cstdlib>
  6. #include <stdexcept>
  7. #include <initializer_list>
  8.  
  9.  
  10. struct SalesData
  11. {
  12. std::string book_no;
  13. unsigned units_sold = 0;
  14. double revenue = 0.0;
  15.  
  16. std::istream &read (std::istream&, SalesData&);
  17.  
  18. SalesData () = default;
  19. SalesData (const std::string &s): book_no(s) {}
  20. SalesData (const std::string &s, unsigned n, double p)
  21. : book_no(s), units_sold(n), revenue(p*n) {}
  22. SalesData (std::istream &is)
  23. {
  24. read (is, *this);
  25. }
  26.  
  27. std::string isbn() const { return book_no; }
  28. SalesData& combine(const SalesData&);
  29. double avg_price() const;
  30. };
  31.  
  32. SalesData add (const SalesData&, const SalesData&);
  33. std::ostream &print (std::ostream&, const SalesData&);
  34. // std::istream &read (std::istream&, SalesData&);
  35.  
  36. double SalesData::avg_price() const
  37. {
  38. if (units_sold)
  39. return revenue/units_sold;
  40. else
  41. return 0;
  42. }
  43.  
  44. SalesData& SalesData::combine (const SalesData &rhs)
  45. {
  46. units_sold += rhs.units_sold;
  47. revenue += rhs.revenue;
  48. return *this;
  49. }
  50.  
  51. std::istream& read (std::istream &is, SalesData &item)
  52. {
  53. double price = 0.0;
  54. is >> item.book_no >> item.units_sold >> price;
  55. item.revenue = price * item.units_sold;
  56. return is;
  57. }
  58.  
  59. std::ostream& print (std::ostream &os, const SalesData &item)
  60. {
  61. os << item.isbn() << " " << item.units_sold <<
  62. " " << item.revenue << " " << item.avg_price();
  63. return os;
  64. }
  65.  
  66. SalesData add (const SalesData &lhs, const SalesData &rhs)
  67. {
  68. SalesData sum = lhs;
  69. sum.combine (rhs);
  70. return sum;
  71. }
  72.  
  73. int main()
  74. {
  75. SalesData total(std::cin), trans(std::cin), sum;
  76. std::cout << total.revenue << trans.revenue << std::endl;
  77.  
  78.  
  79. print (std::cout, total) << std::endl;
  80. print (std::cout, trans) << std::endl;
  81.  
  82. sum = add (trans, total);
  83. total.combine (trans);
  84.  
  85. print (std::cout, sum) << std::endl;
  86. print (std::cout, total) << std::endl;
  87. return 0;
  88. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/8T7H6O/ccSvktTR.o: In function `SalesData::SalesData(std::istream&)':
prog.cpp:(.text._ZN9SalesDataC2ERSi[_ZN9SalesDataC5ERSi]+0x23): undefined reference to `SalesData::read(std::istream&, SalesData&)'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty