Ejemplo n.º 1
0
int main() {
    graph_type graph;

    static const std::size_t vertex_count = 5;
    graph.m_vertices.reserve(vertex_count);
    /*
    C++ -> STL -> Boost -> C++ guru <- C
    */
    
    typedef boost::graph_traits<graph_type>
            ::vertex_descriptor descriptor_t;
    descriptor_t cpp 
        = boost::add_vertex(vertex_t("C++"), graph);
    descriptor_t stl 
        = boost::add_vertex(vertex_t("STL"), graph);
    descriptor_t boost 
        = boost::add_vertex(vertex_t("Boost"), graph);
    descriptor_t guru 
        = boost::add_vertex(vertex_t("C++ guru"), graph);
    descriptor_t ansic 
        = boost::add_vertex(vertex_t("C"), graph);

    BOOST_STATIC_ASSERT((boost::is_same<descriptor_t, std::size_t>::value));

    boost::add_edge(cpp, stl, graph);
    boost::add_edge(stl, boost, graph);
    boost::add_edge(boost, guru, graph);
    boost::add_edge(ansic, guru, graph);

    
    find_and_print(graph, "Boost");
    find_and_print(graph, "C++ guru");
}
Ejemplo n.º 2
0
int main(int argc, char** argv)
{
	if (!strcmp(argv[1], "load")) {
		FILE* file = fopen("trie-data", "r");
		Trie_typ* loaded = Trie_load(file);

		struct termios oldT, newT;
		ioctl(0, TCGETS, &oldT);
		newT = oldT;
		newT.c_lflag &= ~(ECHO | ICANON);
		ioctl(0, TCSETS, &newT);	

		char word[1024];
		int pos = 0;
		while (1) {
			char c;
			read(0, &c, 1);
			if (c == '/') {
				pos = 0;
				memset(word, 0, 1024);
			} else if (c == '?') {
				Trie_destroy(loaded);
				fclose(file);
				return 0;
			} else {
				word[pos++] = c;
				word[pos] = '\0';
				find_and_print(loaded, word);
			}
		}
		return 0;
	}	

	Trie_typ* trie = Trie_init();

	char word[1024];

	while (fgets(word, 1024-1, stdin)) {
		word[strlen(word)-1] = '\0';  // replace the newline
		Trie_add(trie, word);
	}

	if (!strcmp(argv[1], "build")) {
		FILE* stream = fopen("trie-data", "w");
		if (stream) {
			Trie_save(trie, stream);
			fclose(stream);	
		} else {
			printf("Error occurred\n");
		}	
	}

	find_and_print(trie, argv[1]);

	Trie_destroy(trie);

	trie = NULL;

	return 0;
}
Ejemplo n.º 3
0
void main()
{
	char operation;
	int input;
	node * root;
	int exit_flag=0;


	printf ("press i for insertion;\npress d for deletion;\npress l for lookup;\npress p to print tree;\n ");

	while(exit_flag != 1)
	{
		scanf("%c", &operation);
		switch(operation)
		{

		case 'i':
			scanf("%d", &input);
			root = insert(root, input, input);
			//	print_tree(root);
			printf("the number inserted is %d ",input);
		        break;

		case 'd':
			scanf("%d",& input);
			//root = delete(root, input);
			//		print_tree(root);
			printf("the number deleted is %d ",input);
			break;

		case 'l':
			scanf("%d", &input);
			find_and_print(root, input);
			printf("the number lookedup for is %d ",input);
			break;

		case 'p':
			//print_tree(root);
			break;

		case 'e':
			exit_flag = 1;
			break;

		default:
			//printf("there is no such option!!");
			break;
		}

	}

}
Ejemplo n.º 4
0
int main( int argc, char ** argv ) {

	char * input_file;
	FILE * fp;
	node * root;
	int input, range2;
	char instruction;
	char license_part;

	root = NULL;
	verbose_output = false;
/*
	if (argc > 1) {
		order = atoi(argv[1]);
		if (order < MIN_ORDER || order > MAX_ORDER) {
			fprintf(stderr, "Invalid order: %d .\n\n", order);
			usage_3();
			exit(EXIT_FAILURE);
		}
	}

	license_notice();
	usage_1();
	usage_2();
*/
	if (argc > 2) {
		switch (argc[1]) {
			case 's';
				
		}
				
		/*input_file = argv[2];
		fp = fopen(input_file, "r");
		if (fp == NULL) {
			perror("Failure to open input file.");
			exit(EXIT_FAILURE);
		}
		while (!feof(fp)) {
			fscanf(fp, "%d\n", &input);
			root = insert(root, input, input);
		}
		fclose(fp);
		print_tree(root);*/
	}

	printf("> ");
	while (scanf("%c", &instruction) != EOF) {
		switch (instruction) {
		case 'd':
			scanf("%d", &input);
			root = delete_key(root, input);
			print_tree(root);
			break;
		case 'i':
			scanf("%d", &input);
			root = insert(root, input, input);
			print_tree(root);
			break;
		case 'f':
		case 'p':
			scanf("%d", &input);
			find_and_print(root, input, instruction == 'p');
			break;
		case 'r':
			scanf("%d %d", &input, &range2);
			if (input > range2) {
				int tmp = range2;
				range2 = input;
				input = tmp;
			}
			find_and_print_range(root, input, range2, instruction == 'p');
			break;
		case 'l':
			print_leaves(root);
			break;
		case 'q':
			while (getchar() != (int)'\n');
			return EXIT_SUCCESS;
		case 's':
			if (scanf("how %c", &license_part) == 0) {
				usage_2();
				break;
			}
			switch(license_part) {
			case 'w':
				print_license(LICENSE_WARRANTEE);
				break;
			case 'c':
				print_license(LICENSE_CONDITIONS);
				break;
			default:
				usage_2();
				break;
			}
			break;
		case 't':
			print_tree(root);
			break;
		case 'v':
			verbose_output = !verbose_output;
			break;
		case 'x':
			if (root)
				root = destroy_tree(root);
			print_tree(root);
			break;
		default:
			usage_2();
			break;
		}
		while (getchar() != (int)'\n');
		printf("> ");
	}
	printf("\n");

	return EXIT_SUCCESS;
}