对象、继承和引用
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#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 << " mmn"; 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); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22Enter 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(),将所有的格式化设置恢复到原来的值
最后
以上就是虚幻小蝴蝶最近收集整理的关于C++对象、继承和引用的全部内容,更多相关C++对象、继承和引用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复