实数的输出格式(C++程序设计第7周)

来源:互联网 发布:原始网卡mac地址 编辑:程序博客网 时间:2024/05/16 09:09

问题描述

利用流操纵算子实现: 输入一个实数,先以非科学计数法输出,小数点后面保留5位有效数字;再以科学计数法输出,小数点后面保留7位有效数字。

注意:在不同系统、编译器上的输出格式略有不同,但保证在程序中采用默认格式设置一定能在OJ平台上得到正确结果。

输入

以非科学计数法表示的一个正实数,保证可以用double类型存储。

输出

第一行:以非科学计数法输出该实数,小数点后面保留5位有效数字;

第二行:以科学计数法输出该实数,小数点后面保留7位有效数字。

样例输入

12.34

样例输出

12.340001.2340000e+01

提示

参考网址:http://arachnoid.com/cpptutor/student3.html

源码

#include <iostream>using namespace std;int main(){    double num;    cin >> num;    cout.setf(ios::fixed);//makes cout print floats with a fixed number of decimals    cout.precision(5);//sets this number to be five    cout << num << endl;    cout.unsetf(ios::fixed);//remove the setf(ios::fixed)     cout.setf(ios::scientific);    cout.precision(7);    cout << num << endl;    return 0;}
0 0
原创粉丝点击