Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define MAXLINES 1000
- #define MAXLENGHT 26
- #define PRODUCTS "products.txt"
- void combinations(int n, int k, int b, char* products_names[], float products_prices[], char* combos[], int start, int end, int index);
- void main() {
- int i=0, s, number;
- FILE *fp_products;
- char tmp[MAXLENGHT];
- int K = 3;
- float B = 10.0;
- fp_products = fopen(PRODUCTS, "r");
- if (fp_products == NULL) {
- printf("Error opening on or both files\n");
- exit(1);
- }
- fscanf(fp_products, "%d", &number);
- if (number > MAXLINES || number < 0) {
- printf("Invalid number of lines");
- exit(1);
- }
- char** products_names = malloc(number * sizeof(char *));
- char** combos = malloc(number * sizeof(char *));
- float* products_prices = malloc(number * sizeof(float));
- for(; i<number; i++){
- fscanf(fp_products, "%s %f", tmp, products_prices[i]);
- s = strlen(tmp);
- products_names[i] = malloc(s);
- strncpy(products_names[i], tmp, s);
- }
- combinations(number, K, B, products_names, products_prices, combos, 0, K-1, 0);
- }
- void combinations(int n, int k, int b, char* products_names[], float products_prices[], char* combos[], int start, int end, int index) {
- /*for (int i = 0; i < n; ++i) {
- printf("%s %f\n", &products_names[i], products_prices[i]);
- }*/
- if (index == k) {
- for (int j=0; j<k; j++)
- printf("%s ", combos[j]);
- printf("\n");
- return;
- }
- for (int i=start; i<=end && end-i+1 >= k-index; i++) {
- strcpy(combos[i], products_names[i]);
- printf("Copio %s\n", &products_names[i]);
- combinations(n, k, b, products_names, products_prices, combos, i+1, end, index+1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement