Thinking In C++ 第二章部分作业
- C++编程参考手册
http://www.cplusplus.com/reference/
注:以下代码仅用于学习交流,请勿用于商业用途
author:yjljobrequest@163.com
- 2-1 修改Hello.cpp , 使它能打印你的名字和年龄。编译并运行修改后的程序
- 2-2 编写一个程序,让它根据输入的半径值求出圆面积
- 2-3 编写一个程序用来打开文件并统计文件中以空格隔开的单词个数
- 2-4 编写一个程序统计文件中特定单词的出现次数
- 2-5 编写一个程序使它能从后向前打印文件中的各行
- 2-6 将vector中的元素链接成一个单独的字符串
- 2-7 编写一个程序,一次显示文件的一行,等待用户按回车键后显示下一行
2-1
修改Hello.cpp , 使它能打印你的名字和年龄。编译并运行修改后的程序
#include <iostream>
/*print to console : author name , age*/
int main() {
using namespace std;
cout << "I am JianleiYan, 27 years old" << "\n"
<< "And I love my family, I got a nice cat" << endl;
return 0;
}
2-1~ 小练习 控制台输出cout: 整数进制输出
#include <iostream>
using namespace std;
int main() {
cout << "a num in decimal: "
<< dec << 15 << endl;
cout << "in octoal: " << oct << 15 << endl;
cout << "in hex: " << hex << 15 << endl;
cout << "a floating-point number: "
<< 3.1415926 << endl;
cout << "none printing char (escape): "
<< char (27) << endl;
return 0;
}
2-2 编写一个程序,让它根据输入的半径值求出圆面积
#include <iostream>
/*input circle r: s= pi*r*r */
int main() {
using namespace std;
double r = 0;
cout << "Please input circle r lenth: " ;
cin >> r;
cout << "circle S = " << 3.1415926 * r * r << endl;
return 0;
}
2-3 编写一个程序用来打开文件并统计文件中以空格隔开的单词个数
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned int count_words_each_line(string const & line)
{
unsigned int counter (0);
unsigned int lenth = line.length();
if(!lenth)
{
return 0;
}
else
{
for(int i = 0; i < lenth;i++)
{
if(line.at(i) != ' ')
{
if(i + 1 < lenth)
{
if(line.at(i + 1) == ' ')
{
counter++;
}
continue;
}
else
{
counter++;
}
}
}
}
return counter;
}
int main(void) {
string filename;
string each_line;
unsigned int words(0);
cout << "Please enter the filename for words counting" << endl;
cin >> filename;
ifstream ifile(filename.c_str());
if(ifile.is_open())
{
while(getline(ifile, each_line))
{
words += count_words_each_line(each_line);
}
}
cout << "words from file " << filename <<" is "
<< words << endl;
return 0;
}
2-4 统计文件中 指定单词 出现的次数
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
unsigned int specific_word_in_line_counting(string const& line, string const& word)
{
unsigned int words (0);
unsigned int word_length = word.length();
unsigned int line_length = line.length();
unsigned int i (0);
unsigned int j (0);
if(word_length==0 || line_length ==0
||word_length > line_length)
{
return 0;
}
//遍历一行字符
for (;i < line_length; i++)
{
//找到首字符和目标单词首字符匹配项 i
if(line.at(i) == word.at(0))
{
//位置 i 字符 和目标单词首字符匹配
//一行位置i 字符后面存在 目标单词个字符,考察后面字符是否匹配
if (i + word_length - 1 < line_length)
{
j = 0;
for(;j < word_length;j++)
{
if (line.at(i + j) == word.at(j))
continue;
else
break;
}
if(j == word_length)
{
//字符匹配,位置 i + length
words += 1;
i += word_length;
}
else
continue;
}
else
{
//位置i 后面不存在足够字符, 结束算法
break;
}
}
else
{
//位置i 字符和 目标单词首字符不匹配,继续查找
continue;
}
}
return words;
}
int main()
{
ifstream ifile;
string each_line;
string file_name;
string specific_word;
unsigned int wordsnum (0);
cout << "Please enter the filename for specific word counting" << endl;
cin >> file_name;
cout << "Please enter the specific word " << endl;
cin >> specific_word;
ifile.open(file_name.c_str(),std::ifstream::in);
if(ifile.is_open())
{
while (getline(ifile,each_line))
{
wordsnum += specific_word_in_line_counting(each_line,specific_word);
}
}
else
{
cout << "Can not open file " << file_name << endl;
return 0;
}
cout << "File: " << file_name << " has " << wordsnum << " " << specific_word << endl;
return 0;
}
2-5 编写一个程序使它能从后向前打印文件中的各行
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void print_vector_string(vector<string> & vec, bool back)
{
vector<string>::iterator f_item ;
vector<string>::reverse_iterator b_item ;
unsigned int line_num (0);
if(back)
{
for(b_item = vec.rbegin();b_item != vec.rend();b_item++)
{
cout <<"line num : " << line_num++ << endl
<< *b_item << endl;
}
}
else
{
for(f_item = vec.begin();f_item != vec.end();f_item++)
{
cout <<"line num : " << line_num++ << endl
<< *f_item << endl;
}
}
}
int main()
{
string file_name;
string file_line;
ifstream ifile;
vector<string> string_v;
cout << "Please enter the filename For back print : ";
cin >> file_name;
ifile.open(file_name.c_str(),std::ifstream::in);
//buffer file lines
if(ifile.is_open())
{
while(getline(ifile, file_line))
{
string_v.push_back(file_line);
}
}
else
{
cout << "File : " << file_name << " could not find" << endl;
return -1;
}
//front print the file lines
print_vector_string(string_v, false);
//back print file lines
print_vector_string(string_v, true);
return 0;
}
2-6 将vector中的元素链接成一个单独的字符串
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
void link_vector_to_string(vector<string>& vec)
{
string str;
vector<string>::iterator item = vec.begin();
while(item != vec.end())
{
str += *item;
item++;
}
cout << str << endl;
}
void file_to_vector_string(ifstream &file, vector<string>& vec)
{
string tmp;
while(getline(file, tmp))
{
vec.push_back(tmp);
}
}
int main()
{
vector<string> vec;
ifstream ifile("readme.txt");
file_to_vector_string(ifile,vec);
link_vector_to_string(vec);
return 0;
}
2-7 编写一个程序,一次显示文件的一行,等待用户按回车键后显示下一行
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
void get_enter(void)
{
char tmp = ' ';
while(tmp != '\n')
{
cin.get(tmp);
}
}
int main()
{
string str;
ifstream ifile("readme.txt");
while(true)
{
get_enter();
if(getline(ifile,str))
{
cout << str << endl;
}
else
{
cout << "End of file , exited" << endl;
break;
}
}
return 0;
}