Advertisement
Guest User

note_subtract.c

a guest
Apr 5th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5.  
  6. // A struct note * IS a Note
  7. typedef struct note *Note;
  8.  
  9. //There are 10 octaves (0 to 9) and 12 notes (0 to 11)
  10. struct note {
  11.     int octave;
  12.     int key;
  13.     Note next;
  14. };
  15.  
  16. Note note_subtract(Note higher, Note lower);
  17. void print_note(Note n);
  18.  
  19. int main(void) {
  20.     int octave, key;
  21.     scanf("%d %d", &octave, &key);
  22.     struct note a = {octave, key, NULL};
  23.     scanf("%d %d", &octave, &key);
  24.     struct note b = {octave, key, NULL};
  25.     Note diff = note_subtract(&a, &b);
  26.     print_note(diff);
  27.     free(diff);
  28.     return 0;
  29. }
  30.  
  31. // For a note with octave 0, and note 9,
  32. // `print_note` should print:
  33. // "0 09\n"
  34. void print_note(Note n) {
  35.     printf("%d %02d\n", n->octave, n->key);
  36. }
  37.  
  38.  
  39.  
  40. //Returns a pointer to a malloced struct containing the difference between a
  41. //higher and a lower note
  42. Note note_subtract(Note higher, Note lower) {
  43.     struct note *returnNote;
  44.     returnNote = malloc(sizeof(struct note));
  45.    
  46.     int octave = higher->octave - lower->octave;
  47.     int key = higher->key - lower->key;
  48.     if (key < 0) {
  49.         octave--;
  50.         key += 12;
  51.     }
  52.    
  53.     returnNote->octave = octave;
  54.     returnNote->key = key;
  55.    
  56.     return returnNote;
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement