复数四则运算

烈酒焚心 提交于 2019-12-03 06:32:22
# 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;
 
 
 
 }
   
 
 

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!