c++primer5 第一章

1.1 编写一个简单的C++程序


1.一个函数的定义包含四部分:返回类型,函数名,形参列表,函数体。

2.当return语句包括一个值时,此返回值的类型必须与函数的返回类型相容。

3.程序所处理的数据都保存在变量中,而每个变量都有自己的类型。如果一个名为v的变量的类型为T,我们通常说“v具有类型T”,或等价的,“v是一个T类型变量。”


1.2 初识输入输出


1.标准输入输出对象
  cin:标准输入
  cout:标准输出
  cerr:标准错误
  clog:输出运行时的一般信息

2.namespace即“命名空间”,也称“名称空间” ,cout和cin都是定义在名为std的命名空间中的。命名空间可以帮助我们避免不经意的名字名字定义冲突,以及使用库中相同名字导致的冲突。标准库定义的所有名字都在命名空间std中。


3.当使用标准库中的一个名字时,必须显示说明我们想使用来自命名空间std中的名字。例如std::cout,通过使用作用域运算符(::)来指出我们想使用定义在命名空间std中的名字cout。


1.3 注释简介

1.单行注释以双斜线(//)开始,以换行符结束。当前行双斜线右侧的所有内容都会被编译器忽略,这种注释可以包括任何文本,包括额外的双斜线。


2.多行注释以/*开始,以*/结束,可以包含除*/以外的任意内容。编译器将落在/*和*/之间的所有内容都当做注释。


1.4 控制流

1.while语句

while语句的形式为:

  1. while(condition)  
  2. {  
  3.     statement;  
  4. }  
while语句的执行过程就是交替的检测其条件语句(condition)和执行循环体(statement)。

只要条件语句的判断为真,那么循环体就会一直执行下去,知道条件语句得出的结果为假才终止循环。


2.for语句

for语句的形式为:

  1. for(init-statement;condition;expression)  
  2. {  
  3.     statement;  
  4. }  
每个for语句都包含两部分:循环头和循环体(statement)。

循环头控制循环体的执行次数,它由三部分组成:一个初始化语句(init-statement),一个循环条件(condition),一个表达式(expression)


3.if语句

  1. if(condition)  
  2. {  
  3.     statement;  
  4. }  
if语句和while语句类似,但是if语句的statement只会在condition条件满足的情况下执行一次。


1.5 类的初识


1.在C++中,我们通过定义一个类来定义自己的数据结构。一个类定义了一个类型,以及与其关联的一组操作。类机制是C++最重要的特性之一。


2.成员函数是定义为类的一部分的函数,有时也被称为方法。我们通常以一个类对象的名义来调用成员函数。



PS:部分练习答案


练习1.3

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     std::cout<<"Hello, World"<<std::endl;  
  6.     return 0;  
  7. }  

练习1.4

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int v1 = 0,v2 = 0;  
  6.     std::cout<<"请输入两个数:";  
  7.     std::cin>>v1>>v2;  
  8.     std::cout<<"它们的乘积为:"<<v1<<"*"<<v2<<"="<<v1*v2<<std::endl;  
  9.     return 0;  
  10. }  

练习1.5

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int v1 = 0,v2 = 0;  
  6.     std::cout<<"请输入两个数:";  
  7.     std::cin>>v1>>v2;  
  8.     std::cout<<"它们的乘积为:";  
  9.     std::cout<<v1;  
  10.     std::cout<<"*";  
  11.     std::cout<<v2;  
  12.     std::cout<<"=";  
  13.     std::cout<<v1*v2;  
  14.     std::cout<<std::endl;  
  15.     return 0;  
  16. }  

练习1.6

该程序是不合法的,它会返回一个"expected primary-expression before '<<' token"的错误。
改法有两种
其一:去掉中间的分号

  1. std::cout << "The sum of " << v1  
  2.           << " and " << v2  
  3.           << " is " << v1 + v2 << std::endl;  
其二:添加std::out
  1. std::cout << "The sum of " << v1;  
  2. std::cout << " and " << v2  
  3.           << " is " << v1 + v2 << std::endl;  


练习1.9

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int sum = 0,v = 50;  
  6.     while(v<=100)  
  7.     {  
  8.         sum+=v;  
  9.         v++;  
  10.     }  
  11.     std::cout<<sum<<std::endl;  
  12.     return 0;  
  13. }  

练习1.10

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int v = 10;  
  6.     while(v>=0)  
  7.     {  
  8.         std::cout<<v--<<std::endl;  
  9.     }  
  10.     return 0;  
  11. }  

练习1.11

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int a,b,_max,_min;  
  6.     std::cout<<"请输入两个数:";  
  7.     std::cin>>a>>b;  
  8.     _max = a>b?a:b;  
  9.     _min = a>b?b:a;  
  10.     std::cout<<"这两个数区间内的整数有:"<<std::endl;  
  11.     while(_min<=_max)  
  12.     {  
  13.         std::cout<<_min++<<" ";  
  14.     }  
  15.     std::cout<<std::endl;  
  16.     return 0;  
  17. }  


练习1.13

  1. //1-9  
  2. #include <iostream>  
  3. int main()  
  4. {  
  5.     int sum = 0,v;  
  6.     for(v = 50;v<=100;v++)  
  7.     {  
  8.         sum+=v;  
  9.     }  
  10.     std::cout<<sum<<std::endl;  
  11.     return 0;  
  12. }  
  13.   
  14. //1-10  
  15. #include <iostream>  
  16. int main()  
  17. {  
  18.     int v = 10;  
  19.     for(v = 10;v>=0;v--)  
  20.     {  
  21.         std::cout<<v<<std::endl;  
  22.     }  
  23.     return 0;  
  24. }  
  25.   
  26. //1-11  
  27. #include <iostream>  
  28. int main()  
  29. {  
  30.     int a,b,_max,_min,i;  
  31.     std::cout<<"请输入两个数:";  
  32.     std::cin>>a>>b;  
  33.     _max = a>b?a:b;  
  34.     _min = a>b?b:a;  
  35.     std::cout<<"这两个数区间内的整数有:"<<std::endl;  
  36.     for(i = _min;i<=_max;i++)  
  37.     {  
  38.         std::cout<<i<<" ";  
  39.     }  
  40.     std::cout<<std::endl;  
  41.     return 0;  
  42. }  

练习1.16

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int sum = 0,a;  
  6.     while(std::cin>>a)  
  7.     {  
  8.         sum+=a;  
  9.     }  
  10.     std::cout<<sum<<std::endl;  
  11.     return 0;  
  12. }  

练习1.19

  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     int a,b,t;  
  6.     std::cout<<"请输入两个数:";  
  7.     std::cin>>a>>b;  
  8.     if(a>b)  
  9.     {  
  10.         t = a;  
  11.         a = b;  
  12.         b = t;  
  13.     }  
  14.     std::cout<<"这两个数区间内的整数有:"<<std::endl;  
  15.     while(a<=b)  
  16.     {  
  17.         std::cout<<a++<<" ";  
  18.     }  
  19.     std::cout<<std::endl;  
  20.     return 0;  
  21. }  

练习1.20

首先是接下来几个代码要用到的头文件Sales_item.h

  1. #ifndef SALESITEM_H  
  2. // we're here only if SALESITEM_H has not yet been defined  
  3. #define SALESITEM_H  
  4.   
  5. // Definition of Sales_item class and related functions goes here  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. class Sales_item {  
  10. // these declarations are explained section 7.2.1, p. 270  
  11. // and in chapter 14, pages 557, 558, 561  
  12. friend std::istream& operator>>(std::istream&, Sales_item&);  
  13. friend std::ostream& operator<<(std::ostream&, const Sales_item&);  
  14. friend bool operator<(const Sales_item&, const Sales_item&);  
  15. friend bool  
  16. operator==(const Sales_item&, const Sales_item&);  
  17. public:  
  18.     // constructors are explained in section 7.1.4, pages 262 - 265  
  19.     // default constructor needed to initialize members of built-in type  
  20.     Sales_item() = default;  
  21.     Sales_item(const std::string &book): bookNo(book) { }  
  22.     Sales_item(std::istream &is) { is >> *this; }  
  23. public:  
  24.     // operations on Sales_item objects  
  25.     // member binary operator: left-hand operand bound to implicit this pointer  
  26.     Sales_item& operator+=(const Sales_item&);  
  27.   
  28.     // operations on Sales_item objects  
  29.     std::string isbn() const { return bookNo; }  
  30.     double avg_price() const;  
  31. // private members as before  
  32. private:  
  33.     std::string bookNo;      // implicitly initialized to the empty string  
  34.     unsigned units_sold;  // explicitly initialized  
  35.     double revenue;  
  36. };  
  37.   
  38. // used in chapter 10  
  39. inline  
  40. bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)  
  41. return lhs.isbn() == rhs.isbn(); }  
  42.   
  43. // nonmember binary operator: must declare a parameter for each operand  
  44. Sales_item operator+(const Sales_item&, const Sales_item&);  
  45.   
  46. inline bool  
  47. operator==(const Sales_item &lhs, const Sales_item &rhs)  
  48. {  
  49.     // must be made a friend of Sales_item  
  50.     return lhs.units_sold == rhs.units_sold &&  
  51.            lhs.revenue == rhs.revenue &&  
  52.            lhs.isbn() == rhs.isbn();  
  53. }  
  54.   
  55. inline bool  
  56. operator!=(const Sales_item &lhs, const Sales_item &rhs)  
  57. {  
  58.     return !(lhs == rhs); // != defined in terms of operator==  
  59. }  
  60.   
  61. // assumes that both objects refer to the same ISBN  
  62. Sales_item& Sales_item::operator+=(const Sales_item& rhs)  
  63. {  
  64.     units_sold += rhs.units_sold;  
  65.     revenue += rhs.revenue;  
  66.     return *this;  
  67. }  
  68.   
  69. // assumes that both objects refer to the same ISBN  
  70. Sales_item  
  71. operator+(const Sales_item& lhs, const Sales_item& rhs)  
  72. {  
  73.     Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return  
  74.     ret += rhs;           // add in the contents of (|rhs|)  
  75.     return ret;           // return (|ret|) by value  
  76. }  
  77.   
  78. std::istream&  
  79. operator>>(std::istream& in, Sales_item& s)  
  80. {  
  81.     double price;  
  82.     in >> s.bookNo >> s.units_sold >> price;  
  83.     // check that the inputs succeeded  
  84.     if (in)  
  85.         s.revenue = s.units_sold * price;  
  86.     else  
  87.         s = Sales_item();  // input failed: reset object to default state  
  88.     return in;  
  89. }  
  90.   
  91. std::ostream&  
  92. operator<<(std::ostream& out, const Sales_item& s)  
  93. {  
  94.     out << s.isbn() << " " << s.units_sold << " "  
  95.         << s.revenue << " " << s.avg_price();  
  96.     return out;  
  97. }  
  98.   
  99. double Sales_item::avg_price() const  
  100. {  
  101.     if (units_sold)  
  102.         return revenue/units_sold;  
  103.     else  
  104.         return 0;  
  105. }  
  106. #endif  
然后是这道题的代码

  1. #include <iostream>  
  2. #include "Sales_item.h"  
  3. int main()  
  4. {  
  5.     Sales_item book;  
  6.     while(std::cin>>book)  
  7.     {  
  8.         std::cout<<book<<std::endl;  
  9.     }  
  10.     return 0;  
  11. }  

练习1.21

  1. #include <iostream>  
  2. #include "Sales_item.h"  
  3. int main()  
  4. {  
  5.     Sales_item book1,book2;  
  6.     std::cin>>book1>>book2;  
  7.     if(book1.isbn()==book2.isbn())  
  8.     {  
  9.         std::cout<<book1+book2<<std::endl;  
  10.     }  
  11.     else  
  12.     {  
  13.         std::cout<<"两本书的isbn必须相同"<<std::endl;  
  14.     }  
  15.     return 0;  
  16. }  

练习1.22

  1. #include <iostream>  
  2. #include "Sales_item.h"  
  3. int main()  
  4. {  
  5.     Sales_item book,sum;  
  6.     if(std::cin>>sum)  
  7.     {  
  8.         while(std::cin>>book)  
  9.         {  
  10.             if(book.isbn()==sum.isbn())  
  11.             sum+=book;  
  12.             else  
  13.             {  
  14.                 std::cout<<sum<<std::endl;  
  15.                 sum = book;  
  16.             }  
  17.         }  
  18.         std::cout<<sum<<std::endl;  
  19.     }  
  20.     else  
  21.     {  
  22.         std::cout<<"没有输入?"<<std::endl;  
  23.     }  
  24.     return 0;  
  25. }  

练习1.23

  1. #include <iostream>  
  2. #include "Sales_item.h"  
  3. int main()  
  4. {  
  5.     Sales_item book,now;  
  6.     if(std::cin>>now)  
  7.     {  
  8.         int cnt = 1;  
  9.         while(std::cin>>book)  
  10.         {  
  11.             if(book.isbn()==now.isbn())  
  12.             {  
  13.                 cnt++;  
  14.             }  
  15.             else  
  16.             {  
  17.                 std::cout<<now.isbn()<<"有"<<cnt<<"次销售记录"<<std::endl;  
  18.                 now = book;  
  19.                 cnt = 1;  
  20.             }  
  21.         }  
  22.         std::cout<<now.isbn()<<"有"<<cnt<<"次销售记录"<<std::endl;  
  23.     }  
  24.     else  
  25.     {  
  26.         std::cout<<"没有输入?"<<std::endl;  
  27.     }  
  28.     return 0;  
  29. }  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值