RSieve

Sheet number divider

Dec 20th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. /**
  2.  *  Useless stuff.
  3.  *  I was bored and I wanted to know the number of sheets of paper that were
  4.  *  were going to be printed to buy the correct folder.
  5.  *  You can actually change it to fit your needs. With num page you define
  6.  *  the number of slides you are going to put in a single side. I usually use
  7.  *  8 per side and that's my way.
  8.  *  In the file.txt write all the number of slides for each pdfs and it will
  9.  *  print to a txt file the number of sheets you will be needed.
  10.  *  Enjoy lazy uselessness
  11.  *
  12.  *  @author Riccardo Sieve
  13.  */
  14.  
  15. #include<stdio.h>
  16. #include<stdlib.h>
  17.  
  18. #define NUM_PAGE 8
  19.  
  20. typedef enum {FALSE, TRUE} boolean;
  21.  
  22. boolean is_divisible(int , int );
  23. boolean is_even(int);
  24.  
  25. int main() {
  26.  
  27.     int num=0, sum=0;
  28.     FILE *fin, *fout;
  29.  
  30.     if((fin = fopen("file.txt", "r")) == NULL) {
  31.         fprintf(stderr, "Error in opening the file");
  32.         exit(1);
  33.     }
  34.  
  35.     if((fout = fopen("out.txt", "w")) == NULL) {
  36.         fprintf(stderr, "Error with output file");
  37.         exit(2);
  38.     }
  39.  
  40.     while(fscanf(fin, "%d", &num) != EOF) {
  41.         if(is_divisible(num, NUM_PAGE))
  42.             num /= NUM_PAGE;
  43.         else
  44.             num /= NUM_PAGE + 1;
  45.  
  46.         if(is_even(num))
  47.             sum += (num / 2);
  48.         else
  49.             sum += (num / 2) + 1;
  50.     }
  51.  
  52.     fprintf(fout, "%d", sum);
  53.  
  54.     return 0;
  55. }
  56.  
  57. boolean is_divisible(int a, int b) {
  58.     return (a % b == 0);
  59. }
  60.  
  61. boolean is_even(int a) {
  62.     return is_divisible(a, 2);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment