[C++ Primer Plus] 第5章、循环和关系表达式(一)程序清单——指针自加减优先级
程序5.4
#include<iostream>
using namespace std; const int Size = ; void main() {
long long factorials[Size];
factorials[] = factorials[] = 1LL;
for (int i = ; i < Size; i++)
{
factorials[i] = i*factorials[i - ];
}
for (int i = ; i < Size; i++)
{
cout << i << "!=" << factorials[i] << endl;
}
system("pause");
}
factorials阶乘函数
递增/递减运算符和指针
前缀递增递减和*优先级相同,从右到左;
后缀递增递减比前缀优先级高,从左到右。
比如
int arr[] = { ,,,, };
int *p = arr;
*++p:p先自+,然后*p,最终为3
++*p:先*,然后再++,最终为2
*p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1](因为后缀优先级高,所以等价于*(p++),自增p并返回自增之前指向的值)
程序5.8
#include<iostream>
using namespace std; void main() {
double sum = 0.0, number;
cout << "The Amazing Accounto will sum and average five numbers for you.." << endl;
cout << "Please enter five values: " << endl;
for (int i = ; i < ; i++)
{
cout << "Value " << i << ":";
cin >> number;
sum += number;
}
cout << "Sum=" << sum << endl;
cout << "Average=" << sum/ << endl; system("pause");
}

程序5.9(反转字符串)
#include<iostream>
#include<string>
using namespace std; void main() {
cout << "Enter a word:";
string word;
cin >> word;
char tmp;//tmp也可以在循环体内部定义,这样的话将在每次循环中都分配和释放,效率稍微慢一点
for (int j = ,i=word.size()-; j < i; ++j,--i)//word.size()不包括‘\0’,前缀递增递减在某些情况下比后缀递增递减效率更高
{
tmp = word[i];
word[i] = word[j];
word[j] = tmp;
}
cout << word << endl; system("pause");
}

程序5.14(延时等待)
#include<iostream>
#include<ctime>
using namespace std; void main() {
cout << "Enter the delay time,in seconds:";
float sec;
cin >> sec;
clock_t delay = sec*CLOCKS_PER_SEC;//CLOCKS_PER_SEC每秒钟包含的系统时间单位数。 秒数*CLOCKS_PER_SEC=系统时间
cout <<"starting\a"<< endl;
clock_t start = clock();
while (clock() - start < delay)//当不满足条件时跳出循环
;
cout << "done\a\n";
system("pause");
}

当starting显示的时候会响一声,done出现的时候再响一声,中间的时间间隔取决于输入的数字。
类型别名

程序5.16(cin)
#include<iostream>
using namespace std; void main() {
cout << "Enter characters(enter # to quit):" << endl;
char ch;
int count = ;
cin >> ch;
while (ch != '#')
{
cout << ch;
++count;
cin >> ch;
}
cout << endl << count << " characters read\n";
system("pause");
}

可以注意到:1.没有空格和换行符;2.输入#之后还能继续输入
这是因为cin在读取char值时,将忽略空格和换行符;另外,发送给cin的输入被缓冲,这意味着只有在按下回车后,输入的内容才会发送给程序。
程序5.17+5.18+5.19(cin.get和EOF)



程序5.20
#include<iostream>
using namespace std; const int City = ;
const int Year = ; void main() {
const char *city[City] = {
"Gribble City ",
"Gribbletown",
"New Gribble ",
"San Gribble ",
"Gribble Vista"
};
int maxtemps[Year][City] =
{
{,,,,},
{,,,, },
{,,,,},
{,,,,}
};
cout << "Maxinum temperatures for 2008~2011" << endl;
for (int i = ; i < City; i++)
{
cout << city[i] << ":\t";
for (int year = ; year < Year; year++)
cout << maxtemps[year][i] << "\t";
cout << endl;
}
system("pause");
}

[C++ Primer Plus] 第5章、循环和关系表达式(一)程序清单——指针自加减优先级的更多相关文章
- C++ primer plus读书笔记——第5章 循环和关系表达式
第5章 循环和关系表达式 1. cout.setf(ios_base::boolalpha); cout << (100 > 3) << endl;将输出true,而不是 ...
- 《C++ Primer Plus》第5章 循环和关系表达式 学习笔记
C++提供了3种循环: for 循环. while 循环 和 do while 循环 .如果循环测试条件为 true 或非零,则循环将重复执行一组指令: 如果测试条件为 false 或 0 , 则结束 ...
- [C++ Primer Plus] 第7章、函数(一)程序清单——递归,指针和const,指针数组和数组指针,函数和二维数组
程序清单7.6 #include<iostream> using namespace std; ; int sum_arr(int arr[], int n);//函数声明 void ma ...
- 《C++ Primer Plus》读书笔记之三—循环与关系表达式
第五章 循环与关系表达式 1.表达式是值或者值与操作符的结合,每个C++表达式都有值.表达式到语句的转换只要加一个分号就可以完成.但是,反过来,从语句中删除分号,并不一定能将它转化成表达式. 2.前缀 ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- [C++ Primer Plus] 第5章、循环和关系表达式(二)课后习题
1.编写一个要求用户输入两个整数的程序,将程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和.这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44 ...
- c++primerplus(第六版)编程题——第5章(循环和关系表达式)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- 学习C++.Primer.Plus 5 循环和关系表达式
C++将赋值表达式的值定义为左侧成员的值 赋值操作符是自右向左结合的 cout.setf(ios:: boolalpha);//调用设置标记,命令cout输出true或false,而非1或0. 任何表 ...
- C++ Primer Plus读书笔记(五)循环和关系表达式
1.前缀运算符的优先级是从右到左,例如: 1 *++p; //这句话的含义是先对p进行++,再取* 2.循环 c++11新增了一种循环方式,有点和python类似,如下: 1 array<int ...
随机推荐
- Ubuntu 安装mono
Ubuntu 安装mono 我的系统:Ubuntu 16 Mono参考: http://www.mono-project.com/docs/getting-started/install/linu ...
- Cookiecutter: 更好的项目模板工具:(1)简介及可用资源汇总
原文档地址:https://cookiecutter.readthedocs.io/en/latest/ 本系列只介绍cookiecutter的基础使用,而且会删除与功能使用无关的部分.深度使用及了解 ...
- NB-Iot的应用领域、覆盖范围,是什么
该部分分享的是物联网各垂直应用领域里,NB-IoT技术的部署,看看适合NB-IoT技术的垂直应用场景有哪些?垂直应用服务商又该如何部署? 1 NB-IoT适合的垂直应用场景有哪些? 2 NB-IoT垂 ...
- Sping boot和mybatis整合
在没有配置数据库时,注释这样@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 接下来我们DataSourceA ...
- lxml xpath 爬取并正常显示中文内容
在使用python爬虫提取中文网页的内容,为了能正确显示中文的内容,在转为字符串时一定要声明编码为utf-8,否则无法正常显示中文,而是显示原编码的字符,并没有正确转换.比如下面这个简单的爬取百度页面 ...
- js 第二课
=赋值 ==比较 ===绝对比较 &&且 || 或 !取反 a?1:0 a=ture a?1:0 function LeyBc() { var a={d:11,b:22,c:&quo ...
- PAT1018 Public Bike Management【dfs】【最短路】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024 题意: 给定一个图,一个目的地和每个节 ...
- plsvo
1 图像对齐 std::for_each(ref_frame->seg_fts_.begin(), ref_frame->seg_fts_.end(), [&](plsvo::Fe ...
- Consul的应用
Consul在集群上的每一个节点(包括Server和Client)都运行一个Agent,通过这个Agent可以进行对Consul所提供的功能的操作,通过调用一系列HTTP API与Agent的交互即可 ...
- The way to unwind the stack on Linux EABI
I. probe the stack frame structure The original idea is to unwind the function call stack according ...