100到简单加减乘除算法的程序

Posted Kimm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了100到简单加减乘除算法的程序相关的知识,希望对你有一定的参考价值。

public class Complex {   
    double real; 
    double image;   
       
    Complex(){    
        Scanner input = new Scanner(System.in); 
        double real = input.nextDouble(); 
        double image = input.nextDouble(); 
        Complex(real,image); 
    
   
    private void Complex(double real, double image) {
        
        this.real = real; 
        this.image = image; 
    
   
    Complex(double real,double image){   
        this.real = real; 
        this.image = image; 
    
   
    public double getReal() { 
        return real; 
    
   
    public void setReal(double real) { 
        this.real = real; 
    
   
    public double getImage() { 
        return image; 
    
   
    public void setImage(double image) { 
        this.image = image; 
    
       
    Complex add(Complex a){
        double real2 = a.getReal(); 
        double image2 = a.getImage(); 
        double newReal = real + real2; 
        double newImage = image + image2; 
        Complex result = new Complex(newReal,newImage); 
        return result; 
    
       
    Complex sub(Complex a){
        double real2 = a.getReal(); 
        double image2 = a.getImage(); 
        double newReal = real - real2; 
        double newImage = image - image2; 
        Complex result = new Complex(newReal,newImage); 
        return result; 
    
       
    Complex mul(Complex a){   
        double real2 = a.getReal(); 
        double image2 = a.getImage(); 
        double newReal = real*real2 - image*image2; 
        double newImage = image*real2 + real*image2; 
        Complex result = new Complex(newReal,newImage); 
        return result; 
    
       
    Complex div(Complex a){   
        double real2 = a.getReal(); 
        double image2 = a.getImage(); 
        double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2); 
        double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2); 
        Complex result = new Complex(newReal,newImage); 
        return result; 
    
       
    public void print(){
        if(image > 0){ 
            System.out.println(real + " + " + image + "i"); 
        }else if(image < 0){ 
            System.out.println(real + "" + image + "i"); 
        }else
            System.out.println(real); 
        
    
}

以上是关于100到简单加减乘除算法的程序的主要内容,如果未能解决你的问题,请参考以下文章

C语言100个经典算法源码片段

简单的四则运算

c语言设计一个简单的计算器程序

策略模式及优化

c语言设计一个简单的计算器程序

理解 Trachtenberg 速算法