示例#1
0
/**************************************************************
 * *  This main function creates a vector of randomly generated integers
 * *  for use in testing the implementations of Algorithms 1-4. The first part measures
 * *  the running time in ms for each Algorithm for display. The second part tests
 * *  input output by reading input from a specified text file, running each Algorithm
 * *  on this input, and writing the results to another specified text file.
 * ***************************************************************/
int main(int argc, char* argv[]){
	std::string filename;
	std::string outputfilename;
	/* The first argument (argc) is the number of elements in the array so we should have two elements the program name and file name 
	Credit: http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html
	*/
    if(argc != 2)
    {
        std::cout << "Please enter an input filename." << std::endl << std::endl;
        exit(1);
    }
	/* which is the second argument (argv). The second argument is always an array of char*, */
    else
    {      
        filename = std::string(argv[1]);
		std::string tempoutputfilename = std::string(argv[1]);
		//http://www.cplusplus.com/reference/string/string/length/
		int strsize = tempoutputfilename.length() - 4;
		/* http://www.cplusplus.com/reference/string/string/operator+/ 
		http://www.cplusplus.com/reference/string/string/substr/ */
		outputfilename = (tempoutputfilename.substr(0, strsize)) + "change.txt";
		std::cout << outputfilename << std::endl;
	}
	//cout << filename << endl;
	/* Stream class provided by C++ to read from files
	Credit: http://www.cplusplus.com/doc/tutorial/files/*/
    std::ifstream textfile;
	/* In order to open a file with a stream object we use its member function open */
    textfile.open(filename);
	/* To check if a file stream was successful opening a file, you can do it by calling to member is_open
	Credit: http://www.cplusplus.com/doc/tutorial/files/*/
    if(!textfile.is_open())
    {
        std::cout << "The file could not be opened." << std::endl;
        textfile.close();
        exit(1);
    }
	/* Call function to put first alternating lines as the coin set input and the second alternating lines as total change V */
    std::vector<std::vector<int>> coinsetinput;
    std::vector<int> changevalueV;
    getinput( textfile, coinsetinput, changevalueV );
    textfile.close();
	/* Stream class to write on files
	Credit: http://www.cplusplus.com/doc/tutorial/files/*/
	std::ofstream textfile2;
    textfile2.open(outputfilename);
    if(!textfile2.is_open())
    {
        std::cout << "Cannot open for writing. Check the permissions of the directory." << std::endl;
        textfile2.close();
        exit(1);
    }
	/* Display a babel for brute force algorithm time trial */
    std::cout << "Testing changeslow...." << std::endl;
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        /* Run brute force algorithm on input numbers from first to last element */
		std::vector<int> coinCount;
		runtimetrial( changeslow, coinsetinput.at(i), changevalueV.at(i), coinCount );
    }
	/* Display a babel for greedy algorithm time trial */
    std::cout << "Testing changegreedy...." << std::endl;
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        /* Run greedy algorithm on input numbers from first to last element */
		std::vector<int> coinCount;
        runtimetrial( changegreedy, coinsetinput.at(i), changevalueV.at(i), coinCount);
    }
    /* Display a babel for dynamic programming algorithm time trial */
    std::cout << "Testing changedp...." << std::endl;
    /*for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
         Run greedy algorithm on input numbers from first to last element 
		
    }*/
	/* Call function to output is to be written to text file */
    textfile2 << "Brute Force \n\n";
	 /* Run changeslow algorithm on input numbers */
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        std::vector<int> coinCount;
        int minCoins = changeslow( coinsetinput.at(i), changevalueV.at(i), coinCount );
        writeResults( textfile2, coinCount, minCoins );
    }
	/* Call function to output is to be written to text file */
    textfile2 << "Greedy\n\n";
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        std::vector<int> coinCount;
        int minCoins = changegreedy(coinsetinput.at(i), changevalueV.at(i), coinCount);
        writeResults( textfile2, coinCount, minCoins );
    }
	/* Call function to output is to be written to text file */
    textfile2 << "Dynamic Programming\n\n";
    //for( unsigned int i = 0; i < coinsetinput.size(); i++ )
   // {
      //  std::vector<int> coinCount;
       // int minCoins = changedp();
        //writeResults( textfile2, coinCount, minCoins );
	//}
    textfile2.close();	
}
示例#2
0
int main(){

	FILE *fp, *fp2;
	char discard;
	char str[100]; //assume each line has less than 100 characters
	int number_lines;
	int i, j;
	int *V = malloc(100 * sizeof(int)); //assume each line has less than 100 numbers
	int *C = malloc(100 * sizeof(int)); //assume each line has less than 100 numbers
	int A;
	int size;
	int minimum;
	fp = fopen ("Coin.txt", "r");
	for (i = 0; i < 100; i++){
		if (feof(fp)) //end of file
			break;
		fgets(str, 100, fp);
	}
	number_lines = i;
	rewind(fp);
	fp2 = fopen ("Coinchange.txt", "w");
	for (i = 0; i < number_lines / 2; i++){
		discard = fgetc(fp); //discard [
		for (j = 0; j < 100; j++){
			fscanf(fp, "%d%c ", &V[j], &discard);
			if (discard != ',') //end of line
				break;
		}
		size = j + 1;
		fscanf(fp, "%d", &A);
		discard = fgetc(fp); //discard newline character

		//start changeslow
		int* array = malloc(100 * sizeof(int));
		array = changeslow(V, size, A);
		int summ = 0;
		for (j = 0; j < size; j++){
			//printf("%d ", array[j]);
			summ += array[j];
		}
		fprintf(fp2, "[");
		for (j = 0; j < size - 1; j++){
			fprintf(fp2, "%d, ", array[j]);
		}
		fprintf(fp2, "%d]\n", array[size - 1]);
		fprintf(fp2, "%d\n", summ);
		//end changeslow
		//start changechangegreedy
		for (j = 0; j < 100; j++){
			C[j] = 0; //initiate to zero
		}
		minimum = changegreedy(V, C, size, A);
		fprintf(fp2, "[");
		for (j = 0; j < size - 1; j++){
			fprintf(fp2, "%d, ", C[j]);
		}
		fprintf(fp2, "%d]\n", C[size - 1]);
		fprintf(fp2, "%d\n", minimum);
		//end changechangegreedy
		//start changedp
		for (j = 0; j < 100; j++){
			C[j] = 0; //initiate to zero
		}
		minimum = changedp(V, C, size, A);
		fprintf(fp2, "[");
		for (j = 0; j < size - 1; j++){
			fprintf(fp2, "%d, ", C[j]);
		}
		fprintf(fp2, "%d]\n", C[size - 1]);
		fprintf(fp2, "%d\n", minimum);
		//end changedp
		fprintf(fp2, "\n");
	}
	fclose(fp);
	fclose(fp2);
	printf("See Coinchange.txt for results.\n");
	return 0;
}