vector<int> countSmaller(vector<int>& nums) { vector<node>clone; for ( int i = 0 ; i < nums.size(); i++) { clone.push_back(node(nums[i], i)); } vector<node>sort_nums(nums.size(), node(0,0)); merge_sort(clone, 0, clone.size() - 1, sort_nums); vector<int>result(nums.size(), 0); for ( int i = 0; i < nums.size(); i++) { result[clone[i].index] = clone[i].count; } return result; }
int main( int argc, char *argv[] ) { bool print_usage_and_exit = false; bool generate = false; long number_count = 100; bool has_number_count = false; int number_min = 1; bool has_number_min = false; int number_max = 255; bool has_number_max = false; int seed = 0; bool has_seed = false; const char* ifile_name = NULL; const char* ofile_name = NULL; const char* cfile_name = NULL; ///overallocate the numbers a bit, just in case int* number_array = malloc(sizeof(int)*argc); size_t number_array_size = 0; if (!number_array) { printf("error allocating, somethings wrong\n"); exit(-1); } for (int i = 1; i < argc; ++i) { const char* argi = argv[i]; //if (atoi(argi) != 0 || arg_is_zero(argi)) if (atoi(argi) != 0 || arg_is_zero(argi)) { ///this was a number input number_array[number_array_size] = atoi(argi); number_array_size++; continue; } char dash = argi[0]; if (strlen(argi) != 2 || dash != '-') { print_usage(); return -1; } char flag = argi[1]; if (flag == 'u') { if (print_usage_and_exit) { ///Example: ./Lab1 -u -u print_usage(); return -1; } print_usage_and_exit = true; continue; } if (flag == 'g') { if (generate || number_array_size > 0) { ///Example: ./Lab1 -g -g ///Example: ./Lab1 -g 5 5 print_usage(); return -1; } generate = true; continue; } if (!(i + 1 < argc)) { ///Example: ./Lab1 -m print_usage(); return -1; } if (flag == 'n') { if (has_number_count) { ///Example: ./Lab1 -n <number> -n <number> print_usage(); return -1; //continue; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_number_count = true; number_count = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (number_count == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -n NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 'm') { if (has_number_min) { ///Example: ./Lab1 -m <min> -m <min> print_usage(); return -1; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_number_min = true; number_min = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (number_min == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -m NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 'M') { if (has_number_max) { ///Example: ./Lab1 -M <max> -M <max> print_usage(); return -1; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_number_max = true; number_max = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (number_max == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -M NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 's') { if (has_seed) { ///Example: ./Lab1 -s <seed> -s <seed> print_usage(); return -1; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_seed = true; seed = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (seed == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -s NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 'i') { if (ifile_name != NULL) { ///Example: ./Lab1 -i INPUTFILE -i INPUTFILE print_usage(); return -1; } ifile_name = argv[i+1]; i++; continue; } if (flag == 'o') { if (ofile_name != NULL) { ///Example: ./Lab1 -o OUTPUTFILE -o OUTPUTFILE print_usage(); return -1; } ofile_name = argv[i+1]; i++; continue; } if (flag == 'c') { if (cfile_name != NULL) { ///Example: ./Lab1 -c OUTPUTFILE -c OUTPUTFILE print_usage(); return -1; } cfile_name = argv[i+1]; i++; continue; } } if (print_usage_and_exit) { print_usage(); exit(0); } if (ifile_name) { free(number_array); number_array = get_input_from_file(ifile_name, &number_array_size); } FILE* ofile = NULL; if (ofile_name) { ofile = fopen(ofile_name, "w"); if (!ofile) { printf("Cannot open %s for writing\n", ofile_name); exit(-1); } } FILE* cfile = NULL; if (cfile_name) { cfile = fopen(cfile_name, "w"); if (!cfile) { printf("Cannot open %s for writing\n", cfile_name); exit(-1); } } if (!generate && number_array_size == 0) { fprintf(stdout, "Enter the number count\n"); fflush(stdout); free(number_array); int ret = fscanf(stdin, "%zi", &number_array_size); fgetc(stdin); if (ret == EOF) { fprintf(stdout, "Error reading from command line\n"); exit(-1); } number_array = (int*)malloc(sizeof(int)*number_array_size); for (int i = 0; i < number_array_size; ++i) { int value; int ret = fscanf(stdin, "%i", &value); if (ret == EOF) { fprintf(stdout, "Error reading from command line\n"); exit(-1); } fgetc(stdin); number_array[i] = value; } } sort_nums(number_array, number_array_size); if (generate) { if(has_number_count) { if(has_number_max && has_number_min && !(has_seed) && !(ofile)) //3 min_num_Max(number_count, number_min, number_max); if(has_number_max && has_number_min && has_seed && ofile) // 1 min_num_Max_seed(number_count, number_min, number_max, seed, ofile); if(has_number_max && has_number_min && has_seed && !(ofile)) // 2 //7 min_num_Max_seed_Nf(number_count, number_min, number_max, seed); else if(has_number_max && !(has_number_min) && !(has_seed) && !(ofile)) // 15 max_num(number_count,number_max); else if (has_number_min &&!(has_number_max) && !(has_seed) && !(ofile)) //4 min_num(number_count,number_min); else if(has_number_max && !(has_number_min) && (has_seed)&& !(ofile)) //12 max_num_seed(number_count,number_max,seed); else if (has_number_min &&!(has_number_max) && (has_seed) && !(ofile) ) //5 min_num_seed(number_count,number_min,seed); else if (has_number_min &&!(has_number_max) && !(has_seed) && (ofile) ) //6 min_num_of(number_count,number_min,ofile); else if (has_number_min && (has_number_max) && !(has_seed) && (ofile) ) //8 // I have problem here min_max_num_of(number_count,number_min,number_max,ofile); else if (!(has_number_min) && (has_number_max) && !(has_seed) && (ofile) ) //9 // I am heve problem max_num_of(number_count,number_max,ofile); else if (!(has_number_min) && !(has_number_max) && (has_seed) && (ofile) ) //10 // completed num_seed_of(number_count,seed,ofile); else if (!(has_number_min) && !(has_number_max) && !(has_seed) && (ofile) ) //11 // result in negative num_of(number_count,ofile); else if(has_seed && !(has_number_min) && !(has_number_max) && !(ofile)) //14 // good random_case_generator_seed(number_count,seed); else if (!(has_number_min) && (has_number_max) && (has_seed) && (ofile) ) //13 // good max_num_of_seed(number_count,number_max,seed,ofile); if(has_number_count && !(has_seed) && !(has_number_max) && !(has_number_min) && !(ofile)) //16 random_num_generator(number_count); if (has_number_count) { goto here; } } if(!(has_number_count)) { if(has_number_max && has_number_min && !(has_seed) && !(ofile)) //3 /////////////// Done min_num_Max_nd(number_min, number_max); if(has_number_max && has_number_min && has_seed && ofile) // 1 /////////////////Done min_num_Max_seed_nd( number_min, number_max, seed, ofile); //done if(has_number_max && has_number_min && has_seed && !(ofile)) // 2 //7 ////////////// Done min_num_Max_seed_Nf_nd( number_min, number_max, seed); else if(has_number_max && !(has_number_min) && !(has_seed) && !(ofile)) // 15 /////////// Done max_num_nd(number_max); else if (has_number_min &&!(has_number_max) && !(has_seed) && !(ofile)) //4 //////////// Done min_num_nd(number_min); else if(has_number_max && !(has_number_min) && (has_seed)&& !(ofile)) //12 /////////// Done max_num_seed_nd(number_max,seed); else if (has_number_min &&!(has_number_max) && (has_seed) && !(ofile) ) //5 /////////// Done min_num_seed_nd(number_min,seed); else if (has_number_min &&!(has_number_max) && !(has_seed) && (ofile) ) //6 //////////// Done min_num_of_nd(number_min,ofile); else if (has_number_min && (has_number_max) && !(has_seed) && (ofile) ) //8 //////////// Done min_max_num_of_nd(number_min,number_max,ofile); else if (!(has_number_min) && (has_number_max) && !(has_seed) && (ofile) ) //9 //////////// Done max_num_of_nd(number_max,ofile); else if (!(has_number_min) && !(has_number_max) && (has_seed) && (ofile) ) //10 //////////// Done num_seed_of_nd(seed,ofile); else if (!(has_number_min) && !(has_number_max) && !(has_seed) && (ofile) ) //11 //////////// Problem num_of_nd(ofile); else if(has_seed && !(has_number_min) && !(has_number_max) && !(ofile)) //14 // good //////// Done random_case_generator_seed_nd(seed); else if (!(has_number_min) && (has_number_max) && (has_seed) && (ofile) ) //13 // good /////// Done max_num_of_seed_nd(number_max,seed,ofile); if (!(has_number_count) && !(has_seed) && !(has_number_max) && !(has_number_min) && !(ofile)) //// Done { random_case_generator(); } } here: printf(""); } else { ///get input from user const char* userid = getenv("USER"); size_t useridlen = strlen(userid); Begin_clock(); for (int i = 0; i < useridlen; ++i) { char c = userid[i]; int freq = calculate_frequency(c, number_array, number_array_size); if (cfile) fprintf(cfile, "%c %d\t%d\n", c, (int)c, freq); if (!cfile) fprintf(stdout, "%c %d\t%d\n", c, (int)c, freq); } if (ofile) { for (int i = 0; i < number_array_size; ++i) { fprintf(ofile, "%d\n", number_array[i]); } fprintf(ofile, "\n"); } if (!ofile) { for (int i = 0; i < number_array_size; ++i) { printf("\n"); fprintf(stdout, "%d\t", number_array[i]); } printf("\n"); } Eng_clock(); Calculate_Time(); } return 0; }