c语言抽象数据类型-复数

设计一个可进行复数运算的演示程序,要求实现下列四种基本运算:

1)由输入的实部和虚部生成一个复数;

2)两个复数求和;

3)两个复数求差;

4)两个复数求积;

5)打印输出一个复数。

复制代码
#include <stdio.h>
typedef struct {
	float x;
	float y;
} complex;
void creat(complex* c);
void outputc(complex a);
complex add(complex k, complex h);
complex sub(complex k, complex h);
complex chengji(complex k, complex h);
complex a, b, a1;
int cmd;
int main() {
	creat(&a);
	outputc(a);
	creat(&b);
	outputc(b);
	printf("相加");
	a1 = add(a, b);
	outputc(a1);
	printf("相减");
	a1 = sub(a, b);
	outputc(a1);
	printf("相乘");
	a1 = chengji(a, b);
	outputc(a1);
	return 0;
}
void creat(complex* c)
{
	float x1, y1;
	printf("\n输入实部 x = ");
	scanf_s("%f", &x1);
	printf("\n输入虚部 y = ");
	scanf_s("%f", &y1);
	c->x = x1;
	c->y = y1;
}

void outputc(complex a)
{
	printf("复数: %lf + %lfi\n", a.x, a.y);
}
complex add(complex k, complex h)
{
	complex l;
	l.x = k.x + h.x;
	l.y = k.y + h.y;
	return l;
}
complex sub(complex k, complex h)
{
	complex l;
	l.x = k.x - h.x;
	l.y = k.y - h.y;
	return l;
}
complex chengji(complex k, complex h)
{
	complex l;
	l.x = k.x * h.x - k.y * h.y;
	l.y = k.x * h.y + k.y * h.x;
	return l;
}
相关推荐
口袋物联18 分钟前
设计模式之工厂模式在 C 语言中的应用(含 Linux 内核实例)
linux·c语言·设计模式·简单工厂模式
Want5952 小时前
C/C++跳动的爱心①
c语言·开发语言·c++
lingggggaaaa3 小时前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
gfdhy3 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
weixin_457760003 小时前
Python 数据结构
数据结构·windows·python
我不会插花弄玉3 小时前
vs2022调试基础篇【由浅入深-C语言】
c语言
明洞日记4 小时前
【数据结构手册002】动态数组vector - 连续内存的艺术与科学
开发语言·数据结构·c++
福尔摩斯张5 小时前
《C 语言指针从入门到精通:全面笔记 + 实战习题深度解析》(超详细)
linux·运维·服务器·c语言·开发语言·c++·算法
fashion 道格5 小时前
数据结构实战:深入理解队列的链式结构与实现
c语言·数据结构
xxxxxxllllllshi5 小时前
【LeetCode Hot100----14-贪心算法(01-05),包含多种方法,详细思路与代码,让你一篇文章看懂所有!】
java·数据结构·算法·leetcode·贪心算法