《C++编程思想》(第二版)第2章 对象的创建和使用(习题及答案)
来源:互联网 发布:win10内置ubuntu子系统 编辑:程序博客网 时间:2024/05/16 19:32
与习题相关的代码
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;}
0 0
- 《C++编程思想》(第二版)第2章 对象的创建和使用(习题及答案)
- 《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(一)
- 《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(二)
- C++编程思想 第二章对象的创建和使用
- C++编程思想 第二章 对象的创建与使用
- 《C++编程思想》第二章 数 据 抽 象(原书代码+习题+答案)
- 《编程珠玑》读书笔记2------------第二章习题及个人答案
- JAVA编程思想第四版—第二章—习题与答案
- c++编程思想-第2章 对象的创建与使用-2
- 【Java】《Java编程思想》笔记(含练习题答案代码)-第二章 一切都是对象
- Python核心编程(第二版)一些习题答案心得
- python核心编程习题答案(第二章)
- 《C++编程思想》 第十二章 动态对象创建 (原书代码+习题+解答)
- c++ primer第五版(中文)习题答案 第十章第三节第二小节-lambda表达式
- c++ primer第五版(中文)习题答案 第二章第三节-复合类型
- c++ primer第五版(中文)习题答案 第二章第五节-处理类型
- C++ Primer Plus (第6版)课后习题答案 第二章
- C Primer Plus(第6版)第二章答案
- linux内核定时器
- hdu 1242 rescue
- IntelliJ IDEA 开发前设置与快捷键
- Python语法精要
- 获取图片的真实宽高
- 《C++编程思想》(第二版)第2章 对象的创建和使用(习题及答案)
- uva 10069 Distinct Subsequences 不同的序列
- Cocos2d js 3.2使用Scale9Sprite 九宫图
- 程序猿利器(一) 批量替换 为妹子写了个批量替换内容的代码,无奈友好性和可操作性太渣,能改的给改提示好看点,功能性就别动了
- 网页布局实例
- 【精】Xcode 常用快捷键
- poj 3246 Balanced Lineup(线段树)
- poj 3683 Priest John's Busiest Day 【2-sat 经典建图输出一组解】【有向图tarjan + 反向拓扑 + 染色】
- adb devices找不到设备