3

ユーザーが入力する番号にコンマを追加する際に助けが必要です。ガイダンスや助けをいただければ幸いです。これまでのところ、最初の 3 桁と最後の 6 桁を保存してから、単純にフォーマットするだけです。

#include<iostream>

using namespace std;
int main ( int argc, char * argv[] ) 
{
unsigned long long userInput;
int fthreeDigit;

cout << "Enter a long long number: " << endl;
cin >> userInput;

fthreeDigit = ( userInput / 1000 );
userInput %= 1000;

cout << "Your Number: " << fthreeDigit << "," << userInput << endl;
system("pause");
return 0;
 }
4

5 に答える 5

9

これはあなたが必要とするものですか?ロケールはこれを正しく行います。

#include <iostream>
using namespace std;

int main ( int argc, char * argv[] ) 
{
  unsigned long long userInput;
  int fthreeDigit;
  cout << "Enter a long long number: " << endl;
  cin >> userInput;
  std::cout.imbue(std::locale(""));
  std::cout << userInput << std::endl;

  return 0;
}
于 2013-03-20T01:43:46.007 に答える
1

編集: 2 つの解決策があります。最初は数字をいじらずに (推奨)、2 つ目は (除算)。最初の解決策は次のとおりです。

#include <cstdlib>
#include <iostream>
#include <locale>
#include <string>
using namespace std;

struct my_facet : public std::numpunct<char>{
        explicit my_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
        virtual char do_thousands_sep() const { return ','; }
        virtual std::string do_grouping() const { return "\003"; }
};

/*
 * 
 */
int main(int argc, char** argv) {

    cout<<"before. number 5000000: "<<5000000<<endl;

    std::locale global;
    std::locale withgroupings(global, new my_facet);
    std::locale was = std::cout.imbue(withgroupings);

    cout<<"after. number 5000000: "<<5000000<<endl;

    std::cout.imbue(was);

    cout<<"and again as before. number 5000000: "<<5000000<<endl;

    return 0;
}

前。番号 5000000: 5000000
以降。数 5000000: 5,000,000

そして再び以前のように。数 5000000: 5000000

RUN SUCCESSFUL (合計時間: 54ms)

2番目(推奨されません)は次のとおりです。

double f = 23.43;
std::string f_str = std::to_string(f);

またはこれ

int a = 1;
stringstream ss;
ss << a;
string str = ss.str();

次にstring::substr() string::find() string::find_first_of()、 および同様の方法を使用して、文字列を変更およびフォーマットできます。
似たような話題


あなたが本当に分割したい(しなければならない)場合:(私のバージョンは他のバージョンよりもクリーンで効率的だと思います)

unsigned long long userInput;
    std::stringstream ss,s0;
    std::string nr;
        std::cout << "Enter a long long number: " << std::endl;
        std::cin >> userInput;
        int input=userInput;
        int digits;

        while(input>999){
            input=input/1000;
            digits=userInput-input*1000;
            int mdigits=digits;
            while(mdigits<100){s0<<"0";mdigits*=10;}
            std::string s=ss.str();
            ss.str("");
            ss<<","<<s0.str()<<digits<<s;
            userInput=input;
            s0.str("");
        }

        std::string sf=ss.str();
        ss.str("");
        ss<<input<<sf;

        std::cout << "Your Number: " << userInput << ";" << digits <<";"<<ss.str()<<std::endl;

長い長い番号を入力してください: 12345678 あなたの番号: 12;345;12,345,678

于 2013-03-20T01:42:43.760 に答える
0

これは総当たりですが、ベクトルを使用して 1000 桁ごとに取得する方法を理解するのが最も簡単かもしれません。

#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ( int argc, char * argv[] ) 
{
    long long userInput;
    int fthreeDigit;

   cout << "Enter a long long number: " << endl;
   cin >> userInput;
   vector <int> res;  //use vector to store every 3 digits
   while (userInput !=0)
   {
     fthreeDigit = userInput %1000;
     res.push_back(fthreeDigit);
     userInput = userInput / 1000 ;
   }

   std::reverse(res.begin(), res.end());
   for (size_t i = 0; i < res.size()-1; ++i)
   {
     if (res[i] ==0)
     {
        cout << "000"<<",";
     }
     else
     {
       cout << res[i] << ",";
     }
   }

   if (res[res.size()-1] == 0)
   {
       cout << "000";
   }
   else{
       cout << res[res.size()-1];
   }
   cout <<endl;
   cin.get();
   return 0;
}

このコードを次のケースでテストしました。

Input: 123456 Output: 123,456
Input: 12     Output: 12
Input: 12345  Output: 12,345
Input: 1234567 Output: 1,234,567
Input: 123456789 Output: 123,456,789
Input: 12345678 Output: 12,345,678

コメントへの返信によると、これがあなたが望むものだと思います。

于 2013-03-20T02:05:58.427 に答える
0

あなたはこれを行うことができます:

#include <iostream>
#include <string>

using namespace std;

string commify(unsigned long long n)
{
  string s;
  int cnt = 0;
  do
  {
    s.insert(0, 1, char('0' + n % 10));
    n /= 10;
    if (++cnt == 3 && n)
    {
      s.insert(0, 1, ',');
      cnt = 0;
    }
  } while (n);
  return s;
}

int main()
{
  cout << commify(0) << endl;
  cout << commify(1) << endl;
  cout << commify(999) << endl;
  cout << commify(1000) << endl;
  cout << commify(1000000) << endl;
  cout << commify(1234567890ULL) << endl;
  return 0;
}

出力 ( ideone ):

0
1
999
1,000
1,000,000
1,234,567,890
于 2013-03-20T02:36:15.957 に答える
0
// Accepts a long number, returns a comma formatted string
CString num_with_commas(long lnumber)
{
    CString num;
    num.Format(%d",lnumber);
    if(num.GetLength() > 3) {num.Insert(num.GetLength()-3, ',');}
    if(num.GetLength() > 7) { num.Insert(num.GetLength()-7, ','); }
    if (num.GetLength() > 12) { num.Insert(num.GetLength()-12, ','); }
    return(num);
}
于 2016-03-07T14:33:45.843 に答える