C语言实现复数的几个基本操作(四则运算,初始化,销毁...)

1.前言

所期待实现的功能包括:

1.复数的初始化
2.销毁复数
3.获取复数的实部、虚部
4.复数的四则运算
5.如何正确地表示运算过程并输出结果

2.内容

1.头文件 Complex.h

#pragma once    //保证编译一次
#include <stdio.h>
#include <stdlib.h>   //exit
#include <stdbool.h> //bool
typedef struct {
	double Real, Imag;
	bool exist_r, exist_i;
}Complex;
void InitComplex(Complex* c, double real, double  imag);  //初始化
void DestroyComplex(Complex* c);   //销毁复数
double GetReal(Complex c);    //获取复数的实部
double GetImag(Complex c);   //获取复数的虚部
Complex Add(Complex c1, Complex c2);  //  +
Complex Sub(Complex c1, Complex c2); //  -
Complex Mul(Complex c1, Complex c2); //  *
Complex Div(Complex c1, Complex c2); //  /
void PrintComplex(Complex c);    //输出复数[被PrintRes调用]
void PrintRes(Complex c1, Complex c2, Complex c3, char op);  //输出答案
//注1:不用typedef时,定义时要加struct
//注2: 为了较小地影响精度 以及 保持美观,运算结果只保留一位小数输出

2.实现 Complex.c

#include <stdio.h>
#include <Complex.h>
#include <stdbool.h>
typedef struct{
	double Real, Imag;
	bool exist_r, exist_i;
}Complex;

char op[] = { ' ','+','-','*','/' };

void InitComplex(Complex *c, double real, double  imag)
{
	c->Real = real, c->Imag = imag;
	c->exist_r = true, c->exist_i = true;
	puts("Init Successfully");
}

void DestroyComplex(Complex *c)
{
	if (c->exist_r || c->exist_i)
	{
		c->exist_r = c->exist_i = false;
		puts("Destroyed Successfully");
	}
	else
	{
		puts("ERROR ! Invalid Destroy !");
		exit(0);
	}
}

double GetReal(Complex c)
{
	if (c.exist_r) return c.Real;
	else
	{
		puts("ERROR ! Invalid Get Real !");
		exit(0);
	}
}
double GetImag(Complex c)
{
	if (c.exist_i) return c.Imag;
	else
	{
		puts("ERROR ! Invalid Get Imag !");
		exit(0);
	}
}

// +
Complex Add(Complex c1,Complex c2)
{
	if (c1.exist_r && c1.exist_i && c2.exist_r && c2.exist_i)
	{
		Complex c3;
		c3.Real = c1.Real + c2.Real;
		c3.Imag = c1.Imag + c2.Imag;
		return c3;
	}
	else
	{
		puts("ERROR ! Complex Has Been Destroyed !");
		exit(0);
	}
}
// -
Complex Sub(Complex c1,Complex c2)
{
	if (c1.exist_r && c1.exist_i && c2.exist_r && c2.exist_i)
	{
		Complex c3;
		c3.Real = c1.Real - c2.Real;
		c3.Imag = c1.Imag - c2.Imag;
		return c3;
	}
	else
	{
		puts("ERROR ! Complex Has Been Destroyed !");
		exit(0);
	}
}

// *
Complex Mul(Complex c1,Complex c2)
{
	if (c1.exist_r && c1.exist_i && c2.exist_r && c2.exist_i)
	{
		Complex c3;
		c3.Real = c1.Real * c2.Real - c1.Imag * c2.Imag;
		c3.Imag = c1.Real * c2.Imag + c1.Imag * c2.Real;
		return c3;
	}
	else
	{
		puts("ERROR ! Complex Has Been Destroyed !");
		exit(0);
	}
}

//  /
Complex Div(Complex c1,Complex c2)
{
	if (c1.exist_r && c1.exist_i && c2.exist_r && c2.exist_i)
	{
		double t = c2.Real * c2.Real + c2.Imag * c2.Imag;
		if (t)
		{
			Complex c3;
			double r = (c1.Real * c2.Real + c1.Imag * c2.Imag) / t;
			double i = (c1.Imag * c2.Real - c1.Real * c2.Imag) / t;
			c3.Real = r;
			c3.Imag = i;
			return c3;
		}
		else
		{
			puts("The Divisor Is Zero !");
			exit(0);
		}
	}
	else
	{
	puts("ERROR ! Complex Has Been Destroyed !");
	exit(0);
	}
}

void PrintComplex(Complex c)
{
	if (c.exist_r || c.exist_i)
	{
		//存在实部
		if (c.exist_r)
		{
			//实部为0
			if (!c.Real)
			{
				//虚部存在
				if (c.exist_i && c.Imag)
					printf("%.1lfi", c.Imag);
				else //虚部不存在 或者 虚部为0
					printf("0");
			}
			else //实部不为0
			{
				//虚部存在
				if (c.exist_i && c.Imag)
				{
					//正负号处理
					if(c.Imag > 0)
						printf("%.1lf+%.1lfi", c.Real, c.Imag);
					else
						printf("%.1lf%.1lfi", c.Real, c.Imag);
				}
				else  //虚部不存在 或者 虚部为0
					printf("%.1lf", c.Real);
			}
		}
	}
	else  //实部、虚部均不存在
		puts("ERROR ! Invalid Print !");
}
void PrintRes(Complex c1, Complex c2, Complex c3,char op)
{
	PrintComplex(c1);
	printf(" %c ", op);
	PrintComplex(c2);
	printf(" = ");
	PrintComplex(c3);
	puts("");
}

int main()
{
	//Init
	Complex c1,c2,c3;
	puts("请输入复数c1的实部和虚部:");
	double r1, i1;
	scanf("%lf%lf", &r1, &i1);
	InitComplex(&c1, r1, i1);
	puts("请输入复数c2的实部和虚部:");
	double r2, i2;
	scanf("%lf%lf", &r2, &i2);
	InitComplex(&c2, r2, i2);

	//Choose Operation
	printf("请选择运算方式:\n1. +  2. - \n3. *  4. /\n");
	int cs;
	scanf("%d", &cs);
	while (cs < 1 || cs >4)
	{
		puts("Invalid Operation ! Please reinput :");
		scanf("%d", &cs);
	}
	switch (cs)
	{
		case 1: 
		{
			//Operation: Add
			c3 = Add(c1, c2);
			break;
		}
		case 2:
		{
			//Operation: Sub
			c3 = Sub(c1, c2);
			break;
		}
		case 3:
		{
			//Operation: Mul
			c3 = Mul(c1, c2);
			break;
		}
		case 4:
		{
			//Operation: Div
			c3 = Div(c1, c2);
			break;
		}
	}
	
	//Output
	PrintRes(c1, c2, c3,op[cs]);

	return 0;
}

3.总结及思路提示

1.初始化

运用指针,修改结构体复数的实部、虚部,并且将exist_r 、exist_i 赋为true,表示实部、虚部未被销毁

2.销毁

由于C语言中无引用,则不利于销毁操作,则取巧,用两个bool类型的变量记录复数的实部、虚部是否被销毁

3.四则运算

// +
(a+bi) + (c+di) = (a+c) + (b+d)i
// -
(a+bi) - (c+di) = (a-c) + (b-d)i;
// *
(a+bi) * (c+di) = ac + adi +bci - bd = (ac-bd) + (ad+bc)
// /
(a+bi) / (c+di) = (a+bi)(c-di)/(c+di)(c-di) = ( (ac+bd) + (bc-ad)i ) / (c^2 + d^2)

4.输出格式注意点

1. 已经销毁的部分不输出
2. 负数 、 零  的特判输出
3. 连接实部、虚部的符号

4.更新日志

2022.9.8 整理

欢迎评论留言、指正~~

  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值