char *ft_convert_base(char *nbr, char *base_from, char *base_to) { int nbr_dec; int bf; int bt; if (valid_base(base_from, &bf) == 0 || valid_base(base_to, &bt) == 0) return (0); if (ft_atodec(nbr, base_from, bf, &nbr_dec) == 0) return (0); return(ft_itoa_base(nbr_dec, bt, base_to)); }
void output_anc_sites( const Aln_ChrsP acsp, const int min_diff, const int cpg_mask, const char* id_to_assign ) { int not_anc, diff; size_t pos, i; char r, anc_b; for( pos = 0; pos < acsp->anc_cp->len; pos++ ) { not_anc = 0; diff = 0; /* Are ancestral and focal bases the same? */ if (valid_base(acsp->anc_cp->seq[pos]) && (acsp->anc_cp->seq[pos] == acsp->foc_cp->seq[pos]) && (acsp->mask[pos]) ) { anc_b = acsp->anc_cp->seq[pos]; /* Are none of the other bases ancestral? */ for( i = 0; i < acsp->num_cps; i++ ) { if ( valid_base(acsp->cps[i]->seq[pos]) ) { if ( acsp->cps[i]->seq[pos] != anc_b ) { diff++; } else { not_anc = 1; // this base is ancestral - we don't care about this site } } } /* We've looked at all other bases. Is this site ancestral? */ if ( not_anc ) { ; // skip it } else { if ( diff >= min_diff ) { printf( "%s %d\n", id_to_assign, (int)pos ); } } } } }
/* * MAIN: Run the program and tests your functions. * sample command: ./bignum 4 12 + 13 * Result: 31 */ int main(int argc, char** argv) { int input_base; int* input1; int* input2; int* result; if(argc != 5) { print_usage(argv[0]); } input_base = string_to_integer(argv[1]); if(!valid_base(input_base)) { fprintf(stderr, "Invalid base: %s\n", argv[1]); print_usage(argv[0]); } if(!valid_input(argv[2], input_base)) { fprintf(stderr, "Invalid input1: %s\n", argv[2]); print_usage(argv[0]); } if(!valid_input(argv[4], input_base)) { fprintf(stderr, "Invalid input2: %s\n", argv[4]); print_usage(argv[0]); } if(argv[3][0] != '-' && argv[3][0] != '+') { fprintf(stderr, "Invalid operation: %s\n", argv[3]); print_usage(argv[0]); } input1 = string_to_integer_array(argv[2]); input2 = string_to_integer_array(argv[4]); result = perform_math(input1, input2, argv[3][0], input_base); printf("Result: "); bignum_print(result); printf("\n"); exit(0); }
/* * TODO * Returns true if the given string (char array) is a valid input, * that is: digits 0-9, letters A-Z, a-z * and it should not violate the given base and should not handle negative numbers */ bool valid_input(char* input, int base) { /* * check for valid base and if negative */ if (!valid_base(base) || input[0]=='-') { return false; } else { int len = strlen(input); int i; for (i =0; i< len; i++){ /* * check if the input string is a digit/letter */ if (!(is_digit(input[i]) || is_lower_alphabetic(input[i]) || is_upper_alphabetic(input[i]))){ return false; } /* * if the int excesses the base? */ else if (is_digit(input[i])){ if (input[i]-'0'>=base){ //convert char to int and compare with the base return false; } } /* *or if the letter excesses the base? */ else if (is_lower_alphabetic(input[i])){ if (input[i]-'a'+10 >=base){ return false; } } else if (is_upper_alphabetic(input[i])){ if (input[i] - 'A' + 10 >=base) { return false; } } } return true; } }