#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include "lib.h" void findBestItems(int index, int currentValue, int currentWeight, int *itemWeights, int *itemValues, int maxCapacity, int totalItems, int &highestValue, int *optimalItems, int **bestItemsList, int &bestItemCount, int *currentItems) { if (index == totalItems) { if (currentValue > highestValue) { highestValue = currentValue; bestItemCount = 1; std::memcpy(optimalItems, currentItems, sizeof(int) * totalItems); std::memcpy(bestItemsList[0], currentItems, sizeof(int) * totalItems); } else if (currentValue == highestValue) { std::memcpy(bestItemsList[bestItemCount], currentItems, sizeof(int) * totalItems); bestItemCount++; } return; } if (currentWeight + itemWeights[index] <= maxCapacity) { currentItems[index] = index + 1; findBestItems(index + 1, currentValue + itemValues[index], currentWeight + itemWeights[index], itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); } currentItems[index] = 0; findBestItems(index + 1, currentValue, currentWeight, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); } Solution solveKnapsack(char *inputFile) { FILE *inputStream = std::fopen(inputFile, "r"); if (inputStream == NULL) { std::cout << "Error open file\n"; std::exit(1); } int maxCapacity; std::fscanf(inputStream, "%d", &maxCapacity); char inputLine[256]; std::fgets(inputLine, sizeof(inputLine), inputStream); std::fgets(inputLine, sizeof(inputLine), inputStream); char *item = std::strtok(inputLine, ","); int totalItems = 0; int itemValues[256]; while (item != NULL) { itemValues[totalItems] = std::atoi(item); item = std::strtok(NULL, ","); totalItems++; } std::fgets(inputLine, sizeof(inputLine), inputStream); item = std::strtok(inputLine, ","); int itemWeights[256]; for (int i = 0; i < totalItems; i++) { itemWeights[i] = std::atoi(item); item = std::strtok(NULL, ","); } std::fclose(inputStream); int highestValue = 0; int optimalItems[256]; int **bestItemsList = new int *[256]; for (int i = 0; i < 256; i++) { bestItemsList[i] = new int[totalItems]; } int bestItemCount = 0; int currentItems[256]; findBestItems(0, 0, 0, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); Solution solution = {highestValue, bestItemsList, optimalItems, totalItems, bestItemCount}; return solution; }请帮我把这个cpp文件转换为c文件
时间: 2023-06-26 17:10:10 浏览: 99
以下是将给定的CPP文件转换为C文件的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib.h"
void findBestItems(int index, int currentValue, int currentWeight, int *itemWeights, int *itemValues, int maxCapacity, int totalItems, int *highestValue, int *optimalItems, int **bestItemsList, int *bestItemCount, int *currentItems) {
if (index == totalItems) {
if (currentValue > *highestValue) {
*highestValue = currentValue;
*bestItemCount = 1;
memcpy(optimalItems, currentItems, sizeof(int) * totalItems);
memcpy(bestItemsList[0], currentItems, sizeof(int) * totalItems);
} else if (currentValue == *highestValue) {
memcpy(bestItemsList[*bestItemCount], currentItems, sizeof(int) * totalItems);
(*bestItemCount)++;
}
return;
}
if (currentWeight + itemWeights[index] <= maxCapacity) {
currentItems[index] = index + 1;
findBestItems(index + 1, currentValue + itemValues[index], currentWeight + itemWeights[index], itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems);
}
currentItems[index] = 0;
findBestItems(index + 1, currentValue, currentWeight, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems);
}
Solution solveKnapsack(char *inputFile) {
FILE *inputStream = fopen(inputFile, "r");
if (inputStream == NULL) {
printf("Error open file\n");
exit(1);
}
int maxCapacity;
fscanf(inputStream, "%d", &maxCapacity);
char inputLine[256];
fgets(inputLine, sizeof(inputLine), inputStream);
fgets(inputLine, sizeof(inputLine), inputStream);
char *item = strtok(inputLine, ",");
int totalItems = 0;
int itemValues[256];
while (item != NULL) {
itemValues[totalItems] = atoi(item);
item = strtok(NULL, ",");
totalItems++;
}
fgets(inputLine, sizeof(inputLine), inputStream);
item = strtok(inputLine, ",");
int itemWeights[256];
for (int i = 0; i < totalItems; i++) {
itemWeights[i] = atoi(item);
item = strtok(NULL, ",");
}
fclose(inputStream);
int highestValue = 0;
int optimalItems[256];
int **bestItemsList = (int **)malloc(sizeof(int *) * 256);
for (int i = 0; i < 256; i++) {
bestItemsList[i] = (int *)malloc(sizeof(int) * totalItems);
}
int bestItemCount = 0;
int currentItems[256];
findBestItems(0, 0, 0, itemWeights, itemValues, maxCapacity, totalItems, &highestValue, optimalItems, bestItemsList, &bestItemCount, currentItems);
Solution solution = {highestValue, bestItemsList, optimalItems, totalItems, bestItemCount};
return solution;
}
```
阅读全文