其他分享
首页 > 其他分享> > 复数四则运算

复数四则运算

作者:互联网

# include <stdio.h>
# include <stdlib.h>
struct complex{
  float real;
  float imaginary; };
void initcomplex(complex &c,float r1,float r2)
{
 c.real=r1;
 c.imaginary=r2;
}
void addcomplex(complex &c,complex c1,complex c2)
{
 c.real=c1.real+c2.real;
 c.imaginary=c1.imaginary+c2.imaginary;
}
void subcomplex(complex &c,complex c1,complex c2)
{
 c.real=c1.real-c2.real;
 c.imaginary=c1.imaginary-c2.imaginary;  
}
void mulcomplex(complex &c,complex c1,complex c2)
{
 c.real=(c1.real*c2.real)-(c1.imaginary*c2.imaginary);
 c.imaginary=(c1.real)*(c2.imaginary)+(c1.imaginary)*(c2.real);
}
void main()
{
 complex c,c1,c2;
 printf("请输入十组实部:\n");
 float r[9],imaginary[9];
 int i,node;
 for(i=0;i<=9;i++)
 {
  scanf("%f",&r[i]);  }
    printf("请输入十组虚部:\n");
    for(i=0;i<=9;i++)
 {
 scanf("%f",&imaginary[i]);
 }
 printf("1:复数相加   2:复数相减   3:复数相乘\n");
 printf("请输入序号:\n");
 scanf("%d",&node);
 int j=0;
 switch(node)
 {
 case 1:printf("请输入复数序号(0-9):\n");
  scanf("%d",&j);
  if(j>=0&&j<=9)
  {
   initcomplex(c1,r[j],imaginary[j]);   }
  printf("请输入复数序号(0-9):\n");
   scanf("%d",&j);
  if(j>=0&&j<=9)
  {
  initcomplex(c2,r[j],imaginary[j]);
  }
  addcomplex(c,c1,c2);
  printf("结果为:%f+%fi",c.real,c.imaginary);
  break;
 case 2:printf("请输入复数序号:\n");
  scanf("%d",&j);
  if(j>=0&&j<=9)
  {
   initcomplex(c1,r[j],imaginary[j]);   }
  printf("请输入复数序号:\n");
   scanf("%d",&j);
  if(j>=0&&j<=9)
  {
  initcomplex(c2,r[j],imaginary[j]);
  }
  subcomplex(c,c1,c2);
  printf("结果为:%f+%fi",c.real,c.imaginary);
  break;
 case 3:printf("请输入复数序号:\n");
  scanf("%d",&j);
  if(j>=0&&j<=9)
  {
   initcomplex(c1,r[j],imaginary[j]);   }
  printf("请输入复数序号:\n");
   scanf("%d",&j);
  if(j>=0&&j<=9)
  {
  initcomplex(c2,r[j],imaginary[j]);
  }
  mulcomplex(c,c1,c2);
  printf("结果为:%f+%fi",c.real,c.imaginary);
  break;    
 
 }
       
}

标签:real,四则运算,complex,复数,printf,c2,imaginary,c1
来源: https://www.cnblogs.com/P201821430014/p/11780717.html