c++将一个数字三位三位加逗号,用recursive function

这是我写的,但是最后一个三位也会有逗号,比如1,000,000,希望大家给个解决方法,谢谢

#include<iostream>
using namespace std;
void showCommas(int);
int main(){
showCommas(1000000);
return 0;
}
void showCommas(int number)
{
if(number > 0){
showCommas(number / 1000);
if((number % 1000) == 0){
cout << number % 1000 << "00,";
}
else
cout << number % 1000 << ",";
}
}

#include<iostream>
#include <conio.h>
using namespace std;
void showCommas(int);
int main(){
int number;
cin>>number;
showCommas(number);
getch();
return 0;
}
void showCommas(int number)
{
if(number/1000 > 0)
{
showCommas(number/1000);
if (number%1000>9 && number%1000<100)
{
cout<<','<<0<<number%1000;
}
if (number%1000>=0 && number%1000<10)
{
cout<<','<<0<<0<<number%1000;
}
if (number%1000>99&&number%1000<1000)
{
cout<<","<<number%1000;
}
}
else
{
cout<<number%1000;
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答