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;
}
相关推荐
RuoZoe17 小时前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
祈安_4 天前
C语言内存函数
c语言·后端
norlan_jame6 天前
C-PHY与D-PHY差异
c语言·开发语言
琢磨先生David6 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
czy87874756 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237176 天前
C语言-数组练习进阶
c语言·开发语言·算法
qq_454245036 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝6 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA6 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc6 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法