Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. /* inc, x2,x3 calculator */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /* actions enum */
  5. enum {
  6.     INC,
  7.     X2,
  8.     X3
  9. } ctions;
  10.  
  11. /* step list item */
  12. typedef struct calcFlow{
  13.     short int action;
  14.     struct calcFlow* prev;
  15. }calcFlow;
  16.  
  17. /* create step & allocate it with previes step */
  18. struct calcFlow* createNewStep(short int action, struct calcFlow* prev)  {
  19.     struct calcFlow *step;
  20.     step = (struct calcFlow*) malloc (sizeof(calcFlow));
  21.     if (step != NULL) {
  22.         step->prev = prev;
  23.         step->action = action;
  24.     }
  25.     return step;
  26. }
  27.  
  28.  
  29.  
  30. /* get valid incoming integer */
  31. int getSource(void) {
  32.     int value=0;
  33.     do{
  34.         printf("%s", "Input positive integer: ");
  35.         scanf("%6d", &value);
  36.     } while ( (value <= 0) );
  37.     return value;
  38. }
  39.  
  40. /* actionHandler */
  41. int executeAction(int src,int action) {
  42.     switch (action) {
  43.         case INC:
  44.             printf ("increment \t%d\n", ++src);
  45.             break;
  46.         case X2:  
  47.             src*=2;
  48.             printf("multiply x2 \t%d\n", src);
  49.             break;
  50.         case X3:  
  51.             src*=3;
  52.             printf("multiply x3 \t%d\n", src);
  53.             break;
  54.     }  
  55.     return src;
  56. }
  57.  
  58.  
  59.  
  60. int main (int argc, char **argv) {
  61.     int source, reverse=0, stepNumber=1;
  62.     source=getSource();
  63.     printf("Source is %d\n", source);
  64.  
  65.     struct calcFlow *currentStep=NULL, *prev=NULL;
  66.  
  67. /* calculate */
  68.     while (source >0) {
  69. //      printf("%d\n", source);
  70.         if (source%3==0) {
  71.             currentStep = createNewStep(X3,currentStep);
  72.             source/=3;
  73.         } else
  74.         if (source%2==0) {
  75.             currentStep = createNewStep(X2,currentStep);
  76.             source /=2;
  77.         }  else
  78.         {
  79.             currentStep = createNewStep(INC,currentStep);
  80.             source--;
  81.         }
  82.     }
  83. /* reverse */
  84.     do {
  85.         printf("step \t#%d\t",stepNumber++);
  86.         prev=currentStep->prev;
  87.         switch (currentStep->action) {
  88.             case INC:
  89.             case X2:  
  90.             case X3:  reverse = executeAction( reverse, currentStep->action ); break;
  91.             default: printf("unknown action %d", currentStep->action);
  92.            
  93.         }
  94.         free(currentStep);
  95.         currentStep=prev;
  96.     } while (currentStep!= NULL );
  97.  
  98.    
  99.     return 0;  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement