计算两个复数之和、差,同时以 a+bi 的字符串形式显示

//班级:软件技术(2)班
//姓名:B07
//完成日期:2014年12月11日
//问题描述:(1)设计复数类Complex,计算两个复数之和、差,同时以 a+bi 的字符串形式显示。
            (2)使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i ,相减产生一个新的复数 -2-2i。
//输入描述:两个实数,两个虚数。
//输出描述:两个复数,两个复数的和,两个复数的差。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication74
{
    class Program
    {
        static void Main(string[] args)
        {
            Complex P1 = new Complex();
            Console.WriteLine("请输入第一个实数");
            P1.A = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入第一个虚数");
            P1.B = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入第二个实数");
            P1.C = Convert.ToDouble(Console.ReadLine());
            Conso
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个复数Complex的示例代码,包括成员变量、getter和setter、构造方法及方法: ```java public class Complex { private double real; // 复数的实部 private double imaginary; // 复数的虚部 public Complex() { this.real = 0; this.imaginary = 0; } public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public double getReal() { return real; } public void setReal(double real) { this.real = real; } public double getImaginary() { return imaginary; } public void setImaginary(double imaginary) { this.imaginary = imaginary; } // 计算两个复数之和 public Complex add(Complex other) { double sumReal = this.real + other.real; double sumImaginary = this.imaginary + other.imaginary; return new Complex(sumReal, sumImaginary); } // 计算两个复数 public Complex subtract(Complex other) { double diffReal = this.real - other.real; double diffImaginary = this.imaginary - other.imaginary; return new Complex(diffReal, diffImaginary); } // 以 a+bi字符串形式显示复数 public String toString() { String sign = (this.imaginary >= 0) ? "+" : "-"; return this.real + sign + Math.abs(this.imaginary) + "i"; } } ``` 使用方法如下: ```java Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, 4); Complex sum = c1.add(c2); Complex diff = c1.subtract(c2); System.out.println("Sum: " + sum.toString()); // 输出 "Sum: 4+6i" System.out.println("Difference: " + diff.toString()); // 输出 "Difference: -2-2i" ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值