1 #include <iostream>
2 #include <iomanip>
3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
4
5 using namespace std;
6 int main(int argc, char** argv) {
7 //float型变量的声明、输入、计算和输出
8 float fx,fy;
9 cout<<"fx=";
10 cin>>fx;
11 cout<<"fy=";
12 cin>>fy;
13 cout<<fx<<"+"<<fy<<"="<<fx+fy<<endl;
14 cout<<fx<<"-"<<fy<<"="<<fx-fy<<endl;
15 cout<<fx<<"*"<<fy<<"="<<fx*fy<<endl;
16 cout<<fx<<"/"<<fy<<"="<<fx/fy<<endl<<endl;
17 //cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error!
18
19 //double型变量的声明、输入、计算和输出
20 float dx,dy;
21 cout<<"dx=";
22 cin>>dx;
23 cout<<"dy=";
24 cin>>dy;
25 cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl;
26 cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl;
27 cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl;
28 cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl;
29 //cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error!
30
31 //测试float和double类型数据的有效位
32 fx=10.0;fy=6.0;
33 float fz=fx/fy;
34 dx=10.0;dy=6.0;
35 double dz=dx/dy;
36 cout<<"fz=";
37 cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl;
38 cout<<"dz=";
39 cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl;;
40
41 //float型溢出
42 float x=3.5e14;
43 cout<<"x="<<x<<endl;
44 cout<<"x*x="<<x*x<<endl;
45 cout<<"x*x*x="<<x*x*x<<endl;
46 return 0;
47 }