C++primer plus第十七章编程练习

1.
#include<iostream>
using namespace std;
int main()
{
	cout<<"enter the string:";
	int count=0;
	char ch;
	while(cin.peek()!='$'&&cin.get(ch))//先将peek函数放在前面,在进行读取字符

		count++;
	cout<<count<<endl;
	return 0;
}

2.

#include<fstream>
#include<iostream>
using namespace std;
int main()
{
	char fname[50];
	char inf[100];
	ofstream fout;
	cout<<"enter the name of file:";
	cin>>fname;
	fout.open(fname);
	cout<<"enter the information:";
	cin.get();  //cin将enter留在了输入流中,因此需要将enter去除
	cin.get(inf,100);
	fout<<inf;
	fout.close();
	return 0;
}
4.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	ifstream fin1("zhu.txt");
	ifstream fin2("yue.txt");
	ofstream fout("zhuyue.txt");
	char ch1[100],ch2[100];

	while(fin1.peek()!='\n'&&fin1.getline(ch1,100,'\n'))
	{
		fout<<ch1;
	fout<<" ";
	if(fin2.peek()!='\n'&&fin2.getline(ch2,100,'\n'))
		fout<<ch2;
	fout<<endl;
	}
	while(fin2.peek()!='\n'&&fin2.getline(ch2,100,'\n'))
		fout<<ch2<<endl;

	return 0;
}

5.

#include<iostream>
#include<fstream>
#include<list>
#include<string>
using namespace std;
int main()
{
	list<string> mat;
	list<string> pat;
	char str[20];
	ifstream fin;
	ofstream fout("matpat.txt");
	fin.open("mat.txt");
	while(fin.getline(str,20,'\n'))
		mat.push_back(str);
	mat.sort();
	for(list<string>::iterator it=mat.begin();it!=mat.end();it++)
		cout<<(*it)<<endl;
	fin.close();
	fin.clear();//close只是将文件与流之间的关系切断,还需要clear,否则再次使用fin时其文件指针指向不对

	fin.open("pat.txt");
		
	while(fin.getline(str,20,'\n'))
		pat.push_back(str);
	pat.sort();
	for(it=pat.begin();it!=pat.end();it++)
		cout<<(*it)<<endl;
	fin.close();
	fin.clear();

	mat.merge(pat);
	mat.unique();
	for(it=mat.begin();it!=mat.end();it++)	
		fout<<*it<<endl;



	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值