Created
March 18, 2019 20:35
-
-
Save maehrm/b53f38ad8540de6c9fe46026fe4db225 to your computer and use it in GitHub Desktop.
平成20年度秋期試験_基本情報午後問6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void convert(long, char []); | |
void convert(long num, char str[]) { | |
int minus = 0, i = 0, j = 0; | |
char table[] = "0123456789"; | |
char tmp; | |
if (num < 0) { | |
minus = 1; | |
num = -num; | |
} | |
do { | |
str[j++] = table[num % 10]; /* 数値の下位から順に文字に変換 */ | |
num /= 10; | |
i++; | |
if (i % 3 == 0 && num != 0) { | |
str[j++] = ','; | |
} | |
} while (num != 0); | |
if (minus != 0) { | |
str[j++] = '-'; | |
} | |
str[j--] = '\0'; | |
for (i = 0; i < j; i++, j--) { /* 順序を逆にする。 */ | |
tmp = str[i]; | |
str[i] = str[j]; | |
str[j] = tmp; | |
} | |
} | |
int main(void) { | |
long nums[] = {1234567, -57482, 63, -999999}; | |
char str[BUFSIZ]; | |
int i, size = sizeof(nums) / sizeof(nums[0]); | |
for (i = 0; i < size; i++) { | |
convert(nums[i], str); | |
printf("%s\n", str); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
平成20年度秋季基本情報午後問6 - Mae向きなブログ https://maehrm.hatenablog.com/entry/2019/03/19/054007