Exemple #1
0
void addWordsTable(FILE * f, RESIZABLE_TABLE * rt) {
    char c;
    char * charBuf = (char *)malloc(sizeof(char) * 2);
    char * buf = (char *)malloc(MAXLINE);
    void * val;
    long num;
    while ((c = fgetc(f)) != EOF) {
        //charBuf[0] = c;
        charBuf[1] = '\0';
        if ((c > 64) && (c < 91)) {
            charBuf[0] = c + 32;
            strcat(buf, charBuf);
        }
        else if ((c > 96) && (c < 123)) {
            charBuf[0] = c;
            strcat(buf, charBuf);
        }
        else {// ((c == 32) || (c == 10)) {
            if (strcmp(buf, "") == 0) {
                continue;
            }
            else {
                val = rtable_lookup(rt, buf);
                if (val == NULL) {
                    rtable_add_int(rt, buf, 1);
                } else {
                    num = (long)val;
                    num++;
                    rtable_add_int(rt, buf, num);
                }
            }
            memset(buf, 0, strlen(buf));
        }
    }
}
void test14() {
	RESIZABLE_TABLE *rt;
	long grade;
	int result;

	rt = rtable_create();

	rtable_print_int(rt);

	printf("Add George\n");
	result = rtable_add_int(rt, "George", 89);

	rtable_print_int(rt);

	printf("Add Peter\n");
	result = rtable_add_int(rt, "Peter", 97);
	assert(result==0);

	rtable_print_int(rt);

	printf("Add George again\n");
	result = rtable_add_int(rt, "George", 91);

	rtable_print_int(rt);

	grade =(long)  rtable_lookup(rt, "Peter");
	printf("Peter's grade is: %ld\n", grade);

	grade = (long) rtable_lookup(rt, "George");
	printf("George's grade is: %ld\n", grade);
}
void test15() {
	char name[20];
	long grade;
	int i = 0;
	int result;
	RESIZABLE_TABLE *rt;
	RESIZABLE_TABLE *rt2;

	rt = rtable_create();
	rtable_print_int(rt);

	for (i=0; i < 30; i++) {
		printf("Adding %d\n", i);
		sprintf(name,"name%d", i);
		grade = 70 + i;
		result = rtable_add_int(rt, name, grade );
	}
	rtable_print_int(rt);

	printf("Saving table /notallowed\n");
	result = rtable_save_int(rt, "/notallowed");
	printf("result1=%d\n", result);

	printf("Saving table grades1.rt\n");
	result = rtable_save_int(rt, "grades1.rt");
	printf("result2=%d\n", result);
	
	rt2 = rtable_create();
	result = rtable_read_int(rt2, "grades1.rt");

	rtable_print_int(rt2);
}