-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcircular_buffer.c
More file actions
140 lines (104 loc) · 4.1 KB
/
circular_buffer.c
File metadata and controls
140 lines (104 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Name : Charles Dobson
// Date : 26 April 2019
// Description : This program is an implementation of a circular buffer. It
// stores and accesses values in a FIFO manner. It gets user input
// to write new values to the buffer and reads these values by
// printing them to the screen.
// LIBRARIES
#include <stdio.h>
#include <stdlib.h>
// CONSTANTS
#define SIZE_OF_BUFFER 8 // Maximum size of buffer
#define MAX_LENGTH_OF_STRING 81 // Arbitrary number for temporary strings
#define SUCCESS 0
#define FAILURE -1
#define EXIT_LOOP 1
// PROTOTYPES
void displayMainMenu(void);
int getNumber(int* pNumber);
int main(void) {
int circularBuffer[SIZE_OF_BUFFER] = { 0 }; // Empty circular buffer
int optionNumber = 0; // User-input option number
int readIndex = 0; // Index of the read pointer
int writeIndex = 0; // Index of the write pointer
int bufferLength = 0; // Number of values in circular buffer
int loopStatus = 0; // Loop condition variable
while (loopStatus != EXIT_LOOP) {
displayMainMenu();
getNumber(&optionNumber); // Get option number from user
/* ---------------------------- OPTION 1 - WRITE --------------------------- */
if (optionNumber == 1) {
// Check if buffer is full
if (bufferLength == SIZE_OF_BUFFER) {
printf("\n Buffer is full!\n\n ");
getchar();
continue;
}
printf("\n Please input an integer to store in the array\n\n ");
getNumber(&circularBuffer[writeIndex]); // Write number to address of buffer index
bufferLength++; // Increase buffer size after writing
writeIndex++; // Increase writeIndex position to prepare for next write
// If at last index in buffer, set writeIndex back to 0
if (writeIndex == SIZE_OF_BUFFER) {
writeIndex = 0;
}
}
/* ---------------------------- OPTION 2 - READ ---------------------------- */
else if (optionNumber == 2) {
// Check if buffer is empty
if (bufferLength == 0) {
printf("\n Buffer is empty!\n\n ");
getchar();
continue;
}
printf("\n The output value is %d\n\n ", circularBuffer[readIndex]);
getchar();
bufferLength--; // Decrease buffer size after reading
readIndex++; // Increase readIndex position to prepare for next read
// If at last index in buffer, set readIndex back to 0
if (readIndex == SIZE_OF_BUFFER) {
readIndex = 0;
}
}
/* ---------------------------- OPTION 3 - EXIT ---------------------------- */
else if (optionNumber == 3) {
printf("\n Thanks for using the circular buffer!\n\n");
loopStatus = EXIT_LOOP;
continue;
}
/* ----------------------------- INVALID OPTION ---------------------------- */
else {
printf("\n Invalid option number!\n\n ");
getchar();
}
}
}
// Function : displayMainMenu()
// Description : This function clears the screen and then displays the main menu
// for the program.
// Parameters : N/A
// Returns : N/A
void displayMainMenu(void) {
printf("\n======================= CIRCULAR BUFFER ======================\n\n");
printf(" Please choose from the following options:\n\n");
printf(" 1 Input Value\n");
printf(" 2 Output Value\n");
printf(" 3 Exit Program\n");
printf("\n==============================================================\n\n ");
}
// Function : getNumber()
// Description : This function gets a single integer as user input.
// Parameters : int* pNumber: pointer to an int to store the number
// Returns : int SUCCESS: if input was an int
// int FAILURE: if input was not an int
int getNumber(int* pNumber) {
// An array of char to store the string
char userInput[MAX_LENGTH_OF_STRING] = { 0 };
// Get user input
fgets(userInput, MAX_LENGTH_OF_STRING, stdin);
// Parse the user input for an integer and store it in the pNumber parameter
if (sscanf(userInput, "%d", pNumber) != 1) {
return FAILURE;
}
return SUCCESS;
}