C++ Primer 学习笔记_2_快速入门(续)

来源:互联网 发布:广通速递淘宝合作了吗 编辑:程序博客网 时间:2024/04/30 21:04


P15习题

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //题1.14: 试分析如果v1 == v2的情况下,该程序的输出结果  
  2. #include <iostream>  
  3.   
  4. int main()  
  5. {  
  6.     std::cout << "Enter two numbers:" << std::endl;  
  7.     int v1,v2;  
  8.     std::cin >> v1 >> v2;  
  9.   
  10.     int lower,upper;  
  11.     if (v1 <= v2)  
  12.     {  
  13.         lower = v1;  
  14.         upper = v2;  
  15.     }  
  16.     else  
  17.     {  
  18.         lower = v2;  
  19.         upper = v1;  
  20.     }  
  21.   
  22.     int sum = 0;  
  23.     for (int i = lower; i <= upper; ++i)  
  24.     {  
  25.         sum += i;  
  26.     }  
  27.   
  28.     std::cout << "Sum of " << lower  
  29.               << " to " << upper  
  30.               << " inclusive is "  
  31.               << sum << std::endl;  
  32.   
  33.     return 0;  
  34. }  

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //1.16  
  2. #include <iostream>  
  3.   
  4. int main()  
  5. {  
  6.     std::cout << "Please input a sequence of numbers:" << std::endl;  
  7.     int val;  
  8.     int count = 0;  
  9.     //为判断条件,先执行输入操作  
  10.     while (std::cin >> val)  
  11.     {  
  12.         if (val < 0)  
  13.         {  
  14.             ++ count;  
  15.         }  
  16.     }  
  17.   
  18.     std::cout << "There is " << count << " negatives" << std::endl;  
  19.     return 0;  
  20. }  

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //题1.19  
  2. #include <iostream>  
  3.   
  4. int main()  
  5. {  
  6.     std::cout << "Please input two numbers:" << std::endl;  
  7.     int v1,v2;  
  8.     int count = 1;  
  9.     std::cin >> v1 >> v2;  
  10.     for (int i = v1 ; i <= v2; ++i)  
  11.     {  
  12.         std::cout << i << ' ';  
  13.         if (count % 10 == 0)  
  14.         {  
  15.             std::cout << std::endl;  
  16.         }  
  17.         ++ count;  
  18.     }  
  19.   
  20.     return 0;  
  21. }  

五、类的简介

1C++中,我们通过定义类来定义自己的数据结构

       实际上,C++设计的主要焦点就是使所定义的类类型的行为可以像内置类型一样自然!!!


2、使用类的时候,我们不需要指定哦啊这个类是怎样实现的,相反,我们需要知道的是:这个类能够提供什么样的操作!


3、对于自定义的类,必须使得编译器可以访问和类相关的定义。


4、通常文件名和定义在头文件中的类名是一样的。通常后缀名是.h,但是有些IDE会挑剔头文件的后缀名!


5、当使用自定义头文件时,我们采用双引号(””)把头文件包含进来。


6、当调用成员函数的时候,(通常)指定函数要操作的对象,语法是使用点操作符(”.”,左操作符必须是类类型,右操作符必须指定该类型的成员。


P19

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //习题1.21  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include "Sales_item.h"  
  5.   
  6. int main()  
  7. {  
  8.     freopen("book_sales","r",stdin);  
  9.     Sales_item item;  
  10.     while (std::cin >> item)  
  11.     {  
  12.         std::cout << item << std::endl;  
  13.     }  
  14.   
  15.     return 0;  
  16. }  

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //习题1.22  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include "Sales_item.h"  
  5.   
  6. int main()  
  7. {  
  8.     freopen("book_sales","r",stdin);  
  9.     Sales_item item1;  
  10.     Sales_item item2;  
  11.     while (std::cin >> item1 >> item2)  
  12.     {  
  13.         if (item1.same_isbn(item2))  
  14.         {  
  15.             std::cout << item1 + item2 << std::endl;  
  16.         }  
  17.     }  
  18.   
  19.     return 0;  
  20. }  

六、C++程序

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <fstream>  
  3. #include "Sales_item.h"  
  4.   
  5. int main()  
  6. {  
  7.     //freopen("book_sales.","r",stdin);  
  8.   
  9.     Sales_item total,trans;  
  10.   
  11.     if (std::cin >> total)  
  12.     {  
  13.         while (std::cin >> trans)  
  14.         {  
  15.             if (total.same_isbn(trans))  
  16.             {  
  17.                 total += trans;  
  18.             }  
  19.             else  
  20.             {  
  21.                 std::cout << total << std::endl;  
  22.                 total = trans;  
  23.             }  
  24.         }  
  25.         std::cout << total << std::endl;  
  26.     }  
  27.     else  
  28.     {  
  29.         std::cout << "No Data?!" << std::endl;  
  30.         return -1;  
  31.     }  
  32.     return 0;  
  33. }  

【说明:Sales_item类需要到图灵网站上下载并引用:http://www.ituring.com.cn/book/656】
0 0