Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- // A struct note * IS a Note
- typedef struct note *Note;
- //There are 10 octaves (0 to 9) and 12 notes (0 to 11)
- struct note {
- int octave;
- int key;
- Note next;
- };
- Note note_subtract(Note higher, Note lower);
- void print_note(Note n);
- int main(void) {
- int octave, key;
- scanf("%d %d", &octave, &key);
- struct note a = {octave, key, NULL};
- scanf("%d %d", &octave, &key);
- struct note b = {octave, key, NULL};
- Note diff = note_subtract(&a, &b);
- print_note(diff);
- free(diff);
- return 0;
- }
- // For a note with octave 0, and note 9,
- // `print_note` should print:
- // "0 09\n"
- void print_note(Note n) {
- printf("%d %02d\n", n->octave, n->key);
- }
- //Returns a pointer to a malloced struct containing the difference between a
- //higher and a lower note
- Note note_subtract(Note higher, Note lower) {
- struct note *returnNote;
- returnNote = malloc(sizeof(struct note));
- int octave = higher->octave - lower->octave;
- int key = higher->key - lower->key;
- if (key < 0) {
- octave--;
- key += 12;
- }
- returnNote->octave = octave;
- returnNote->key = key;
- return returnNote;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement