Vasilena

PracticeDayFourExercise1

Jul 8th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void setBit(int* mask, int n)
  5. {
  6.     *mask |= (1 << n);
  7. }
  8.  
  9. void eraseBit(int* mask, int n)
  10. {
  11.     *mask &= ~(1 << n);
  12. }
  13.  
  14. int isSet(int mask, int n)
  15. {
  16.     return mask & (1 << n);
  17. }
  18.  
  19. //HW
  20. void funkci(int mask){
  21.     for (int i = 0; i < sizeof(int) * 8; i++)
  22.     {
  23.         if (isSet(mask, i))
  24.         {
  25.             printf("%d ", i);
  26.         }
  27.     }
  28. }
  29.  
  30. //HW
  31. void funkti(int mask){
  32.     for (int i = 0; i < sizeof(int) * 8; i++)
  33.     {
  34.         if (!isSet(mask, i))
  35.         {
  36.             printf("%d ", i);
  37.         }
  38.     }
  39. }
  40.  
  41. int onesCount(int mask)
  42. {
  43.     int count = 0;
  44.     for (int i = 0; i < sizeof(int) * 8; i++)
  45.     {
  46.         if (isSet(mask, i))
  47.         {
  48.             count++;
  49.         }
  50.     }
  51.     return count;
  52. }
  53.  
  54. int zerosCount(int mask)
  55. {
  56.     return sizeof(int) * 8 - onesCount(mask);
  57. }
  58.  
  59. int main()
  60. {
  61.     int prisustviq = 0;
  62.     int option;
  63.     while (1)
  64.     {
  65.         printf("1. ADD PRESENCE\n");
  66.         printf("2. ADD ABSENCE\n");
  67.         printf("3. INFORMATION\n");
  68.         printf("4. EXIT\n");
  69.         scanf("%d", &option);
  70.         if (option == 1)
  71.         {
  72.             int n;
  73.             printf("Enter student`s number: ");
  74.             scanf("%d", &n);
  75.             if(n >= 0 && n <= 32)
  76.             {
  77.                  setBit(&prisustviq, n);
  78.             }
  79.             else
  80.             {
  81.                  printf("    -ERROR-   \n Invalid option! \n");
  82.             }
  83.         }
  84.         else if (option == 2)
  85.         {
  86.             int n;
  87.             printf("Enter student`s number: ");
  88.             scanf("%d", &n);
  89.             if(n >= 0 && n <= 32)
  90.             {
  91.                  eraseBit(&prisustviq, n);
  92.             }
  93.             else
  94.             {
  95.                  printf("    -ERROR-   \n Invalid option! \n");
  96.             }
  97.         }
  98.         else if (option == 3)
  99.         {
  100.             printf("1. OVERALL CLASS INFORMATION\n");
  101.             printf("2. INFORMATION BY NUMBERS\n");
  102.             int n;
  103.             scanf("%d", &n);
  104.             if(n == 1)
  105.             {
  106.                 printf("Total number of presence students: %d\n", onesCount(prisustviq));
  107.                 printf("Total number of absence students: %d\n", zerosCount(prisustviq));
  108.             }
  109.             else if(n == 2)
  110.             {
  111.                 printf("Presence students by number: ");
  112.                 funkci(prisustviq);
  113.                 printf("\n");
  114.  
  115.                 printf("Absence students by number: ");
  116.                 funkti(prisustviq);
  117.                 printf("\n");
  118.             }
  119.             else
  120.             {
  121.                 printf("    -ERROR-   \n Invalid option! \n");
  122.             }
  123.         }
  124.         else if (option == 4)
  125.         {
  126.             break;
  127.         }
  128.         else
  129.         {
  130.             printf("    -ERROR-   \n Invalid option! \n");
  131.         }
  132.     }
  133.     return 0;
  134. }
  135.  
Add Comment
Please, Sign In to add comment