jweinst1
11/12/2019 - 7:47 PM

iterating through buy and sell orders in C

iterating through buy and sell orders in C

#include <stdio.h>
#include <stdlib.h>

typedef struct {
   int cid;
   unsigned amount;
} Order;

typedef struct {
  Order* selling;
  Order* cur_selling;
  size_t selling_count;
  Order* buying;
  Order* cur_buying;
  size_t buying_count;
} Orders;

int Orders_process(Orders* orders)
{
  const Order* selling_end = orders->selling + orders->selling_count;
  const Order* buying_end = orders->buying + orders->buying_count;
  if(orders->cur_selling == selling_end)
      return 0;
  if(orders->cur_buying == buying_end)
    return 0;

  if(orders->cur_selling->amount > orders->cur_buying->amount) {
    // Process customer transaction
    orders->cur_buying++;
  } else if(orders->cur_selling->amount < orders->cur_buying->amount) {
    // process customer transaction.
    orders->cur_selling++;
  } else if(orders->cur_selling->amount == orders->cur_buying->amount) {
    // process customer transaction;
    orders->cur_buying++;
    orders->cur_selling++;
  }
  return 1;
}

int main(void) {
  printf("Hello World\n");
  return 0;
}