/*-*-mode:c-*- *************************************************************************** CSN381: Project 1 (2:a) http://homepage.mac.com/hteric/Project1.pdf *************************************************************************** File : sumofodd.c Author : Jeremy Russell Date : September 5, 2002 Description : 3. Write a program called 'nsplit.c' which prompts the user an integer between 1 and 32768 and print the 'splitted' form (add a space between each digits), eg. suppose you input 1234, you get 1 2 3 4 Some requirements: a. Print a message like "Please input a number between 1 and 32768:" when you prompt the users for a input. b. Check if the user has typped in a number (assume it is a positive integer to avoid more hassle) not in range and print out warning if the user did. c. After you print out the 'splitted' number, print out a message to ask users if they want to continue, your program has to repeat the above process again and if the user choose to continue . ***************************************************************************/ #include <stdio.h> //Declare functions int whichPowerOfTen ( int ); /*************************************************************************** Function : main Author : Jeremy Russell Date : September 5, 2002 Description : Split a given set of nummbers out from Arguments : void Returns : void Notes : None See Also : None ***************************************************************************/ int main ( void ) { int number, flag; int tmp, digit, size; char again; do { do { flag = 0; printf("Please input an integer: "); scanf("%d", &number); if (( number < 0 ) || ( number > 32678 )) { printf ("The input is not valid.\n"); } else { flag++; } } while ( ! flag ); // Find the power of ten whith which to start splitting. size = whichPowerOfTen ( number ); do { digit = number / size; number = number - (size * digit); size /= 10; printf("%d ", digit); } while (( number != 0) || (size)); printf ( "\n" ); printf ("Do you want to try again? (y/n): "); scanf ("\n%c", &again); } while ( again != 'n' && again != 'N'); return 0; } /*************************************************************************** Function : whichPoerOfTen Author : Jeremy Russell Date : September 5, 2002 Description : Find the power of ten to start with to split the integer. Arguments : number Returns : int Notes : None See Also : None ***************************************************************************/ int whichPowerOfTen ( int left ) { int tmp, size = 1; while (left != tmp) { size *= 10; tmp = left % size; } return size / 10; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 2126 | Jeremy Russell | Going to school. | ||
#1 | 2117 | Jeremy Russell |
Project 1 nsplit.c has problesm still. |