Skip to content

Instantly share code, notes, and snippets.

@gledsoncruz
Last active January 20, 2021 21:11
Show Gist options
  • Select an option

  • Save gledsoncruz/abeb645f3eb57b453002aa115be20273 to your computer and use it in GitHub Desktop.

Select an option

Save gledsoncruz/abeb645f3eb57b453002aa115be20273 to your computer and use it in GitHub Desktop.
Soma de 2 números complexos
public class ComplexNumber {
double num_real, num_img;
public ComplexNumber () {
this (0f, 0f);
}
ComplexNumber(double r, double i) {
this.num_real = r;
this.num_img = i;
}
public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) {
ComplexNumber temp = new ComplexNumber(0, 0); //iniciando com zero
temp.num_real = c1.num_real + c2.num_real; //soma da parte real
temp.num_img = c1.num_img + c2.num_img; //soma da parte imaginaria
return temp;
}
public static void main(String args[]) {
ComplexNumber c1 = new ComplexNumber(5.5, 4); // primeiro numero complexo
ComplexNumber c2 = new ComplexNumber(1.2, 3.5); //segundo numero complexo
ComplexNumber temp = sum(c1, c2); // soma
System.out.printf("Soma é igual a: "+ temp.num_real+" + "+ temp.num_img +"i"); //resultado
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment