aboutsummaryrefslogtreecommitdiffstats
path: root/C++/final_test/ExampleTest.cpp
blob: ccab6e266913d3a3e2d8d7abfc6aab1cf9e99a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct element {
	string name;
	double weight;
	double value;

	double evaluateRatio() {
		return value / weight;
	}

	string formatString() {
		stringstream ss;
		ss << name << " w: " << weight << " v: " << value;
		return ss.str();
	}
};

void sortItems(element*&, int&, int&);
void selectItems(element*&, int&, element*&, int&, double&);
bool inArray(element*&, int&, element&);

int main() {
	ifstream inputFile;
	string fileName;
	int arraySize, count;
	element tmpEl;

	element* elArray;

	count = 0;

	cout << "Enter the name of the input file:" << endl;
	cin >> fileName;

	inputFile.open(fileName.c_str());
	if(!inputFile.is_open()) {
		cout << "Error opening file... Exiting now" << endl;
		exit(EXIT_FAILURE);
	}

	inputFile >> arraySize;

	elArray = new element[arraySize];

	while(inputFile >> tmpEl.name >> tmpEl.weight >> tmpEl.value) {
		elArray[count++] = tmpEl;
	}

	cout << endl << "input:" << endl;
	for(int i = 0; i < count; ++i) {
		cout << elArray[i].formatString() << endl;
	}

	sortItems(elArray, arraySize, count);
	cout << endl << "Sorted input by value / weight ratio:" << endl;
	for(int i = 0; i < count; ++i) {
		cout << elArray[i].formatString() << endl;
	}

	double usrIn = 0;
	element* targetArray = new element[arraySize];
	int targetCount;
	double wght, vlue;
	
	while(usrIn >= 0) {
		wght = 0;
		vlue = 0;
		cout << endl << "Enter target maximum weight (negative to exit): ";
		cin >> usrIn;
		if(usrIn >= 0) {
			selectItems(elArray, count, targetArray, targetCount, usrIn);
			cout << endl << "The best approximation for value and given weight is: " << endl << endl;
			for(int i = 0; i < targetCount; ++i) {
				cout << targetArray[i].formatString() << endl;
				wght += targetArray[i].weight;
				vlue += targetArray[i].value;
			}
			cout << endl << "weight: " << wght << endl << "value: " << vlue << endl;
		}
	}
	return 0;
}

void sortItems(element* &elArr, int &maxSize, int &count) {
	element* tmpArr = new element[maxSize];
	element tmpEl;

	for(int i = 0; i < count; ++i) {
		for(int j = 0; j < count; ++j) {
			if(!inArray(tmpArr, i, elArr[j])) {
				tmpEl = elArr[j];
			}
		}
		for(int j = 0; j < count; ++j) {
			if(!inArray(tmpArr, i, elArr[j]) && elArr[j].evaluateRatio() > tmpEl.evaluateRatio()) {
				tmpEl = elArr[j];
			}
		}
		tmpArr[i] = tmpEl;
	}

	delete[] elArr;
	elArr = tmpArr;
}

void selectItems(element* &elArr, int &count, element* &output, int &outCount, double &tarWeight) {
	outCount = 0;
	for(int i = 0; i < count; ++i) {
		if(elArr[i].weight < tarWeight) {
			output[outCount++] = elArr[i];
			tarWeight -= elArr[i].weight;
		}
	}
}

bool inArray(element* &elArr, int &count, element &el) {
	for(int i = 0; i < count; ++i) {
		if(elArr[i].name == el.name) {
			return true;
		}
	}
	return false;
}