Skip to content

Commit 8a05bc3

Browse files
committed
Conversor de Base adicionado e algumas modificações
1 parent 0efecd8 commit 8a05bc3

21 files changed

+320
-69
lines changed

src/app/analise-combinatoria/Metodos.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
export class Metodos {
22

3-
private intTryParse(s: string): boolean {
4-
try {
5-
return !isNaN(parseInt(s));
6-
} catch (NumberFormatException) {
7-
return false;
8-
}
3+
private isPositiveInteger(s: string): boolean {
4+
return (/^\+?\d+$/).test(s);
95
}
106

117
private fatorial(n: number): bigint {
@@ -94,8 +90,8 @@ export class Metodos {
9490
if (n == '') {
9591
throw 'O campo \'n\' está vazio.';
9692
}
97-
if (!this.intTryParse(n)) {
98-
throw 'O valor do campo \'n\' não é um inteiro.';
93+
if (!this.isPositiveInteger(n)) {
94+
throw 'O valor do campo \'n\' não é um inteiro positivo.';
9995
} else {
10096
_n = parseInt(n);
10197
}
@@ -110,8 +106,8 @@ export class Metodos {
110106
if (p == '') {
111107
throw 'O campo \'p\' está vazio.';
112108
}
113-
if (!this.intTryParse(n)) {
114-
throw 'O valor do campo \'n\' não é um inteiro.';
109+
if (!this.isPositiveInteger(n)) {
110+
throw 'O valor do campo \'n\' não é um inteiro positivo.';
115111
} else {
116112
_n = parseInt(n);
117113
}
@@ -126,16 +122,16 @@ export class Metodos {
126122
if (p == '') {
127123
throw 'O campo \'p\' está vazio.';
128124
}
129-
if (!this.intTryParse(n)) {
130-
throw 'O valor do campo \'n\' não é um inteiro.';
125+
if (!this.isPositiveInteger(n)) {
126+
throw 'O valor do campo \'n\' não é um inteiro positivo.';
131127
} else {
132128
_n = parseInt(n);
133129
}
134130
if (_n < MIN_NUMBER || _n > MAX_NUMBER) {
135131
throw `O valor do campo \'n\' deve ser um número inteiro positivo de ${MIN_NUMBER} a ${MAX_NUMBER}.`;
136132
}
137-
if (!this.intTryParse(p)) {
138-
throw 'O valor do campo \'p\' não é um inteiro.';
133+
if (!this.isPositiveInteger(p)) {
134+
throw 'O valor do campo \'p\' não é um inteiro positivo.';
139135
} else {
140136
_p = parseInt(p);
141137
}
@@ -156,8 +152,8 @@ export class Metodos {
156152
let sPP = p.replace(' ', '').split(',');
157153
let pp = new Array<number>();
158154
for (let i = 0; i < sPP.length; i++) {
159-
if (!this.intTryParse(sPP[i])) {
160-
throw 'Um valor do campo \'p,p,...\' não é um inteiro.';
155+
if (!this.isPositiveInteger(sPP[i])) {
156+
throw 'Um valor do campo \'p,p,...\' não é um inteiro positivo.';
161157
} else {
162158
_p = parseInt(sPP[i]);
163159
}

src/app/analise-combinatoria/analise-combinatoria.component.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ label.info {
22
font-size: 14px;
33
font-style: italic;
44
color: navy;
5+
}
6+
7+
textarea {
8+
resize: vertical;
59
}

src/app/analise-combinatoria/analise-combinatoria.component.html

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<nav class="navbar navbar-expand py-0">
2-
<ul class="navbar-nav ml-auto">
3-
<li class="nav-item dropdown">
4-
<a class="nav-link dropdown-toggle" href="" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
5-
Versões
6-
</a>
7-
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
8-
<a class="dropdown-item" href="https://github.com/iagocolodetti/Analise_Combinatoria">C# Desktop</a>
9-
<a class="dropdown-item" href="https://github.com/iagocolodetti/Analise_Combinatoria_Android">Java Android</a>
10-
<a class="dropdown-item" href="https://github.com/iagocolodetti/Analise_Combinatoria_WEB">Java WEB</a>
11-
</div>
12-
</li>
13-
<li class="nav-item">
14-
<a class="nav-link" routerLink="/">Voltar</a>
15-
</li>
16-
</ul>
17-
</nav>
2+
<ul class="navbar-nav ml-auto">
3+
<li class="nav-item dropdown">
4+
<a class="nav-link dropdown-toggle" href="" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
5+
Versões
6+
</a>
7+
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
8+
<a class="dropdown-item" href="https://github.com/iagocolodetti/Analise_Combinatoria">C# Desktop</a>
9+
<a class="dropdown-item" href="https://github.com/iagocolodetti/Analise_Combinatoria_Android">Java Android</a>
10+
<a class="dropdown-item" href="https://github.com/iagocolodetti/Analise_Combinatoria_WEB">Java WEB</a>
11+
</div>
12+
</li>
13+
<li class="nav-item">
14+
<a class="nav-link" routerLink="/">Voltar</a>
15+
</li>
16+
</ul>
17+
</nav>
1818
<div class="row justify-content-center">
1919
<div class="form-group text-center">
2020
<h3>{{title}}</h3>
@@ -54,11 +54,12 @@ <h5>{{tipos[tipoSelecionado].tipo}}</h5>
5454
</div>
5555
<div class="row justify-content-center">
5656
<div class="form-group text-center">
57-
<button class="btn btn-primary btn-fix" (click)="calcular()">Calcular</button>
57+
<button class="btn btn-primary btn-fix" (click)="calcular()" [disabled]="desativarCalcular()">Calcular</button>
5858
</div>
5959
</div>
6060
<div class="row justify-content-center">
6161
<div class="form-group col-xs-10 col-sm-7 col-md-5 col-lg-5 text-center">
62-
<h6 style="word-wrap:break-word;">{{resultado}}</h6>
62+
<label for="resultado">Resultado</label>
63+
<textarea class="form-control text-center" rows="5" [(ngModel)]="resultado" id="resultado" name="resultado" readonly></textarea>
6364
</div>
6465
</div>

src/app/analise-combinatoria/analise-combinatoria.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@ export class AnaliseCombinatoriaComponent {
8484
}
8585
}
8686

87+
desativarCalcular() {
88+
if (this.tipoSelecionado == 0) {
89+
return this.n.length == 0;
90+
} else {
91+
return (this.p.length == 0 || this.n.length == 0);
92+
}
93+
}
94+
8795
}

src/app/app-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33
import { IndexComponent } from './index/index.component';
44
import { AnaliseCombinatoriaComponent } from './analise-combinatoria/analise-combinatoria.component';
5+
import { ConversorDeBaseComponent } from './conversor-de-base/conversor-de-base.component';
56
import { CriptografiaComponent } from './criptografia/criptografia.component';
67
import { CifraXorComponent } from './criptografia/cifra-xor/cifra-xor.component';
78
import { CifraDeCesarComponent } from './criptografia/cifra-de-cesar/cifra-de-cesar.component';
@@ -12,6 +13,7 @@ import { JogoDaVelhaComponent } from './jogo-da-velha/jogo-da-velha.component';
1213
const routes: Routes = [
1314
{path: '', component: IndexComponent},
1415
{path: 'analisecombinatoria', component: AnaliseCombinatoriaComponent},
16+
{path: 'conversordebase', component: ConversorDeBaseComponent},
1517
{path: 'criptografia', component: CriptografiaComponent},
1618
{path: 'criptografia/cifraxor', component: CifraXorComponent},
1719
{path: 'criptografia/cifradecesar', component: CifraDeCesarComponent},

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CifraDeCesarComponent } from './criptografia/cifra-de-cesar/cifra-de-ce
1111
import { CifraRailFenceComponent } from './criptografia/cifra-rail-fence/cifra-rail-fence.component';
1212
import { CifraXorComponent } from './criptografia/cifra-xor/cifra-xor.component';
1313
import { AnaliseCombinatoriaComponent } from './analise-combinatoria/analise-combinatoria.component';
14+
import { ConversorDeBaseComponent } from './conversor-de-base/conversor-de-base.component';
1415

1516
@NgModule({
1617
declarations: [
@@ -22,7 +23,8 @@ import { AnaliseCombinatoriaComponent } from './analise-combinatoria/analise-com
2223
CifraDeCesarComponent,
2324
CifraRailFenceComponent,
2425
CifraXorComponent,
25-
AnaliseCombinatoriaComponent
26+
AnaliseCombinatoriaComponent,
27+
ConversorDeBaseComponent
2628
],
2729
imports: [
2830
BrowserModule,
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
export class Metodos {
2+
3+
private isPositiveInteger(s: string): boolean {
4+
return (/^\+?\d+$/).test(s);
5+
}
6+
7+
private reverterString(s: string): string {
8+
let string = '';
9+
for (let i = s.length-1; i >= 0; i--) {
10+
string += s[i];
11+
}
12+
return string;
13+
}
14+
15+
private checarBaseAlgarismo(algarismo: string, base: number): boolean {
16+
let numero: number;
17+
for (let i = 0; i < algarismo.length; i++) {
18+
let charCode = algarismo.charCodeAt(i);
19+
if (charCode >= 48 && charCode <= 57) {
20+
numero = parseInt(algarismo.charAt(i));
21+
} else if (charCode >= 65 && charCode <= 86) {
22+
numero = charCode - 55;
23+
} else if (charCode >= 97 && charCode <= 118) {
24+
numero = charCode - 87;
25+
} else {
26+
return false;
27+
}
28+
if (numero >= base) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
}
34+
35+
private algarismoParaNumero(algarismo: string): number {
36+
let numero: number;
37+
let charCode = algarismo.charCodeAt(0);
38+
if (charCode >= 65 && charCode <= 86) {
39+
numero = charCode - 55;
40+
} else if (charCode >= 97 && charCode <= 118) {
41+
numero = charCode - 87;
42+
} else {
43+
numero = parseInt(algarismo.charAt(0));
44+
}
45+
return numero;
46+
}
47+
48+
private numeroParaAlgarismo(numero: number): string {
49+
let algarismo: string;
50+
if (numero >= 10 && numero <= 31) {
51+
algarismo = String.fromCharCode(numero + 55);
52+
} else {
53+
algarismo = String.fromCharCode(numero + 48);
54+
}
55+
return algarismo;
56+
}
57+
58+
private converterAlgarismoParaDecimal(algarismo: string, base: number): string {
59+
if (algarismo.length <= 0) {
60+
throw 'Digite algum algarismo para ser convertido.';
61+
}
62+
if (algarismo.includes('.') || algarismo.includes(',')) {
63+
throw 'Use apenas algarismos inteiros.';
64+
}
65+
if (!this.checarBaseAlgarismo(algarismo, base)) {
66+
throw 'Esse algarismo não pertecem a essa base.';
67+
}
68+
let numero = BigInt(0);
69+
for (let i = 0; i < algarismo.length; i++) {
70+
let exp = (algarismo.length - (1 + i));
71+
let tmpN = BigInt(this.algarismoParaNumero(algarismo.charAt(i)));
72+
tmpN *= BigInt(base) ** BigInt(exp);
73+
numero += tmpN;
74+
}
75+
if (!this.isPositiveInteger(numero.toString())) {
76+
throw 'O valor a ser convertido é muito alto.';
77+
}
78+
return numero.toString();
79+
}
80+
81+
private converterDecimalParaAlgarismo(algarismo: string, base: number): string {
82+
if (algarismo.length <= 0) {
83+
throw 'Digite algum algarismo para ser convertido.';
84+
}
85+
if (algarismo.includes('.') || algarismo.includes(',')) {
86+
throw 'Use apenas algarismos inteiros.';
87+
}
88+
if (!this.checarBaseAlgarismo(algarismo, 10)) {
89+
throw 'Esse algarismo não pertecem a essa base.';
90+
}
91+
let numero = BigInt(algarismo);
92+
algarismo = '';
93+
while (numero > BigInt(0)) {
94+
let tmpB = BigInt(base);
95+
let resto = numero % tmpB;
96+
algarismo += this.numeroParaAlgarismo(parseInt(resto.toString()));
97+
numero /= tmpB;
98+
}
99+
return this.reverterString(algarismo);
100+
}
101+
102+
public converterAlgarismo(algarismo: string, daBase: number, paraBase: number): string {
103+
try {
104+
return this.converterDecimalParaAlgarismo(this.converterAlgarismoParaDecimal(algarismo, daBase), paraBase);
105+
} catch (erro) {
106+
throw erro;
107+
}
108+
}
109+
110+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
textarea {
2+
resize: vertical;
3+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<nav class="navbar navbar-expand py-0">
2+
<ul class="navbar-nav ml-auto">
3+
<li class="nav-item dropdown">
4+
<a class="nav-link dropdown-toggle" href="" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
5+
Versões
6+
</a>
7+
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
8+
<a class="dropdown-item" href="https://github.com/iagocolodetti/Conversor_de_Base">C# Desktop</a>
9+
<a class="dropdown-item" href="https://github.com/iagocolodetti/Conversor_de_Base_Android">Java Android</a>
10+
<a class="dropdown-item" href="https://github.com/iagocolodetti/Conversor_de_Base_WEB">Java WEB</a>
11+
</div>
12+
</li>
13+
<li class="nav-item">
14+
<a class="nav-link" routerLink="/">Voltar</a>
15+
</li>
16+
</ul>
17+
</nav>
18+
<div class="row justify-content-center">
19+
<div class="form-group text-center">
20+
<h3>{{title}}</h3>
21+
</div>
22+
</div>
23+
<div class="row justify-content-center">
24+
<div class="form-group col-xs-10 col-sm-7 col-md-5 col-lg-5 text-center">
25+
<label for="algarismo">Algarismo:</label>
26+
<input type="text" class="form-control text-center" [(ngModel)]="algarismo" id="algarismo" name="algarismo" placeholder="Digite o algarismo">
27+
</div>
28+
</div>
29+
<div class="row justify-content-center">
30+
<div class="form-group col-xs-10 col-sm-7 col-md-5 col-lg-5 text-center">
31+
<label for="daBase">Base:</label>
32+
<input type="number" min="2" max="32" class="form-control text-center" [(ngModel)]="daBase" id="daBase" name="daBase" placeholder="Digite a base do algarismo">
33+
</div>
34+
</div>
35+
<div class="row justify-content-center">
36+
<div class="form-group col-xs-10 col-sm-7 col-md-5 col-lg-5 text-center">
37+
<label for="paraBase">Para base:</label>
38+
<input type="number" min="2" max="32" class="form-control text-center" [(ngModel)]="paraBase" id="paraBase" name="paraBase" placeholder="Digite para qual base será convertido">
39+
</div>
40+
</div>
41+
<div class="row justify-content-center">
42+
<div class="form-group text-center">
43+
<button class="btn btn-primary btn-fix" (click)="calcular()" [disabled]="desativarCalcular()">Converter</button>
44+
</div>
45+
</div>
46+
<div class="row justify-content-center">
47+
<div class="form-group col-xs-10 col-sm-7 col-md-5 col-lg-5 text-center">
48+
<label for="resultado">Resultado</label>
49+
<textarea class="form-control text-center" rows="5" [(ngModel)]="resultado" id="resultado" name="resultado" readonly></textarea>
50+
</div>
51+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { ConversorDeBaseComponent } from './conversor-de-base.component';
4+
5+
describe('ConversorDeBaseComponent', () => {
6+
let component: ConversorDeBaseComponent;
7+
let fixture: ComponentFixture<ConversorDeBaseComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ ConversorDeBaseComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(ConversorDeBaseComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});

0 commit comments

Comments
 (0)