1. 程式人生 > >[c++菜鳥]《Accelerate C++》習題解答

[c++菜鳥]《Accelerate C++》習題解答

包含 log color col 計算 hello 註釋 clu 不同的

第0章

0-0 編譯並運行Hello, world! 程序。

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

0-1 下面的表達式是做什麽的?

 3+4

計算3+4,結果為7

0-2 編寫一個程序,使它在運行時輸出:

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

0-3 字符串直接量"\t"代表一個水平制表符;不同的C++實現以不同的形式顯示制表符。在你的實現中實驗一下,看它是怎樣處理制表符的。

\t處理為4個空格

0-4 編寫一個程序,運行時以Hello, world!程序作為這個程序輸出。

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

0-5 下面的程序是一個有效的程序嗎?說出理由。

#include <iostream>
int main()  std::cout << "Hello, world!" << std::endl;
這是一個無效程序,因為函數的函數體必須用花括號括起來,就算函數的函數體只有一條語句,也必須用花括號括住它。

0-6 下面的程序是一個有效的程序嗎?說出理由。

#include <iostream>
int main()  {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}}
這是一個有效程序,一般來說,函數必須包含至少一條的return語句,而且函數的最後一定要有return語句,但main比較特殊,它可以沒有返回語句,若果這樣,編譯器就會假設它返回0。

0-7 那下面的這個程序呢?

#include <iostream>
int main()  
{
    /*這是一個註釋,因為我們使用了/*和*/來作為它的定界符,
    所以它占據了幾行的範圍*/
    std::cout << "Does this work?" << std::endl;
    return 0;
}
無效程序,註釋有誤,註釋在前一個結束符*/就結束了,所以後面的內容都未能註釋。

0-8 ······這個呢?

#include <iostream>
int main()  
{
    //這是一個註釋,它占據了幾行的範圍
    //在這裏,我們使用了//而不是/*
    //和*/來為註釋定界
    std::cout << "Does this work?" << std::endl;
    return 0;
}
有效程序,單行註釋使用後,後面的多行註釋符號不在起作用。

參考https://blog.csdn.net/u013706695/article/details/19493443

[c++菜鳥]《Accelerate C++》習題解答