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.第三方开放 ...
随机推荐
- Codeforce 57C Array
C. Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Android开发之模板模式初探
模板模式我认为在Android的开发中是最长用到的,基本是随处可见的,认识该模式,有助于我们对Android的源代码及框架有一个更深层次的认识.那什么是模板模式呢,模板模式就是定义一个基本框架,将当中 ...
- hdu2063+hdu1083(最大匹配数)
传送门:hdu2063过山车 #include <cstdio> #include <cstring> #include <string> #include < ...
- ASP.NET MVC 4高级编程(第4版)
<ASP.NET MVC 4高级编程(第4版)> 基本信息 作者: (美)Jon Galloway Phil Haack Brad Wilson K. Scott All ...
- J2EE互联网产品打造
CSDN的各位技术朋友们,你们好: 我司最近正在研发一套J2EE的互联网产品,前期功能设计例如以下: 1.权限管理 2.菜单管理 3.系统设置 4.页面管理[主要做静态化] 5.任务管理[数据同步以及 ...
- 2014年辛星解读Javascript之用DOM动态操纵HTML元�
关于DOM,我们了解了能够用DOM操纵HTML的一些属性和样式,还能够为HTML元素绑定事件等等,那么接下来,我们将涉及到用DOM来动态的创建.删除HTML等一些操作,我的核心思路还是重实战,因此,代 ...
- Creating Contextual Menus创建上下文菜单
A contextual menu offers actions that affect a specific item or context frame in the UI. You can pro ...
- SE 2014年4月17日
描述BGP路由属性 MED.首选值 的特点 MED相当于IGP协议中的度量值,在其他条件相同时,当本自治系统有多条到达外部自治系统的链路时,MED值小的路由优选.MED属性只能在两个自治系统间传递. ...
- NLP 苏图南 打破自我设限 突破自我—在线播放—优酷网,视频高清在线观看
http://v.youku.com/v_show/id_XNTAyNDg3MTky.html?x
- 如何通过shell脚本操作MongoDB
通过shell脚本对MongoDB进行自动化操作 运行写好的 ./show.sh 脚本 发现能够建立mongo链接 #!/bin/sh mongo WordPress --eval "sho ...