#include "armstrong_numbers.h" #include #include //#include #define UNUSED(x) (void)(x) static int getNoLen(int candidate); static void getNoInDec(int candidate, int len, int* array); static int getNoLen(int candidate) { int i = 1; int base = 1; int quotients = 0; do { base = pow(10, i); quotients = candidate / base; if (quotients > 0) i++; else break; } while (quotients > 0); UNUSED(candidate); UNUSED(quotients); return i; } void getNoInDec(int candidate, int len, int* array) { for (int i = len - 1; i >= 0; i--) { int base = pow(10, i); array[i] = candidate / base; candidate -= base * array[i]; } } bool is_armstrong_number(int candidate) { //求 位数 int len = getNoLen(candidate); //求 位数 int* array = (int*)malloc(len * sizeof(int)); getNoInDec(candidate, len, array); int sum = 0; for (int i = 0; i < len; i++) { sum += pow(array[i], len);; } //拆数据 UNUSED(len); free(array); return sum == candidate; } resistor_band_t color_code(int color) { return (resistor_band_t)color; } int colors(void) { return (int)COLOR_MAX; } #define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0])) static int test_colors(void) { const resistor_band_t expected[] = { BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GREY, WHITE }; int i = colors(); int j = ARRAY_LENGTH(expected) ; return i == j; } int main(void) { int cnt = colors(); test_colors(); is_armstrong_number(153); }