与习题相关的代码
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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
《C++编程思想》(第二版)第2章 对象的创建和使用(习题及答案)
原文地址:http://blog.csdn.net/qaz3171210/article/details/47031357