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


    1.前言

    所期待实现的功能包括:

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

    2.内容

    1.头文件 Complex.h

    #pragma once    //保证编译一次
    #include 
    #include    //exit
    #include  //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: 为了较小地影响精度 以及 保持美观,运算结果只保留一位小数输出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.实现 Complex.c

    #include 
    #include 
    #include 
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.输出格式注意点

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

    4.更新日志

    2022.9.8 整理

    欢迎评论留言、指正~~

  • 相关阅读:
    【matplotlib基础】--文本标注
    Dubbo传输层及交换层实现
    瞎扯:修仙文明VS科技文明发展潜力
    黑客动态播报 | 一封假offer,盗取6.25亿美元
    【培训】国产CAE软件(流体、结构仿真、优化、数据建模)免费实操培训课程报名通知
    C#使用MX Component实现三菱PLC软元件数据采集的完整步骤(仿真)
    环保行业采购协同管理系统:助推环保行业电子采购管理转型升级
    第十章 配置数据库(二)
    【付费推广】常见问题合集,推荐榜单FAQ
    屋顶太阳能光伏系统的性能分析指标研究
  • 原文地址:https://blog.csdn.net/qq_60404548/article/details/126772321