Last active
May 29, 2018 13:55
-
-
Save KeitetsuWorks/37b3c36b51942fb81ee651e205a3d335 to your computer and use it in GitHub Desktop.
10進数の整数をN進数に基数変換する
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
/** | |
* @file n_num.c | |
* @brief 10進数の整数をN進数に基数変換する | |
* @author Keitetsu | |
* @date 2017/06/11 | |
* @copyright Copyright (c) 2017 Keitetsu | |
* @par License | |
* This software is released under the MIT License. | |
*/ | |
#include <stdio.h> | |
#define DIGIT_MAX 10 /**< 基数変換後の最大桁数 */ | |
/** | |
* @struct n_num_st | |
* @brief N進数値格納用構造体 | |
* | |
* @typedef N_NUM_T | |
* @brief N進数値格納用構造体 | |
*/ | |
typedef struct n_num_st { | |
int radix; /**< 基数 */ | |
int digit[DIGIT_MAX]; /**< N進数表示用文字のインデックス */ | |
int digit_num; /**< 桁数 */ | |
} N_NUM_T; | |
/** | |
* @var digit_char | |
* @brief N進数表示用文字 | |
*/ | |
const char digit_char[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
/** | |
* @brief N進数格納用構造体を初期化する | |
* @param[out] target N進数格納用構造体 | |
*/ | |
static void N_Num_init(N_NUM_T *target); | |
/** | |
* @brief 10進数の整数をN進数に基数変換する | |
* @param[in] decimal 10進数の整数 | |
* @param[in] radix 基数 | |
* @param[out] target N進数格納用構造体 | |
* @retval 0 変換成功 | |
* @retval 1 変換失敗 | |
*/ | |
static int N_Num_convertRadix( | |
int decimal, | |
int radix, | |
N_NUM_T *result); | |
/** | |
* @brief N進数を表示する | |
* @param[in] target N進数格納用構造体 | |
*/ | |
static void N_Num_print(const N_NUM_T target); | |
/** | |
* @brief メイン関数 | |
* @retval 0 正常終了 | |
* @retval 1 異常終了 | |
*/ | |
int main(void) | |
{ | |
int x, n; | |
N_NUM_T x_n; | |
int retval; | |
N_Num_init(&x_n); | |
printf("10進数の整数を入力: "); | |
scanf("%d", &x); | |
printf("基数を入力: "); | |
scanf("%d", &n); | |
if (N_Num_convertRadix(x, n, &x_n) == 0) { | |
N_Num_print(x_n); | |
retval = 0; | |
} | |
else { | |
printf("基数変換に失敗しました。対応範囲外の入力値です。\n"); | |
retval = 1; | |
} | |
return retval; | |
} | |
static void N_Num_init(N_NUM_T *target) | |
{ | |
int i; | |
/* 基数の初期化 */ | |
target->radix = 0; | |
/* 数字の初期化 */ | |
for (i = 0; i < DIGIT_MAX; i++) { | |
target->digit[DIGIT_MAX - (i + 1)] = 0; | |
} | |
/* 桁数の初期化 */ | |
target->digit_num = 0; | |
return; | |
} | |
static int N_Num_convertRadix( | |
int decimal, | |
int radix, | |
N_NUM_T *result) | |
{ | |
N_NUM_T work; | |
int retval; | |
/* 一時変数の初期化 */ | |
N_Num_init(&work); | |
/* 基数変換 */ | |
work.radix = radix; | |
while (work.digit_num < DIGIT_MAX && decimal != 0) { | |
work.digit[DIGIT_MAX - (work.digit_num + 1)] = decimal % work.radix; | |
decimal /= work.radix; | |
work.digit_num++; | |
} | |
if (decimal != 0) { | |
retval = 1; | |
} | |
else { | |
*result = work; | |
retval = 0; | |
} | |
return retval; | |
} | |
static void N_Num_print(const N_NUM_T target) | |
{ | |
int i; | |
printf("%d進数: ", target.radix); | |
for (i = (DIGIT_MAX - target.digit_num); i < DIGIT_MAX; i++) { | |
printf("%c", digit_char[target.digit[i]]); | |
} | |
printf("\n"); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment