Przeglądaj źródła

began working on the Makefile. Issues referencing the class and I don't know if it's the .h file or the makefile. Began adding basic json capabilities to heapsort

Natalie Pueyo 8 lat temu
rodzic
commit
f03c24f50e

+ 28 - 1
program2/Makefile

@@ -2,17 +2,44 @@ CC=g++
 DEV=-Wall -g -std=c++14
 OPT=-O3 -std=c++14
 
+# explanatioin for compiler options
+# https://web.stanford.edu/class/cs193d/handouts/make.pdf
+
 JSON=json.hpp
 
 CREATE_DATA_SRC=createsortingdata.cxx createheapoperationdata.cxx
 CREATE_DATA_EXE=$(CREATE_DATA_SRC:.cxx=.exe)
 
+TEST_DATA_SRC=testCode.cxx
+TEST_DATA_EXE=$(TEST_DATA_SRC:.cxx=.exe)
+
+PRIORITY_SOURCES=priorityqueue.cpp
+PRIORITY_HEADERS=$(PRIORITY_SOURCES:.cpp=.h)
+PRIORITY_DEV_OBJ=$(PRIORITY_SOURCES:.cpp=.o)
+PRIORITY_OPT_OBJ=$(PRIORITY_SOURCES:.cpp=.o3)
+
 .PHONY: all
-all: $(CREATE_DATA_EXE) 
+all: priority_cla $(CREATE_DATA_EXE) $(TEST_DATA_EXE) 
+
+# Priority Queue
+.PHONY: $(PRIORITY_CLA)
+priority_cla: $(PRIORITY_DEV_OBJ) $(PRIORITY_OPT_OBJ)
 
 $(CREATE_DATA_EXE): %.exe: %.cxx $(JSON)
 	$(CC) $(OPT) $< -o $@
 
+# priority queue objects(?)
+$(PRIORITY_DEV_OBJ): %.o: %.cpp %.h
+	$(CC) $(DEV) -c $< -o $@
+
+$(PRIORITY_OPT_OBJ): %.o3: %.cpp %.h
+	$(CC) $(OPT) -c $< -o $@
+
+# test code executable
+$(TEST_DATA_EXE): %.exe: %.cxx $(PRIORITY_OPT_OBJ)
+	$(CC) $(OPT) $< -o $@
+
+
 # Build
 .PHONY: clean
 clean:

+ 20 - 1
program2/heapsort.cxx

@@ -1,2 +1,21 @@
+#include <cstdio>
+#include <iostream>
+#include <string>
+#include <fstream>
+#include <sstream>
+
+#include "json.hpp"
+
 int main(int argc, char** argv) {
-}
+  // read JSON file and store in jsonObject
+  std::ifstream file;
+  file.open(argv[1]);
+  nlohmann::json jsonObject;
+  // Store the contents filename into jsonObject
+  if (file.is_open()) {
+    file >> jsonObject;
+  }
+
+  std::string sampleName;     // Sample key name to iterate
+  nlohmann::json outputJSON;  // output JSON file
+}

+ 9 - 0
program2/priorityqueue.cpp

@@ -6,6 +6,7 @@
 #include "json.hpp"
 
 // priority queue class using binary heap
+/*
 class PriorityQueue{
   int *heapArray;   // pointer to heap array
   int max_size;     // max size of heap array
@@ -23,6 +24,7 @@ public:
   // other required functions (for now)
   void initiateHeap(int);
 };
+*/
 
 void PriorityQueue::initiateHeap(int capacity){
     heap_size = 0;
@@ -125,3 +127,10 @@ void PriorityQueue::heapifyUp(int key, int index){
     parentIndex = (parentIndex-1)/2;
   }
 }
+
+void PriorityQueue::printArray(){
+  for (int i = 0; i >= heap_size; i++) {
+    std::cout << heapArray[i];
+  }
+  std::cout << std::endl;
+}

+ 35 - 7
program2/priorityqueue.h

@@ -1,12 +1,40 @@
+
+// include guard
+#ifndef PRIORITYQ_H
+#define PRIORITYQ_H
+
 #include "json.hpp"
+class PriorityQueue{
+  int *heapArray;   // pointer to heap array
+  int max_size;     // max size of heap array
+  int heap_size;    // elements in heap
+public:
+  // required functions
+  void insert(int);
+  void removeMax();
+  void removeKey(int);
+  void change(int*, int*);
+  // helpful functions
+  void heapifyUp(int, int);
+  void heapifyDown(int);
+  //nlohmann::json JSON();
+  // other required functions (for now)
+  void initiateHeap(int);
+  void printArray();
+};
+
 
-void insert(int k);
+void insert(int newNode);
 void removeMax();
-void removeKey();
-void change(int*, int*);
+void removeKey(int key);
+void change(int* key, int* newK);
 // helpful functions
-void heapifyUp();
-void heapifyDown();
-nlohmann::json JSON();
+void heapifyUp(int key, int index);
+void heapifyDown(int node);
+//nlohmann::json JSON();
 // other required functions (for now)
-void initiateHeap(int);
+void initiateHeap(int capacity);
+void printArray();
+
+
+#endif

+ 23 - 0
program2/testCode.cxx

@@ -0,0 +1,23 @@
+//testing priority queue
+#include <cstdio>
+#include <iostream>
+#include <string>
+#include <fstream>
+#include <sstream>
+#include "priorityqueue.h"
+
+int main(){
+  class PriorityQueue h;
+  h.initiateHeap(20);
+  h.insert(3);
+  h.insert(2);
+  h.removeKey(1);
+  h.insert(15);
+  h.insert(5);
+  h.insert(4);
+  h.insert(45);
+  h.removeMax();
+  //h.decreaseKey(2, 1);
+  h.printArray();
+  return 0;
+}