C++对象、继承和引用

对象、继承和引用

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void file_it(ostream &os,double fo,const double fe[],int n);
const int Limit=5;

int main()
{
	ofstream fout;
	const char *fn="ep-data.txt";
	fout.open(fn);
	if(!fout.is_open())
	{
		cout << "Can't open " << fn << ". Bye.\n";
		exit(EXIT_FAILURE);
	}
	double objective;
	cout << "Enter the focal lenth of your telescope objective in mm: ";
	cin >> objective;
	double eps[Limit];
	cout << "Enter the focal lenths, in mm, of " << Limit << " eyepieces.\n";
	for(int i=0; i<Limit; i++){
		cout << "Eyepiece #" << i+1 << ": ";
		cin >>eps[i];
	}
	file_it(fout,objective,eps,Limit);
	file_it(cout,objective,eps,Limit);
	cout << "Done.\n";
	return 0;
}

void file_it(ostream &os,double fo,const double fe[],int n)
{
	ios_base::fmtflags initial;
	initial=os.setf(ios_base::fixed);
	os.precision(0);
	os << "Focal lenth of objective: " << fo << " mm\n";
	os.setf(ios::showpoint);
	os.precision(1);
	os.width(12);
	os << "f.1 eyepiece";
	os.width(15);
	os << "magnification" << endl;
	for(int i=0; i<n; i++){
		os.width(12);
		os << fe[i];
		os.width(15);
		os << int(fo/fe[i]+0.5) << endl;
	}
	os.setf(initial);
}
Enter the focal lenth of your telescope objective in mm: 1800
Enter the focal lenths, in mm, of 5 eyepieces.
Eyepiece #1: 30
Eyepiece #2: 19
Eyepiece #3: 14
Eyepiece #4: 8.8
Eyepiece #5: 7.5
Focal lenth of objective: 1800 mm
f.1 eyepiece  magnification
        30.0             60
        19.0             95
        14.0            129
         8.8            205
         7.5            240
Done.

setf(ios_base::showpoint)将对象置于显示小数点模式,即使小数部分为零

precision()指定显示多少位小数(假定对象已经位于定点模式下)

width()设置下一次输出操作使用的字段宽度,这种设置只在显示下一个值时有效,然后恢复到默认设置

ios_base::fmtflags存储信息所需的数据类型名称,将返回值赋给initial将存储调用file_it()之前的格式化设置,然后使用变量initial作为参数来调用setf(),将所有的格式化设置恢复到原来的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值