7-32 complex four operations

7-32 four operations complex (15 minutes)

Subject description:

This problem requires programming, and complex calculation 2, difference, product, quotient.

Input formats:

2 is given in accordance with a plurality of inputs a1 b1 a2 b2 format on one line C1 = a1 + b1i and C2 = a2 + b2i the real and imaginary parts. Topic guarantee C2 is not zero.

Output formats:

According to (a1 + b1i) operator (a2 + b2i) = 2 sequentially output a plurality of formats, respectively, and the results of line 4, the difference, product, quotient, a number after the decimal point. If the result of the real part or the imaginary part is 0, it is output. If the result is 0, the output 0.0.

Sample input and output:

Here Insert Picture Description

Analysis of ideas:

This question is not very difficult, just to test the input format and output format, This question is just trouble, 1: you have to consider when a float is less than 0.05, greater than -0.05, will be treated as 0. 2: This question can be a time 0 to the imaginary number, negative imaginary outputs only a real number on the list. 3: This question is to consider four operations Points to consider on the line.

Code:

#include<stdio.h>
double lemon(double a,double b)
{
	if(b<0)//考虑负数的问题
	{
		printf("(%.1lf%.1lfi)",a,b);
	}
	else
	printf("(%.1lf+%.1lfi)",a,b);
}
double lemon1(double a,double b)
{
	if(a>=-0.05 && a<=0.05 && b>=-0.05 && b<=0.05)//考虑实数与虚数
	{
		printf("0.0");
	}
	else if(a>=-0.05 && a<=0.05)//只考虑实数
	{
		printf("%.1lfi",b);
	}
	else if(b>=-0.05 && b<=0.05)//只考虑虚数
	{
		printf("%.1lf",a);
	}
	else if(b<0)//当虚数小于0,就输出
	{
		printf("%.1lf%.1lfi",a,b);
	}
	else
	{
		printf("%.1lf+%.1lfi",a,b);
	}
}
int main()
{
	double a,b,c,d,m,n;
		scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
	m=a+c;n=b+d;//加法运算
	lemon(a,b);
	printf(" + ");
	lemon(c,d);
	printf(" = ");
	lemon1(m,n);
	printf("\n");
	 
	 
	 
	 
	m=a-c;n=b-d;//减法运算
	lemon(a,b);
	printf(" - ");
	lemon(c,d);
	printf(" = ");
	lemon1(m,n);
	printf("\n");
	
	
	
	
	m=a*c-b*d;n=a*d+b*c;//乘法运算
	lemon(a,b);
	printf(" * ");
	lemon(c,d);
	printf(" = ");
	lemon1(m,n);
	printf("\n");
	
	
	
	
	
	m=(a*c+b*d)/(c*c+d*d);n=(b*c-a*d)/(c*c+d*d);//除法运算
	lemon(a,b);
	printf(" / ");
	lemon(c,d);
	printf(" = ");
	lemon1(m,n);
} 

Sample test point:

Here Insert Picture Description

Published 17 original articles · won praise 3 · Views 3701

Guess you like

Origin blog.csdn.net/xiaosuC/article/details/104055060
Recommended