Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PROTOTIPO PREGUNTA 1 DE GUÍA 4
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#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 ); void baseConversion(unsigned long long, unsigned short, char *); void investNumber(char *); // --------------------------------------Main program----------------------------------------- int main(void) { unsigned short i; unsigned short N; unsigned long long numberTo10; char array[101]; 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, array); printf("El numero %s escrito en base %hu es igual a %s en base %hu.\n", numeros[i].number, numeros[i].x, array,numeros[i].y); } } else printf("El numero %s NO se encuentra en base %hu.\n", numeros[i].number, numeros[i].x); return 0; } // -------------------------------------------Functions-------------------------------------------- unsigned short checkTheNumber(char *number, unsigned short base) { unsigned short digit; char *num = number; if (base < 11) { while ( *number != '\0') { if ( isalpha( *number )) return 0; digit = ( (unsigned short) *number ) - 48; if (digit >= base) return 0; number ++; } } else { while ( *num != '\0') { if ( isalpha ( *num )) if ( islower( *num ) ) *num = toupper( *num ); num ++; } 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; } void baseConversion(unsigned long long number, unsigned short base, char *numero) { short i; char sum[101]; unsigned long long rest; char numbers[17] = "0123456789ABCDEF"; //char *array = sum; i = 0; while ( number > 0) { rest = number % base; sum[i] = numbers[rest]; number /= base; i ++; } sum[i] = '\0'; investNumber(sum); strcpy(numero, sum); } /* *--------------------Procedimiento *investNumber------------------------ * Propósito: invertir un número. * Parámetros : '*number' que representará la cadena de un número a invertir. */ void investNumber(char *number) { char *digit = number; char num[101]; unsigned short length; short i; length = strlen(number); num[length] = '\0'; for (i = length - 1 ; i >= 0 ; i--) { num[i] = *digit; digit ++; } strcpy(number, num); }
gcc
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion