2010-04-15 20 views
8

मुझे ओवरलोडेड ऑपरेटर < < के साथ एक साधारण श्रेणी मुद्रा मिली है। मुझे नहीं पता कि मैं प्रत्येक 3 अंकों में रिक्त स्थान के साथ संख्या को कैसे अलग कर सकता हूं, ऐसा लगता है: "1 234 567 आईएसके"।हजारों विभाजक के रूप में एक स्थान के साथ एक संख्या मुद्रित करने के लिए कैसे?

#include <cstdlib> 
#include <iostream> 

using namespace std; 

class Currency 
{ 
    int val; 
    char curr[4]; 

    public: 
    Currency(int _val, const char * _curr) 
    { 
     val = _val; 
     strcpy(curr, _curr); 
    } 

    friend ostream & operator<< (ostream & out, const Currency & c); 
}; 

ostream & operator<< (ostream & out, const Currency & c) 
{ 
    out << c.val<< " " << c.curr; 
    return out; 
} 

int main(int argc, char *argv[]) 
{ 
    Currency c(2354123, "ISK"); 
    cout << c; 
} 

मुझे क्या रूचि है, किसी भी तरह इस विशेष स्थिति के लिए सबसे आसान समाधान है।

+0

@danben: कैसे के रूप में टैग होगा [होमवर्क] प्रश्न या कितना अच्छा जवाब के बारे में कुछ भी बदल न्याय किया जाना चाहिए? –

+2

@ रॉगर पाट: होमवर्क के रूप में एक प्रश्न टैग करने से एसओ समुदाय को पता चलता है कि उन्हें मार्गदर्शन देना चाहिए और पोस्टर को उनके लिए समाधान लिखने के बजाय समाधान पर आने में मदद करना चाहिए। Http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions देखें। – danben

+0

@ डैनबेन: http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions/10825#10825 और http://meta.stackexchange.com/questions/10811 देखें/कैसे-से-पूछ-और-उत्तर-गृहकार्य-प्रश्न/10839 # 10839 –

उत्तर

15

यह

struct myseps : numpunct<char> { 
    /* use space as separator */ 
    char do_thousands_sep() const { return ' '; } 

    /* digits are grouped by 3 digits each */ 
    string do_grouping() const { return "\3"; } 
}; 

int main() { 
    std::cout.imbue(std::locale(std::locale(), new myseps)); 
    std::cout << 10000; // 10 000 
} 

वैकल्पिक रूप से पहलुओं के साथ किया जा सकता है, तो आप अपने पाश कोड सकता है

void printGrouped(ostream &out, int n) { 
    if(n < 0) { 
    out << "-"; 
    return printGrouped(out, -n); 
    } 

    if(n < 1000) { 
    out << n; 
    } else { 
    printGrouped(out, n/1000); 
    out << " " << setw(3) << setfill('0') << (n % 1000); 
    } 
} 

ostream & operator<< (ostream & out, const Currency & c) { 
    printGrouped(out, c.val); 
    out << " " << c.curr; 
    return out; 
} 
+0

हालांकि * सभी * संख्याओं को मुद्रित करने के तरीके को बदलने के बावजूद लगभग निश्चित रूप से वांछित नहीं है। –

+0

@ रोगर मेला पॉइंट। एक कस्टम पाश जोड़ा गया। –

+0

'लोकेल (" ")' कार्यान्वयन-परिभाषित है। ओएस एक्स 10.5 पर यह काम नहीं करेगा ('runtime_exception' को 'what()' "locale :: facet :: _ S_create_c_locale नाम मान्य नहीं है") के साथ फेंकता है। आप वर्तमान वैश्विक लोकेल का उपयोग इसके बजाय डिफ़ॉल्ट कन्स्ट्रक्टर के साथ कर सकते हैं। – wilhelmtell

2
struct Currency { 
    static char const sep = ' '; 
    static int const group_size = 3; 

    Currency(int val, std::string unit) 
    : val(val), unit(unit) 
    {} 

    friend std::ostream& operator<<(std::ostream& out, Currency const& v) { 
    // currently ignores stream width and fill 
    std::ostringstream ss; 
    bool const neg = v.val < 0; 
    int const val = (neg ? -v.val : v.val); 
    if (neg) out << '-'; 
    ss << val; 
    std::string const s = ss.str(); 
    std::string::size_type n = s.size() % v.group_size; 
    if (n) out << s.substr(0, n); 
    for (; n < s.size(); n += v.group_size) { 
     out << sep << s.substr(n, v.group_size); 
    } 
    out << ' ' << v.unit; 
    return out; 
    } 

private: 
    int val; 
    std::string unit; 
}; 

सितम्बर बनाने जा सका और गैर स्थिर सदस्य GROUP_SIZE, यदि आप अल्पविराम, आदि के साथ प्रत्येक वस्तु को अनुकूलित करना चाहते हैं (यदि हां, तो उन्हें निजी बनाएं और ctor में प्रारंभ, शायद डिफ़ॉल्ट पैरामीटर मान के साथ।) आप एक आउटपुट वर्ग नियंत्रण आउटपुट स्वरूपण का भी उपयोग कर सकते हैं।

लोकेल भी मुद्रापंथी पहलू के माध्यम से मुद्रा स्वरूपण का समर्थन करते हैं।

7

एक संभावना यह इस बात के लिए locales उपयोग करने के लिए हो सकता है।

#include <locale> 
#include <string> 
#include <cstddef> 

class SpaceSeparator: public std::numpunct<char> 
{ 
public: 
    SpaceSeparator(std::size_t refs): std::numpunct<char>(refs) {} 
protected: 
    char do_thousands_sep() const { return ' '; } 
    std::string do_grouping() const { return "\03"; } 
}; 

//...  
ostream & operator<< (ostream & out, const Currency & c) 
{ 
    SpaceSeparator facet(1); //1 - don't delete when done 
    std::locale prev = out.imbue(std::locale(std::locale(), &facet)); 
    out << c.val<< " " << c.curr; 
    out.imbue(prev); //restore previous locale 
    return out; 
} 
+0

+1 :) –

+1

हालांकि, std :: moneypunct उपयोग करने के लिए एक बेहतर पहलू होगा। –

-1
#include <iostream> 

#include <sstream> 

#include <cstdlib> 

#define GROUP_SEP ',' 

#define GROUP_SIZE 3 

using namespace std; 

string output_formatted_string(long long num); 


int main() { 

    string temp; 

    cout << "Enter a large number: "; 

    getline(cin, temp); 

    long long num = atoll(temp.c_str()); 

    string output = output_formatted_string(num); 


    cout << output << endl; 

    return 0; 

    } 

    string output_formatted_string(long long num) 

    { 

    stringstream temp, out; 

    temp << num; 

    string s = temp.str(); 


    int n = s.size() % GROUP_SIZE; 

    int i = 0; 

    if(n>0 && s.size() > GROUP_SIZE) 

     { 

     out << s.substr(i, n) << GROUP_SEP; 

     i += n; 

     } 


    n = s.size()/GROUP_SIZE - 1; 

    while(n-- > 0) 
     { 

     out << s.substr(i, GROUP_SIZE) << GROUP_SEP; 

     i += GROUP_SIZE;   
} 

    out << s.substr(i); 

    return out.str(); 

    } 
संबंधित मुद्दे

 संबंधित मुद्दे