-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseNumericaCase1.java
176 lines (137 loc) · 7.77 KB
/
BaseNumericaCase1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.Scanner;
import static java.lang.Math.floor;
import static java.lang.Math.pow;
public class BaseNumericaCase1 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double numero = 0, conversor = 0, resto = 0;
int binario = 2, octal = 8, hexa = 16;
String[] stringArrayBinario = new String[16], stringArrayOctal = new String[8], stringArrayHexadecimal = new String[4];
String stringConversao = new String(), reversedStr = new String();
String stringBinario = new String(), stringOctal = new String(), stringHexadecimal = new String();
int operacaoDesejada;
System.out.println("************* Conversor de Numeros em bases Numéricas *********");
System.out.println("Entre com a operação desejada: ");
System.out.println("1 - Decimal -> Binário + Octal + Hexadecimal");
System.out.println("2 - Binário -> Decimal + Octal + Hexadecimal");
System.out.println("3 - Octal -> Decimal + Binário + Hexadecimal");
System.out.println("4 - Hexadecimal -> Decimal + Binário + Octal");
operacaoDesejada = reader.nextInt();
switch (operacaoDesejada) {
case 1:
System.out.println("Entre com o numero Decimal (de 2 a 65535): ");
numero = reader.nextDouble();
break;
case 2:
System.out.println("Entre com o número Binário: ");
stringBinario = reader.next();
break;
case 3:
System.out.println("Entre com o numero Octal: ");
stringOctal = reader.next();
break;
case 4:
System.out.println("Entre com o número Hexadecimal: ");
stringHexadecimal = reader.next().toUpperCase();
break;
}
if (operacaoDesejada == 1) {
stringConversao = "";
stringConversao = conversaoDecimal(numero, binario);
System.out.printf("\nRepresentação binária do decimal\t\t %5.0f = %17s", numero, stringConversao);
stringConversao = "";
stringConversao = conversaoDecimal(numero, octal);
System.out.printf("\nRepresentação Octal do decimal\t\t\t %5.0f = %17s", numero, stringConversao);
stringConversao = "";
stringConversao = conversaoDecimal(numero, hexa);
System.out.printf("\nRepresentação Hexadecimal do decimal\t %5.0f = %17s", numero, stringConversao);
} else if (operacaoDesejada == 2) {
stringConversao = "";
numero = conversorParaDecimal (stringBinario,binario);
System.out.printf("\nRepresentação Decimal do Binário\t %17s = \t\t\t %5.0f",stringBinario, numero);
stringConversao = "";
stringConversao = conversaoDecimal(numero, octal);
System.out.printf("\nRepresentação Octal do Binário\t\t %17s = %17s", stringBinario, stringConversao);
stringConversao = "";
stringConversao = conversaoDecimal(numero, hexa);
System.out.printf("\nRepresentação Hexadecimal do Binário %17s = %17s", stringBinario, stringConversao);
} else if (operacaoDesejada == 3) {
stringConversao = "";
numero = conversorParaDecimal (stringOctal,octal);
System.out.printf("\nRepresentação Decimal do Octal\t\t %17s = \t\t\t %5.0f",stringOctal, numero);
stringConversao = "";
stringConversao = conversaoDecimal(numero, binario);
System.out.printf("\nRepresentação Binária do Octal\t\t %17s = %17s", stringOctal, stringConversao);
stringConversao = "";
stringConversao = conversaoDecimal(numero, hexa);
System.out.printf("\nRepresentação Hexadecimal do Octal\t %17s = %17s", stringOctal, stringConversao);
} else if (operacaoDesejada == 4) {
stringConversao = "";
numero = conversorParaDecimal (stringHexadecimal,hexa);
System.out.printf("\nRepresentação Decimal do Hexadecimal\t %17s = \t\t\t %5.0f",stringHexadecimal, numero);
stringConversao = "";
stringConversao = conversaoDecimal(numero, binario);
System.out.printf("\nRepresentação Binária do Hexadecimal\t %17s = %17s", stringHexadecimal, stringConversao);
stringConversao = "";
stringConversao = conversaoDecimal(numero, octal);
System.out.printf("\nRepresentação Octal do Hexadecimal\t\t %17s = %17s", stringHexadecimal, stringConversao);
}
}
public static String conversaoDecimal(double numero, int tipoConversao) {
double conversor = numero;
int resto = 0;
String[] stringArray = new String[17];
String[] digitosBaseBinaHex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
String stringConversao = "", reversedStr = "" ;
int i = 0;
boolean end = false, chave = true;
int index = 0;
while (!end) {
//resto = conversor % tipoConversao;
stringArray[index] = digitosBaseBinaHex[(int) conversor % tipoConversao];
index++;
conversor = floor(conversor / tipoConversao);
if (conversor == 0) {
stringArray[index] = digitosBaseBinaHex[(int) conversor % tipoConversao];
for (i = 0; i < stringArray.length; i++) {
if (stringArray[i] != null) {
stringConversao = stringConversao + stringArray[i];
}
}
end = true;
}
for (i = 0; i < stringConversao.length(); i++) {
reversedStr = stringConversao.charAt(i) + reversedStr;
}
}
return (reversedStr);
}
public static double conversorParaDecimal (String stringEntrada,int tipoConversao) {
double [] arrayNumEntrada = new double[17];
double numero= 0;
String reversedStr = new String();
for (int i = 0; i < stringEntrada.length(); i++) {
reversedStr = stringEntrada.charAt(i) + reversedStr;
}
char [] arrayStringEntrada= reversedStr.toCharArray();
for (int i = 0; i < stringEntrada.length(); i++) {
if (arrayStringEntrada[i] == 'A') {
arrayNumEntrada[i] = 10;
} else if (arrayStringEntrada[i] == 'B') {
arrayNumEntrada[i] = 11;
} else if (arrayStringEntrada[i] == 'C') {
arrayNumEntrada[i] = 12;
} else if (arrayStringEntrada[i] == 'D') {
arrayNumEntrada[i] = 13;
} else if (arrayStringEntrada[i] == 'E') {
arrayNumEntrada[i] = 14;
} else if (arrayStringEntrada[i] == 'F') {
arrayNumEntrada[i] = 15;
} else {
arrayNumEntrada[i] = Integer.parseInt(String.valueOf(arrayStringEntrada[i]));
}
numero = numero + (arrayNumEntrada[i] * pow(tipoConversao, i));
}
return (numero);
}
}