2009-09-19 170 views
7

我一直在試圖格式化輸出到控制檯最長的時間,沒有真正發生。我一直在嘗試儘可能多地使用iomanip和我的功能,以及ofstream&的功能。格式化C++控制檯輸出

void list::displayByName(ostream& out) const 
{ 
node *current_node = headByName; 

// I have these outside the loop so I dont write it everytime. 

out << "Name\t\t" << "\tLocation" << "\tRating " << "Acre" << endl; 
out << "----\t\t" << "\t--------" << "\t------ " << "----" << endl; 

while (current_node) 
{ 
    out << current_node->item.getName()// equivalent tabs dont work? 
     << current_node->item.getLocation() 
    << current_node->item.getAcres() 
    << current_node->item.getRating() 
    << endl; 

    current_node = current_node->nextByName; 
} 

// The equivalent tabs do not work because I am writing names, 
// each of different length to the console. That explains why they 
// are not all evenly spaced apart. 
} 

他們是什麼我可以用它來讓它們彼此正確對齊?我調用的函數是不言自明的,並且所有不同的長度,所以不能很好地對齊。

我已經試過iomanip中的一切。

回答

2

您可以編寫一個總是在標準輸出中輸出相同數量字符的程序。 像

string StringPadding(string original, size_t charCount) 
{ 
    original.resize(charCount, ' '); 
    return original; 
} 

,然後用這樣在你的程序

void list::displayByName(ostream& out) const 
{ 
    node *current_node = headByName; 

    out << StringPadding("Name", 30) 
     << StringPadding("Location", 10) 
     << StringPadding("Rating", 10) 
     << StringPadding("Acre", 10) << endl; 
    out << StringPadding("----", 30) 
     << StringPadding("--------", 10) 
     << StringPadding("------", 10) 
     << StringPadding("----", 10) << endl; 

    while (current_node) 
    { 
     out << StringPadding(current_node->item.getName(), 30) 
      << StringPadding(current_node->item.getLocation(), 10) 
      << StringPadding(current_node->item.getRating(), 10) 
      << StringPadding(current_node->item.getAcres(), 10) 
      << endl; 
     current_node = current_node->nextByName; 
    } 
} 
3

放棄的選項卡。您應該可以使用io操縱器來設置字段寬度,填充字符和格式標記(以獲得左對齊或右對齊)。爲標題使用相同的數值,並且所有內容都應該很好地顯示出來。

另請注意,您已在您的示例中更改了評分和英畝數。

15

認爲它像使用Microsoft Excel :) 你認爲你的流爲字段。因此,您先設置字段的寬度,然後在該字段中插入文本。例如:

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

int main() 
{ 
    using namespace std; 

    string firstName = "firstName", 
      secondName = "SecondName", 
      n = "Just stupid Text"; 
    size_t fieldWidth = n.size(); // length of longest text 

    cout << setw(fieldWidth) << left << firstName << endl // left padding 
     << setw(fieldWidth) << left << secondName << endl 
     << setw(fieldWidth) << left << n << endl; 

    cout << setw(fieldWidth) << right << firstName << endl // right padding 
     << setw(fieldWidth) << right << secondName << endl 
     << setw(fieldWidth) << right << n << endl; 
} 

......

alt text

......

字段寬度意味着什麼,但text + spaces的寬度。你可以比其他場所的任何fill

string name = "My first name"; 
cout << setfill('_') << setw(name.size() + 10) << left << name; 

.....

output:: 
My first name__________ 

......

我認爲最好的辦法是要弄清楚你的格式,那麼,寫一個新的格式化程序,可以做所有你想要的:

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

std::ostream& field(std::ostream& o) 
{ 
    // usually the console is 80-character wide. 
    // divide the line into four fields. 
    return o << std::setw(20) << std::right; 
} 

int main() 
{ 
    using namespace std; 

    string firstName = "firstName", 
      secondName = "SecondName", 
      n = "Just stupid Text"; 
    size_t fieldWidth = n.size(); 

    cout << field << firstName << endl 
     << field << secondName << endl 
     << field << n << endl; 
} 

如果你開始考慮參數化操作rs,只接受一個intlong參數很容易實現,如果您不熟悉C++中的流,則其他類型真的很模糊。

+3

+1標準IO操縱(運輸及工務局局長和人),一爲定義自定義了iomanip,-1爲Excel – outis 2009-09-20 01:04:26

+0

而且你還可以限制字符串的大小要打印,以防萬一你有大字符串(如完整名稱) – Vargas 2009-09-20 01:28:41

6

Boost有一個格式庫,它允許您輕鬆地格式化舊的C printf(),但是具有C++類型安全性。

請記住,舊的C printf()允許您指定一個字段寬度。如果輸出尺寸過小,此空間會填充該字段(請注意,它無法應對超大字段)。

#include <iostream> 
#include <iomanip> 
#include <boost/format.hpp> 

struct X 
{ // this structure reverse engineered from 
    // example provided by 'Mikael Jansson' in order to make this a running example 

    char*  name; 
    double  mean; 
    int   sample_count; 
}; 
int main() 
{ 
    X stats[] = {{"Plop",5.6,2}}; 

    // nonsense output, just to exemplify 

    // stdio version 
    fprintf(stderr, "at %p/%s: mean value %.3f of %4d samples\n", 
      stats, stats->name, stats->mean, stats->sample_count); 

    // iostream 
    std::cerr << "at " << (void*)stats << "/" << stats->name 
       << ": mean value " << std::fixed << std::setprecision(3) << stats->mean 
       << " of " << std::setw(4) << std::setfill(' ') << stats->sample_count 
       << " samples\n"; 

    // iostream with boost::format 
    std::cerr << boost::format("at %p/%s: mean value %.3f of %4d samples\n") 
       % stats % stats->name % stats->mean % stats->sample_count; 
}