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

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


1-0. Compile, execute, and test the programs in this chapter.


#include   
#include   
  
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<


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 and why not?

const std::string hello = “Hello”;

const std::string message = hello + “, world” + “!”;

Answer:  Yes.

1-2. Are the following definitions valid? Why and why not?

const std::string exclam = “!”;

const std::string message1 = “Hello” + “, world” + exclam;

Answer: Unfortunately, the second statement will cause an “invalid operands to binary expressions(‘const char*’ and ‘const char*’). Which happend at ‘”Hello” + “, world”‘.

There is no ‘+’ operator to add two C-style strings – char *, char[]. while the “Hello” and “, world” alike strings are actually C-style strings. But the string from standard library contains the ‘+’ operator which should do the right job. So simply change the code to the following and it will work:

const std::string exclam = “!”;

const std::string message1 = std::string(“Hello”) + std::string(“, world”) + exclam;

1-3. Is the following program valid? If so, what does it do? If not, why not?
#include
#include
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
#include
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
#include
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
#include
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!


参考链接:

http://blog.csdn.net/yj_cs/article/details/39674105

https://acceleratedcplusplus.wordpress.com/


你可能感兴趣的:(C++)