Exemple #1
0
unsigned char *lzw_decode(
	struct lzw_state *state,
	unsigned code,
	unsigned char outbuf[DICTSIZE])
{
	unsigned char *outbuf_curs = &outbuf[DICTSIZE];
	struct dict_entry *entry, *child;

	if(state->current == NULL) {
		*(--outbuf_curs) = state->dict[code].ch; 

		state->current = state->dict + state->dict[code].ch;
	} else if(code >= state->next_code) {
		// this is a bit tricky: we need to emit the first character in the
		// state chain leading up to current both first and last in the output.
		unsigned char *last = --outbuf_curs;
		for(entry = state->current ; entry->parent; entry = entry->parent) 
			*(--outbuf_curs) = entry->ch;
		// entry points to first item in state chain here
		*(--outbuf_curs) = *last = entry->ch;
		for(unsigned char *curs = outbuf_curs+1; curs < outbuf+DICTSIZE; curs++)
			printf("decode emit %c ", *curs);

		// we also need add a dict entry with the current state chain's first
		// entry appended, and step using that character
		child = alloc_dict_entry(state, state->current, entry->ch);
		assert(child); // encoder didn't reset, so we better not
		printf("decode add dict %u ", child->code);
		print_reverse(child);
		printf("\n");

		state->current = step(state->current, entry->ch);
	} else {
		for(entry = state->dict + code; entry->parent; entry = entry->parent) 
			*(--outbuf_curs) = entry->ch;
		// entry points to first item in state chain here
		*(--outbuf_curs) = entry->ch;
		for(unsigned char *curs = outbuf_curs+1; curs < outbuf+DICTSIZE; curs++)
			printf("decode emit %c\n", *curs);

		child = alloc_dict_entry(state, state->current, entry->ch);
		if(!child) {
			printf("decode reset dict\n");
			reset_dict(state);
		} else {
			printf("decode add dict %u ", child->code);
			print_reverse(child);
			printf("\n");
		}

		state->current = state->dict + code;
	}

	printf("decode cursor @ ");
	print_reverse(state->current);
	printf(" (%u)\n", state->current->code);

	return outbuf_curs;
}
Exemple #2
0
int main ( int argc, char* argv[] ) {

	NODE* start = NULL ;
	float data ;
	int test ;

	/* create first node */
	printf ( "Enter new data : " ) ;
	test = scanf ( "%f" , &data ) ;
	/* if first input if EOF skips the loop and also all calling functions
	   so that it doesn't seg fault. */
	if ( test != EOF ) {
		/* makes rest of nodes until EOF */
		while ( test != EOF ) {		
			start = insert_node( data , start ) ;
			printf ( "Enter new data : " ) ;
			test = scanf ( "%f" , &data ) ;	       		
		}
		/* print data entered */
		printf ( "\nHere is your data as it was stored :\n" ) ;
		print_entered( start ) ;
		printf ( "\nHere is your data as it was entered :\n" ) ;
		print_reverse( start ) ;			
		/* free memory */
		free_list( start ) ;
	}	
	/* make it look pretty */
	putchar ( '\n' ) ;	
	return 0 ;
}
void print_reverse(struct node *node){
	if(node==NULL)
		return;
	if(node->next!=NULL)
		print_reverse(node->next);
	printf("%d ",node->value);
}
Exemple #4
0
int main()
{
	char search_for[80];
	printf("искать: ");
	fgets(search_for, 80, stdin);
	search_for[strlen(search_for)-1] = '\0';
	find_track(search_for);
	printf("%s\n", search_for);
	print_reverse(search_for);
	return 0;
}
Exemple #5
0
void print_reverse( NODE* start ) {

	/* prints data in order entered. If it is last node than print right 
	   away otherwise call yourself until you get to node with NULL and 
	   then print back up the recursion */
	if ( start->next == NULL ) 
		printf ( "%f\n" , start->data ) ;
	else {
		print_reverse ( start->next ) ;	
		printf ( "%f\n" , start->data ) ;
	}	
}
int main()
{
	char moji[20];
	char gyaku[20];
	printf("文字列入力:");
	fgets(moji,sizeof(moji),stdin);
	moji[strlen(moji) -1] = '\0';
	printf("入力した文字は%sです\n",moji);
	printf("逆順だと");
	print_reverse(moji);
	return 0;
}
// Pre-condition: string is a string of length n or greater.
// Post-condition: Prints out the first n characters of string in reverse order.
void print_reverse(char string[], int n) {
     
     // Only one character to print, so print it!
     if (n == 1)
         printf("%c", string[0]);
         
     // Solve the problem recursively: print the last character, then reverse
     // the substring without that last character.
     else {
         printf("%c", string[n-1]);
         print_reverse(string, n-1);
     }
} 
Exemple #8
0
int main()
{
	char *juices[] = {
	"dragonfruit", "waterberry", "sharonfruit", "uglifruit",
	"rumberry", "kiwifruit", "mulberry", "strawberry",
	"blueberry", "blackberry", "starfruit"
	};
	char *a;
	puts(juices[6]);
	print_reverse(juices[7]);
	a = juices[2];
	juices[2] = juices[8];
	juices[8] = a;
	puts(juices[8]);
	print_reverse(juices[(18 + 7) / 5]);
	puts(juices[2]);
	print_reverse(juices[9]);
	juices[1] = juices[3];
	puts(juices[10]);
	print_reverse(juices[1]);
	return 0;
}
Exemple #9
0
int lzw_encode(
	struct lzw_state *state,
	unsigned char ch,
	unsigned *code)
{
	int status = 0;
	struct dict_entry *child;
	printf("encode '%c'\n", ch);
	if(state->current == NULL) {
		printf("encode ... (!current)\n");
		state->current = state->dict + ch;
	} else if((child = step(state->current, ch))) {
		printf("encode ... (step)\n");
		// have transition via ch
		state->current = child;
	} else {
		printf("encode emit %u\n", state->current->code);
		// don't have transition via ch
		*code = state->current->code;
		status = 1;
		child = alloc_dict_entry(state, state->current, ch);
		if(!child) {
			printf("encode reset dict\n");
			reset_dict(state);
		} else {
			printf("encode add dict %u ", child->code);
			print_reverse(child);
			printf("\n");
		}
		
		state->current = state->dict + ch;
	}
	printf("encode cursor @ ");
	print_reverse(state->current);
	printf(" (%u)\n", state->current->code);

	return status;
}
Exemple #10
0
void print_reverse (node* rope){
    //printf("print_reverse\n");
    int i;
    if(rope->next != 0) {
        print_reverse(rope->next);
        i = rope->length - 1;
    }
    else{
        i = rope->length - 2;
    }
    for(i; i>=0; i--)
        write(1, rope->str + i, 1);
    //printf("exit print_reverse\n");
}
int main()
{
	insert_node_at_start(4);
	insert_node_at_start(3);
	insert_node_at_end(5);
	insert_node_at_nth_position(6,3);
	print();
	print_reverse();
	delete_node_from_start();
	print();
	delete_node_from_end();
	print();
	delete_node_from_nth_position(3);
	print();
	return 0;


}
int main() {
    
    // Very basic, NOT comprensive, tests of the recursive methods.
    
    int vals[4];
    printf("7! = %d\n", fact(7));
    printf("31^2 + 32^2 + ... + 200^2 = %d\n", sumsq(31, 200));
    
    vals[0] = 37; vals[1] = 48; vals[2] = 56; vals[3] = 63;
    printf("vals has %d Odd values.\n", ArrayOdd(vals, 4));
    
    print_reverse("writethisbackwards", 18);
    printf("\n");
    printf("3^10 = %d\n", powerA(3,10));
    printf("3^11 = %d\n", powerB(3,11));
     
    if (Rbinary(33, vals, 0, 3) == -1)
        printf("33 was not found in vals.\n");
        
    dectobin(179);
    printf("\n");
    
    if (check("madamimadam", 11))
        printf("madamimadam is a palindrome.\n");
        
    printf("The 27th Fibonacci number is %d\n", fibonacci(27)); 
    
    // Test of fast exponentiation vs. regular version
    
    int start = time(0);
    int ans1 = slowModPow(6874, 1000000000, 13713);
    int end1 = time(0);
    int ans2 = modPow(6874, 1000000000, 13713);
    int end2 = time(0);
    
    printf("ans1 = %d, ans2 = %d.\n", ans1, ans2);
    printf("slowModExp took %d sec.\n", end1-start);
    printf("modPow took %d sec.\n", end2-end1);
    
    system("PAUSE");
    return 0;
}
Exemple #13
0
void print_dict_item(struct lzw_state *state, unsigned code)
{
	print_reverse(state->dict + code);
}
Exemple #14
0
int main()
{
    char s[80];
    fgets(s, 80, stdin);
    print_reverse(s);
}
Exemple #15
0
int main() {
    char text[] = "Your mom likes to drink";
    print_reverse(text);
    return 0;
}
Exemple #16
0
static void print_reverse(struct dict_entry *entry)
{
	if(entry->parent)
		print_reverse(entry->parent);
	printf("%c", entry->ch);
}
Exemple #17
0
int main (void)
{
    system ("clear");
    node_t *head = NULL;
    int choice = 0;

    int n;

    while (TRUE)
    {
	print_menu();
	scanf("%d", &choice);

	switch(choice)
	{
	    case 1:
		head = insert_rear(head);
		view_list(head);
		break;
	    case 2:
		head = insert_front(head);
		view_list(head);
		break;
	    case 3:
		head = delete_rear(head);
		view_list(head);
		break;
	    case 4:
		head = delete_front(head);
		view_list(head);
		break;
	    case 5:
		print_reverse(head);
		printf("\n");
		break;
		//	    case 6: 
		//		link_sort1(head);
		//		view_list(head);
		//		break;
	    case 7:
		head = reverse_list_iterative(head);
		view_list(head);
		break;
	    case 8:
		head = reverse_list_recursive(head);
		view_list(head);
		break;
	    case 9:
		head = swap_alternate(head);
		view_list(head);
		break;
	    case 10:
		head = insert_sorted(head);
		view_list(head);
		break;
	    case 11:
		printf ("Enter the Value of n: ");
		scanf ("%d", &n);
		head = reverse_n_list(head, n);
		view_list(head);
		break;

	    case 100:
		view_list(head);
		break;
	    default : printf("Invalid Choice\n");
	}
    }
    return 0;
}
Exemple #18
0
int main () {
    char buffer[BUFSIZE];
    int len, i;
	int discard_line = 0;
	int start = 0;
    int end = 0;

	node* rope = (node*)malloc(sizeof(node));
    rope->next = 0;
	rope->str = 0;
	rope->length = 0;
	rope->total = 0;
	
	while (1) {
		len = read (0, buffer, BUFSIZE);
        //printf("read %d chars\n", len);
		if(len!=0) {
            start = 0;
            end = 0;
            while(end != -1){
                end = -1;
                for(i = start; i < len; i++)
                    if(buffer[i] == '\n') {
                        end = i;
                        break;
                    }
                if(end == -1) {
                    if(!discard_line) {
                        if(rope->total + len - start > SIZE)
                            discard_line = 1;
                        else
                            add(rope, buffer, start, len -1);
                    }
                }
                else {
                    if(rope->total + end - start + 1 > SIZE)
                        discard_line = 1;

                    if(!discard_line) {
                        add(rope, buffer, start, end);
                        print_reverse(rope);
                        write(1, buffer+end, 1);
                    }

                    discard_line = 0;
                    clear(rope);
                    node* rope = (node*)malloc(sizeof(node));
                    rope->next = 0;
                    rope->str = 0;
                    rope->length = 0;
                    rope->total = 0;
                }
                start = i + 1;
                if(start>=len)
                    break;
            }
		}
		else {
			break;
		}
	}

    clear(rope);

	return 0;
}