C语言判断一个正整数在十进制下是不是“快乐数”

快乐数(happy number)有以下的特性:在给定的进位制下,该数字所有数位(digits)的平方和,得到的新数再次求所有数位的平方和,如此重复进行,最终结果必为1。

#include <stdio.h>

#define FALSE 0
#define TRUE 1
#define NO 0
#define YES 1
#define INFINITE 2


typedef struct
{
	int index;
	int arr[1000];
} DICT;

int sum_of_square(int number) 
{
	int sum = 0;
	while (number >= 10)
	{
		sum += (number % 10) *(number % 10);
		number = number / 10;
	}
	sum += number * number;

	return sum;
}

void push(DICT *dict, int number)
{
	dict->arr[dict->index] = number;
	dict->index++;
}

int is_number_in_dict(DICT *dict, int number) 
{
	int result = FALSE;
	for (int i = 0; i < dict->index; i++)
	{
		if (number == dict->arr[i])
		{
			result = TRUE;
			break;
		}
	}
	return result;
}

void print_dict(DICT *dict)
{
	for (int i = 0; i < dict->index; i++)
	{
		printf("%d -> ", dict->arr[i]);
	}
}

int is_happy_number(int number)
{
	DICT dict;
	dict.index = 0;
	int result = NO;
	while (TRUE)
	{
		if(1 == number)
		{
			result = YES;
			break;
		}
		else if (is_number_in_dict(&dict, number)) 
		{
			result = INFINITE;
			break;
		}
		else 
		{
			push(&dict, number);
			number = sum_of_square(number);
		}
	}
	print_dict(&dict);
	printf("%d\n", number);
	return result;
}

void print_result(int result)
{
	if (YES == result)
	{
		printf("happy number\n");
	} 
	else if (NO == result)
	{
		printf("not happy\n");
	}
	else if (INFINITE == result)
	{
		printf("INFINITE\n");
	}
}

int main() {
	long number;
	printf("please input a positive number:\n");
	input_positive_number:
	scanf("%ld", &number);
	if (number < 1) {
		printf("%d is not a positive number, try again:\n", number);
		goto input_positive_number;
	}
	
	int result = is_happy_number(number);
	print_result(result);
	return 0;
}