feat: update structure

This commit is contained in:
2024-01-22 14:27:40 +08:00
parent 7836c9185c
commit 3544a28a2e
559 changed files with 120846 additions and 4102 deletions

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include "queue.h"
int main() {
double v;
for(int i = 0; i<= MAX_Q_SIZE; i++) {
v = ((double) i / 10.0);
printf("Adding %3.2f\n", v);
enq(v);
}
for(int i = 0; i<= MAX_Q_SIZE; i++) {
v = deq();
printf("Element %d is %3.2f\n", i, v);
}
}

View File

@@ -0,0 +1,31 @@
#include <stdio.h>
int (*fptr)(int);
int func(int x) {
return 2 * x;
}
int y = 10;
int *(*pfptr)();
int *func2() {
return &y;
}
int main() {
printf("Calling func with value 6: %d\n", func(6));
printf("Now setting fptr to point to func.\n");
fptr = func;
printf("Caling fptr with value 6: %d\n", fptr(6));
printf("\nNow caling func2 which returns the address of global variable y: %p\n", func2());
printf("Pointing pfptr to func2.\n");
pfptr = func2;
printf("Now calling fpfptr: %p\n", pfptr());
}

View File

@@ -0,0 +1,83 @@
#include "queue.h"
#include <stdio.h>
static double _queue[MAX_Q_SIZE];
static int _front = 0, _rear = 0;
void enq(double data) {
if ((_front + 1) % MAX_Q_SIZE == _rear){
printf("Error: Queue is full. Item value %3.2f is not added.\n", data);
}
else {
_queue[_front] = data;
_front = (_front + 1) % MAX_Q_SIZE;
}
}
double deq() {
double val = -1;
if (_rear == _front){
printf("Error: Queue is empty. Nothing to return\n");
}
else {
val = _queue[_rear];
_rear = (_rear + 1) % MAX_Q_SIZE;
}
return val;
}
/* This section is for the function pointers exercise */
static double _res;
void sum(double x) {
_res += x;
}
void prod(double x) {
_res *= x;
}
void clear_sum() {
_res = 0;
}
void clear_prod() {
_res = 1.0;
}
double reduce() {
int ndx = _rear;
clear_sum();
while(ndx != _front) {
sum(_queue[ndx]);
ndx = (ndx + 1) % MAX_Q_SIZE;
}
return _res;
}
/* Implement flex_reduce here:
double flex_reduce(clear, op){
// clear(); // Clear _res to either 0 or 1
// for every element in queue:
//Call op with element.
return _res;
}
*/
double flex_reduce(void (*clear)(), void (*op)(double)) {
int ndx = _rear;
clear();
while(ndx != _front) {
op(_queue[ndx]);
ndx = (ndx + 1) % MAX_Q_SIZE;
}
return _res;
}

View File

@@ -0,0 +1,17 @@
// Maximum number of elements in a queue
//
#define MAX_Q_SIZE 10
// Place function prototypes here.
void enq(double);
double deq();
void sum(double);
void prod(double);
void clear_sum();
void clear_prod();
double reduce();
double flex_reduce(void (*)(), void (*)(double));

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include "queue.h"
int main() {
int i;
for(i=1; i<=9; i++)
{
printf("Enqueing %d\n", i);
enq((double) i);
}
printf("\nCalling reduce result is %3.2f\n", reduce());
/* Uncomment the following two statements to test flex_reduce */
printf("Calling flex reduce with sum. Result is %3.2f\n", flex_reduce(clear_sum, sum));
printf("Calling flex reduce with prod. Result is %3.2f\n", flex_reduce(clear_prod, prod));
}