Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
lambda
#include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include <math.h> typedef struct { char number[101]; unsigned short x; unsigned short y; }datos; // Prototypes. unsigned short checkTheNumber(char *, unsigned short); unsigned long long convertToBase10(char *, unsigned short ); unsigned long long baseConversion(unsigned long long, unsigned short); // --------------------------------------Main program----------------------------------------- int main(void) { unsigned short i; unsigned short N; unsigned long long numberTo10; char * pEnd; scanf("%hu\n", &N); datos numeros[N]; for (i = 0 ; i < N ; i++) scanf("%100s %hu %hu", numeros[i].number, &numeros[i].x, &numeros[i].y); for (i = 0 ; i < N ; i++) if (checkTheNumber(numeros[i].number, numeros[i].x)) { numberTo10 = strtoull(numeros[i].number, &pEnd,numeros[i].x); //printf("%llu\n", numberTo10); if ( numeros[i].y == 10) printf("El numero %s escrito en base %hu es igual a %llu en base %hu.\n", numeros[i].number, numeros[i].x, numberTo10,numeros[i].y); else { //baseConversion(numberTo10, numeros[i].y); printf("El numero %s escrito en base %hu es igual a %llu en base %hu.\n", numeros[i].number, numeros[i].x, baseConversion(numberTo10, numeros[i].y),numeros[i].y); } } else printf("El numero %s NO se encuentra en base %hu.\n", numeros[i].number, numeros[i].x); return 0; } unsigned short checkTheNumber(char *number, unsigned short base) { unsigned short digit; if (base < 11) { while ( *number != '\0') { if ( isalpha( *number )) return 0; digit = ( (unsigned short) *number ) - 48; if (digit >= base) return 0; number ++; } } else { while ( *number != '\0') { if ( isalpha ( *number ) ) { if ( islower( *number ) ) *number = toupper( *number ); digit = (unsigned short) *number - 55; if ( digit >= base) return 0; } else { digit = ( (unsigned short) *number ) - 48; if (digit >= base) return 0; } number ++; } } return 1; } unsigned long long baseConversion(unsigned long long number, unsigned short base) { short i; unsigned long long sum; unsigned long long rest; i = 0; sum = 0; /* while ( number >= base) { rest = number % base; sum = sum + ( rest * (pow(10, i) ) ); number /= 10; i ++; }*/ while ( number > 0) { rest = number % base; sum = sum + ( rest * (pow(10, i) ) ); number /= base; i ++; } return sum; } /* unsigned long long convertToBase10(char *number, unsigned short base) { unsigned long long sum = 0; unsigned long long digit; unsigned short exponent = 0; unsigned short length; short i; length = strlen(number); for (i = length - 1 ; i >= 0 ; i--) { if ( isalpha ( number[i] ) ) { if ( islower( number[i] ) ) number[i] = toupper( number[i] ); digit = ( (unsigned long long) number[i] ) - 55; digit = digit * ( (unsigned long long) pow(base, exponent) ); } else { digit = ( (unsigned long long) number[i] ) - 48; digit = digit * ( (unsigned long long) pow(base, exponent) ); } exponent ++; sum += digit; } return sum; }*/
run
|
edit
|
history
|
help
0
part 3
th
Spring 2017 Project 2 v1.1
Struct
Rotación matriz anti horaria
20171116:FUNCIONES
3,6,8
Swiss Infotech Tutorial
armstrong number using a while loop
Assignment 7