SHAOXIAOJ正在加载中...

1622: 绪论-复数运算

金币值:2 定数:3 时间限制:1.000 s 内存限制:128 M
正确:27 提交:57 正确率:47.37% 命题人:
点赞量:0 收藏量:0 题目类型:程序 知识点: 数据结构-绪论

题目描述

设计一个复数数据结构,主要操作有复数的初始化、加、减和乘4种,要求利用这个数据结构,计算两个复数之和与两个复数之差的乘积,并打印此结果。
#include <stdio.h>

struct complex {
	double real;
	double image;
};

struct complex init(double real, double image);
struct complex add(struct complex a, struct complex b);
struct complex sub(struct complex a, struct complex b);
struct complex mul(struct complex a, struct complex b);

int main(void) {
	double real,image;
	struct complex x,y,z,t1,t2;
	scanf("%lf%lf",&real,&image);
	x=init(real,image);
	scanf("%lf%lf",&real,&image);
	y=init(real,image);
	t1=add(x,y);
	t2=sub(x,y);
	z=mul(t1,t2);

	if(z.image>=0){
		printf("%.2lf+%.2fi\n",z.real,z.image);
	}
	else{
		printf("%.2lf%.2fi\n",z.real,z.image);
	}

	return 0;
}

/*提交以下代码*/
struct complex init(double real, double image) {
	
}

struct complex add(struct complex a, struct complex b) {
	
}

struct complex sub(struct complex a, struct complex b) {
	
}

struct complex mul(struct complex a, struct complex b) {
	
}

输入格式

共2行:每行2个实数,分别表示复数的实部和虚部

输入样例    复制

1 2
2 3

输出样例    复制

2.00-8.00i

提示

本题的目的是让学生初步了解抽象数据类型的实现