Beispiel #1
0
int
main(int argc, const char * argv[]) {
    namespace po = boost::program_options;
    po::options_description desc("Options");
    desc.add_options()
    ("help,h", "Help Screen")
    ("concurrent", po::value<bool>()->default_value(false), "Use concurrent solver")
    ("files", po::value<std::vector<std::string>>(), "Input Files")
    ("justHeat", po::value<bool>()->default_value(false), "Prevent to print solutions");
    
    po::variables_map variables_map;
    po::positional_options_description files;
    files.add("files", 1);
    
    try {
        po::store(po::command_line_parser(argc, argv).options(desc).positional(files).run(), variables_map);
        po::notify(variables_map);
    } catch (po::error& e) {
        std::cerr << std::endl << e.what() << std::endl;
        return 1;
    }
    
    if (!variables_map.count("files")) {
        std::cerr << "No file specified. Aborting." << std::endl;
        return 1;
    } else if (variables_map.count("files") > 1) {
        std::cerr << "WARN: Multiple files found, solving the first one only." << std::endl;
    }
    
    auto file_paths = variables_map["files"].as<std::vector<std::string>>();
    
    std::cout << "Parsing from ........ " << file_paths[0] << std::endl;
    auto G = init_graph_from_file(file_paths[0]);
    
    std::array<std::vector<int>, 2> solutions;
    
    if (variables_map["concurrent"].as<bool>()) {
        solutions = win(G, win_concurrent);
    } else {
        solutions = win(G, win_improved);
    }
    
    
    
    if (variables_map["justHeat"].as<bool>()) {
        return 0;
    }
    
    std::cout << "\nSolution for Player 0:" << std::endl;
    std::sort(solutions[0].begin(), solutions[0].end());
    std::sort(solutions[1].begin(), solutions[1].end());
    std::cout << "{";
    for (auto &v : solutions[0]) {
        if (v == solutions[0][solutions[0].size()-1]) {
            printf("%d}", v);
        } else {
            printf("%d, ", v);
        }
    }
    printf("\n\nSolution for Player 1:\n{");
    for (auto &v : solutions[1]) {
        if (v == solutions[1][solutions[1].size()-1]) {
            printf("%d}", v);
        } else {
            printf("%d, ", v);
        }
    }
    printf("\n");
    return 0;
}
int main(int argc, char *argv[])
{
    Graph *g;
    int test_num;
    char continue_run;
    char rebuild_graph;
    char *path = (char *)malloc(256 * sizeof(char));
    
    int opt;
    while((opt = getopt(argc, argv, "p:")) > 0) {
        switch(opt) {
            case 'p':
                g = init_graph_from_file(optarg);
                break;
            default:
                break;
        }
    }

    printf("Enter test unit? y or n: ");
    scanf("%c", &continue_run);
    clean_stdin();
    while(continue_run == 'y' || continue_run == 'Y') {
        printf("Need rebuild graph? y or n: ");
        scanf("%c", &rebuild_graph);
        clean_stdin();
        if(rebuild_graph == 'y' || rebuild_graph == 'Y') {
            size_t n = 255;
            int byte_read;
            printf("Input source file path: ");
            byte_read = getline(&path, &n, stdin);
            if(byte_read == -1) {
                printf("ERROR");
            }
            if(path[byte_read-1] == '\n')
                path[byte_read-1] = '\0';
            if(g != NULL)
                destroy(g);
            g = init_graph_from_file(path);
        }

        if(g == NULL) {
            printf("The test graph is NULL\n");
            exit(EXIT_FAILURE);
        }

        printf("Input a number to choose test functions: ");
        scanf("%d", &test_num);
        clean_stdin();
        switch(test_num) {
            case 0:
                print_graph(g);
                break;
            case 1:
                test_dfs(g);
                break;
            case 2:
                test_bfs(g);
                break;
            case 3:
                test_top_sort(g);
                break;
            case 4:
                test_strong_component(g);
                break;
            case 5:
                print_reverse_graph(g);
                break;
            default:
                break;
        }

        printf("Continue test? y or n: ");
        scanf("%c", &continue_run);
        clean_stdin();
    }
    if(path != NULL)
        free(path);
    destroy(g);
    return 0;
}