| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:求大神解决问题!!复数类,其他都正常,一旦输入2+i 和 3-5i 结果就成了 2 ...
取消只看楼主 加入收藏
左左奋强
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-10-27
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:0 
求大神解决问题!!复数类,其他都正常,一旦输入2+i 和 3-5i 结果就成了 2i ??
程序代码:
package com.ShiYan;

import *;

public class ShiYan03_01 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Complex c, c1, c2;
        c = new Complex(0, 0);

        double Real[], Imginary[];
        Real = new double[2];
        Imginary = new double[2];
        String str;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; i < 2; i++) {
            System.out.println("请分别输入第" + (i + 1) + "个数的实部和虚部:");
            str = br.readLine();
            Real[i] = Double.parseDouble(str);
            str = br.readLine();
            Imginary[i] = Double.parseDouble(str);
        }
        c1 = new Complex(Real[0], Imginary[0]);
        c2 = new Complex(Real[1], Imginary[1]);
        int select;
        System.out.println("请选择想要进行的运算序号: 0.加     1.减     2.乘     3。除");
        System.out.println("请输入想要进行的运算(请输入相应运算序号):");
        str = br.readLine();
        select = Integer.parseInt(str);
        if (select == 0)
            c = c.Add(c1, c2);
        else if (select == 1)
            c = c.Minus(c1, c2);
        else if (select == 2)
            c = c.Multiply(c1, c2);
        else if (select == 3)
            c = c.Divide(c1, c2);
        if (c.getImginaryPart() > 0)
            System.out.println(c.getRealPart() + "+" + c.getImginaryPart()
                    + "i");
        else
            System.out.println(c.getRealPart() + c.getImginaryPart() + "i");
    }

}

class Complex { // 构造复数类
    private double RealPart, ImginaryPart;

    public Complex(double r, double i) {// 构造函数
        RealPart = r;
        ImginaryPart = i;
    }

    public Complex() {// 无参构造函数
        RealPart = 0;
        ImginaryPart = 0;
    }

    public double getRealPart() {
        return RealPart;
    }

    public double getImginaryPart() {
        return ImginaryPart;
    }

    public Complex Add(Complex c1, Complex c2) {
        Complex sum = new Complex();
        sum.RealPart = c1.RealPart + c2.RealPart;
        sum.ImginaryPart = c1.ImginaryPart + c2.ImginaryPart;
        return sum;
    }

    public Complex Minus(Complex c1, Complex c2) {
        Complex minus = new Complex();
        minus.RealPart = c1.RealPart - c2.RealPart;
        minus.ImginaryPart = c1.ImginaryPart - c2.ImginaryPart;
        return minus;
    }

    public Complex Multiply(Complex c1, Complex c2) {
        Complex multiply = new Complex();
        multiply.RealPart = c1.RealPart * c2.RealPart - c1.ImginaryPart
                * c2.ImginaryPart;
        multiply.ImginaryPart = c1.ImginaryPart * c2.RealPart + c1.RealPart
                * c2.ImginaryPart;
        return multiply;
    }

    public Complex Divide(Complex c1, Complex c2) {
        Complex divide = new Complex();
        divide.RealPart = (c1.RealPart * c2.RealPart + c1.ImginaryPart
                * c2.ImginaryPart)
                / (c2.RealPart * c2.RealPart + c2.ImginaryPart
                        * c2.ImginaryPart);
        divide.ImginaryPart = (c1.ImginaryPart * c2.RealPart - c1.RealPart
                * c2.ImginaryPart)
                / (c2.RealPart * c2.RealPart + c2.ImginaryPart
                        * c2.ImginaryPart);
        return divide;
    }

}
	
		
			
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: Complex 
2013-10-13 10:01
快速回复:求大神解决问题!!复数类,其他都正常,一旦输入2+i 和 3-5i 结果就成 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028207 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved