Thinking in C++ C2 作业

本文集展示了C++编程的多个实际应用场景,包括控制台输出个性化信息、计算圆面积、文件单词计数、特定单词搜索、逆序打印文件内容、字符串链接及逐行读取文件等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Thinking In C++ 第二章部分作业

注:以下代码仅用于学习交流,请勿用于商业用途
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值