93 lines
2.3 KiB
C++
93 lines
2.3 KiB
C++
#include<string>
|
|
#include <iostream>
|
|
|
|
#include "cookies.h"
|
|
|
|
void addOrder(string * cookies[], int * size, string new_name) {
|
|
string * s = findOrder(cookies, size, new_name);
|
|
if(s == NULL)
|
|
s = new string(new_name);
|
|
cookies[*size] = s;
|
|
(*size)++;
|
|
}
|
|
|
|
int deliverOrder(string * cookies[], int * size, string name) {
|
|
string * s = findOrder(cookies, size, name);
|
|
if(s == NULL)
|
|
return 0;
|
|
|
|
// interesting note, temp_cookies gets allocatied on the stack and (see
|
|
// note above return) ...
|
|
string * temp_cookies [MAXIMUM_SIZE] = {};
|
|
|
|
int removes = 0;
|
|
int temp_pos = 0;
|
|
for(int i = 0; i < *size; i++) {
|
|
if(cookies[i] == s) {
|
|
// pass
|
|
removes += 1;
|
|
}
|
|
else {
|
|
temp_cookies[temp_pos] = cookies[i];
|
|
temp_pos++;
|
|
}
|
|
}
|
|
|
|
// copy from the tmp buffer into the original passed-by-pointer array
|
|
for(int i = 0; i < temp_pos; i++) {
|
|
cookies[i] = temp_cookies[i];
|
|
}
|
|
|
|
*size = temp_pos;
|
|
|
|
// this removes the memory newed above (in addOrder) from the HEAP
|
|
delete s;
|
|
|
|
// ... temp_cookies (along with removes and temp_pos) gets cleaned up at
|
|
// this point ... Stack stack stack
|
|
return removes;
|
|
}
|
|
|
|
bool modifyOrder(string * cookies[], int * size, string original_name, string new_name) {
|
|
string * s = findOrder(cookies, size, original_name);
|
|
if(s == NULL)
|
|
return false;
|
|
|
|
*s = new_name;
|
|
return true;
|
|
}
|
|
|
|
string displayOrders(string * cookies[], int * size) {
|
|
string r;
|
|
for(int i=0; i < *size; i++) {
|
|
r += *cookies[i];
|
|
if(i < *size - 1)
|
|
r += ", ";
|
|
}
|
|
return r;
|
|
}
|
|
|
|
string * findOrder(string * cookies[], int * size, string name) {
|
|
string * r = NULL;
|
|
for(int i=0; i<*size; i++) {
|
|
if(*cookies[i] == name) {
|
|
r = cookies[i];
|
|
break;
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
void clean_up(string * cookies[], int * size) {
|
|
string * deletable_cookies [MAXIMUM_SIZE] = {};
|
|
int del_size = 0;
|
|
for(int i = 0; i < *size; i++) {
|
|
if(!findOrder(deletable_cookies, &del_size, *cookies[i])) {
|
|
deletable_cookies[del_size] = cookies[i];
|
|
del_size++;
|
|
}
|
|
}
|
|
for(int i = 0; i < del_size; i++)
|
|
delete deletable_cookies[i];
|
|
}
|