如何对齐控制台输出的数字在同一位置

How can I align the numbers that the console outputs at the same position

我正在尝试编写一个非常简单的 C++ 程序,它输出查找 table 以及正弦函数的相应 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

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

#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
//...

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

#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

我也不鼓励将 cerr 用于此类打印操作。 It is intended for printing errors. 请改用 cout(对于所有实际用途,它的工作方式相同)。

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