int main (int argc, char * * argv) { if (argc != 3) { printf("\nInvalid number of arguments\n"); return EXIT_FAILURE; } char * in_file = argv[1]; char * out_file = argv[2]; int num_b; // number of boxes int num_n; // nubmer of nodes // arr is an array implementation of a binary tree // the root node is the node of index num_n Node * arr = Load_File(in_file, &num_b, &num_n); double x_tot = 0; double y_tot = 0; clock_t pack_t = clock(); Coord * crd = Pack(arr, num_n, num_b, &x_tot, &y_tot); pack_t = clock() - pack_t; printf("\n\nElapsed Time: %le\n\n", ((double) pack_t) / CLOCKS_PER_SEC); Save_File(out_file, arr, crd, num_b); free(arr); free(crd); return EXIT_SUCCESS; }
int main (int argc, char ** argv) { if (argc != 5) { printf("\nInvalid number of arguments"); return EXIT_FAILURE; } //Assign inputs to variables char * mode = argv[1]; char * in_file = argv[2]; char * seq_file = argv[3]; char * out_file = argv[4]; int Size = 0; long * arr; double N_Comp = 0; double N_Move = 0; clock_t sort_t = 0; //keeps track of sort time clock_t io_sum = 0; //keeps track of total I/O time clock_t io = clock(); arr = Load_File(in_file, &Size); //Load the fille and store the contents in arr io_sum = clock() - io; if (*mode == 'i') { sort_t = clock(); Shell_Insertion_Sort(arr, Size, &N_Comp, &N_Move); sort_t = clock() - sort_t; } else if (*mode == 's') { sort_t = clock(); Shell_Selection_Sort(arr, Size, &N_Comp, &N_Move); sort_t = clock() - sort_t; } else { printf("\nInvalid argument for sort type"); return EXIT_FAILURE; } io = clock(); Print_Seq(seq_file, Size); Save_File(out_file, arr, Size); io_sum += clock() - io; free(arr); printf("Number of comparisons: %le\n", N_Comp); printf("Number of moves: %le\n", N_Move); printf("I/O Time: %le\n", ((double)io_sum) / CLOCKS_PER_SEC); printf("Sorting Time: %le\n", ((double)sort_t) / CLOCKS_PER_SEC); return EXIT_SUCCESS; }
int main (int argc, char * * argv) { if (argc != 3) { printf("\nInvalid number of arguments\n"); return EXIT_FAILURE; } char * in_file = argv[1]; char * out_file = argv[2]; // arr is an array implementation of a binary tree // the root node is the node of index num_n Node * head = Load_File(in_file); clock_t pack_t = clock(); Find_Area(head); double x_new = 0; double y_prev = head -> height; Find_Coords(head, &x_new, &y_prev); pack_t = clock() - pack_t; double x = -1; double y = -1; printf("\nPreorder: \n"); Preorder(head); printf("\n\nInorder: \n"); Inorder(head); printf("\n\nPostorder: \n"); Postorder(head, &x, &y); printf("\n\nWidth: %le\nHeight: %le\n", head -> width, head -> height); printf("\nX-coordinate: %le\nY-coordinate:%le\n", x, y); printf("\nElapsed Time: %le\n\n", ((double) pack_t) / CLOCKS_PER_SEC); FILE * f = fopen(out_file, "w"); Save_File(f, head); fclose(f); Tree_Destroy(head); return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { if(argc != 3) { printf("Incorrect arguments\n"); exit(EXIT_FAILURE); } clock_t io_start; clock_t io_end; clock_t io_time; clock_t sort_start; clock_t sort_end; clock_t sort_time; Node* list = NULL; io_start = clock(); list = Load_File(argv[1]); io_end = clock(); io_time = io_end - io_start; if(list == NULL) { exit(EXIT_FAILURE); } sort_start = clock(); Shell_Sort(list); sort_end = clock(); sort_time = sort_end - sort_start; io_start = clock(); Save_File(argv[2], list); io_end = clock(); io_time += (io_end - io_start); double iotime = (double) io_time / CLOCKS_PER_SEC; double sorttime = (double) sort_time / CLOCKS_PER_SEC; // Report printf("I/O time: %le\n", iotime); printf("Sorting time: %le\n", sorttime); return 0; }
int main(int argc, char* argv[]) { if(argc != 6) { printf("Incorrect arguments\n"); exit(EXIT_FAILURE); } char func = argv[2][0]; int size; double ncomp = 0; double nmove = 0; clock_t io_start; clock_t io_end; clock_t io_time; clock_t sort_start; clock_t sort_end; clock_t sort_time; io_start = clock(); long *Array = Load_File(argv[3], &size); io_end = clock(); io_time = io_end - io_start; if(Array == NULL) { exit(EXIT_FAILURE); } if(argv[1][0] == '1'){ io_start = clock(); Print_Seq_1(argv[4], size); io_end = clock(); io_time += (io_end - io_start); if(func == 'i') { sort_start = clock(); Shell_Insertion_Sort_Seq1(Array, size, &ncomp, &nmove); sort_end = clock(); sort_time = sort_end - sort_start; } else if(func == 'b') { sort_start = clock(); Shell_Bubble_Sort_Seq1(Array, size, &ncomp, &nmove); sort_end = clock(); sort_time = sort_end - sort_start; } else { printf("Incorrect arguments\n"); exit(EXIT_FAILURE); } } else if(argv[1][0] == '2') { Print_Seq_2(argv[4], size); if(func == 'i') { sort_start = clock(); Shell_Insertion_Sort_Seq2(Array, size, &ncomp, &nmove); sort_end = clock(); sort_time = sort_end - sort_start; } else if(func == 'b') { sort_start = clock(); Shell_Bubble_Sort_Seq2(Array, size, &ncomp, &nmove); sort_end = clock(); sort_time = sort_end - sort_start; } else { printf("Incorrect arguments\n"); exit(EXIT_FAILURE); } } else { printf("Incorrect arguments\n"); exit(EXIT_FAILURE); } io_start = clock(); if(Save_File(argv[5], Array, size) != 0) { // will free the array exit(EXIT_FAILURE); } io_end = clock(); io_time += (io_end - io_start); double iotime = io_time / CLOCKS_PER_SEC; double sorttime = sort_time / CLOCKS_PER_SEC; // Report printf("Number of comparisons: %le\n", ncomp); printf("Number of moves: %le\n", nmove); printf("I/O time: %le\n", iotime); printf("Sorting time: %le\n", sorttime); return 0; }
int main(int argc, char * argv[]) { // Check to make sure the correct number of input arguments are provided if(argc != 5) { printf("Usage: ./proj1 <sorting_method> <input_file> <sequence_file> <output_file>\n"); return EXIT_FAILURE; } // Set these variables to the input arguments (Make things easier) char method = * argv[1]; char * input_file = argv[2]; char * sequence_file = argv[3]; char * output_file = argv[4]; // Used to store values used in this program int Size; long * values; clock_t t_sort, t_load, t_save, t_seq; // Load the file, keeping track of the time it takes to load the file t_load = clock(); values = Load_File(input_file, &Size); t_load = clock() - t_load; // Check to make sure the files was loaded correctly if(values == NULL) { printf("Error loading the input file\n"); return EXIT_FAILURE; } // Print the sequence to a file, keeping track of the time it takes to write to the file t_seq = clock(); int i = Print_Seq(sequence_file, Size); t_seq = clock() - t_seq; // Check to make sure the sequence was correctly output to the file if(i == 0) { printf("Failed to print sequence\n"); free(values); return EXIT_FAILURE; } // Used to keep track of the number of moves and comparisons in the sorting algorithms double N_Comp = 0.0, N_Move = 0.0; // Check for the sorting method chosen by the user // Keep track of the time it takes to sort the values as well t_sort = clock(); if(method == 'i') { Shell_Insertion_Sort(values, Size, &N_Comp, &N_Move); } else if(method == 's') { Shell_Selection_Sort(values, Size, &N_Comp, &N_Move); } else { printf("Sorting method must be either i (Insertion) or s (Selection) sort\n"); free(values); return EXIT_FAILURE; } t_sort = clock() - t_sort; // Save the sorted array to the output file, keeping track of th time it takes as well t_save = clock(); int returned = Save_File(output_file, values, Size); t_save = clock() - t_save; // Check to make sure that the file was correctly output to if (!returned) { printf("Failed to output values to file\n"); free(values); return EXIT_FAILURE; } else if(returned != Size) { printf("Number of values returned not equal to number of values specified\n"); free(values); return EXIT_FAILURE; } // Free the malloced values free(values); // Display all the required information to the screen Screen_Dump(N_Comp, N_Move, (t_save + t_load + t_seq) / CLOCKS_PER_SEC, t_sort); // SUCCESS!!! return EXIT_SUCCESS; }
/* * Save_List_Of_Files: Function to save a list of files. * - force_saving_files = TRUE => force saving the file even if it wasn't changed * - force_saving_files = FALSE => force saving only the changed files */ static gint Save_List_Of_Files (GList *etfilelist, gboolean force_saving_files) { EtApplicationWindow *window; gint progress_bar_index; gint saving_answer; gint nb_files_to_save; gint nb_files_changed_by_ext_program; gchar *msg; gchar progress_bar_text[30]; GList *l; ET_File *etfile_save_position = NULL; File_Tag *FileTag; File_Name *FileNameNew; double fraction; GAction *action; GVariant *variant; GtkWidget *widget_focused; GtkTreePath *currentPath = NULL; g_return_val_if_fail (ETCore != NULL, FALSE); window = ET_APPLICATION_WINDOW (MainWindow); /* Save the current position in the list */ etfile_save_position = ETCore->ETFileDisplayed; et_application_window_update_et_file_from_ui (window); /* Save widget that has current focus, to give it again the focus after saving */ widget_focused = gtk_window_get_focus(GTK_WINDOW(MainWindow)); /* Count the number of files to save */ /* Count the number of files changed by an external program */ nb_files_to_save = 0; nb_files_changed_by_ext_program = 0; for (l = etfilelist; l != NULL; l = g_list_next (l)) { GFile *file; GFileInfo *fileinfo; const ET_File *ETFile = (ET_File *)l->data; const File_Tag *file_tag = (File_Tag *)ETFile->FileTag->data; const File_Name *FileName = (File_Name *)ETFile->FileNameNew->data; const gchar *filename_cur = ((File_Name *)ETFile->FileNameCur->data)->value; const gchar *filename_cur_utf8 = ((File_Name *)ETFile->FileNameCur->data)->value_utf8; gchar *basename_cur_utf8 = g_path_get_basename(filename_cur_utf8); // Count only the changed files or all files if force_saving_files==TRUE if (force_saving_files || (FileName && FileName->saved == FALSE) || (file_tag && file_tag->saved == FALSE)) nb_files_to_save++; file = g_file_new_for_path (filename_cur); fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, NULL); g_object_unref (file); if (fileinfo) { if (ETFile->FileModificationTime != g_file_info_get_attribute_uint64 (fileinfo, G_FILE_ATTRIBUTE_TIME_MODIFIED)) { nb_files_changed_by_ext_program++; } g_object_unref (fileinfo); } g_free(basename_cur_utf8); } /* Initialize status bar */ et_application_window_progress_set_fraction (window, 0.0); progress_bar_index = 0; g_snprintf(progress_bar_text, 30, "%d/%d", progress_bar_index, nb_files_to_save); et_application_window_progress_set_text (window, progress_bar_text); /* Set to unsensitive all command buttons (except Quit button) */ et_application_window_disable_command_actions (window); et_application_window_browser_set_sensitive (window, FALSE); et_application_window_tag_area_set_sensitive (window, FALSE); et_application_window_file_area_set_sensitive (window, FALSE); /* Show msgbox (if needed) to ask confirmation ('SF' for Save File) */ SF_HideMsgbox_Write_Tag = FALSE; SF_HideMsgbox_Rename_File = FALSE; Main_Stop_Button_Pressed = FALSE; /* Activate the stop button. */ action = g_action_map_lookup_action (G_ACTION_MAP (MainWindow), "stop"); g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE); /* * Check if file was changed by an external program */ if (nb_files_changed_by_ext_program > 0) { // Some files were changed by other program than EasyTAG GtkWidget *msgdialog = NULL; gint response; msgdialog = gtk_message_dialog_new(GTK_WINDOW(MainWindow), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE, ngettext ("A file was changed by an external program", "%d files were changed by an external program", nb_files_changed_by_ext_program), nb_files_changed_by_ext_program); gtk_dialog_add_buttons (GTK_DIALOG (msgdialog), _("_Discard"), GTK_RESPONSE_NO, _("_Save"), GTK_RESPONSE_YES, NULL); gtk_dialog_set_default_response (GTK_DIALOG (msgdialog), GTK_RESPONSE_YES); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msgdialog),"%s",_("Do you want to continue saving the file?")); gtk_window_set_title(GTK_WINDOW(msgdialog),_("Quit")); response = gtk_dialog_run(GTK_DIALOG(msgdialog)); gtk_widget_destroy(msgdialog); switch (response) { case GTK_RESPONSE_YES: break; case GTK_RESPONSE_NO: case GTK_RESPONSE_DELETE_EVENT: /* Skip the following loop. */ Main_Stop_Button_Pressed = TRUE; break; default: g_assert_not_reached (); break; } } for (l = etfilelist; l != NULL && !Main_Stop_Button_Pressed; l = g_list_next (l)) { FileTag = ((ET_File *)l->data)->FileTag->data; FileNameNew = ((ET_File *)l->data)->FileNameNew->data; /* We process only the files changed and not saved, or we force to save all * files if force_saving_files==TRUE */ if ( force_saving_files || FileTag->saved == FALSE || FileNameNew->saved == FALSE ) { /* ET_Display_File_Data_To_UI ((ET_File *)l->data); * Use of 'currentPath' to try to increase speed. Indeed, in many * cases, the next file to select, is the next in the list. */ currentPath = et_application_window_browser_select_file_by_et_file2 (window, (ET_File *)l->data, FALSE, currentPath); fraction = (++progress_bar_index) / (double) nb_files_to_save; et_application_window_progress_set_fraction (window, fraction); g_snprintf(progress_bar_text, 30, "%d/%d", progress_bar_index, nb_files_to_save); et_application_window_progress_set_text (window, progress_bar_text); /* Needed to refresh status bar */ while (gtk_events_pending()) gtk_main_iteration(); // Save tag and rename file saving_answer = Save_File ((ET_File *)l->data, nb_files_to_save > 1 ? TRUE : FALSE, force_saving_files); if (saving_answer == -1) { /* Stop saving files + reinit progress bar */ et_application_window_progress_set_text (window, ""); et_application_window_progress_set_fraction (window, 0.0); et_application_window_status_bar_message (window, _("Saving files was stopped"), TRUE); /* To update state of command buttons */ et_application_window_update_actions (window); et_application_window_browser_set_sensitive (window, TRUE); et_application_window_tag_area_set_sensitive (window, TRUE); et_application_window_file_area_set_sensitive (window, TRUE); if (currentPath) { gtk_tree_path_free (currentPath); } return -1; /* We stop all actions */ } } } if (currentPath) gtk_tree_path_free(currentPath); if (Main_Stop_Button_Pressed) msg = g_strdup (_("Saving files was stopped")); else msg = g_strdup (_("All files have been saved")); Main_Stop_Button_Pressed = FALSE; action = g_action_map_lookup_action (G_ACTION_MAP (MainWindow), "stop"); g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE); /* Return to the saved position in the list */ et_application_window_display_et_file (ET_APPLICATION_WINDOW (MainWindow), etfile_save_position); et_application_window_browser_select_file_by_et_file (ET_APPLICATION_WINDOW (MainWindow), etfile_save_position, TRUE); /* FIXME: Find out why this is a special case for the artist/album mode. */ action = g_action_map_lookup_action (G_ACTION_MAP (MainWindow), "file-artist-view"); variant = g_action_get_state (action); if (strcmp (g_variant_get_string (variant, NULL), "artist") == 0) { et_application_window_browser_toggle_display_mode (window); } g_variant_unref (variant); /* To update state of command buttons */ et_application_window_update_actions (ET_APPLICATION_WINDOW (MainWindow)); et_application_window_browser_set_sensitive (window, TRUE); et_application_window_tag_area_set_sensitive (window, TRUE); et_application_window_file_area_set_sensitive (window, TRUE); /* Give again focus to the first entry, else the focus is passed to another */ gtk_widget_grab_focus(GTK_WIDGET(widget_focused)); et_application_window_progress_set_text (window, ""); et_application_window_progress_set_fraction (window, 0.0); et_application_window_status_bar_message (window, msg, TRUE); g_free(msg); et_application_window_browser_refresh_list (window); return TRUE; }
int main ( int Argc, char **Argv) { // main program - menu for functions with using database double cstart, cend; int Response = 0; int Saved = 0; int Size = 0; double N_Comp = 0; double N_Move = 0; long *Array = NULL; char Filename[MAXFILELEN] = ""; while (1) { printf("\n"); printf("TEST MENU\n"); printf("1. Load array from file\n"); printf("2. Save array to file\n"); printf("3. Shell Sort (Insertion)\n"); printf("4. Improved Bubble Sort\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d",&Response); getchar(); if (Response == 5) // quit program { if (Array != NULL) { printf("Removing and deallocating array\n"); free((void *)Array); Array = NULL; printf("done!\n"); } return (OK); } if (Response == 1) // load file { if (Array != NULL) { printf("\nRemoving and deallocating array\n"); free((void *)Array); Array = NULL; printf("done!\n"); } printf("\nEnter input file (including path): "); scanf("%s", Filename); Array = Load_File (Filename, &Size); if (Size <= 0) { printf("\nError in inputs, file not loaded..\n"); if (Array != NULL) { printf("Removing and deallocating array\n"); free((void *)Array); Array = NULL; printf("done!\n"); } } else { printf("\nLoaded %d long integers\n", Size); } } if (Response == 2) // save file { if (Array == NULL) { printf("\nMust load in data from file (option 1) before saving one!\n"); } else { printf("\nEnter output file (including path): "); scanf("%s", Filename); Saved = Save_File (Filename, Array, Size); if (Saved != Size) { printf("Error in saving! Only %d out of %d long integers saved\n", Saved, Size); } else { printf("Saved all %d long integers\n", Saved); } } } if ( (Response > 2) && (Response < 5)) { if (Array == NULL) { printf("\nMust load in data from file (option 1) before sorting one!\n"); } else { // initialize time function cstart = (double) clock(); // initialize numbers of comparisons and moves N_Comp = 0; N_Move = 0; switch(Response) { case 3: printf("Sorting by Shell Sort (Insertion)\n"); Shell_Insertion_Sort (Array, Size, &N_Comp, &N_Move); break; case 4: printf("Sorting by Improved Bubble Sort\n"); Improved_Bubble_Sort (Array, Size, &N_Comp, &N_Move); break; } // print results: Time, N_Comp, N_Move cend = (double) clock(); printf("\n"); printf(" Elapsed Time (sec): %f\n", (cend - cstart)/CLOCKS_PER_SEC); printf(" # Comparisons: %f\n", N_Comp); printf(" # Moves: %f\n", N_Move); } } } return (OK); } // main()
int main(int argc, char **argv){ if(argc != 6) //check if there are enough input arguments { return EXIT_FAILURE; } int Size;//Size of the array double N_Comp = 0;//number of comparisions double N_Move = 0;//number of moves clock_t S_time = clock();//the sorting time clock_t I_time = clock();//the I/O time clock_t temp; long *Array = Load_File(argv[3], &Size); I_time = clock() - I_time; //calling different function depends on the input if(atoi(argv[1]) == 1 && argv[2][0] == 'i') { S_time = clock(); Shell_Insertion_Sort_Seq1(Array, Size, &N_Comp, &N_Move); S_time = clock() - S_time; temp = I_time; I_time = clock(); if(Print_Seq_1(argv[4], Size) == -1) { return -1; } I_time = clock() - I_time + temp; } else if(atoi(argv[1]) == 1 && argv[2][0] == 'b') { S_time = clock(); Shell_Bubble_Sort_Seq1(Array, Size, &N_Comp, &N_Move); S_time = clock() - S_time; temp = I_time; I_time = clock(); if(Print_Seq_1(argv[4], Size) == -1) { return -1; } I_time = clock() - I_time + temp; } else if(atoi(argv[1]) == 2 && argv[2][0] == 'i') { S_time = clock(); Shell_Insertion_Sort_Seq2(Array, Size, &N_Comp, &N_Move); S_time = clock() - S_time; temp = I_time; I_time = clock(); if(Print_Seq_2(argv[4], Size) == -1) { return -1; } I_time = clock() - I_time + temp; } else if(atoi(argv[1]) == 2 && argv[2][0] == 'b') { S_time = clock(); Shell_Bubble_Sort_Seq2(Array, Size, &N_Comp, &N_Move); S_time = clock() - S_time; temp = I_time; I_time = clock(); if(Print_Seq_2(argv[4], Size) == -1) { return -1; } I_time = clock() - I_time + temp; } temp = I_time; I_time = clock(); if(Save_File(argv[5], Array, Size) == -1) { return -1; } I_time = clock() - I_time + temp; //print all the necessary result printf("\nNumber of comparisons: %le\n", N_Comp); printf("Number of moves: %le\n", N_Move); printf("I/O time: %le\n", ((double)I_time) / CLOCKS_PER_SEC); printf("Sorting time: %le\n\n", ((double)S_time) / CLOCKS_PER_SEC); return 1;//if reaches here, safe return }