Accelerated C++ 课后练习精选答案

目录

第0章

0-2: Write a program that, when run, writes

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

0-7:What about this one?

0-9:What is the shortest valid program?

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

第1章

1-1:Are the following definitions valid? Why or why not?

1-2:Are the following definitions valid? Why or why not?

1-3:Is the following program valid? If so, what does it do? If not, why not?

1-4:What about this one? What if we change }}to };}in the third line from the end?

1-5:Is this program valid? If so, what does it do? If not, say why not, and rewrite it to be valid.

1-6:What does the following program do if, when it asks you for input, you type twonames (for example, Samuel Beckett)? Predict the behavior before running the program,then try it.

第2章


第0章

0-2: Write a program that, when run, writes

This (") is a quote, and this (\) is a backslash.
#include <iostream>

int main() {
    std::cout << "This (\") is a quote, and the (\\) is a backslash." << std::endl;
    return 0;
}

0-5: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?

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;
}

多行注释符不可以嵌套使用 /* 会和第一个*/匹配;嵌套使用会使两个/*之间的内容属于非注释内容

0-9:What is the shortest valid program?

int main(){}

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

#include <iostream>

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

第1章

 一种定义字符串的方式:

std::string stars(5,'*'); // 等同于 "*****"
//第一个参数为一个整数,第二个参数为一个字符(char)

字符串字面量(string literal):string star = "***"; 字面量即为:***, 字符串即为 star。 

1-1:Are the following definitions valid? Why or why not?

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

有效定义,hello是字符串,"+"为左结合,先hello 与 ", world" 结合后,再与 "!" 结合。

1-2:Are the following definitions valid? Why or why not?

const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;

 无效定义,因为 + 不能连接两个字符串字面量 "Hello" 和 ", world"。

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;
}

有效,输出:

a string 
another string

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;
}

有效。有一个作用域(内部域)嵌套在外部的作用域(外部域)里面。因为内部的作用域里定义了一个变量s,使得外部域的s被隐藏。增加分号; 不改变程序的含义。

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;
}

 无效,x的声明在内部作用域内,不能在外部域中使用 

{ std::string s = "a string";
    { std::string x = s + ", really";
    std::cout << s << std::endl; 
    std::cout << x << std::endl; }//将花括号挪到此处
    }

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;
    std::cout << "Hello, " << name << std::endl << "And what is yours? ";
    std::cin >> name;
    std::cout << "Hello, " << name << "; nice to meet you too!" << std::endl;
    return 0;
}
What is your name? Samuel
Hello, Samuel
And what is yours? Beckett
Hello, Beckett; nice to meet you too!

第2章

参考阅读:

https://github.com/bitsai/book-exercises/tree/master/Accelerated%20C%2B%2B

https://www.parkscomputing.com/accelerated-cpp-solutions/01-workingwithstrings/

猜你喜欢

转载自blog.csdn.net/qwe641259875/article/details/86767375