3

我需要帮助在用户输入的数字中添加逗号,我们将不胜感激一些指导或帮助。到目前为止,我有它存储前三位数字和后六位数字,然后简单地格式化它。

#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

编辑:我有两个解决方案。第一个不玩数字(推荐)和第二个(除法)。第一个解决方案是:

#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

运行成功(总时间:54ms)

第二个(不推荐)是:

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

这是蛮力,但可能是最容易理解的方法,在向量的帮助下获得每千位数字。

#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 回答