C++ primer plus(第六版)学习笔记、习题答案(4.2)
来源:互联网 发布:知乎 一二级市场套利 编辑:程序博客网 时间:2024/06/10 06:36
还以为博客没有了,就不添到上一篇了
5_8(1)
// 2014/12/11#include <iostream>#include <cstring>using namespace std;int main(){char test[20] = {0};int count = 0;char ch;int i = 0;cout << "Enter words(to stop ,type the word done):";do{i = 0;ch = cin.get();while (ch != ' ' && ch != '\n'){test[i++] = ch;ch = cin.get();}test[i] = '\0';count ++;}while (strcmp(test,"done") != 0);cout << "you entered a total of " << count -1 << " words.";cin.get();cin.get();cin.get();return 0; }
note:程序有个小bug,只输入一个空格,或者回车也会当做一个字符来看待。
后来改了一下
5.8(2)
// 2014/12/10#include <iostream>#include <cstring>using namespace std;int main(){char test[20] = {0};int count = 0;char ch;int i = 0;cout << "Enter words(to stop ,type the word done):";do{i = 0;ch = cin.get();while (ch != ' ' && ch != '\n'){test[i++] = ch;ch = cin.get();}//avoid enter the blank or enter the first timetest[i] = '\0';if (strcmp(test,"\0") != 0){count ++;}}while (strcmp(test,"done") != 0);cout << "you entered a total of " << count -1 << " words.";//cin.get();cin.get();cin.get();return 0; }
5.9
// 2014/12/11#include<iostream>#include<string>using namespace std;int main(){const string str = "done";string test;int i = 0;cout << "Enter words (to stop, type the word done)";do {cin >> test;if (test != str){i++;}elsebreak;} while (true);cout << "you entered a total of " << i << "words";cin.get();cin.get();return 0;}
5.10
// 2014/12/11#include<iostream>#include<string>using namespace std;int main(){int n;cout << "Enter number of rows:";cin >> n;int i,j,k;for (i = 1; i <= n; i++){for (j = 1; j <= n - i; j++){cout << ".";}for (k = 1; k <= i; k++){cout << "*";}cout << endl;}cin.get();cin.get();cin.get();return 0;}
希望大家指正。
0 0
- C++ primer plus(第六版)学习笔记、习题答案(4.2)
- C++ primer plus(第六版)学习笔记、习题答案(1)
- C++ primer plus(第六版)学习笔记、习题答案(2)
- C++ primer plus(第六版)学习笔记、习题答案(3)
- C++ primer plus(第六版)学习笔记、习题答案(4)
- C++ primer plus(第六版)学习笔记、习题答案(5)
- C Primer Plus(第6版)第六章答案
- c++ primer(第五版)学习笔记及习题答案代码版(第六章)函数
- c++primer plus 第六版 练习答案
- c++Primer Plus第六版练习题答案
- C primer plus课后习题答案(部分)
- C++primer plus第六版课后编程练习答案4.2
- C Primer Plus(第六版)第六章编程练习答案。
- c++ primer plus第六章习题答案
- 《C++ Primer Plus(第六版)》(1)(第二章 开始学习C++ 笔记和答案)
- 整理下C++primer plus 第六版第六章习题。
- C ++ Primer Plus 第六版 第六章编程练习答案
- C++ PRIMER PLUS 第六版编程答案(一)
- Red and Black
- C++中的向上类型转换和向下类型转换
- C++中重定义、重写、重载的区别以及隐藏与覆盖的访问
- C++ 虚函数表解析
- C++中虚函数工作原理和(虚)继承类的内存占用大小计算
- C++ primer plus(第六版)学习笔记、习题答案(4.2)
- 数字图象处理之二维码图像提取算法(四)
- STM32入门篇之通用定时器彻底研究新手教程,含11个例程,全面剖析TIMER,另外有个2.0库的
- 在ubuntu系统上生成SSH密钥,并把对应的公开密钥添加到GitHub账户
- 2014年12月10日
- 美国安全公司报告
- 一条消息就崩溃:印度研究人员发现美国版微信WhatsApp漏洞
- 在IAR的集成开发环境中实现数据变量定位方法如下三种
- centos下安装OpenOffice