私のプログラムは、入力された数値を目的の基数に変換します。しかし、私は数の現在の基数を確認したい. 例 : 1 の基数が 3 で、入力した数が 4 であるため、これは不可能です。それを確認する関数を書きました。しかし、うまくいきません。私のせいはどこですか?
#include <stdlib.h>
#include <stdio.h>
int check(int tmp_number , int source_base)
{
int check = 0 ;
char msg[tmp_number];
sprintf(msg, "%d", tmp_number);
int i=0;
if(source_base>tmp_number)
{
return 1;
}
while( tmp_number )
{
if(check<source_base)
{
check = tmp_number % 10 ;
tmp_number /= 10 ;
i++;
}
}
if (sizeof(msg)==i)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int tmp_number=1;
int decimal=0;
int source_base,target_base;
char s[9];
int i=0;
do
{
printf("Enter the base1\n");
scanf("%d",&source_base);
printf("Enter the number\n");
scanf("%d",&tmp_number);
} while (!check(tmp_number,source_base));
printf("Enter the base2\n");
scanf("%d",&target_base);
while( tmp_number )
{
decimal += (tmp_number % 10) * pow( source_base, i ) ;
tmp_number /= 10 ;
++i ;
}
itoa(decimal, s, target_base);
printf("The result is %s\n", s);
getchar();
getchar();
return 1;
}