本章学习了如何定义main函数,以及定义基本变量,如何进行输入输出,以及常见的条件语句结果和循环结构。
以一个书店小程序结尾,可以统计连续出现的数据的次数。
Sales_item.h
#ifndef SALESITEM_H // we're here only if SALESITEM_H has not yet been defined #define SALESITEM_H // Definition of Sales_item class and related functions goes here #include#include class Sales_item { // these declarations are explained section 7.2.1, p. 270 // and in chapter 14, pages 557, 558, 561 friend std::istream& operator>>(std::istream&, Sales_item&); friend std::ostream& operator<<(std::ostream&, const Sales_item&); friend bool operator<(const Sales_item&, const Sales_item&); friend bool operator==(const Sales_item&, const Sales_item&); public: // constructors are explained in section 7.1.4, pages 262 - 265 // default constructor needed to initialize members of built-in type Sales_item() : units_sold(0), revenue(0.0) { } Sales_item(const std::string &book) : bookNo(book), units_sold(0), revenue(0.0) { } Sales_item(std::istream &is) { is >> *this; } public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item& operator+=(const Sales_item&); // operations on Sales_item objects std::string isbn() const { return bookNo; } double avg_price() const; // private members as before private: std::string bookNo; // implicitly initialized to the empty string unsigned units_sold; double revenue; }; // used in chapter 10 inline bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) { return lhs.isbn() == rhs.isbn(); } // nonmember binary operator: must declare a parameter for each operand Sales_item operator+(const Sales_item&, const Sales_item&); inline bool operator==(const Sales_item &lhs, const Sales_item &rhs) { // must be made a friend of Sales_item return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.isbn() == rhs.isbn(); } inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs) { return !(lhs == rhs); // != defined in terms of operator== } // assumes that both objects refer to the same ISBN Sales_item& Sales_item::operator+=(const Sales_item& rhs) { units_sold += rhs.units_sold; revenue += rhs.revenue; return *this; } // assumes that both objects refer to the same ISBN Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return ret += rhs; // add in the contents of (|rhs|) return ret; // return (|ret|) by value } std::istream& operator>>(std::istream& in, Sales_item& s) { double price; in >> s.bookNo >> s.units_sold >> price; // check that the inputs succeeded if (in) s.revenue = s.units_sold * price; else s = Sales_item(); // input failed: reset object to default state return in; } std::ostream& operator<<(std::ostream& out, const Sales_item& s) { out << s.isbn() << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(); return out; } double Sales_item::avg_price() const { if (units_sold) return revenue / units_sold; else return 0; } #endif
书店小程序.cpp
#include#include"Sales_item.h" using namespace std; 统计输入中每个值的出现次数 //void test01() //{ // //currVal是正在统计的数,val用于存读入的新值 // int currVal = 0, val = 0; // if (cin >> currVal) // { // int cnt = 1;//保存正在处理的当前值的个数 // while (cin >> val)//读取剩余的数 // { // if (val == currVal)//如果值相同 // { // ++cnt; // } // else // { // cout << currVal << " 出现了 " << cnt << " 次" << endl; // currVal = val; // cnt = 1; // // } // } // cout << currVal << " 出现了 " << cnt << " 次" << endl; // // } // //} void test01() { //读取多条销售记录,并统计每个ISBN有几条销售记录 Sales_item currItem,item; if (cin >> currItem) { int cnt = 1; while (cin >> item) { if (currItem.isbn() == item.isbn())//ISBN相同 { cnt++; } else//ISBN不同 { cout << currItem.isbn() << "有" << cnt << "条销售记录" << endl; currItem = item; cnt = 1; } cout << currItem.isbn() << "有" << cnt << "条销售记录" << endl; } } } void test02() { //保存下一条交易记录的变量 Sales_item total; //读入第一条交易记录,确保有数据可以处理 if (cin >> total) { Sales_item trans; while (cin >> trans) { //如果仍在处理相同的书 if (total.isbn() == trans.isbn()) { //更新总销售额 total += trans; } else { //打印前一本书的结果 cout << total << endl; //total现在表示下一本书的销售额 total = trans; } } //打印最后一本书的结果 cout << total << endl; } else { //没有输入 cout << "没有数据输入" << endl; } } int main() { test02(); system("pause"); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)