qq_33691951 2016-04-06 07:10 采纳率: 40%
浏览 3499
已采纳

返回值是一个复数类的,有虚部和实部,

返回值是一个复数类的,有虚部和实部,代码的返回那块应该怎么写呢?

  • 写回答

6条回答 默认 最新

  • threenewbee 2016-04-06 08:40
    关注
     public struct Complex
    {
        public int real;
        public int imaginary;
    
        // Constructor.
        public Complex(int real, int imaginary)  
        {
            this.real = real;
            this.imaginary = imaginary;
        }
    
        // Specify which operator to overload (+), 
        // the types that can be added (two Complex objects),
        // and the return type (Complex).
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
        }
    
        // Override the ToString() method to display a complex number 
        // in the traditional format:
        public override string ToString()
        {
            return (System.String.Format("{0} + {1}i", real, imaginary));
        }
    }
    
    class TestComplex
    {
        static void Main()
        {
            Complex num1 = new Complex(2, 3);
            Complex num2 = new Complex(3, 4);
    
            // Add two Complex objects by using the overloaded + operator.
            Complex sum = num1 + num2;
    
            // Print the numbers and the sum by using the overridden 
            // ToString method.
            System.Console.WriteLine("First complex number:  {0}", num1);
            System.Console.WriteLine("Second complex number: {0}", num2);
            System.Console.WriteLine("The sum of the two numbers: {0}", sum);
    
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    /* Output:
        First complex number:  2 + 3i
        Second complex number: 3 + 4i
        The sum of the two numbers: 5 + 7i
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波