2020-07-22 16:24:10
浏览数 (1)
算结构题练习题吧
Talk is cheap,show you the code.
代码语言:javascript
复制#include<stdio.h>
#include<math.h>
#include<string.h>
typedef struct complex{
double real;
double virt;
}COM;
//复数求和的实现函数
COM summation(COM num1,COM num2){
COM result;
result.real=num1.real num2.real;
result.virt=num1.virt num2.virt;
return result;
}
//复数乘积的实现函数 (a bi)*(c di) = (ac-bd) (bc ad)i
COM multiplication(COM num1,COM num2){
COM result;
result.real= num1.real*num2.real-num1.virt*num2.virt;
result.virt= num1.virt*num2.real num1.real*num2.virt;
return result;
}
//复数除法的实现 (a bi)/(c di)=(ac bd)/(c2 d2) ((bc-ad)/(c2 d2))i
COM division(COM num1,COM num2){
COM result;
result.real=(num1.real*num2.real num1.virt*num2.virt)/(num2.real*num2.real num2.virt*num2.virt);
result.virt=(num1.virt*num2.real-num1.real*num2.virt)/(num2.real*num2.real num2.virt*num2.virt);
return result;
}
// 将结果进行输出
void printResult(COM result){
double virtPart=fabs(result.virt);
char virtPartStr[100],realPartStr[100];
sprintf(virtPartStr,"%lf",virtPart);
sprintf(realPartStr,"%lf",result.real);
//清除末尾0,如将1.16000输出为1.16
for(int i=strlen(virtPartStr);i>0;i--){
if(virtPartStr[i]=='0'&&virtPartStr[i-1]!='0'){
virtPartStr[i]='