-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTComplejo.java
178 lines (150 loc) · 4.25 KB
/
TComplejo.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
177
178
//BENEMERITA UNIVERSIDAD AUTONOMA DE PUEBLA
//FACULTAD DE CIENCIAS DE LA COMPUTACION
//PROGRAMACION II - JESUS HUERTA AGUILAR
// --------------------> TComplejo
package com.mycompany.matricescomplejas;
public class TComplejo{
// - - - - - ATRIBUTOS - - - - -
private long numreal,denreal,numimag,denimag;
// - - - - - METODOS - - - - -
//CONSTRUCTORES
TComplejo(){
numreal = 0;
denreal = 1;
numimag = 0;
denimag = 1;
}
TComplejo(long numreal, long denreal, long numimag, long denimag){
this.numreal = numreal;
this.denreal = denreal;
this.numimag = numimag;
this.denimag = denimag;
//DENOMINADOR REAL
if(denreal!=0)
this.denreal = denreal;
else
this.denreal = Integer.MIN_VALUE;
//DENOMINADOR IMAGINARIO
if(denimag!=0)
this.denimag = denimag;
else
this.denimag = Integer.MIN_VALUE;
}
//METODOS SET
public void setNumReal(long numreal){
this.numreal = numreal;
}
public void setDenReal(long denreal){
this.denreal = denreal;
}
public void setNumImag(long numimag){
this.numimag = numimag;
}
public void setDenImag(long denimag){
this.denimag = denimag;
}
//METODOS GET
public long getNumReal(){
return numreal;
}
public long getDenReal(){
return denreal;
}
public long getNumImag(){
return numimag;
}
public long getDenImag(){
return denimag;
}
//METODOS DIVERSOS
private long mcd(long a, long b){
if(b == 0)
return a;
else
return mcd(b, a%b);
}
public void simplificaComplejoReal(){
long MCD;
if(numreal>denreal){
MCD = mcd(numreal,denreal);
}
else{
MCD = mcd(denreal,numreal);
}
numreal = numreal/MCD;
denreal = denreal/MCD;
}
public void simplificaComplejoImaginario(){
long MCD;
if(numimag>denimag){
MCD = mcd(numimag,denimag);
}
else{
MCD = mcd(denimag,numimag);
}
numimag = numimag/MCD;
denimag = denimag/MCD;
}
@Override
public String toString(){
String cadena,cadenaReal,cadenaImag = "";
int negative = 0;
//SOLUCION DENOMINADOR NEGATIVO REAL
if(denreal<0){
denreal = denreal*-1;
numreal = numreal*-1;
}
if(denreal == 1){
cadenaReal = Long.toString(numreal);
}
else if(numreal == 0){
cadenaReal = "0";
}
else{
cadenaReal = Long.toString(numreal) + "/" + Long.toString(denreal);
}
//SOLUCION DENOMINADOR NEGATIVO IMAGINARIO
if(denimag<0){
denimag = denimag*-1;
numimag = numimag*-1;
}
if(numimag < 0){
negative = 1;
numimag = numimag*-1;
}
if(denimag == 1){
cadenaImag = Long.toString(numimag);
}
else if(numimag == 0){
cadenaImag = "0";
}
else{
cadenaImag = "(" + Long.toString(numimag) + "/" + Long.toString(denimag) + ")";
}
if(("0".equals(cadenaReal)) && ("0".equals(cadenaImag))){
cadena = "[0]";
}
else if("0".equals(cadenaImag)){
cadena = "[" + cadenaReal + "]";
}
else if("0".equals(cadenaReal) && negative == 0){
cadena = "[" + cadenaImag + "i]";
}
else if("0".equals(cadenaReal) && negative == 1){
cadena = "[ -" + cadenaImag + "i]";
}
else if("1".equals(cadenaImag) && negative == 0){
cadena = "[" + cadenaReal + " + i]";
}
else if("1".equals(cadenaImag) && negative == 1){
cadena = "[" + cadenaReal + " - i]";
}
else if(negative == 1){
cadena = "[" + cadenaReal + " - " + cadenaImag + "i]";
}
else{
cadena = "[" + cadenaReal + " + " + cadenaImag + "i]";
}
return cadena;
}
}//FIN CLASE TComplejo