繁体   English   中英

字符串格式(c ++)

[英]String formatting (c++)

我试图在我的控制台应用程序中格式化输出字符串(如表)

cout <<  "\n\n-----------------\n";
cout << setw(8) << left << "F\t|";
cout << setw(8) << left << "x\t|";
cout <<  "\n-----------------\n";
//...
cout.width(8);
cout.setf(ios::left);
cout << fixed << setprecision(3) << F << "\t|";

cout.width(8);
cout.setf(ios::left);
cout << x << "\t|";
cout <<  "\n-----------------\n\n";

但结果我的输出看起来像这样
在此输入图像描述
我的上部字符串格式有什么问题?

我使用了与你相同的代码,得到了相同的输出,直到我删除了行尾的\\ t。 查看新代码:

cout <<  "\n\n-----------------\n";
cout << setw(8) << left << "F\t|";
cout << setw(8) << left << "x\t|";
cout <<  "\n-----------------\n";
//...
cout.width(8);
cout.setf(ios::left);
cout << fixed << setprecision(3) << F << "|";

cout.width(8);
cout.setf(ios::left);
cout << x << "|";
cout <<  "\n-----------------\n\n";

如前所述,它是导致问题的选项卡。

我不会因为删除标签停下来。 就目前而言,您的代码具有高度重复性,几乎无法维护。 我会做(几乎)完全重写,有几个功能可以减少重复。 我的第一次切割可能看起来像这样:

// format a value in a field of specified width, followed by a separator
template <class T>
string field(T val, int w, char sep = '|') {
    stringstream b;
    b << setw(w) << left << fixed << setprecision(3) << val << sep;
    return b.str();
}

// generate a separator for a specified number of fields,
// each of a specified width
string sep(int c, int w, char val = '-') {
    string s(c * (w + 1), val);
    return string("\n") + s + "\n";
}

int main() {
    static const int w = 8;
    double F = 1.234, x = 3.45;
    string s = sep(2, w);

    cout << "\n" << s;
    cout << field("F", w) << field("x", w) << s;
    cout << field(F, w) << field(x, w) << s;
}

在我看来,这使得代码更具可读性,而且更易于维护。 例如,如果我们决定在下一行显示ab ,那么添加类似的内容似乎相当明显:

cout << field(a, w) << field(b, w) << s;

...而且我们不必非常努力地确定它会与前一行匹配。 同样,如果我们想要改变列宽等。

你可以尝试:

cout <<  "\n\n-----------------\n";
cout << setw(8) << left << "F\t\t|";   // insert more tab here
cout << setw(8) << left << "x\t|";
cout <<  "\n-----------------\n";
//...
cout.width(8);
cout.setf(ios::left);
cout << fixed << setprecision(3) << F << "\t|"; 

cout.width(8);
cout.setf(ios::left);
cout << x << "\t|";
cout <<  "\n-----------------\n\n";

控制台屏幕看起来像Windows一样可疑。 如果您使用的是Windows,则可以使用Win32 API更精确地格式化输出。 特别是,您可以使用SetConsoleCursorPosition

COORD position = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
std::cout<<"This will be printed starting at position x, y"<<std::endl;

尝试这个:

#include <iostream>
#include <iomanip>
#include <map>
#include <string>

using namespace std;


int main()
{
    map< float, float > table =
    {
        { 8232.0f, 89.0f },
        { 8232.1f, 89.0f },
        { 8232.2f, 89.0f },
        { 8232.3f, 89.0f },
        { 8232.4f, 89.0f },
        { 8232.5f, 89.0f },
        { 8232.6f, 89.0f },
        { 8232.7f, 89.0f },
        { 8232.8f, 89.0f },
    };

    const size_t CELL_WIDTH = 25;
    const string CELL_LINE( CELL_WIDTH, '=' );

    // print the header of table
    cout << '|' << CELL_LINE << '|' << CELL_LINE << '|' << endl
         << '|'
         << left << setw( CELL_WIDTH ) << "F" << '|'
         << setw( CELL_WIDTH ) << "R" << "|\n|"
         << CELL_LINE << '|' << CELL_LINE << '|' << endl;


    // print the body
    // change cout precision
    cout << fixed << setprecision( 3 );
    for ( auto it : table )
        cout << "| "  << setw( CELL_WIDTH - 1 ) << it.first
             << "| " << setw( CELL_WIDTH - 1 ) << it.second
             << "|\n";

    // print the footer
    cout << '|' << CELL_LINE << '|' << CELL_LINE << '|' << endl;


    return 0;
}

这是结果: 在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM