《C++編程思想》(第二版)第2章 對象的建立和使用(習題及答案),java編程思想第二版

來源:互聯網
上載者:User

《C++編程思想》(第二版)第2章 對象的建立和使用(習題及答案),java編程思想第二版

與習題相關的代碼

Hello.cpp

<span style="font-size:18px;">#include <iostream>using namespace std; int main(){ cout << "Hello, World! I am "     << 8 << " Today!" << endl;}</span>

Stream2.cpp
<span style="font-size:18px;">#include <iostream>using namespace std; int main(){cout<<"a number in decimal:"<<dec<<15<<endl;cout<<"in octal:"<<oct<<15<<endl;cout<<"in hex:"<<hex<<15<<endl;cout<<"a floatint-point number:"<<3.14159<<endl;cout<<"non-printing char (escape):"<<char(27)<<endl;return 0;}</span>


Nomconv.cpp
<span style="font-size:18px;">#include <iostream>using namespace std; int main(){int number;cout<<"Enter a decimal number:";cin>>number;cout<<"value in octal = 0"<<oct<<number<<endl;cout<<"value in hex = 0x"<<hex<<number<<endl;return 0;}</span>


Fillvector.cpp
<span style="font-size:18px;">#include <iostream>#include <fstream>#include <vector>using namespace std; int main(){vector<string> v;ifstream in("Fillvector.cpp");string line;while(getline(in,line)){v.push_back(line);}for(int i = 0;i < v.size();++i){cout<<i<<":"<<v[i]<<endl;}return 0;}</span>

2-1修改Hello.cpp,使他列印你的名字和年齡(或者你的鞋碼、愛犬的年齡等,只要你喜歡)。編譯並運行修改後的程式。
<span style="font-size:18px;">#include <iostream>using namespace std;int main() {  cout << "Hello, World! I am ZY." << endl;  cout << "I am 19 years:" << endl;  return 0;}</span>

2-2以Stream.cpp、Numconv.cpp為例,編一個程式,讓它根據輸入的半徑值求出圓面積並列印。可以用運算子“*”求直徑的平方。注意,不要用八進位或十六進位格式列印(它們只適用整數類型)。
#include <iostream>using namespace std;int main() {  const float pi = 3.141592654;  cout << "Enter the radius: ";    float radius;  cin >> radius;  cout << "The area is " << pi * radius * radius << endl;  return 0;}


2-3編一個程式用來開啟檔案並統計檔案中以空格隔開的單詞數目。
#include <string>#include <iostream>#include <fstream>#include <string>using namespace std;int main() {  ifstream f("test.cpp");  int nwords = 0;  string word;  while (f >> word)  {    ++nwords;  }  cout << "Number of words = " << nwords << endl;  return 0;}


2-4編一個程式統計檔案中特定單詞出現的次數(要求使用string類的運算子“==”來尋找單詞)。
#include <iostream>#include <fstream>#include <string>using namespace std;int main(int argc, char* argv[]) {  // Process command-line arguments:  if (argc < 3)   {    cerr << "usage: WordCount word file\n";    return -1;  }  string word(argv[1]);  ifstream file(argv[2]);  // Count occurrences:  long wcount = 0;  string token;  while (file >> token)    if (word == token)      ++wcount;  // Print result:  cout << '"' << word << "\" appeared "       << wcount << " times\n";} 


2-5修改Fillvector.cpp是他從後向前列印各行。
方法一:
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;int main() {  vector<string> v;  ifstream in("Fillvector.cpp");  string line;  while(getline(in, line))  {    v.push_back(line);  }  // Print backwards:  for(int i = v.size()-1; i > 0; --i)   {    cout << v.size()-1 << ": " << v[i] << endl;  }  return 0;} 

方法二:
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;int main() {  vector<string> v;  ifstream in("Fillvector.cpp"<span style="font-family: Arial, Helvetica, sans-serif;">);</span>  string line;  while(getline(in, line))    v.push_back(line);  // Print backwards:  int nlines = v.size();  for(int i = 0; i < nlines; i++)   {    int lineno = nlines-i-1;    cout << lineno << ": " << v[lineno] << endl;  }  return 0;} 


2-6修改Fillvector.cpp使他把vector中的所有元素串連成單獨的一個字串,並列印,但不要加上行號。
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;int main() {  vector<string> v;  ifstream in("Fillvector.cpp");  string line;  while(getline(in, line))  {    v.push_back(line);  }  // Put lines into a single string:  string lines;  for(int i = 0; i < v.size(); i++)  {    lines += v[i] + "\n";  }  cout << lines;  return 0;} 


2-7編一個程式,一次顯示檔案的一行,然後,等待使用者按斷行符號鍵後顯示下一行。
#include <string>#include <iostream>#include <fstream>using namespace std;int main() {  ifstream in("test.cpp");  string line;  while(getline(in, line))   {    cout << line;  // No endl!    cin.get();  }  return 0;}



2-8建立一個vector<float>,並用一個for迴圈語句向它輸入25個浮點數,顯示vector的結果。
#include <iostream>#include <vector>using namespace std;int main() {  // Fill vector:  vector<float> v;  for (int i = 0; i < 25; ++i)  {    v.push_back(i + 0.5);  }  // Display  for (int i = 0; i < v.size(); ++i)   {  cout << v[i]<<" ";  }  cout << endl;  return 0;}


2-9建立三個vector<float>對象,與第八題一樣填寫前兩個對象。編一個for迴圈,把前兩個vector的每一個對應的元素相加起來,結果放入第三個vector的相應元素中。顯示這三個vector的結果。
方法一:
#include <iostream>#include <vector>using namespace std;int main(){  vector<float> v1, v2;  for (int i = 0; i < 25; ++i)   {    v1.push_back(i);    v2.push_back(i + 0.2);  }  // Form sum:  vector<float> v3;  for (int i = 0; i < v1.size(); ++i)  {    v3.push_back(v1[i] + v2[i]);  }  // Display:  for (int i = 0; i < v1.size(); ++i)   {    cout << v1[i] << " + " << v2[i]         << " = " << v3[i] << endl;  }  return 0;}

方法二:
#include <iostream>#include <vector>using namespace std;int main(){  vector<float> v1, v2;  for (int i = 0; i < 25; ++i)   {    v1.push_back(i);    v2.push_back(i + 0.2);  }  // Form sum:  vector<float> v3;  v3.resize(v1.size());  // pre-allocate space  for (int i = 0; i < v1.size(); ++i)  {    v3[i] = v1[i] + v2[i];  }  // Display:  for (int i = 0; i < v1.size(); ++i)   {    cout << v1[i] << " + " << v2[i]         << " = " << v3[i] << endl;  }  return 0;}


2-10編一個程式,建立一個vector<float>,像前面的練習那樣輸入25個數。求每個數的平方,並把它們放入vector的同樣位置。顯示運算前後的vector。
#include <iostream>#include <vector>using namespace std;int main() {  // Fill and print:  vector<float> v;  for (int i = 0; i < 25; ++i)  {    v.push_back(i);  }  for (int i = 0; i < v.size(); ++i)   {    cout << v[i]<<" ";  }  cout<<endl;  // Square and print:  for (int i = 0; i < v.size(); ++i)  {    v[i] = v[i] * v[i];  }  for (int i = 0; i < v.size(); ++i)   {    cout << v[i]<<" ";  }  cout<<endl;  return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.