字符串格式(c ++)

时间:2013-08-21 14:19:26

标签: 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";

但结果我的输出看起来像这样 enter image description here
我的上部字符串格式有什么问题?

5 个答案:

答案 0 :(得分:4)

我使用了与你相同的代码并得到了相同的输出,直到我删除了行尾的\ 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";

答案 1 :(得分:3)

如前所述,它是导致问题的标签。

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

// 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;

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

答案 2 :(得分:1)

您可以尝试:

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";

答案 3 :(得分:1)

控制台屏幕看起来很像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;

答案 4 :(得分:1)

试试这个:

#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;
}

这是结果: enter image description here