Atheuz

Untitled

Sep 20th, 2011 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. /*
  2.  *  Dato: 17-09-2011
  3.  *  Kursus: Imperativ Programming
  4.  *  Underviser: Kurt Nørmark
  5.  *  Opgave: http://people.cs.aau.dk/~normark/impr-c/control-conditional-exp-slide-exercise-1.html
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. #define SEC_PER_HOUR 3600
  11. #define SEC_PER_MIN 60
  12.  
  13. int main()
  14. {
  15.     // Initialiser variabler
  16.     int total_seconds, rest, hours, minutes, seconds;
  17.  
  18.     // Få antal sekunder
  19.     printf("Skriv et positivt antal sekunder: ");
  20.     scanf("%d", &total_seconds);
  21.  
  22.     // Lav udregningerne.
  23.     hours = total_seconds / SEC_PER_HOUR;
  24.     rest = total_seconds % SEC_PER_HOUR;
  25.     minutes = rest / SEC_PER_MIN;
  26.     seconds = rest % SEC_PER_MIN;
  27.  
  28.     // Start på if statements
  29.    
  30.     if(total_seconds <= 0)
  31.     {
  32.         printf("Ikke godkendt.");
  33.         return 0;
  34.     }
  35.    
  36.     if(hours > 0)
  37.     {
  38.         if(hours == 1)
  39.         {
  40.             printf("%d time", hours);
  41.         }
  42.         else
  43.         {
  44.             printf("%d timer", hours);
  45.         }
  46.         if(minutes > 0 || seconds > 0)
  47.         {
  48.             if(minutes > 0 && seconds > 0)
  49.             {
  50.                 printf(", ");
  51.             }
  52.             else
  53.             {
  54.                 printf(" og ");
  55.             }
  56.         }
  57.     }
  58.  
  59.     if(minutes > 0)
  60.     {
  61.         if(minutes == 1)
  62.         {
  63.             printf("%d minut", minutes);
  64.         }
  65.         else
  66.         {
  67.             printf("%d minutter", minutes);
  68.         }
  69.         if(seconds > 0)
  70.         {
  71.             printf(" og ");
  72.         }
  73.     }
  74.  
  75.     if(seconds > 0)
  76.     {
  77.         if(seconds == 1)
  78.         {
  79.             printf("%d sekund", seconds);
  80.         }
  81.         else
  82.         {
  83.             printf("%d sekunder", seconds);
  84.         }
  85.     }
  86.  
  87.     // Afslut.
  88.     return 0;
  89. }
Add Comment
Please, Sign In to add comment