Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Copyright (c) 2015, ScreenBlack <screenblack at gmail dot com>
- All rights reserved.
- Redistribution and use in source and binary forms, with
- or without modification, are permitted provided that the
- following conditions are met:
- 1. Redistributions of source code must retain the above
- copyright notice, this list of conditions and the following
- disclaimer.
- 2. Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the following
- disclaimer in the documentation and/or other materials
- provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of
- its contributors may be used to endorse or promote products
- derived from this software without specific prior written
- permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- **/
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- typedef struct
- {
- unsigned char a:1;
- unsigned char b:1;
- unsigned char c:1;
- unsigned char d:1;
- unsigned char e:1;
- unsigned char f:1;
- unsigned char g:1;
- unsigned char h:1;
- } Bits;
- typedef union
- {
- Bits list;
- unsigned char A;
- unsigned int Num;
- } Char;
- void carregaCaracter(Char *);
- void carregaDecimal(Char *);
- void carregaHexadecimal(Char *);
- void carregaBinario(Char *);
- void mostra(Char);
- void menu(void);
- int main(void)
- {
- Char bit;
- int opcao = -1;
- while(1)
- {
- menu();
- scanf("%d%*c", &opcao);
- switch (opcao)
- {
- case 0:
- return 0;
- case 1:
- carregaBinario(&bit);
- mostra(bit);
- break;
- case 2:
- carregaDecimal(&bit);
- mostra(bit);
- break;
- case 3:
- carregaCaracter(&bit);
- mostra(bit);
- break;
- case 4:
- carregaHexadecimal(&bit);
- mostra(bit);
- break;
- default:
- printf("Opcao invalida.\nEncerrando programa");
- }
- }
- return 0;
- }
- void carregaCaracter(Char *bit)
- {
- printf("Digite um caracter: ");
- scanf("%c%*c", &bit->A );
- }
- void carregaDecimal(Char *bit)
- {
- printf("Digite numero decimal (max 255): ");
- scanf("%u%*c", &bit->Num );
- }
- void carregaHexadecimal(Char *bit)
- {
- printf("Digite numero hexadecimal (max FF): ");
- scanf("%x%*c", &bit->Num);
- }
- void carregaBinario(Char *bit)
- {
- char binario[9] = {'\0'};
- printf("Digite numero binario (8 digitos): ");
- scanf("%s%*c", binario );
- if ( binario[0] == '\0' )
- bit->list.h = 0;
- else
- bit->list.h = binario[0]-48;
- if ( binario[1] == '\0' )
- bit->list.g = 0;
- else
- bit->list.g = binario[1]-48;
- if ( binario[2] == '\0' )
- bit->list.f = 0;
- else
- bit->list.f = binario[2]-48;
- if ( binario[3] == '\0' )
- bit->list.e = 0;
- else
- bit->list.e = binario[3]-48;
- if ( binario[4] == '\0' )
- bit->list.d = 0;
- else
- bit->list.d = binario[4]-48;
- if ( binario[5] == '\0' )
- bit->list.c = 0;
- else
- bit->list.c = binario[5]-48;
- if ( binario[6] == '\0' )
- bit->list.b = 0;
- else
- bit->list.b = binario[6]-48;
- if ( binario[7] == '\0' )
- bit->list.a = 0;
- else
- bit->list.a = binario[7]-48;
- }
- void mostra(Char bit)
- {
- printf("\n###########\nChar: %c\nDec: %d\nOctal: %o\nHexa: %X\n", bit.A, bit.A, bit.A, bit.A);
- printf("Binario: %d%d%d%d%d%d%d%d\n###########\n\n", bit.list.h, bit.list.g, bit.list.f, bit.list.e, bit.list.d, bit.list.c, bit.list.b, bit.list.a);
- }
- void menu(void)
- {
- printf("---- Conversor NERD ----\n\n");
- printf("1 - Leitura binaria\n");
- printf("2 - Leitura decimal\n");
- printf("3 - Leitura caracter\n");
- printf("4 - Leitura hexadecimal\n\n");
- printf("0 - Encerra programa\n\n");
- printf("Opcao: ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement