Пример #1
0
int find_cycle(int d) {
	digit *digits;
	digit *digit_head;
	
	digits = digit_head = add_digit(NULL, 0, 0);
	digits->number = 0;
	
	int count;
	int n = 10;
	
	int q;
	int r = 1;
	
	while (r > 0) {
		q = n/d;
		r = n - q * d;
		
		if ((count = count_cycle(digit_head, q, r)) > 0) {
			clean_digit(digit_head);
			return count;
		}
		
		digits = add_digit(digits, q, r);
		
		n = digits->remainder * 10;
	}
	
	clean_digit(digit_head);
	
	// not a cycle
	return -1;
	
}
Пример #2
0
int main(int ac, char **av) {
	char cmd_line[MAX_CHARS+1] ;
	op_result status ;

	/*
	 * Parse the command arguments.
	 * If this returns, initialize the puzzle.
	 * Then
	 *   - configure the board from the command
	 *     line puzzle file,
	 *   - close the file,
	 *   - print the initial board
	 */
	parse_args(ac, av) ;
	init_puzzle() ;
	configure( puzzle_file() ) ;
	fclose( puzzle_file() ) ;
	print_puzzle() ;

	/*
	 * Command loop.
	 * Read a line and use the first character to decide
	 * what command to execute (or report an error).
	 */
	printf("command: ");

	while(read_line(cmd_line, MAX_CHARS) != EOF ) {
		if(cmd_line[CMD_INDEX] == 'q') {		// quit
			break ;
		} else if (cmd_line[CMD_INDEX] == 'p') {	// print the board
			print_puzzle() ;
		} else if (cmd_line[CMD_INDEX] == 'a') {	// add a digit
			int r, c, d ;
			r = cmd_line[ROW_INDEX] - '0' ;
			c = cmd_line[COL_INDEX] - '0' ;
			d = cmd_line[DIGIT_INDEX] - '0' ;

			status = add_digit(r, c, d) ;
			if( status != OP_OK ) {
				print_error(status) ;
			}
		} else if (cmd_line[CMD_INDEX] == 'e') {	// erase a digit
			int r, c ;
			r = cmd_line[ROW_INDEX] - '0' ;
			c = cmd_line[COL_INDEX] - '0' ;

			status = erase_digit(r, c) ;
			if( status != OP_OK ) {
				print_error(status) ;
			}
		} else {				// error
			printf("Unknown command %s\n", cmd_line) ;
		}
	
	printf("command: ") ;
	}

	return 0 ;
}
/*
 * Function to change an int to a string
 * CHECKED: functions, length, width, brackets
 * TODO: comments
 */
char *int_to_string(int n)
{
	char *str = malloc(sizeof(char)*1);
	*str = 0;

	if (n < 0) {
		str = add_digit('-', str);
	} else {
		n = 0-n;
	}
	if (n == 0) {
		str = add_digit('0', str);
	}
	else {
		str = do_work(n, str);
	}
	return str;
}
Пример #4
0
void add_digit(bigint_t big, unsigned int idx, uint8_t add) {
	unsigned int sum = big->digits[idx] + add;

	if (sum < 10) {
		big->digits[idx] = sum;
	} else {
		big->digits[idx] = sum-10;
		add_digit(big, idx-1, 1);
	}
}
Пример #5
0
int add_digit(struct listnode *head, int a) {
	if(head->next == NULL) {
		head->data += a;
		if(head->data > 9) {
			head->data %= 10;
			return 1;
		 } else {
			return 0;
		}
	}
	head->data += add_digit(head->next, a);
	if(head->data > 9) {
		head->data %= 10;
		return 1;
	} else {
		return 0;
	}
}
/*
 * Function to do??? TODO
 */
char *do_work(int n, char *str)
{
	int divisor = 1;
	int a = n;
	while (a < -9) {
		a /= 10;
		divisor *= 10;
	}

	while (n < 0) {
		int to_print = n / divisor;
		n -= to_print * divisor;
		divisor /= 10;
		to_print = 0-to_print;
		str = add_digit('0' + to_print, str);

	}
  return str;
}
Пример #7
0
bigint_t add(bigint_t a, bigint_t b) {
	unsigned int size = max(a->size,b->size);	
	bigint_t result = alloc_bigint(size);
	bigint_t small, large;

	if (a->size > b->size) {
		small = b;
		large = a;
	} else {
		small = a;
		large = b;
	}

	for (unsigned i = 0; i < large->size; i++)
		result->digits[i] = large->digits[i];

	for (int i = small->size-1; i >= 0; --i)
		add_digit(result, i, small->digits[i]);

	return result;
}
Пример #8
0
	std::optional<T> get_integer (Iter & begin, const Iter & end) noexcept {
	
		std::optional<T> retr;
		
		bool found=false;
		T i=0;
		
		for (;begin!=end;++begin) {
		
			if (std::isspace(*begin)) {
			
				//	End on trailing whitespace
				if (found) break;
		
				//	Skip leading whitespace
				continue;
			
			}
			
			//	See if this character is a hex
			//	digit
			auto iter=map.find(static_cast<char>(std::toupper(*begin)));
			
			if (iter==map.end()) break;
			
			found=true;
			
			//	Try and add this digit in
			auto next=add_digit(i,iter->second);
			if (!next) return retr;
			i=*next;
		
		}
		
		if (found) retr=i;
		
		return retr;
	
	}
Пример #9
0
void main() {
	struct listnode *head = NULL;
	add(&head, getnode(1));
	add(&head, getnode(2));
	add(&head, getnode(3));
	add(&head, getnode(4));
	add(&head, getnode(5));
	add(&head, getnode(6));
	print_list(head);
	reverse(&head);
	print_list(head);
	head = pairwise_swap(head);
	print_list(head);
	head = pairwise_swap(head);
	print_list(head);
	rotate(&head, 3);
	print_list(head);
	segregate_even_odd(&head);
	print_list(head);
	int a = add_digit(head, 9);
	print_list(head);
}