2009-09-19 20 views
7

मैं आउटपुट को लंबे समय तक कंसोल में प्रारूपित करने का प्रयास कर रहा हूं और वास्तव में कुछ भी नहीं हो रहा है। मैं iomanip जितना अधिक कर सकता हूं और ofstream& फ़ंक्शंस के रूप में उपयोग करने का प्रयास कर रहा हूं।स्वरूपण सी ++ कंसोल आउटपुट

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

माइक्रोसॉफ़्ट एक्सेल का उपयोग करने की तरह सोचें :) आप अपनी स्ट्रीम के क्षेत्र के रूप में सोचते हैं। तो आप पहले फ़ील्ड की चौड़ाई सेट करते हैं तो आप उस फ़ील्ड में अपना टेक्स्ट डालते हैं। उदाहरण के लिए:

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

यदि आपने पैरामीट्रिज्ड मैनिपुलेटो के बारे में सोचना शुरू किया है आरएस, केवल एक int या long पैरामीटर को लागू करने के लिए आसान है, अन्य प्रकार वास्तव में अस्पष्ट हैं यदि आप C++ में स्ट्रीम से परिचित नहीं हैं।

+3

मानक कब manipulators (setw और अल), +1 एक कस्टम iomanip परिभाषित करने के लिए लिए +1, एक्सेल – outis

+0

के लिए और -1 आप भी स्ट्रिंग के आकार को सीमित कर सकते हैं सिर्फ मामले में मुद्रित करने के लिए आप बड़े हो तार (जैसे पूर्ण नाम) – Vargas

6

बूस्ट में एक प्रारूप लाइब्रेरी है जो आपको पुराने सी printf() की तरह आसानी से प्रारूपित करने की अनुमति देती है लेकिन सी ++ की सुरक्षा के साथ।

याद रखें कि पुराने सी 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; 
} 
संबंधित मुद्दे