Accelerated C++:通过示例进行编程实践——练习解答(第0章)

第0章开始

0-0. 编译并运行hello world程序。

//main.cpp
#include <iostream>
using std::cout;using std::endl;
int main()
{
	cout<<"Hello,world!"<<endl;
 	return 0;
}

lyj@qt:~/Desktop$ g++ main.cpp -o main
lyj@qt:~/Desktop$ ./main
Hello,world!
lyj@qt:~/Desktop$ 


0-1. 表达式:3 + 4的作用?

3+4仅仅是一个表达式,结果为7,没有副作用产生。

tips:有些表达式计算时会产生影响程序或系统环境状态的副作用,如0-0的输出语句,cout<<"Hello,world!"<<endl;该表达式的值是cout,但是也同时将“Hello,world!"字符串写到了标准输出流输出。


0-2.编写一个程序,使其在运行时输出:This (") is a quote, and this (\) is a backslash。

#include <iostream>
using std::cout;using std::endl;
int main()
{
	cout<<"This (\") is a quote,and this (\\) is a backslash"<<endl;
	return 0;
}

lyj@qt:~/Desktop$ g++ 0-2.cpp -o 0-2
lyj@qt:~/Desktop$ ./0-2
This (") is a quote,and this (\) is a backslash

0-3. The string literal "\t" represents a tab character; different C++ implementations display tabs in different ways. Experiment with your implementation to learn how it treats tabs.
制表符”\t“,只是在字符常量和字符串常量中是代表一定的距离(比如8个空格),用于增加程序可读性。

#include <iostream>
using namespace std;
int main()
{
cout<<"hello\tworld!"<<endl;
return 0;
}

0-4. Write a program that, when run, writes the Hello, world! program as its output.

 #include <iostream>
using std::cout;using std::endl;
int helloworld()
{
cout<<"Hello,World!"<<endl;
return 0;
}
int main()
{
return helloworld();
}

lyj@qt:~/Desktop$ g++ 0-4.cpp -o 0-4
lyj@qt:~/Desktop$ ./0-4
Hello,World!
lyj@qt:~/Desktop$ 
也可以将helloworld()放在一头文件中,而在该cpp文件中include进来。


0-5. Is this a valid program? Why or why not? 

#include <iostream>
int main()
	std::cout << "Hello, world!" << std::endl;

修改后如下:

#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}

0-6. Is this a valid program? Why or why not?

#include <iostream>
int main()
{{{{{{ 
	std::cout << "Hello, world!" << std::endl; 
}}}}}}

同上,无返回值。


0-7. What about this one?

#include <iostream>
int main() 
{
    /* This is a comment that extends over several lines
        because it uses /* and */ as its starting and ending delimiters */
    std::cout << "Does this work?" << std::endl;
    return 0; 
}

那个在/*,*/之间的and看到了吗,删除其左右注释符即可。

tips:单行注释符”//“;多行注释符”/* .......*/“,配对使用。


0-8. ...and this one?

#include <iostream>
int main()
{
    // This is a comment that extends over several lines
    // by using // at the beginning of each line instead of using /*
    // or */ to delimit comments.
    std::cout << "Does this work?" << std::endl;
    return 0;
}

注意到第6行,将or后面的注释符移植comments.尾部。

0-9. What is the shortest valid program?

如0-0题,没有输入和输出,也即没有了文件头<iostream>,从而最短程序为:

void main()
{
}

lyj@qt:~/Desktop$ g++ 0-9.cpp -o 0-9
lyj@qt:~/Desktop$ ./0-9
lyj@qt:~/Desktop$ 

0-10. Rewrite the Hello, world! program so that a newline occurs everywhere that whitespace is allowed in the program.

#include <iostream>
using std::cout;using std::endl;
int main()
{
cout<<"Hello,\n world!"<<endl;
return 0;
}

lyj@qt:~/Desktop$ g++ 0-9.cpp -o 0-9
lyj@qt:~/Desktop$ ./0-9
Hello,
 world!
lyj@qt:~/Desktop$ 

其实可以这么扩展,输入一串字符然后分割,在5.6节我们学了字符串分割函数split(),会扩展程序的通用性。

阅读更多
换一批

没有更多推荐了,返回首页