复数计算器

         复数的加减乘法,和普通的加减乘法有一个区别在于,复数有实部和虚部,虽然进行运算的时候差不多,只需要实部和实部操作,虚部和虚部操作,但是显示的时候就需要考虑到好几种情况了。具体地说有四种情况,1.实部和虚部为0。

2.实部等于0,虚部不等于0。3实部不等于0,虚部等于0. 4.实部和虚部都不等于0.

代码如下

#include<stdio.h>
#include<string.h>

typedef struct MyComplex  //构造复数结构的结构体
{
	double real;          //保存复数的实部
	double img;           //保存复数的虚部
}mc;

mc Plus(mc a,mc b)               //复数的加法操作实现
{
	mc result;                   //声明一个复数结构体变量
	result.real=a.real+b.real;   //两个复数的实部相加
	result.img =a.img+b.img;     //两个复数的虚部相加
	return result;
}
mc Sub(mc a,mc b)                //复数的减法操作实现
{
	mc result;
	result.real=a.real-b.real;
	result.img=a.img-b.img;
	return result;
}
mc Multi(mc a,mc b)              //复数的乘法操作实现
{
	mc result;
	result.real=a.real *b.real-a.img*b.img;
	result.img=a.img*b.real +b.img*a.real;
	return result;
}

void Show(mc a)                 //复数的显示函数
{
	if(a.real==0 &&a.img==0)    //如果实部和虚部都为0
		printf("0.000\n");
	else if(a.real ==0)         //如果实部为0
		printf("%.4fi\n",a.img);//输出小数点后四位的浮点数   
	else if(a.img==0)           //如果虚部等于0
		printf("%.4f\n",a.real);//输出小数点后四位的浮点数
	else                        //如果都不为0
	{
		if(a.img<0)
			printf("%4f%.4fi\n",a.real,a.img);
		else
			printf("%4.f+%.4fi\n",a.real,a.img);
	}
}
int main()
{
	mc a,b;
	printf("输入第一个复数 a:\n");
	scanf("%lf%lf",&a.real,&a.img);
	printf("输入第二个复数 b:\n");
	scanf("%lf%lf",&b.real,&b.img);
	printf("a b 的和为:");
	Show(Plus(a,b));
	printf("a b 的差为:");
	Show(Sub(a,b));
	printf("a b的乘为: ");
	Show(Multi(a,b));
	return 0;
}

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java实现的GUI复数计算器的例子: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComplexCalculator extends JFrame implements ActionListener { private JTextField jtf1, jtf2, jtf3, jtf4; private JButton jb1, jb2, jb3, jb4; public ComplexCalculator() { super("复数计算器"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(3, 4)); JLabel jl1 = new JLabel("实部1:"); add(jl1); jtf1 = new JTextField(10); add(jtf1); JLabel jl2 = new JLabel("虚部1:"); add(jl2); jtf2 = new JTextField(10); add(jtf2); JLabel jl3 = new JLabel("实部2:"); add(jl3); jtf3 = new JTextField(10); add(jtf3); JLabel jl4 = new JLabel("虚部2:"); add(jl4); jtf4 = new JTextField(10); add(jtf4); jb1 = new JButton("+"); add(jb1); jb1.addActionListener(this); jb2 = new JButton("-"); add(jb2); jb2.addActionListener(this); jb3 = new JButton("*"); add(jb3); jb3.addActionListener(this); jb4 = new JButton("/"); add(jb4); jb4.addActionListener(this); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { double a1 = Double.parseDouble(jtf1.getText()); double b1 = Double.parseDouble(jtf2.getText()); double a2 = Double.parseDouble(jtf3.getText()); double b2 = Double.parseDouble(jtf4.getText()); ComplexNumber x = new ComplexNumber(a1, b1); ComplexNumber y = new ComplexNumber(a2, b2); if (e.getSource() == jb1) { ComplexNumber z = x.add(y); JOptionPane.showMessageDialog(null, "结果为:" + z.toString()); } else if (e.getSource() == jb2) { ComplexNumber z = x.subtract(y); JOptionPane.showMessageDialog(null, "结果为:" + z.toString()); } else if (e.getSource() == jb3) { ComplexNumber z = x.multiply(y); JOptionPane.showMessageDialog(null, "结果为:" + z.toString()); } else if (e.getSource() == jb4) { ComplexNumber z = x.divide(y); JOptionPane.showMessageDialog(null, "结果为:" + z.toString()); } } public static void main(String[] args) { new ComplexCalculator(); } } class ComplexNumber { private double real; private double imaginary; public ComplexNumber(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public ComplexNumber add(ComplexNumber other) { double real = this.real + other.real; double imaginary = this.imaginary + other.imaginary; return new ComplexNumber(real, imaginary); } public ComplexNumber subtract(ComplexNumber other) { double real = this.real - other.real; double imaginary = this.imaginary - other.imaginary; return new ComplexNumber(real, imaginary); } public ComplexNumber multiply(ComplexNumber other) { double real = this.real * other.real - this.imaginary * other.imaginary; double imaginary = this.real * other.imaginary + this.imaginary * other.real; return new ComplexNumber(real, imaginary); } public ComplexNumber divide(ComplexNumber other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double real = (this.real * other.real + this.imaginary * other.imaginary) / denominator; double imaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator; return new ComplexNumber(real, imaginary); } public String toString() { if (imaginary == 0) { return Double.toString(real); } else if (real == 0) { return Double.toString(imaginary) + "i"; } else if (imaginary < 0) { return Double.toString(real) + "-" + Double.toString(-imaginary) + "i"; } else { return Double.toString(real) + "+" + Double.toString(imaginary) + "i"; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值