【问题标题】:How can I align the numbers that the console outputs at the same position如何对齐控制台在同一位置输出的数字
【发布时间】:2018-10-12 08:23:30
【问题描述】:

我正在尝试编写一个非常简单的 C++ 程序,它输出一个查找表,其中包含相应的 xy 正弦函数值。我写的代码如下:

#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

    double hw = 4.0;
    int nsteps = 30;
    const double PI = 3.14159;
    const double maxx = hw * PI;
    const double deltax = maxx / nsteps;
    double x = 0.0;
    for (int i = 0; i < nsteps; i++) {
        const double f = sin(x);
        cerr << x << "\t" << f << endl;
        x = x + deltax;
    }
    return 0;
}

现在程序正在运行,但我的问题是,值没有正确对齐,如下图所示 那么有什么方法可以实现第二列值实际上是一列并且所有值都在同一位置对齐?我可以用什么来代替\t

【问题讨论】:

    标签: c++ console alignment text-alignment


    【解决方案1】:

    上述答案提供了一个不正确的解决方案,因为没有正确设置对齐方式。我会使用一个函数来处理格式:

    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    void printxy(double x, double y, int width){
        cout << setw(width) << x << "\t";
        if (y < 0) cout << "\b";
        cout << setw(width) << y << "\n";
    }
    
    int main(){
        double hw = 4.0;
        int nsteps = 30;
        const double PI = 3.14159;
        const double maxx = hw * PI;
        const double deltax = maxx / nsteps;
        double x = 0.0;
    
        int decimals = 6;
        int width = 8;    //Adjust as needed for large numbers/many decimals
        cout << std::setprecision(decimals);
        cout << std::setw(width);
        cout.setf(ios::left);
    
        for (int i = 0; i < nsteps; i++) {
            const double y = sin(x);
            printxy(x, y, width);
            x = x + deltax;
        }
    }
    

    现在输出格式正确:

    0               0
    0.418879        0.406736
    0.837757        0.743144
    1.25664         0.951056
    1.67551         0.994522
    2.09439         0.866026
    2.51327         0.587787
    2.93215         0.207914
    3.35103        -0.207909
    3.76991        -0.587783
    4.18879        -0.866024
    4.60767        -0.994521
    5.02654        -0.951058
    5.44542        -0.743148
    5.8643         -0.406741
    6.28318        -5.30718e-06
    6.70206         0.406731
    7.12094         0.743141
    7.53982         0.951055
    7.95869         0.994523
    8.37757         0.866029
    8.79645         0.587791
    9.21533         0.207919
    9.63421        -0.207904
    10.0531        -0.587778
    10.472         -0.866021
    10.8908        -0.994521
    11.3097        -0.951059
    11.7286        -0.743151
    12.1475        -0.406746
    

    我也不鼓励在此类打印操作中使用cerrIt is intended for printing errors. 改用cout(在所有实际用途中,它的工作方式相同)。

    我还应该提到endl 是一个定时炸弹:它刷新输出,这意味着流的内部缓冲区被写出(无论是控制台、文件还是其他)。当应用程序扩展并变得更加 IO 密集时,这可能会成为一个严重的性能问题:旨在提高 IO 性能的缓冲区可能由于频繁的endl 插入而未使用。解决方法是使用换行符'\n'

    【讨论】:

    • 漂亮。非常感谢。你介意解释一下为什么endl 是一颗定时炸弹吗?
    • @Max 对不起,我忘了。我已经编辑了答案来解释这一点。此外,称其为“滴答作响的炸弹”也有点夸张。
    【解决方案2】:

    使用std::setprecision() 设置小数点后的计数,std::setw() 设置输出长度的宽度。包括&lt;iomanip&gt;需要,例如:

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    
        double hw = 4.0;
        int nsteps = 30;
        const double PI = 3.14159;
        const double maxx = hw * PI;
        const double deltax = maxx / nsteps;
        double x = 0.0;
        cerr << std::setprecision(8);
        for (int i = 0; i < nsteps; i++) {
            const double f = sin(x);
            cerr << std::setw(20) << x << std::setw(20) << f << endl;
            x = x + deltax;
        }
        return 0;
    }
    

    输出是:

                       0                   0
              0.41887867          0.40673632
              0.83775733          0.74314435
                1.256636          0.95105619
               1.6755147          0.99452204
               2.0943933          0.86602629
                2.513272          0.58778697
               2.9321507          0.20791411
               3.3510293         -0.20790892
                3.769908         -0.58778268
               4.1887867         -0.86602363
    //...
    

    【讨论】:

    • 这并不能解决问题。接下来的 19 行(您的答案中没有包含)格式不正确。
    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2016-07-01
    • 2012-05-05
    • 2018-02-19
    • 2018-09-19
    相关资源
    最近更新 更多