예제 #1
0
파일: numline.c 프로젝트: rexos/unix
int main( int argc, char * argv[] ){

  mkdir( "/var/log/numline", S_IRWXU | S_IRWXG | S_IRWXO );
  write_log("PROGRAM STARTED");

  if ( argc < 2 || argc > 2 ) {
    printf("Wrong number of parameters\n");
    write_log("Wrong arguments");
    exit(1);
  }
  if ( argc == 2 && access( argv[1], R_OK ) == -1 ) {
    perror( argv[1] );
    write_log("File access denied");
    exit(1);
  }

  int fd = open(argv[1],0,O_RDONLY);
  int size = (int)(lseek(fd,0,SEEK_END));
  close(fd);
  char * filename = argv[1];
  FILE * file = fopen( filename, "r" );
  char * buffer;
  char * tmp;
  int i = 1;
  buffer = malloc( size + count_digits(i) + 2 );
  tmp = malloc( size + count_digits(i) + 2 );
  while( fgets( buffer, size, file ) ){
    sprintf( tmp, "%d) %s", i, buffer );
    printf("%s", tmp);
    buffer = malloc( size + count_digits(i) + 2 );
    tmp = malloc( size + count_digits(i) + 2 );
    i++;
  }
  return EXIT_SUCCESS;
}
예제 #2
0
int main(void){

    int t, n1, n2, dn1, dn2, sum=0, dsum;

    scanf("%d",&t);

    while(t--){

        scanf("%d %d",&n1, &n2);

        dn1=count_digits(n1);
        dn2=count_digits(n2);

        n1=reverse(n1,dn1);
        n2=reverse(n2, dn2);

        sum=n1+n2;
        dsum=count_digits(sum);
        sum=reverse(sum, dsum);

        printf("%d\n", sum);
    }

    return 0;
}
예제 #3
0
size_t
dt_parse_iso_zone_extended(const char *str, size_t len, int *op) {
    const unsigned char *p;
    int o, h, m, sign;
    size_t n;

    if (len < 1)
        return 0;

    p = (const unsigned char *)str;
    switch (*p) {
        case 'Z':
            o = 0;
            n = 1;
            goto zulu;
        case '+':
            sign = 1;
            break;
        case '-':
            sign = -1;
            break;
        default:
            return 0;
    }

    if (len < 3 || count_digits(p, 1, len) != 2)
        return 0;

    h = parse_number(p, 1, 2);
    m = 0;
    n = 3;

    if (len < 4 || p[3] != ':')
        goto hm;

    if (count_digits(p, 4, len) != 2)
        return 0;

    m = parse_number(p, 4, 2);
    n = 6;

 hm:
    if (h > 23 || m > 59)
        return 0;
    o = sign * (h * 60 + m);
#ifdef DT_PARSE_ISO_STRICT
    if (o == 0 && sign < 0)
        return 0;
#endif

 zulu:
    if (op)
        *op = o;
    return n;
}
예제 #4
0
size_t
dt_parse_iso_time_extended(const char *str, size_t len, int *sp, int *fp) {
    const unsigned char *p;
    int h, m, s, f;
    size_t n;

    p = (const unsigned char *)str;
    if (count_digits(p, 0, len) != 2)
        return 0;

    h = parse_number(p, 0, 2);
    m = s = f = 0;
    n = 2;
    
    if (len < 3 || p[2] != ':')
        goto hms;

    if (count_digits(p, 3, len) != 2)
        return 0;

    m = parse_number(p, 3, 2);
    n = 5;

    if (len < 6 || p[5] != ':')
        goto hms;

    if (count_digits(p, 6, len) != 2)
        return 0;

    s = parse_number(p, 6, 2);
    n = 8;

    /* hh:mm:ss.fffffffff */
    if (n < len && (p[n] == '.' || p[n] == ',')) {
        size_t r = parse_fraction_digits(p, ++n, len, &f);
        if (!r)
            return 0;
        n += r;
    }

  hms:
    if (h > 23 || m > 59 || s > 59) {
        if (!(h == 24 && m == 0 && s == 0 && f == 0))
            return 0;
    }

    if (sp)
        *sp = h * 3600 + m * 60 + s;
    if (fp)
        *fp = f;
    return n;
}
예제 #5
0
파일: out-jotdown.c 프로젝트: KxCode/ferret
/**
 * This function formats an unsigned number, possibly 64-bits in length.
 */
static void 
format_unsigned(char *buf, unsigned length, size_t *r_offset, uint64_t num)
{
	unsigned digits = count_digits(num);
	size_t new_offset;

	if (*r_offset >= length)
		return;
	if (*r_offset + 1 >= length) {
		buf[(*r_offset)++] = '\0';
		return;
	}

	if (digits == 0) {
		buf[(*r_offset)++] = '0';
		buf[(*r_offset)] = '\0';
	} else {
		if (*r_offset + digits >= length) {
			memset(buf+*r_offset, '0', length-*r_offset);
			buf[length-1] = '\0';
			*r_offset = length;
			return;
		}

		buf[*r_offset+digits] = '\0';
		new_offset = *r_offset + digits;

		while (num) {
			buf[*r_offset + --digits] = (num%10)["01234567890"];
			num = num / 10;
		}
		
		*r_offset = new_offset;
	}
}
예제 #6
0
파일: main.cpp 프로젝트: CCJY/coliru
	pair_vec<RaIt> lexicographic_sort(RaIt p_beg, RaIt p_end)
	{
	    if(p_beg == p_end) return {};
	
	    auto max = *std::max_element(p_beg, p_end);
	    auto maxDigits = count_digits(max);
	
	    pair_vec<RaIt> result;
	    result.reserve( std::distance(p_beg, p_end) );
	
	    for(auto i = p_beg; i != p_end; ++i)
	        result.push_back( lexicographic_pair_helper(*i, maxDigits) );
	
	    using value_type = typename pair_vec<RaIt>::value_type;
	
	    std::sort(begin(result), end(result),
	              [](value_type const& l, value_type const& r)
	              {
	                  if(l.first < r.first) return true;
	                  if(l.first > r.first) return false;
	                  return l.second < r.second; }
	             );
	
	    return result;
	}
예제 #7
0
int check_prime(int n)
{
	int i;

	count_digits(n, 0);
	for (i = 3; i < digit_count+1; i += 3)
		if (check(n, 0, i))
			return 1;

	count_digits(n, 1);
	for (i = 3; i < digit_count+1; i += 3)
		if (check(n, 1, i))
			return 1;

	return 0;
}
/*
 * This function converts an integer number which is greater than
 * or equal to zero to a string. If the number is less than zero,
 * then this function returns NULL. The caller of this function
 * is responsible to free the memory allocated by this function.
 */
char* ntoa (int num)
{
    char* ch;
    int digits, rem;
    int i;

    /*
     * If the number is less than, return NULL
     */
    if (num < 0) {
        return(NULL);
    }

    /*
     * Count the number of digits in the number
     */
    digits = count_digits(num);

    /*
     * If the number of digits in the number are zero, then return
     * NULL.
     */
    if (digits == 0) {
        return(NULL);
    }

    /*
     * Allocate a character array to store the string converted
     * number.
     */
    ch = (char*)malloc(sizeof(char) * (digits + 1));

    /*
     * If the allocation fails, then return NULL
     */
    if (!ch) {
        return(NULL);
    }

    memset(ch, 0, sizeof(char) * (digits + 1));

    /*
     * Iterate until the number is zero and add the character
     * digits into the array
     */
    i = digits - 1;
    while (i >= 0) {

        rem = num % 10;
        num /= 10;

        ch[i] = '0' + rem;
        --i;
    }

    /*
     * Return the string version of the number.
     */
    return(ch);
}
예제 #9
0
int main(void)
{
    ull card_number;
        
    ui digits,
       head_num,
       total_sum;
    bool checksum;
    
    card_number = get_number();
    digits = count_digits(card_number);
    head_num = get_head_num(card_number, digits);
    total_sum = sum(card_number, digits);
    checksum = validate_sum(total_sum);

    if (digits == 15 && (head_num == 34 || head_num == 37) && checksum)
    {
        printf("AMEX\n");
    }
    else if (digits == 16 && (head_num > 50 && head_num < 56) && checksum)
    {
        printf("MASTERCARD\n");
    }
    else if ((digits == 13 || digits == 16) && (head_num / 10 == 4) && checksum)
    {
        printf("VISA\n");
    }
    else
    {
        printf("INVALID\n");
    }

    return 0;
}
예제 #10
0
static void
num_digits(void)
{
	struct number *n = NULL;
	struct value *value;
	size_t digits;

	value = pop();
	if (value != NULL) {
		switch (value->type) {
		case BCODE_NONE:
			return;
		case BCODE_NUMBER:
			digits = count_digits(value->u.num);
			n = new_number();
			bn_check(BN_set_word(n->number, digits));
			break;
		case BCODE_STRING:
			digits = strlen(value->u.string);
			n = new_number();
			bn_check(BN_set_word(n->number, digits));
			break;
		}
		stack_free_value(value);
		push_number(n);
	}
}
예제 #11
0
char		*ft_lltoa_base(long long value, int base)
{
	char	*output;
	int		digits;
	int		neg;
	int		negoffset;

	neg = 1;
	negoffset = 0;
	if (value == 0)
		return (ft_strdup("0"));
	if (value < 0)
	{
		neg = -1;
		negoffset = 1;
	}
	digits = count_digits(value, base);
	output = (char *)malloc(digits + 1 + negoffset);
	output[digits + negoffset] = '\0';
	while (digits--)
	{
		output[digits + negoffset] = (value % base) * neg + 48;
		value /= base;
	}
	output[0] = negoffset ? '-' : output[0];
	translate(output);
	return (output);
}
예제 #12
0
/* Sets an integer attribute. */
SLPError SLPAttrSet_int(
		SLPAttributes attr_h,
		const char *tag,
		long val,
		SLPInsertionPolicy policy
) {
	struct xx_SLPAttributes *slp_attr = (struct xx_SLPAttributes *)attr_h;
	value_t *value;
	
	/***** Sanity check. *****/
	if ( is_valid_tag(tag) == SLP_FALSE ) {
		return SLP_TAG_BAD;
	}

	/***** Create new value. *****/
	value = value_new();
	if (value == NULL) {
		return SLP_MEMORY_ALLOC_FAILED;
	}
	
	/**** Set ****/
	value->data.va_int = val;
	value->escaped_len = count_digits(value->data.va_int);
	assert(value->escaped_len > 0);

	return generic_set_val(slp_attr, tag, value, policy, SLP_INTEGER);
}
예제 #13
0
파일: main.cpp 프로젝트: CCJY/coliru
	std::pair<T, T> lexicographic_pair_helper(T const p, U const maxDigits)
	{
	    auto const digits = count_digits(p);
	    // append zeros so that `l` has `maxDigits` digits
	    auto const l = static_cast<T>( p  * my_pow10(maxDigits-digits) );
	    return {l, p};
	}
예제 #14
0
파일: sol10.c 프로젝트: svagionitis/puzzles
int main(int argc, char *argv[])
{
    unsigned int start = 0;
    unsigned int total_iter = 0;

    if (argc != 3)
        usage(argv[0]);

    if (argv[1])
        start = (unsigned int)atoi(argv[1]);
    else
        usage(argv[0]);

    if (argv[2])
        total_iter = (unsigned int)atoi(argv[2]);
    else
        usage(argv[0]);


    // Current sequence.
    unsigned int *seq = NULL;
    unsigned int seq_size = count_digits(start);

    seq = malloc(seq_size * sizeof(unsigned int));

    // Convert the starting number in a number sequence
    int d = 0;
    for(d = seq_size - 1; d >= 0;d--)
    {
        seq[d] = start % 10;
        start /= 10;
    }

    // New sequence.
    unsigned int *new_seq = NULL;
    unsigned int new_seq_size = 0;

    unsigned int i = 0;
    for (i = 0;i<total_iter;i++)
    {

        update_seq(seq, seq_size, &new_seq, &new_seq_size);

        if (i == total_iter - 1)
            print_seq(new_seq, new_seq_size);

        seq_size = new_seq_size;
        new_seq_size = 0;

        swap_ptr(&seq, &new_seq);

        if (new_seq)
            free(new_seq);
    }

    if (seq)
        free(seq);

    return 0;
}
예제 #15
0
void find_next_continuation(const board_t* board, size_t* i_row, size_t* i_col,
							digits_t* digits) {
	size_t i, j, best_row, best_col;
	digits_t tmp_digits, best_digits;
	int min_possibilities = NO_MOVE_POSSIBLE;
	for (i = 0; i < NROWS; i++) {
		for (j = 0; j < NCOLS; j++) {
			if (EMPTY == get_digit(board, i, j)) {
				count_digits(board, i, j, &tmp_digits);
				int n_possible = count_possibilities(&tmp_digits);
				assert(n_possible > 0);
				assert(n_possible < NDIGITS);
				if (n_possible < min_possibilities) {
					min_possibilities = n_possible;
					best_row = i;
					best_col = j;
					best_digits = tmp_digits;
				}
			}
		}
	}
	assert(min_possibilities != NO_MOVE_POSSIBLE);
	memcpy(digits, &best_digits, sizeof(digits_t));
	*i_row = best_row;
	*i_col = best_col;
}
예제 #16
0
size_t
dt_parse_iso_zone_basic(const char *str, size_t len, int *op) {
    const unsigned char *p;
    int o, h, m, sign;
    size_t n;

    if (len < 1)
        return 0;

    p = (const unsigned char *)str;
    switch (*p) {
        case 'Z':
            o = 0;
            n = 1;
            goto zulu;
        case '+':
            sign = 1;
            break;
        case '-':
            sign = -1;
            break;
        default:
            return 0;
    }

    if (len < 3)
        return 0;

    n = count_digits(p, 1, len);
    m = 0;
    switch (n) {
        case 2: /* ±hh */
            h = parse_number(p, 1, 2);
            n = 3;
            break;
        case 4: /* ±hhmm */
            h = parse_number(p, 1, 2);
            m = parse_number(p, 3, 2);
            n = 5;
            break;
        default:
            return 0;
    }

    if (h > 23 || m > 59)
        return 0;
    o = sign * (h * 60 + m);
#ifdef DT_PARSE_ISO_STRICT
    if (o == 0 && sign < 0)
        return 0;
#endif

 zulu:
    if (op)
        *op = o;
    return n;
}
예제 #17
0
int main() {
	char* tabNums;
	uint16_t tabNums_Size;
	tabNums_Size = count_digits(FILE_NAME);
	tabNums = malloc(sizeof(char)*tabNums_Size);
	fill_table(FILE_NAME,tabNums,tabNums_Size);
	algorithm(tabNums,tabNums_Size);
	return 0;
}
예제 #18
0
int nth_digits (int i) {
    int n = 0,res=0,cres=0;
    while (i > res) {
        cres = res;
        res += count_digits (n);
        n++;
    }
    return (cres*10) + (n-1);
} 
예제 #19
0
bool perm(int a, int b)
{
	int digits_a[10];
	int digits_b[10];
	
	count_digits(a, digits_a);
	count_digits(b, digits_b);

	for (int i = 0; i < 10; ++i)
	{
		if (digits_a[i] != digits_b[i])
		{
			return false;
		}
	}

	return true;
}
/* print a number */
void print_number(int n)
{
  int aux1, i, count, x;	/* declaring variables */
  if (n<0) print_char('-');	/* if negative, insert negative sign */
  if (n==-2147483648) { print_char(50); n=-147483648; } /* handles special case of extreme negative */
  if (n<0) n=-n;	     /* if negative, go into positive world */
  count=count_digits(n); x=1; 	/* initialize */
  for (i=1;i<count;i++)		/* make x big */
    x=x*10;
  print_char((n/x)+48);
  
  if (x>1)
    {
      aux1= n - ((int) n/x) * x; /* store magic formula */
      for (i=1;i<count_digits(n)-count_digits(aux1);i++)
	print_char(48);		/* if there's any zero's, don't miss them */
      print_number(n - ((int) n/x) * x); /* recursive protocol */
    }
}
예제 #21
0
static int
count_digits(int x, int ndigits)
{
    int i;
    if (x < 10) {
        return ndigits + 1;
    }

    return count_digits(x / 10, ndigits + 1);
}
예제 #22
0
inline size_t count_digits(Integer value) {
  static_assert(std::is_unsigned<Integer>::value, "");

  const uint64_t p01 = 10ull;
  const uint64_t p02 = 100ull;
  const uint64_t p03 = 1000ull;
  const uint64_t p04 = 10000ull;
  const uint64_t p05 = 100000ull;
  const uint64_t p06 = 1000000ull;
  const uint64_t p07 = 10000000ull;
  const uint64_t p08 = 100000000ull;
  const uint64_t p09 = 1000000000ull;
  const uint64_t p10 = 10000000000ull;
  const uint64_t p11 = 100000000000ull;
  const uint64_t p12 = 1000000000000ull;

  if (value < p01) {
    return 1;
  }

  if (value < p02) {
    return 2;
  }

  if (value < p03) {
    return 3;
  }

#if defined(BOOST_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif

  if (value < p12) {
    if (value < p08) {
      if (value < p06) {
        if (value < p04) {
          return 4;
        }
        return 5 + (value >= p05);
      }
      return 7 + (value >= p07);
    }
    if (value < p10) {
      return 9 + (value >= p09);
    }
    return 11 + (value >= p11);
  }
  return 12 + count_digits(value / p12);

#if defined(BOOST_CLANG)
# pragma clang diagnostic pop
#endif
}
예제 #23
0
static size_t
parse_fraction_digits(const unsigned char *p, size_t i, size_t len, int *fp) {
    size_t n, ndigits;

    ndigits = n = count_digits(p, i, len);
    if (ndigits < 1)
        return 0;
    if (ndigits > 9)
        ndigits = 9;
    if (fp)
        *fp = parse_number(p, i, ndigits) * pow_10[9 - ndigits];
    return n;
}
예제 #24
0
파일: ft_atoi.c 프로젝트: Mouradif/Hexter
int	ft_atoi(char *str)
{
	int i;
	int digits;
	int result;

	result = 0;
	i = -1;
	digits = count_digits(str);
	while (++i < digits)
		result += (str[i] - 48) * ft_power(10, digits - i - 1);
	return (result);
}
void print_number(int n)
{
  int c;

  if (n == 0)
    {
      print_char('0');
      return;
    }
  c = count_digits(n);
  if (n < 0) print_char('-');
  print_digits(n, c);
}
예제 #26
0
static char *
itos(int x)
{
    int ndigits = count_digits(x, 0);
    char *s = malloc(ndigits + 1);
    int i;
    int n = x;
    s[ndigits] = 0;
    for (i = ndigits - 1; i >= 0; i--) {
        s[i] = n % 10 + 48;
        n /= 10;
    }
    return s;
}
예제 #27
0
파일: format.c 프로젝트: jncronin/rPi
int unsigned_to_nstring(char* buffer, size_t blen, unsigned n, unsigned base)
{
    int cnt=0;
    unsigned digits = count_digits(n, base);
    if (digits > blen-1) return -1;
    buffer[digits] = '\0';
    unsigned temp = n;
    while (temp > 0)
    {
        buffer[digits-cnt-1]=to_digit(temp%base);
        temp /= base;
        cnt++;
    }
    return cnt;
}
예제 #28
0
size_t
dt_parse_iso_time_basic(const char *str, size_t len, int *sp, int *fp) {
    const unsigned char *p;
    int h, m, s, f;
    size_t n;

    p = (const unsigned char *)str;
    n = count_digits(p, 0, len);
    m = s = f = 0;
    switch (n) {
        case 2: /* hh */
            h = parse_number(p, 0, 2);
            goto hms;
        case 4: /* hhmm */
            h = parse_number(p, 0, 2);
            m = parse_number(p, 2, 2);
            goto hms;
        case 6: /* hhmmss */
            h = parse_number(p, 0, 2);
            m = parse_number(p, 2, 2);
            s = parse_number(p, 4, 2);
            break;
        default:
            return 0;
    }

    /* hhmmss.fffffffff */
    if (n < len && (p[n] == '.' || p[n] == ',')) {
        size_t r = parse_fraction_digits(p, ++n, len, &f);
        if (!r)
            return 0;
        n += r;
    }

  hms:
    if (h > 23 || m > 59 || s > 59) {
        if (!(h == 24 && m == 0 && s == 0 && f == 0))
            return 0;
    }

    if (sp)
        *sp = h * 3600 + m * 60 + s;
    if (fp)
        *fp = f;
    return n;
}
예제 #29
0
inline void generate(Iterator& sink_out, Integer input_value) {
  using Unsigned = typename std::make_unsigned<Integer>::type;
  Unsigned value(input_value);

  Iterator sink = sink_out;

  if (input_value < 0) {
    *sink = '-';
    ++sink;
    value = 0 - value;
  }

  size_t digits = count_digits(value);
  sink += digits;
  sink_out = sink;

  const char* cache = cache_digits();

  assert(value >= 0);
  while (value >= 100) {
    size_t index = (value % 100) * 2; // 0..198
    value /= 100;

    --sink;
    *sink = cache[index + 1];

    --sink;
    *sink = cache[index];
  }

  if (value < 10) {
    // 0..9
    --sink;
    *sink = static_cast<char>('0' + value);
  }
  else {
    // 10..99
    size_t index = static_cast<size_t>(value * 2); // 20..198

    --sink;
    *sink = cache[index + 1];

    --sink;
    *sink = cache[index];
  }
}
예제 #30
0
char		*ft_ulltoa_base(unsigned long long value, int base)
{
	char	*output;
	int		digits;

	if (value == 0)
		return (ft_strdup("0"));
	digits = count_digits(value, base);
	output = (char *)malloc(digits + 1);
	output[digits] = '\0';
	while (digits--)
	{
		output[digits] = (value % base) + 48;
		value /= base;
	}
	translate(output);
	return (output);
}