/***************************** factoriser.c ***************************** Primarily an exercise in linked lists, this program finds the factors of a number input by the user and returns them. ************************************************************************/ /* Include some other files */ #include #include /* Define some constants */ #define version 0.1 /* Prototype some fns. */ int getInt(); main () { // START OF MAIN /* Would be nice to include code to pass number as argument, as well as * a feature to get `help' and version information. Have seen it * somewhere but can't find it again. */ /* main() variables. */ int number, i, firstPtr; /* As I understand this, below defines a set of types ('num' and 'next') * under the domain 'prime_node_t', if so, what does 'prime_node_tmp' * mean? */ typedef struct prime_node_tmp{ long num; /* Is the following self-referential to its own typedef? */ struct prime_node_tmp *next; } prime_node_t; /* 'Declares' the pointer as type 'prime_node_t' meaning it can * use 'methods' num and next. */ prime_node_t *myPtr; /* Prompt */ printf("Please enter your number to be factorised: "); number = getInt(INT_MAX,INT_MIN); /* Set firstPtr to 0 */ firstPtr = 0; for (i = 2; i < sqrt(number); i++) { if (!number % i) { /* Don't fully understand the following, however have tried to * express how I understand it so far, seems to have parrallels * with Perl's 'object orientated' approach so have included * analogies. I'm probably way off base though ;-) */ /* Allocate some memory of any size 'methods' num and next can * take. myPtr is a pointer the is at start of this memory. */ myPtr = (prime_node_t *)malloc(sizeof(prime_node_t)); /* Save the pointer to the first node for use in retreival. */ if (!firstPtr) { firstPtr = (int)myPtr; } /* Uses num 'method' to assign number/i to myPtr address in * memory. */ myPtr->num = number/i; /* Uses next 'method' to assign next place in memory NULL. */ myPtr->next = NULL; } } /* Print out the factors if it has any, else declare it a prime. Note * this does not yet inlcude code for vetting numbers 1 and itself */ if (firstPtr) { /* True if a factor was found */ printf("%d has the following factors...\n\n",number); printf("%d",firstPtr); /* while (firstPtr->next) { printf("%d",firstPtr*); } */ } else { printf("%d is a prime number.\n",number); } } // END OF MAIN /************************** getInt() - V0.1 ************************* Retrieves an integer from STDIN Changelog: Version 0.2 Additional max and min set to INT_MAX and INT_MIN. ********************************************************************/ int getInt(int max, int min) { /* Declare local vars */ int valid, num; char usrInput[5]; /* Check to see if within limits */ if (max > INT_MAX) { max == INT_MAX; printf("Debug: maximum changed to %d\n", INT_MAX); } if (min < INT_MIN) { min == INT_MIN; printf("Debug: lower limit changed to %d\n", INT_MIN); } do { scanf("%s",usrInput); num = atoi(usrInput); if ((num >= min) && (num <= max)) { valid = 1; } else { valid = 0; printf("Valid range is %d-%d, please re-enter: ",min,max); } } while (!valid); return(num); }