Advertisement
18126

Day4(ex1)

Jul 9th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4.  
  5. void setBit(int* mask, int n) {
  6.     *mask |= (1 << n);
  7. }
  8.  
  9. void eraseBit(int* mask, int n) {
  10.     *mask &= ~(1 << n);
  11. }
  12.  
  13. int isSet(int mask, int n) {
  14.     return mask & (1 << n);
  15. }
  16.  
  17. int onesCount(int mask) {
  18.     int count = 0;
  19.     for (int i = 0; i < sizeof(int) * 8; i++) {
  20.         if (isSet(mask, i)) {
  21.             count++;
  22.         }
  23.     }
  24.     return count;
  25. }
  26.  
  27. int zerosCount(int mask) {
  28.     return sizeof(int) * 8 - onesCount(mask);
  29. }
  30.  
  31. void clearConsole() {
  32.     for(int j = 0; j < 30; j++) {
  33.         printf("\n");
  34.     }
  35. }
  36.  
  37. int main(){
  38.     int information = 0;
  39.     int option;
  40.     while (1) {
  41.         printf("Hello! Welcome! :)\nTo continue, please choose one of the options below:\n============\n 1 - Enter presence of student.\n 2 - Enter absence of student.\n 3 - Get the current information of all students.\n 4 - Exit the program.\n");
  42.         scanf("%d", &option);
  43.         if (option == 1) {
  44.             int n;
  45.             printf("Okay! Please enter the number of the student you want to mark as present.\n");
  46.             scanf("%d", &n);
  47.             if(n < 1 || n > 32) {
  48.                 printf("Invalid class number! Please enter a number between 1 and 32.\n(Please wait, the program will auto-reset.)\n");
  49.                 sleep(5);
  50.                 clearConsole();
  51.             } else {
  52.                 setBit(&information, n-1);
  53.                 printf("Done! %d is marked as present.\n(Please wait, the program will auto-reset.)\n", n);
  54.                 sleep(5);
  55.                 clearConsole();
  56.             }
  57.         }
  58.         else if (option == 2) {
  59.             int n;
  60.             printf("Okay! Please enter the number of the student you want to mark as absent.\n");
  61.             scanf("%d", &n);
  62.             if(n < 1 || n > 32) {
  63.                 printf("Invalid class number! Please enter a number between 1 and 32.\n(Please wait, the program will auto-reset.)\n");
  64.                 sleep(5);
  65.                 clearConsole();
  66.             } else {
  67.                 eraseBit(&information, n-1);
  68.                 printf("Done! %d is marked as absent.\n(Please wait, the program will auto-reset.)\n", n);
  69.                 sleep(5);
  70.                 clearConsole();
  71.             }
  72.         }
  73.         else if (option == 3) {
  74.             printf("\n===========\nAbsent: %d\n", zerosCount(information));
  75.             printf("Present: %d\n===========\n", onesCount(information));
  76.             printf("Would you like to get detailed information about every student?\n(1 : YES , 2 : N0)\n");
  77.             int n;
  78.             scanf("%d", &n);
  79.             if(n == 1) {
  80.                 printf("\nOkay!\n============\n");
  81.                 for(int j = 0; j < 32; j++) {
  82.                     if(isSet(information, j) != 0) {
  83.                         printf("%d is present.\n", j+1);
  84.                     }
  85.                     else {
  86.                         printf("%d is absent.\n", j+1);
  87.                     }
  88.                 }
  89.                 printf("\n(Please wait, the program will auto-reset.)\n");
  90.                 sleep(10);
  91.                 clearConsole();
  92.             } else {
  93.                 printf("Okay!\n(Please wait, the program will auto-reset.)\n");
  94.                 sleep(3);
  95.                 clearConsole();
  96.             }
  97.         }
  98.         else if (option == 4) {
  99.             printf("Have a nice day! :)");
  100.             break;
  101.         }
  102.         else {
  103.             printf("Invalid option! Please make sure you entered a correct number.\n(Please wait, the program will auto-reset.)\n");
  104.             sleep(5);
  105.             clearConsole();
  106.         }
  107.     }
  108.     return EXIT_SUCCESS;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement