1.实现add()方法,将两个complex对象相加后存在一个complex对象中。//这条起码不符合要求
你的add()方法只实现了两个int型数据相加的功能,并不是两个complex对象相加

解决方案 »

  1.   

    add()方法是一个运算符重载,楼主装初学而已。
      

  2.   

    经过重新了解我做了修改但好像SUM有错大家帮我看看还有就是怎么做到第五点。大家快帮帮忙呀!!!
    using  System;  
     
    public  struct  Complex    
    {  
    public  int  real;  
    public  int  imaginary;  
     
    public  Complex(int  real,  int  imaginary)    
    {  
    this.real  =  real;  
    this.imaginary  =  imaginary;  
    }  
       
    public  static  Complex  operator  +(Complex  c1,  Complex  c2)    
    {  
    return  new  Complex(c1.real  +  c2.real,  c1.imaginary  +  c2.imaginary);  
    }  
      
    public  override  string  ToString()  
    {  
    return(String.Format(  "{0}  +  {1}i  ",  real,  imaginary));  
    }  
     
    public  static  void  Main()    
    {  
    Complex  c1  =  new  Complex();  
    Complex  c2  =  new  Complex();  
      
    Complex  sum  =  c1  +  c2;
            Console.WriteLine ("please enter the real1:");
    c1.real=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Real1:    {0}  ",c1.real);      Console.WriteLine ("please enter the imaginary1:");
    c1.imaginary=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Imaginary1:  {0}i  ",c1.imaginary);        Console.WriteLine(  "First  complex  number:    {0}  ",c1);      Console.WriteLine ("please enter the real2:");
    c2.real=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Real2:    {0}  ",c2.real); 

            Console.WriteLine ("please enter the imaginary2:");
    c2.imaginary=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Imaginary2:  {0}i  ",c2.imaginary);
     

     
    Console.WriteLine(  "Second  complex  number:  {0}  ",c2);  
    Console.WriteLine(  "The  sum  of  the  two  numbers:  {0}  ",sum);  
       
    }  
    }  
      

  3.   

    using  System;  
     
    public  class  Complex    
    {  
    public  int  real;  
    public  int  imaginary;  
     
      public Complex()
    {
    real = 1;
    imaginary = 1;
    }
    public  Complex(int  real,  int  imaginary)    
    {  
    this.real  =  real;  
    this.imaginary  =  imaginary;  
    }  
       
    public  static  Complex  operator  +(Complex  c1,  Complex  c2)    
    {  
    return  new  Complex(c1.real  +  c2.real,  c1.imaginary  +  c2.imaginary);  
    }  
      
    public  override  string  ToString()  
    {  
    string outPut; outPut = (real!=0 ? 
    (real.ToString() + " "+ (imaginary != 0?((imaginary > 0?("+ " + (imaginary!=1?(imaginary.ToString()):"")):("- " + (imaginary!=-1?((-1) * imaginary).ToString():"") )) + "i"):"")) 
    : ((imaginary != 0?((imaginary > 0?((imaginary!=1?(imaginary.ToString()):"")):("- " + (imaginary!=-1?((-1) * imaginary).ToString():"" ))) + "i"):"")));
    if(outPut.Trim() == "")
    {
     outPut = "NULL";
    }
    return outPut;
    }  
     
    public  static  void  Main()    
    {  
    Complex  c1  =  new  Complex();  
    Complex  c2  =  new  Complex();  
      

    Console.WriteLine ("please enter the real1:");
    c1.real=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Real1:    {0}  ",c1.real);      Console.WriteLine ("please enter the imaginary1:");
    c1.imaginary=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Imaginary1:  {0}i  ",c1.imaginary);        Console.WriteLine(  "First  complex  number:    {0}  ",c1);      Console.WriteLine ("please enter the real2:");
    c2.real=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Real2:    {0}  ",c2.real); 

            Console.WriteLine ("please enter the imaginary2:");
    c2.imaginary=Int32.Parse (Console.ReadLine ());
    Console.WriteLine(  "Imaginary2:  {0}i  ",c2.imaginary);
    Console.WriteLine(  "Second  complex  number:  {0}  ",c2);

    Complex  sum  =  c1  +  c2;  
    Console.WriteLine(  "The  sum  of  the  two  numbers:  {0}  ",sum);  
       
    Console.ReadLine();

    }  
    }  
    嘿嘿,对括号把眼睛都对花,kao
      

  4.   

    这个ToString重载,真是厉害,活活