daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Nov 24th, 2017 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define MAXLINES 1000
  5. #define MAXLENGHT 26
  6. #define PRODUCTS "products.txt"
  7.  
  8. void combinations(int n, int k, int b, char* products_names[], float products_prices[], char* combos[], int start, int end, int index);
  9.  
  10. void main() {
  11.     int i=0, s, number;
  12.     FILE *fp_products;
  13.     char tmp[MAXLENGHT];
  14.  
  15.     int K = 3;
  16.     float B = 10.0;
  17.  
  18.     fp_products = fopen(PRODUCTS, "r");
  19.  
  20.     if (fp_products == NULL) {
  21.         printf("Error opening on or both files\n");
  22.         exit(1);
  23.     }
  24.  
  25.     fscanf(fp_products, "%d", &number);
  26.  
  27.     if (number > MAXLINES || number < 0) {
  28.         printf("Invalid number of lines");
  29.         exit(1);
  30.     }
  31.  
  32.     char** products_names = malloc(number * sizeof(char *));
  33.     char** combos = malloc(number * sizeof(char *));
  34.     float* products_prices = malloc(number * sizeof(float));
  35.  
  36.     for(; i<number; i++){
  37.         fscanf(fp_products, "%s %f", tmp, products_prices[i]);
  38.         s = strlen(tmp);
  39.         products_names[i] = malloc(s);
  40.         strncpy(products_names[i], tmp, s);
  41.     }
  42.  
  43.     combinations(number, K, B, products_names, products_prices, combos, 0, K-1, 0);
  44. }
  45.  
  46. void combinations(int n, int k, int b, char* products_names[], float products_prices[], char* combos[], int start, int end, int index) {
  47.     /*for (int i = 0; i < n; ++i) {
  48.         printf("%s %f\n", &products_names[i], products_prices[i]);
  49.     }*/
  50.     if (index == k) {
  51.         for (int j=0; j<k; j++)
  52.             printf("%s ", combos[j]);
  53.         printf("\n");
  54.         return;
  55.     }
  56.  
  57.     for (int i=start; i<=end && end-i+1 >= k-index; i++) {
  58.         strcpy(combos[i], products_names[i]);
  59.         printf("Copio %s\n", &products_names[i]);
  60.         combinations(n, k, b, products_names, products_prices, combos, i+1, end, index+1);
  61.     }
  62. }
RAW Paste Data
Top