Holey_yan

20140514_KUAS_VisualC++_Classwork_0507_Question1_Answer

May 14th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. /***********************************************/
  2. /** 20140514 Classwork_0507 Question 1 Answer **/
  3. /** Maker  : Yan                              **/
  4. /** E-Mail : [email protected]            **/
  5. /** Date   : 2014/05/14 am.04:39              **/
  6. /***********************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. void change(int ,int, int, int);
  12.  
  13. void main()
  14. {
  15.     while(1)
  16.     {
  17.         int target = 0, base = 0, count = 1, answer = 0;
  18.         printf("Enter a number of target: ");
  19.         scanf("%d", &target);
  20.         if(target == 0)
  21.         {
  22.             break;
  23.         }
  24.         printf("Enter a number of base: ");
  25.         scanf("%d", &base);
  26.         change(target, base, count, answer);
  27.     }
  28.     printf("Your Program is exiting ..\n");
  29.     system("pause");
  30. }
  31.  
  32. void change(int target, int base, int count, int answer)
  33. {
  34.     int subtrahend = 0;
  35.     if(target >= base)
  36.     {
  37.         while(subtrahend + base <= target)
  38.         {
  39.             subtrahend += base;
  40.         }
  41.         answer = (target - subtrahend) * count + answer;
  42.         target = subtrahend / base;
  43.         count *= 10;
  44.         //debug
  45.         //printf("%d, %d, %d, %d\n", target, base, count, answer);
  46.         change(target, base, count, answer);
  47.     }
  48.     else if(target < base)
  49.     {
  50.         answer = (target - subtrahend) * count + answer;
  51.         printf("Answer = %d.\n\n", answer);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment