adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
CXX=g++
CPPFLAGS=-Wall -g -std=c++0x
SOURCES=cookies.cc main.cc
OBJECTS=$(SOURCES:.cc=.o)
EXE=app
all: $(EXE)
main.o: cookies.cc cookies.h
cookies.o: cookies.cc cookies.h
$(EXE): $(OBJECTS)
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
clean:
@rm -vf *.o
@rm -rvf *.dSYM
@rm -vf app
run: $(EXE)
./$(EXE)
debug: $(EXE)
gdb $(EXE)
valgrind: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
autov: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE) < test.txt
+92
View File
@@ -0,0 +1,92 @@
#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];
}
+73
View File
@@ -0,0 +1,73 @@
#ifndef __COOKIES_H__
#define __COOKIES_H__
#include <string>
using namespace std;
const int MAXIMUM_SIZE = 10;
/*
addOrder
Adds the memory address of a new string to the given array. The new string should have the
same data as that of the given string.
A string may already exist containing the same data as the given string (ie, the string
passed to this function as a parameter) and whose memory address is already stored within
the given array. In this situation, add to the array the memory address of the string that
already exists rather than creating a new string.
Update the reference of size appropriately.
*/
void addOrder(string * cookies[], int * size, string new_name);
/*
deliverOrder
Removes all references to strings containing the same data as the given string and reports
the number of string references removed. The overall order of the list is unchanged.
Update the reference of size appropriately.
*/
int deliverOrder(string * cookies[], int * size, string name);
/*
modifyOrder
Searches the given array for a memory address to a string that contains the same data as the
first given string.
If found, the data of the found string is changed to match the data of the second given string,
and the function returns true. If not found, the function returns false.
*/
bool modifyOrder(string * cookies[], int * size, string original_name, string new_name);
/*
displayOrders
Returns a string containing a list of names referred to in the given array.
Each index in the array represents a particular box of cookies, and there may be more than
one box for each customer. Therefore, this function's output may include the same name
multiple times.
*/
string displayOrders(string * cookies[], int * size);
/*
findOrder
Returns a memory address found in the given array that references a string with the same data
as the given string. Returns null if no such string is found.
It is recommended that you use this function within your other functions. If you do not, you
must still complete this function as described above.
*/
string * findOrder(string * cookies[], int * size, string name);
void clean_up(string * cookies[], int * size);
#endif
+130
View File
@@ -0,0 +1,130 @@
#include <iostream>
#include <string>
using namespace std;
#include "cookies.h"
int getOption(int range);
string getName(string message);
int main()
{
string * cookies [MAXIMUM_SIZE] = {};
int x = 0;
int * size = &(x);
bool done = false;
while(!done)
{
//Get menu option
cout << "MENU:" << endl;
cout << "\t1. Add an order\n\t2. Deliver an order\n\t3. Modify an order\n\t4. Quit\n" << endl;
int option = getOption(4);
//Adding
if(option==1)
{
if((*size) >= MAXIMUM_SIZE)
{
cout << "The car is full; cannot add more orders" << endl;
}
else
{
string new_name = getName("Please enter the customer's name for the new order:");
addOrder(cookies, size, new_name);
cout << "Cookies added for costumer [" << new_name << "]" << endl;
}
}
//Delivering
else if(option==2)
{
string name = getName("Please enter the customer's name for the delivery:");
int delivered = deliverOrder(cookies, size, name);
cout << "Delivered " << delivered << " boxes of cookies to [" << name << "]" << endl;
}
//Modifying
else if(option==3)
{
string original_name = getName("Please enter the original customer's name of the order:");
string new_name = getName("Please enter the new customer's name for the order:");
bool changed = modifyOrder(cookies,size,original_name,new_name);
if(changed)
{
cout << "Changed name from [" << original_name << "] to [" << new_name << "]" << endl;
}
else
{
cout << "Could not find a customer with the name: [" << original_name << "]" << endl;
}
}
//Quitting
else if(option==4)
{
done = true;
}
cout << displayOrders(cookies, size) << endl;
}
clean_up(cookies, size);
cout << "Thank you for using the cookie tracker!" << endl;
//The following line may not work on all systems; therefore, you may change this line as needed
return 0;
}
int getOption(int range)
{
int input = 0;
bool done = false;
while(!done)
{
cout << "Please select an option:" << endl;
input = 0;
cin >> input;
cin.ignore(1000,'\n');
if(cin.fail())
{
cin.clear();
cin.ignore(1000,'\n');
cout << "Error: Invalid option" << endl;
}
else if(input < 1 || input > range)
{
cout << "Error: Invalid option number" << endl;
}
else
{
done = true;
}
}
return input;
}
string getName(string message)
{
string input = "";
bool done = false;
while(!done)
{
cout << message << endl;
input = "";
getline(cin, input);
if(cin.fail())
{
cin.clear();
cin.ignore(1000,'\n');
cout << "Error: Invalid name" << endl;
}
else
{
done = true;
}
}
return input;
}
+17
View File
@@ -0,0 +1,17 @@
1
stephen
1
michael
1
michael
1
stephen
1
derek
3
stephen
Stephen Mardson McQuay
2
michael
2
derek