C++primer习题--第1章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,轉載請注明源地址。
【習題 1.3】
編一個程序,在標準輸出上打印“Hello, World”。
#include <iostream> using namespace std; int main() {cout<<"Hello, World\n";return 0; }【習題 1.4】
我們的程序利用內置加法操作符“+”來產生兩個數的和。編寫程序,使用乘法操作符“*”產生兩個數的積。
【習題 1.5】
我們的程序使用了一條較長的輸出語句。重寫程序,使用單獨的語句打印每一個操作數。
【習題 1.10】
用 for 循環編程,求從 50~100 的所有自然數的和。然后用 while 循環重寫該程序。
用for循環:
用 while 循環:
#include <iostream> using namespace std; int main( ) {int i, sum;i=50;sum=0;while(i++<=100)sum+=i;cout<<"sum of 50 to 100 is "<<sum<<endl;system("PAUSE");return【習題 1.11】
用 while 循環編程,輸出 10~0 遞減的自然數。然后用 for循環重寫該程序。
用 while 循環:
#include <iostream> using namespace std; int main( ) {int i=10;while(i>=0)cout<<i--<<endl;return 0; }用 for循環:
#include <iostream> using namespace std; int main( ) {for(int i=10; i >=0; --i)cout<<i<<endl;return 0; }
【習題 1.16】
編寫程序,輸出用戶輸入的兩個數中的較大者。
【習題 1.17】
編寫程序,要求用戶輸入一組數。輸出信息說明其中有多少個負數
return 0; }
【習題 1.18】
編寫程序,提示用戶輸入兩個數幵將這兩個數范圍內的每個數寫到標準輸出。
return 0; }
【習題 1.21】
本書配套網站(http://www.awprofessional.com/cpp_primer)的第1章的代碼目錄下有 Sales_ item.h 源文件。復制該文件到你的工作目錄。編寫程序,循環遍歷一組書的銷售交易, 讀入每筆交易幵將交易寫至標準輸出。
附上?Sales_ item.h 源文件:
#ifndef SALESITEM_H #define SALESITEM_H// Definition of Sales_item class and related functions goes here #include <iostream> #include <string>class Sales_item { friend bool operator==(const Sales_item&, const Sales_item&); // other members as before public:// added constructors to initialize from a string or an istreamSales_item(const std::string &book):isbn(book), units_sold(0), revenue(0.0) { }Sales_item(std::istream &is) { is >> *this; }friend std::istream& operator>>(std::istream&, Sales_item&);friend std::ostream& operator<<(std::ostream&, const Sales_item&); public:// operations on Sales_item objects// member binary operator: left-hand operand bound to implicit this pointerSales_item& operator+=(const Sales_item&);// other members as beforepublic:// operations on Sales_item objectsdouble avg_price() const;bool same_isbn(const Sales_item &rhs) const{ return isbn == rhs.isbn; }// default constructor needed to initialize members of built-in typeSales_item(): units_sold(0), revenue(0.0) { } // private members as before private:std::string isbn;unsigned units_sold;double revenue;};// 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_itemreturn lhs.units_sold == rhs.units_sold &&lhs.revenue == rhs.revenue &&lhs.same_isbn(rhs); }inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs) {return !(lhs == rhs); // != defined in terms of operator== }using std::istream; using std::ostream;// assumes that both objects refer to the same isbn inline 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 inline Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) {Sales_item ret(lhs); // copy lhs into a local object that we'll returnret += rhs; // add in the contents of rhs return ret; // return ret by value }inline istream& operator>>(istream& in, Sales_item& s) {double price;in >> s.isbn >> s.units_sold >> price;// check that the inputs succeededif (in)s.revenue = s.units_sold * price;else s = Sales_item(); // input failed: reset object to default statereturn in; }inline ostream& operator<<(ostream& out, const Sales_item& s) {out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();return out; }inline double Sales_item::avg_price() const {if (units_sold) return revenue/units_sold; else return 0; } #endif View Code【習題 1.22】
編寫程序,讀入兩個具有相同 ISBN 的 Sales_item 對象幵產生它們的和。
【習題 1.23】
編寫程序,讀入幾個具有相同 ISBN 的交易,輸出所有讀入交易的和。
【習題 1.24】
編寫程序,讀入幾筆不同的交易。對于每筆新讀入的交易,要確定它的 ISBN 是否和以前的交易的 ISBN 一樣,并且記下每一個 ISBN 的交易的總數。
通過給定多筆不同的交易來測試程序。這些交易必須代表多個不同的 ISBN,但是每個ISBN 的記錄應分在同一組。
#include <iostream> #include <map> #include <string> #include "Sales_item.h" using namespace std; int main( ) {Sales_item trans;cout<<"讀入交易:"<<endl;cin>>trans;map<string, int> count;count[trans.getIsbn()]++;while(cin>>trans) {++count[trans.getIsbn()];}map<string, int>::iterator it;for(it=count.begin(); it!=count.end(); it++)cout<<it->first<<":"<<it->second<<endl;system("PAUSE");return 0; }?
轉載于:https://www.cnblogs.com/wuyudong/p/cpp-primer-chapter1-ans.html
總結
以上是生活随笔為你收集整理的C++primer习题--第1章的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 破解mysql数据库的密码
- 下一篇: C++之类和对象