谭浩强C++ 第十三章

本文主要探讨了C++第十三章的课后习题,涵盖了cerr标准错误流的使用、格式化输出、图形绘制、磁盘文件I/O、文件处理以及对例13.17的修改等核心内容。

第十三章课后习题

1、cerr标准错误流使用
//P408
#include<iostream>
#include<cmath>
using namespace std;
int main(){
	double a,b,c;
	cout<<"please input three numbers:"<<endl;
	cin>>a>>b>>c;
	if(a>0&&b>0&&c>0){
		if((a+b)>c&&(a+c)>b&&(b+c)>a){
			double s=(a+b+c)/2;
			cout<<"the triangle's area:"<<sqrt(s*(s-a)*(s-b)*(s-c))<<endl;
		}
		else{
			cerr<<"It is not a triangle!"<<endl;
		}
	}
	else{
		cerr<<"It is not a triangle!"<<endl;
	}	
	return 0;
} 

2、按照格式输出
//P410 使用流成员函数控制输出格式
#include<iostream>
using namespace std;
int main(){
	double a;
	cout.setf(ios::fixed);
	cout.precision(3);
	cout.setf(ios::right);	
	while(cin>>a){
		cout.width(10);//只对其后的一个输出有效 
		cout<<a<<endl;
	}	
	return 0;
}
//与使用控制符相同
#include<iostream>
#include<iomanip>//注意包含头文件
using namespace std;
int main(){
	double a;
	cout<<setiosflags(ios::fixed)<<setprecision(3)<<setiosflags(ios::right); 	
	while(cin>>a){
		cout<<setw(10);//只对其后的一个输出有效 
		cout<<a<<endl;
	}	
	return 0;
} 

3、按要求输出图形
#include<iostream>
using namespace std;
int main(){
	for(int i=0;i<8;i++){
	cout.width(8-i);
		for(int j=0;j<i*2+1;j++){
			
			cout<<'B';
		}
		cout<<endl;
	}	
	return 0;
} 

4、磁盘文件输入输出
//(1)读入20个数字分别保存在两个文件中,每个文件存放30个数
//P421 使用输出文件流,指定文件作为输出文件 
#include<iostream> 
#include<fstream>
using namespace std;
int main(){
	int a[10];
	ofstream outfile("f1.dat",ios::out); //文件缺省路径,默认为当前程序所在目录
	if(!outfile){
		cerr<<"open error!"<<endl;
		exit(1);
	} 
	cout<<"enter 10 integer numbers:"<<endl;
	for(int i=0;i<10;i++){
		cin>>a[i];
		outfile<<a[i]<<" ";
	}
	outfile.close() ;
	
	ofstream outfile2("f2.dat",ios::out); 
	if(!outfile2){
		cerr<<"open error!"<<endl;
		exit(1);
	} 
	cout<<"enter 10 integer numbers:"<<endl;
	for(int i=0;i<10;i++){
		cin>>a[i];
		outfile2<<a[i]<<" ";
	}
	outfile2.close() ;
	return 0;
} 
// 1 2 3 4 5 6 7 8 9 0
// 0 9 8 7 6 1 2 3 4 5
//将f1.dat读入存放在f2.dat原有数据后
#include<iostream> 
#include<fstream>
using namespace std;
int main(){
	int a[10];
	ifstream infile("f1.dat",ios::in);
	ofstream outfile("f2.dat",ios::app); //P422 以输出方式打开且写入数据添加在文件尾 
	if(!infile){
		cerr<<"open error!"<<endl;
		exit(1);
	}	
	for(int i=0;i<10;i++){
		infile>>a[i];
	//	outfile.seekp(ios::end);
		outfile<<a[i]<<" ";
	}
	infile.close();
	outfile.close();

	return 0;
}

//f2.dat中二十个数据排序存放在f2.dat中
#include<iostream> 
#include<fstream>
using namespace std;
int main(){
	int a[20];
	ifstream infile("f2.dat",ios::in);
	if(!infile){
		cerr<<"open error!"<<endl;
		exit(1);
	}	
	for(int i=0;i<20;i++){
		infile>>a[i];
	}
	for(int i=0;i<19;i++){//冒泡排序
		for(int j=0;j<19-i;j++){
			if(a[j]>a[j+1]){
				int t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
	infile.close();
	
	
	ofstream outfile("f2.dat",ios::out);
	if(!outfile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<20;i++){
		outfile<<a[i]<<" ";
	}
	outfile.close();
	return 0;
}

5、文件处理
//按职工号大小顺序输入职工信息
#include<iostream> 
#include<fstream>
using namespace std;
int main(){
	int num[5];
	string name[5];
	int age[5];
	double wage[5];
	ofstream outfile("f1.dat",ios::out);
	if(!outfile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<5;i++){
		cin>>num[i]>>name[i]>>age[i]>>wage[i];
		outfile<<num[i]<<" "<<name[i]<<" "<<age[i]<<" "<<wage[i]<<endl;
	}
	outfile.close();

	return 0;
}
/*
1001 dsjhjh 32 8788
1002 cjhscb 21 3299
1003 hjsbjk 42 2389
1004 hjbkjs 34 8902
1005 jhkkkl 67 1893
*/
//增加2条员工信息
#include<iostream> 
#include<fstream>
using namespace std;
int main(){
	int num[2];
	string name[2];
	int age[2];
	double wage[2];
	ofstream outfile("f1.dat",ios::app);//注意增加在文件末尾
	if(!outfile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<2;i++){
		cin>>num[i]>>name[i]>>age[i]>>wage[i];
		outfile<<num[i]<<" "<<name[i]<<" "<<age[i]<<" "<<wage[i]<<endl;
	}
	outfile.close();
	return 0;
}
/*
1006 sjsdkl 22 3645
1007 hkakam 36 7899
*/
//输出所有职工信息
#include<iostream> 
#include<fstream>
using namespace std;
int main(){
	int num[7];
	string name[7];
	int age[7];
	double wage[7];
	ifstream infile("f1.dat",ios::in);
	if(!infile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<7;i++){
		infile>>num[i]>>name[i]>>age[i]>>wage[i];
		cout<<num[i]<<" "<<name[i]<<" "<<age[i]<<" "<<wage[i]<<endl;
	}
	infile.close();
	return 0;
}
//查询职工号是否存在,要求职工号从小到大输入,所以只需遍历一次即可
//可多次查询输入0结束
#include<iostream> 
#include<fstream>
using namespace std;
int main(){	
	int num[7];
	string name[7];
	int age[7];
	double wage[7];
	ifstream infile("f1.dat",ios::in);
	if(!infile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<7;i++){
		infile>>num[i]>>name[i]>>age[i]>>wage[i];
		cout<<num[i]<<" "<<name[i]<<" "<<age[i]<<" "<<wage[i]<<endl;
	}
	infile.close();
	
	cout<<endl;
	int n;
	int flag=0;
	while(cin>>n){
		flag=0;
		if(n==0) break;
		for(int i=0;i<7;i++){
			if(n==num[i]){
				flag=1;
				cout<<"No."<<i+1<<" ";
				cout<<num[i]<<" "<<name[i]<<" "<<age[i]<<" "<<wage[i]<<endl;
			    break;
			}
		}
		if(flag==0) cout<<"Can't find the num!"<<endl;
	}
	return 0;
}

6、修改例13.17
//没有例13.17? 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值