./src/code.h
#ifndef __CODE_H__
#define __CODE_H__
struct BT {
int x;
BT* l;
BT* r;
};
int foo(int);
void bubbleSort(int*, int);
bool insert(BT*, int);
int nodesBetween(BT*, int, int);
#endif
./src/code.cpp
#include
#include "code.h"
using std::queue;
int foo(int param) {
if (param) {
return 1;
} else {
return 0;
}
}
void bubbleSort(int* data, int size) {
if(size <= 1) {
return;
}
for(int i=0; i<size-1; i++) {
int dataTmp = data[j];
data[j] = data[j+1];
data[j+1] = dataTmp;
}
}
}
}
bool insert(BT* root, int value) {
BT* node = root;
while(1) {
if(value < node->x) {
if(node->l != nullptr) {
node = node->l;
} else {
node->l = new BT;
node = node->l;
node->x = value;
node->l = nullptr;
node->r = nullptr;
return true;
}
} else if(value > node->x) {
if(node->r != nullptr) {
node = node->r;
} else {
node->r = new BT;
node = node->r;
node->x = value;
node->l = nullptr;
node->r = nullptr;
return true;
}
} else {
return false;
}
}
}
int nodesBetween(BT* root, int min, int max) {
int nodeNum = 0;
queue
nodes.push(root);
while(nodes.size()) {
BT* node = nodes.front();
nodes.pop();
if(node->x > min && node->x < max) {
nodeNum++;
if(node->l != nullptr) {
nodes.push(node->l);
}
if(node->r != nullptr) {
nodes.push(node->r);
}
} else if(node->x <= min) {
if(node->x == min) {
nodeNum++;
}
if(node->r != nullptr) {
nodes.push(node->r);
}
} else if(node->x >= max) {
if(node->x == max) {
nodeNum++;
}
if(node->l != nullptr) {
nodes.push(node->l);
}
}
}
return nodeNum;
}
./src/main.cpp
#include
#include "code.h"
#include
using std::queue;
using std::cout;
using std::endl;
int main(int argc, char* argv[]) {
foo(0);
int data[] = {1, 2, 7, 3, 8, 4, 5, 8, 1, 0, 9, 2};
int size = sizeof(data)/sizeof(int);
bubbleSort(data, size);
for(int i=0; i
}
cout << endl;
BT* root;
root = new BT;
root->x = 29;
root->l = nullptr;
root->r = nullptr;
insert(root, 18);
insert(root, 37);
insert(root, 12);
insert(root, 19);
insert(root, 30);
insert(root, 41);
queue
nodes.push(root);
while(nodes.size()) {
BT* node = nodes.front();
nodes.pop();
cout << node->x << " ";
if(node->l != nullptr) {
nodes.push(node->l);
}
if(node->r != nullptr) {
nodes.push(node->r);
}
}
cout << endl;
int nodeNum = nodesBetween(root, 12, 19);
cout << nodeNum << endl;
return 0;
}
./Makefile
SRCDIR = src
SIMDIR = sim
SOURCE = code.cpp main.cpp
PROGRAM = $(SIMDIR)/code.out
COMPFILES = $(patsubst %.cpp,%.o,$(SOURCE))
INCDIR = ./$(SRC)
COMPARG = -std=c++11
SOURCEWITHPATH = $(addprefix src/,$(SOURCE))
all: clean coverageHtmlGen
echo $(COMPFILES)
clean:
rm -rf $(SIMDIR) *.gcno *.out *.gcda *.o coverage.info coverage_html
%.o: $(SRCDIR)/%.cpp
g++ --coverage -I$(INCDIR) $(COMPARG) -c $< -o $@
$(PROGRAM): $(COMPFILES)
if [ ! -e $(SIMDIR) ]; then mkdir $(SIMDIR); fi
g++ --coverage $? -o $@
run: $(PROGRAM)
./$(PROGRAM)
coverageGen: run
gcov $(SOURCEWITHPATH)
coverageHtmlGen: coverageGen
lcov --capture --directory ./ --output-file coverage.info
genhtml coverage.info --output-directory coverage_html
No comments:
Post a Comment