Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void printWithComma(long long a) {
- const int max_digit_in_long_long = 26; // long long のカンマと符合を含んだ最大文字数は26桁 -9,223,372,036,854,775,808
- char buffer[max_digit_in_long_long + 1]; // null 文字を含めた最大桁数をアロケート
- buffer[max_digit_in_long_long] = 0; // 最後をnull 文字で埋めておく。
- char* p = &buffer[max_digit_in_long_long - 1];
- long long temp = abs(a);
- if (temp < 0) { // -9,223,372,036,854,775,808 に対する正の値はないので、特殊ケースとして処理
- printf("-9,223,372,036,854,775,808\n");
- return;
- }
- *p-- = '0' + temp % 10;
- temp = temp / 10;
- int digit = 0;
- while (temp != 0) {
- digit++;
- if (digit % 3 == 0) {
- *p-- = ',';
- }
- *p-- = '0' + temp % 10;
- temp = temp / 10;
- }
- if (a > 0) {
- *p = '+';
- } else if (a < 0) {
- *p = '-';
- } else {
- *p = ' ';
- }
- printf("%s\n", p);
- }
- int main()
- {
- printWithComma(999);
- printWithComma(18999);
- printWithComma(LLONG_MIN + 1);
- printWithComma(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement