C++ Primer 学习笔记_2_高速入口(继续)
P15习题
- //题1.14: 试分析假设v1 == v2的情况下,该程序的输出结果
- #include <iostream>
- int main()
- {
- std::cout << "Enter two numbers:" << std::endl;
- int v1,v2;
- std::cin >> v1 >> v2;
- int lower,upper;
- if (v1 <= v2)
- {
- lower = v1;
- upper = v2;
- }
- else
- {
- lower = v2;
- upper = v1;
- }
- int sum = 0;
- for (int i = lower; i <= upper; ++i)
- {
- sum += i;
- }
- std::cout << "Sum of " << lower
- << " to " << upper
- << " inclusive is "
- << sum << std::endl;
- return 0;
- }
//题1.14: 试分析假设v1 == v2的情况下,该程序的输出结果
#include <iostream> int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1,v2;
std::cin >> v1 >> v2; int lower,upper;
if (v1 <= v2)
{
lower = v1;
upper = v2;
}
else
{
lower = v2;
upper = v1;
} int sum = 0;
for (int i = lower; i <= upper; ++i)
{
sum += i;
} std::cout << "Sum of " << lower
<< " to " << upper
<< " inclusive is "
<< sum << std::endl; return 0;
}
- //1.16
- #include <iostream>
- int main()
- {
- std::cout << "Please input a sequence of numbers:" << std::endl;
- int val;
- int count = 0;
- //为推断条件,先运行输入操作
- while (std::cin >> val)
- {
- if (val < 0)
- {
- ++ count;
- }
- }
- std::cout << "There is " << count << " negatives" << std::endl;
- return 0;
- }
//1.16
#include <iostream> int main()
{
std::cout << "Please input a sequence of numbers:" << std::endl;
int val;
int count = 0;
//为推断条件。先运行输入操作
while (std::cin >> val)
{
if (val < 0)
{
++ count;
}
} std::cout << "There is " << count << " negatives" << std::endl;
return 0;
}
- //题1.19
- #include <iostream>
- int main()
- {
- std::cout << "Please input two numbers:" << std::endl;
- int v1,v2;
- int count = 1;
- std::cin >> v1 >> v2;
- for (int i = v1 ; i <= v2; ++i)
- {
- std::cout << i << ' ';
- if (count % 10 == 0)
- {
- std::cout << std::endl;
- }
- ++ count;
- }
- return 0;
- }
//题1.19
#include <iostream> int main()
{
std::cout << "Please input two numbers:" << std::endl;
int v1,v2;
int count = 1;
std::cin >> v1 >> v2;
for (int i = v1 ; i <= v2; ++i)
{
std::cout << i << ' ';
if (count % 10 == 0)
{
std::cout << std::endl;
}
++ count;
} return 0;
}
五、类的简单介绍
1、C++中,我们通过定义类来定义自己的数据结构
实际上,C++设计的主要焦点就是使所定义的类类型的行为能够像内置类型一样自然!!。
2、使用类的时候,我们不须要指定哦啊这个类是如何实现的。相反。我们须要知道的是:这个类可以提供什么样的操作!
3、对于自己定义的类,必须使得编译器能够訪问和类相关的定义。
4、通常文件名称和定义在头文件里的类名是一样的。
通常后缀名是.h,可是有些IDE会挑剔头文件的后缀名!
5、当使用自己定义头文件时。我们採用双引號(””)把头文件包括进来。
6、当调用成员函数的时候,(通常)指定函数要操作的对象。语法是使用点操作符(”.”),左操作符必须是类类型,右操作符必须指定该类型的成员。
P19
- //习题1.21
- #include <iostream>
- #include <fstream>
- #include "Sales_item.h"
- int main()
- {
- freopen("book_sales","r",stdin);
- Sales_item item;
- while (std::cin >> item)
- {
- std::cout << item << std::endl;
- }
- return 0;
- }
//习题1.21
#include <iostream>
#include <fstream>
#include "Sales_item.h" int main()
{
freopen("book_sales","r",stdin);
Sales_item item;
while (std::cin >> item)
{
std::cout << item << std::endl;
} return 0;
}
- //习题1.22
- #include <iostream>
- #include <fstream>
- #include "Sales_item.h"
- int main()
- {
- freopen("book_sales","r",stdin);
- Sales_item item1;
- Sales_item item2;
- while (std::cin >> item1 >> item2)
- {
- if (item1.same_isbn(item2))
- {
- std::cout << item1 + item2 << std::endl;
- }
- }
- return 0;
- }
//习题1.22
#include <iostream>
#include <fstream>
#include "Sales_item.h" int main()
{
freopen("book_sales","r",stdin);
Sales_item item1;
Sales_item item2;
while (std::cin >> item1 >> item2)
{
if (item1.same_isbn(item2))
{
std::cout << item1 + item2 << std::endl;
}
} return 0;
}
六、C++程序
- #include <iostream>
- #include <fstream>
- #include "Sales_item.h"
- int main()
- {
- //freopen("book_sales.","r",stdin);
- Sales_item total,trans;
- if (std::cin >> total)
- {
- while (std::cin >> trans)
- {
- if (total.same_isbn(trans))
- {
- total += trans;
- }
- else
- {
- std::cout << total << std::endl;
- total = trans;
- }
- }
- std::cout << total << std::endl;
- }
- else
- {
- std::cout << "No Data?
!" << std::endl;
- return -1;
- }
- return 0;
- }
#include <iostream>
#include <fstream>
#include "Sales_item.h" int main()
{
//freopen("book_sales.","r",stdin); Sales_item total,trans; if (std::cin >> total)
{
while (std::cin >> trans)
{
if (total.same_isbn(trans))
{
total += trans;
}
else
{
std::cout << total << std::endl;
total = trans;
}
}
std::cout << total << std::endl;
}
else
{
std::cout << "No Data? !" << std::endl;
return -1;
}
return 0;
}
【说明:Sales_item类需要图灵网站下载及参考:http://www.ituring.com.cn/book/656】
C++ Primer 学习笔记_2_高速入口(继续)的更多相关文章
- APPCAN学习笔记001---app高速开发AppCan.cn平台概述
1.APPCAN学习笔记---app高速开发AppCan.cn平台概述 1. 平台概述 技术qq交流群:JavaDream:251572072 AppCan.cn开发平台是基于HTML5技术的跨平台移 ...
- C++ Primer学习笔记(三) C++中函数是一种类型!!!
C++中函数是一种类型!C++中函数是一种类型!C++中函数是一种类型! 函数名就是变量!函数名就是变量!函数名就是变量! (---20160618最新消息,函数名不是变量名...囧) (---201 ...
- C++ Primer学习笔记(二)
题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题. 接 C++ Primer学习笔记(一) 27.与 vector 类型相比,数组的显著缺陷在于:数组的长度是固定的,无法 ...
- C++ Primer学习笔记(一)
始终对C++念念不忘,看过 一个32岁入门的70后程序员给我的启示 之后,心情激荡,更是一发不可收拾. 认真地说,我不是一个执着的人,见异思迁,好读书而不求甚解,兼之情绪化(~~ 某些方面),于是怒 ...
- C++ Primer学习笔记2--c++标准库中的 vector、string 和 bitset 类型
一.string #include <string> using std::string 初始化函数: string s1; 默认构造函数 s1 为空串 ...
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...
- C++ Primer 学习笔记_35_STL实践与分析(9)--map种类(在)
STL实践与分析 --map类型(上) 引: map是键-值对的集合. map类型通常能够理解为关联数组:能够通过使用键作为下标来获取一个值,正如内置数组类型一样:而关联的本质在于元素的值与某个特定的 ...
- C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】
STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...
- APPCAN学习笔记002---app高速开发AppCan.cn平台特色
技术qq交流群:JavaDream:251572072 1.多窗体机制 常见应用仅仅支持单一窗体 2.原生UI与交互支持 大量原生UI与交互支持(如Action Sheet等) 3.第三方开放 ...
随机推荐
- DataGirdView 设置单元格居中
设置标题行居中: dgvShow.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter ...
- android 自己定义通知栏遇到的问题
首先看报错信息: E/AndroidRuntime(12220): FATAL EXCEPTION: main E/AndroidRuntime(12220): Process: gn.com.and ...
- PHP SPL他们留下的宝石
Rafael Dohms 上面的篇文章 让我为之惊艳,忍不住就翻译了下来,同一时候补充了部分内容. SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的 ...
- loj1370(欧拉函数+线段树)
传送门:Bi-shoe and Phi-shoe 题意:给出多个n(1<=n<=1e6),求满足phi(x)>=n的最小的x之和. 分析:先预处理出1~1e6的欧拉函数,然后建立一颗 ...
- Androidclient和server端数据交互的第一种方法
网上有非常多样例来演示Android客户端和server端数据怎样实现交互只是这些样例大多比較繁杂,对于刚開始学习的人来说这是不利的.如今介绍几种代码简单.逻辑清晰的交互样例,本篇博客介绍第一种: 一 ...
- hdu4553(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 线段树功能:update:区间替换 query:询问满足条件的最左断点 分析:poj3667的加 ...
- OpenCV 通过 MFC 的 Picture Control 控件操作图像
假设希望对显示在MFC Picture Control 控件里的图像进行操作,比方画线画点之类的,能够利用 OpenCV 结合 MFC 本身的鼠标响应函数来实现. 怎样将图像显示到 Picture C ...
- Linking Containers Together
Linking Containers Together In the Using Docker section we touched on connecting to a service runnin ...
- 《软件project》课程报告 —国土资源执法监察管理信息系统建模
***********************************************声明*************************************************** ...
- NVL NVL2 NVLIF
========Oracle=======NVL (expr1, expr2)->expr1为NULL,返回expr2:不为NULL,返回expr1.注意两者的类型要一致