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.第三方开放 ...
随机推荐
- CacheHelper工具类的使用
package com.bbcmart.util; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import ne ...
- selenium2支持无界面操作(HtmlUnit和PhantomJs)
selenium2支持无界面操作(HtmlUnit和PhantomJs) selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaD ...
- ipv6加英文的中括号访问
加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...
- Android 自己定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...
- H3C S5120交换机ACL应用的问题
下午在S5120上ACL的时候发现不起作用,ACL如下: acl number 3001 name deny-wan-to-lan-vpn rule 0 deny ip source 10.3.0.0 ...
- 新版SDK自己主动加入PlaceholderFragment的思考
自从Android SDK更新到22.6.3,发现新建Activity的时候,会自己主动生成一个Fragment.这个Fragment是activity的静态内部类.同一时候生成了一个xml叫frag ...
- Swift 的类、结构体、枚举等的构造过程Initialization(下)
类的继承和构造过程 类里面的全部存储型属性--包含全部继承自父类的属性--都必须在构造过程中设置初始值. Swift 提供了两种类型的类构造器来确保全部类实例中存储型属性都能获得初始值,它们各自是指定 ...
- SWT的TreeVierer的使用
先看一下效果图: 代码结构是这样的: 好的,现在看一下代码: package model; import java.util.List; public interface ITree { public ...
- excel删除问号~?~
1.直接替换(菜单)编辑——替换——查找内容——(输入)~?~——替换为(空,就是什么都不输入)——全部替换.2.设原数据在A列,从A1开始,若得到的数值数据需要参与计算,则在B1输入=--LEFT( ...
- Android在如何建立一个WebServer
今天老板交待任务最终完成了,感觉收获颇多,所以写一个关于它的记录,首先,看一下.老板的需求 需求: 希望移动端的用户标识(IMEI)和HTML页面的用户标识(Cookie)连接起来,当中HTML页面可 ...