【问题标题】:C++ Walkthrough cout.setf(ios::fixed); and cout.precision();C++ 演练 cout.setf(ios::fixed);和 cout.precision();
【发布时间】:2012-04-28 04:44:28
【问题描述】:
/* Problem 38 */
        #include <iostream>
        using namespace std;
        class abc {
        double n;
          public:
        abc() { n = 67.5; cout << "1\n"; }
        abc(double num) { set(num); cout << "2\n"; }
        double get() const { cout<<"3\n"; return n; }
        virtual void set(double num) {
            if (num < 10)
            n = 10;
            else if (num > 100)
            n = 100;
            else
            n = num;
            cout << "4\n";
        }
        };
        class def: public abc {
        double m;
          public:
        def() { m = 6.2; cout << "5\n"; }
        def(double num1, double num2): abc(num1) {
            set(num2 - abc::get()); cout << "6\n"; }
        double get() const {
            cout << "7\n"; return m + abc::get(); }
        void set(double num) {
            if (num < 10 || 100 < num)
            m = num;
            else
            m = 55;
            cout << "8\n";
        }
        };
        void do_it(abc &var, double num)
        {   cout << var.get() << '\n';
        var.set(num);
        cout << var.get() << '\n';
        }
        int main()
        {   abc x(45);
        def y(2, 340);
        cout.setf(ios::fixed);
        cout.precision(3);
        do_it(x, 200);
        do_it(y, 253);
        cout << x.get() << '\n';
        cout << y.get() << '\n';
        return 0;
        }

通过上面的代码,我只是想知道下面两行代码在上面的代码中会做什么

cout.setf(ios::fixed); cout.precision(3);

请不要只给我答案,因为我正在为明天的期末考试做一个演练,所以我将不胜感激。

我搜索了一些消息来源说这是设置标志,但我真的不明白它的概念是什么以及它是如何工作的

【问题讨论】:

  • std::ios_base::setfstd::ios_base::precision。两个页面都解释了函数的作用
  • 谢谢!我真的不想破坏我的程序演练过程,但 setf [Set flag] 指的是所有输出或稍后将要输出的任何内容?
  • 我不明白您所说的所有输出或稍后将输出的任何内容是什么意思。您通过调用setf() 将格式化标志应用到std::cout 流,因此它会影响cout printfs 在未来调用中的填充方式。
  • @Prætorian 对不起,这就是我的意思,我只是想知道它是否会应用任何将在稍后发送到cout 或整个程序的东西。谢谢!

标签: c++ walkthrough


【解决方案1】:
#include<iostream>

using namespace std;

int main(){
    float large = 2000000000;
    cout << large;
    return 0;
}

输出将以科学记数法表示:

2e+009

为了得到原来的值,你应该使用cout.setf(ios::fixed)

#include<iostream>

    using namespace std;

    int main(){
        cout.setf(ios::fixed);
        float large = 2000000000;
        cout << large;
        return 0;
    }

输出将保持默认精度,浮点数为 6。

2000000000.000000

您可以在上面的代码中设置精度为:

cout.precision(7);

.....

【讨论】:

    【解决方案2】:

    类似于包含操作库:
    包括:

    <iomanip>
    

    然后使用下面的函数

    cout << fixed << showpoint;
    cout << setprecision(3);
    

    【讨论】:

      【解决方案3】:

      关于如何格式化输出的优秀文档:Output formatting

      当您尝试创建命令行 UI 时,它总是很有用。

      【讨论】:

        【解决方案4】:
        cout.setf(ios::fixed)
        

        使 cout 打印具有固定小数位数的浮点数和

        cout.precision(3)
        

        将此数字设置为三。

        例如,如果你有一个

        double f = 2.5;
        

        然后

        cout << f;
        

        将打印

        2.500
        

        【讨论】:

        • 根据文档cout.unsetf(ios::fixed) 应该可以解决问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 2018-04-12
        • 1970-01-01
        • 2017-03-28
        相关资源
        最近更新 更多