Accelerated C++:通过示例进行编程实践——练习解答(第1章)
来源:互联网 发布:驱动软件下载排行榜 编辑:程序博客网 时间:2024/06/05 09:58
1-0. Compile, execute, and test the programs in this chapter.
#include <iostream>#include <string>using namespace std;int main(){cout<<"input your name:";string name;cin>>name;const string greet="Hello, "+name+"!";const string row3="* "+greet+" *";const string row2Space(greet.size()+2,' ');const string row2="*"+row2Space+"*";const string row1(row2.size(),'*');cout<<row1<<endl<<row2<<endl<<row3<<endl<<row2<<endl<<row1<<endl;return 0;}lyj@qt:~/Desktop$ g++ 1-0.cpp -o 1-0
lyj@qt:~/Desktop$ ./1-0
input your name:tianya
******************
* *
* Hello, tianya! *
* *
******************
lyj@qt:~/Desktop$
1-1. Are the following definitions valid? Why or why not?
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
Ans: "+"两端不能都是字符串字面量或char类型值,只能进行字符串和字符串字面量的连接; const std::string message="hello"+", world"+"!"。
1-2. Are the following definitions valid? Why or why not?
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;
Ans:同上。
1-3. Is the following program valid? If so, what does it do? If not, why not?
#include <iostream>
#include <string>
int main()
{
{ const std::string s = "a string";
std::cout << s << std::endl; }
{ const std::string s = "another string";
std::cout << s << std::endl; }
return 0;
}
Ans:"{}"的作用,变量作用范围问题。
1-4. What about this one? What if we change }} to };} in the third line from the end?
#include <iostream>
#include <string>
int main()
{
{ const std::string s = "a string";
std::cout << s << std::endl;
{ const std::string s = "another string";
std::cout << s << std::endl; }}
return 0;
}
Ans:同上题,将}}修改为};}依然输出a string \n another string;添加的";"是一条空语句。
1-5. Is this program valid? If so, what does it do? If not, say why not, and rewrite it to be valid.
#include <iostream>
#include <string>
int main()
{
{ std::string s = "a string";
{ std::string x = s + ", really";
std::cout << s << std::endl; }
std::cout << x << std::endl;
}
return 0;
}
Ans:invalid,因为最后一条cout引用了未定义的变量x,x在最内层{}内有效,将最内层{}去除即可。
1-6. What does the following program do if, when it asks you for input, you type two names (for example, Samuel Beckett)? Predict the behavior before running the program, then try it.
#include <iostream>
#include <string>
int main()
{
std::cout << "What is your name? ";
std::string name;
std::cin >> name; ////name1
std::cout << "Hello, " << name<< std::endl << "And what is yours? ";
std::cin >> name;//////name2
std::cout << "Hello, " << name<< "; nice to meet you too!" << std::endl;
return 0;
}
Ans:输入name1,输出Hello, name1 And what is yours?然后要求输入name2,输出Hello, name2;nice to meet you too!
- Accelerated C++:通过示例进行编程实践——练习解答(第1章)
- Accelerated C++:通过示例进行编程实践——练习解答(第0章)
- Accelerated C++:通过示例进行编程实践——练习解答(第2章)
- Accelerated C++:通过示例进行编程实践——练习解答(第3章)
- Accelerated C++:通过示例进行编程实践——练习解答(第4章)
- Accelerated C++:通过示例进行编程实践——练习解答(第5章)
- Accelerated C++:通过示例进行编程实践——练习解答(第6章)
- Accelerated C++:通过示例进行编程实践——练习解答(第7章)
- Accelerated C++:通过示例进行编程实践——练习解答(第8章)
- Accelerated C++:通过示例进行编程实践——练习解答(第9章)
- Accelerated C++:通过示例进行编程实践——练习解答(第10章)
- Accelerated C++:通过示例进行编程实践——练习解答(第11章)
- Accelerated C++:通过示例进行编程实践——练习解答(第12章)
- Accelerated C++:通过示例进行编程实践——练习解答(第13章)
- Accelerated C++:通过示例进行编程实践——练习解答(第14章)
- Accelerated C++:通过示例进行编程实践——练习解答(第15章)
- Accelerated C++:通过示例进行编程实践——练习解答(第16章)
- Accelerated C++:通过示例进行编程实践——习题解答(第1章)
- 深入解析java乱码
- linux内核相关镜像uImage、zImage、bzImage、vmlinuz、vmlinux、Image.rom 、Image.ram 关系分析
- Codeforces 214E. Relay Race dp
- CRC校验的原理和 CRC的用途。
- Xcode5 上64位编译 出错No architectures to compile for
- Accelerated C++:通过示例进行编程实践——练习解答(第1章)
- 异步FIFO的Verilog代码 DCFIFO实现
- Json与Java对象互转之Gson学习
- Git使用基础
- HTML5 经典弹球过关游戏的简化版本
- linux/unix下setuid/seteuid/setreuid/setresuid
- Remove Duplicates from Sorted Array II
- 要这么写if(1==x)
- Oracle进程结构