首页 > 编程语言 > 详细

[C++ Primer Plus] 第5章、循环和关系表达式——(一)程序清单

时间:2020-02-07 18:40:01      阅读:67      评论:0      收藏:0      [点我收藏+]
 

程序5.14(延时等待)

 1 #include<iostream>
 2 #include<ctime>
 3 using namespace std;
 4 
 5 void main() {
 6     cout << "Enter the delay time,in seconds:";
 7     float sec;
 8     cin >> sec;
 9     clock_t delay = sec*CLOCKS_PER_SEC;        //将延迟时间秒-->CPU时钟计时单元;CLOCKS_PER_SEC每秒钟包含的系统时间单位数。
10                                                //秒数*CLOCKS_PER_SEC=以系统时间单位为单位的时间
11     cout << "starting\a" << endl;
12     clock_t start = clock();               //取出一开始的起始cpu时钟计时单元,就是计算延时的起始时间,从这个时候开始计时
13     while (clock() - start < delay)        //当不满足条件时跳出循环
14         ;
15     cout << "done\a\n";
16     system("pause");
17 }

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

技术分享图片

 


类型别名

技术分享图片

 

程序5.16(cin)

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main() 
 5 {
 6     cout << "Enter characters;enter # to quit:" << endl;
 7     char ch;
 8     int count = 0;
 9     cin >> ch;
10     while (ch != #)
11     {
12         cout << ch;
13         ++count;
14         cin >> ch;
15     }
16     cout << endl << count << " characters read\n";
17     while (1);
18     return 0;
19 }

技术分享图片

可以注意到:1.没有空格和换行符;2.输入#之后还能继续输入

这是因为cin在读取char值时,将忽略空格和换行符;另外,发送给cin的输入被缓冲,这意味着只有在按下回车后,输入的内容才会发送给程序。

程序5.17(cin.get())

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main() 
 5 {
 6     char ch;
 7     int count = 0;
 8     cout << "Enter characters;enter # to quit:" << endl;
 9     cin.get(ch);
10     while (ch != #)
11     {
12         cout << ch;
13         ++count;
14         cin.get(ch);
15     }
16     cout << endl << count << " characters read\n";
17     while (1);
18     return 0;
19 }

技术分享图片

程序5.18(EOF)

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main() 
 5 {
 6     char ch;
 7     int count = 0;
 8     cin.get(ch);
 9     while (cin.fail()==false)     //没检测到EOF时继续
10     {
11         cout << ch;
12         ++count;
13         cin.get(ch);
14     }
15     cout << endl << count << " characters read\n";
16     while (1);
17     return 0;
18 }

检测到EOF后,cin将两位(eofbit和failbit)都设置为1,可通过成员函数eof()和fail()来查看,eofbit如果被设置,即检测到EOF,则cin.eof()将返回bool值true,否则返回false。同样,如果eofbit或failbit被设置为1,则fail()返回true,否则返回false。

注:eof()和fail()报告最近读取的结果,在事后报告,而不是预先报告。因此应将cin.eof()或cin.fail()测试放在读取后。另外,fail()相比eof()可用于更多的实现中。

技术分享图片

 最后按Ctrl+Z,Enter,如果在字符串结尾按,无效,会当字符串来处理。

程序5.19(EOF)

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main() 
 5 {
 6     char ch;
 7     int count = 0;
 8 
 9     while ((ch=cin.get())!=EOF)     //调用cin.get()并将返回值赋给ch,如果这个值为EOF,则循环结束
10     {
11         cout.put(char(ch));
12         ++count;
13     }
14     cout << endl << count << " characters read\n";
15     while (1);
16     return 0;
17 }

技术分享图片

程序5.20(二维数组)

 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int City = 5;
 5 const int Year = 4;
 6 
 7 int main() {
 8     const char *city[City] =           //创建char型指针数组
 9     {
10         "Gribble City ",
11         "Gribbletown",
12         "New Gribble ",
13         "San Gribble ",
14         "Gribble Vista"
15     };
16     int maxtemps[Year][City] =
17     {
18         { 96, 100, 87, 101, 105 },
19         { 96, 98, 91, 107, 104 },
20         { 97, 101, 93, 108, 107 },
21         { 98, 103, 95, 109, 108 }
22     };
23     cout << "Maxinum temperatures for 2008~2011" << endl;
24     for (int i = 0; i < City; i++)
25     {
26         cout << city[i] << ":\t";                           //\t为制表符
27         for (int year = 0; year < Year; year++)
28             cout << maxtemps[year][i] << "\t";
29         cout << endl;
30     }
31     while (1);
32     return 0;
33 }

\t :横向制表(HT) (跳到下一个TAB位置)。

\t是补全当前字符串长度到8的整数倍,最少1个最多8个空格,补多少要看你\t前字符串长度。

比如当前字符串长度10,那么\t后长度是16,也就是补6个空格。

如果当前字符串长度12,此时\t后长度是16,补4个空格。

技术分享图片

 

[C++ Primer Plus] 第5章、循环和关系表达式——(一)程序清单

原文:https://www.cnblogs.com/Fionaaa/p/12273167.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!