This repository has been archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.c
115 lines (92 loc) · 2.07 KB
/
algo.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SZ 32
struct Item {
unsigned int value;
float weight;
};
static char *binrep (unsigned int val, char *buff, int sz)
{
char *pbuff = buff;
if (sz < 1) return NULL;
if (val == 0) {
*pbuff++ = '0';
*pbuff = '\0';
return buff;
}
pbuff += sz;
*pbuff-- = '\0';
while (val != 0) {
if (sz-- == 0) return NULL;
*pbuff-- = ((val & 1) == 1) ? '1' : '0';
val >>= 1;
}
return pbuff+1;
}
static int init(int numberOfItems) {
int i = 0;
char maxC[numberOfItems+1];
for (i = 0; i < numberOfItems; i++) {
maxC[i] = '1';
}
maxC[numberOfItems] = '\0';
char *start = maxC;
int max = 0;
while (*start)
{
max <<= 1;
if (*start++ == '1') max ^=1;
}
return max;
}
int main(int argc, const char **argv)
{
int i = 0;
int j = 0;
int k = 0;
float maxWeight = 15;
unsigned int maxValue = 0;
unsigned int currentValue = 0;
float currentWeight = 0.0;
float endWeight = 0.0;
char buff[SZ+1];
char *endBinaryCode = malloc(sizeof(char)*(SZ+1));
int max = init(((argc-1)/2));
struct Item items[(argc-1)/2];
if ((argc-1) % 2 == 0 && argc != 1) {
for (i = 1, j = 2, k = 0; i < argc; i+=2, j+=2, k++) {
/* atoi and atof doesn't return Error! */
items[k].value = atoi(argv[i]);
items[k].weight = atof(argv[j]);
}
} else {
fprintf(stderr, "Please specify correct values\n");
}
for (i = 1; i <= max; i++) {
currentValue = 0;
currentWeight = 0.0;
const char *tmp = binrep(i, buff, SZ);
if (tmp == NULL) {
fprintf(stderr, "Not enought space in! See: 93\n");
}
/* strnlen(char *, size_t) would be better... */
int size =(int) strlen(tmp);
for (j = size; j >= 0; j--) {
if (tmp[j] == '1') {
currentValue += items[j].value;
currentWeight += items[j].weight;
}
}
if (currentWeight <= maxWeight) {
maxValue = currentValue;
endWeight = currentWeight;
memcpy(endBinaryCode, tmp, (SZ+1)*sizeof(char));
}
}
printf("MaxValue: %d\n", maxValue);
printf("MaxWeight: %f\n", endWeight);
printf("Endbinarycode: %s\n", endBinaryCode);
free(endBinaryCode);
return 0;
}