复数四则运算

简介: 复数四则运算

7-76 复数四则运算 (15 分)


本题要求编写程序,计算2个复数的和、差、积、商。


输入格式:


输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部。题目保证C2不为0。


输出格式:


分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。


输入样例1:


2 3.08 -2.04 5.06


结尾无空行


输出样例1:


(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i


结尾无空行


输入样例2:


1 1 -1 -1.01


输出样例2:


(1.0+1.0i) + (-1.0-1.0i) = 0.0
(1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
(1.0+1.0i) * (-1.0-1.0i) = -2.0i
(1.0+1.0i) / (-1.0-1.0i) = -1.0


#include<iostream>
using namespace std;
int main(){
    double a1,b1,a2,b2;
    scanf("%lf%lf%lf%lf",&a1,&b1,&a2,&b2);
    {
        double a,b;
        a=a1+a2;
        b=b1+b2;
//         cout<<a<<endl;
        if(a==0&&b==0) printf("(%.1lf+%.1lfi) + (%.1lf-%.1lfi) = 0.0",a1,b1,a2,b2);
        else if(a!=0&&b!=0) printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a1,b1,a2,b2,a,b);
        else if(a==0) printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = %.1lfi\n",a1,b1,a2,b2,b);
        else if(b==0) printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = %.1lf\n",a1,b1,a2,b2,a);
    }
    {
        double a,b;
        a=a1-a2;
        b=b1-b2;
        if(a==0&&b==0) printf("(%.1lf+%.1lfi) - (%.1lf-%.1lfi) = 0.0",a1,b1,a2,b2);
        else if(a!=0&&b!=0) printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a1,b1,a2,b2,a,b);
        else if(a==0)printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lfi\n",a1,b1,a2,b2,b);
        else if(b==0) printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lf\n",a1,b1,a2,b2,a);
    }
    {
        double a,b;
        a=a1*a2-b1*b2;
        b=a1*b2+a2*b1;
        if(b==0) printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lf\n",a1,b1,a2,b2,a);
        else if(a==0)printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lfi\n",a1,b1,a2,b2,b);
        else if(a!=0&&b!=0) printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a1,b1,a2,b2,a,b);
        else if(a==0&&b==0) printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = 0.0",a1,b1,a2,b2);
    }
    {
        double a,b;
        a=(a1*a2+b1*b2)/(a2*a2+b2*b2);
        b=(-a1*b2+a2*b1)/(a2*a2+b2*b2);
        if(b==0) printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = %.1lf\n",a1,b1,a2,b2,a);
        else if(a==0)printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = %.1lfi\n",a1,b1,a2,b2,b);
        else if(a!=0&&b!=0) printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a1,b1,a2,b2,a,b);
        else if(a==0&&b==0) printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = 0.0",a1,b1,a2,b2);
    }
    return 0;
}


最先开始的思路,结果发现处理不了虚部的符号,只得运用数据结构


#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    double a;
    double b;
}fu;
int pan(double a, double b)  //判断实部和虚部是否为零
{
    if(a==0&& b!=0)
        return 1;
    else if(a!=0 && b==0)
        return 2;
    else if(a==0 && b==0)
        return 3;
    else if(a!=0 && b!=0)
        return 0;
}
double qu(double a)    //四舍五入函数 只对小数点后两位做
{
    double b=(int)(a*10)/10;
    double s=a*100;
    int p=(int)s%10;
    if(abs(p)>=5)
        return a<0?(b-0.1):(b+0.1);
    else if(b==0)
        return 0;
    else if(abs(p)<5 && abs(p)>0)
        return b;
    else if(abs(p)==0)
        return a;
}
int main()
{
    double a1, b1, a2, b2;
    fu f[4];
    char ch[4]={'+', '-', '*', '/'};
    scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);   //输入
    f[0].a=a1+a2;
    f[0].b=b1+b2;
    f[1].a=a1-a2;
    f[1].b=b1-b2;
    f[2].a=a1*a2-b1*b2;
    f[2].b=a1*b2+a2*b1;
    f[3].a=(a1*a2+b1*b2)/(a2*a2+b2*b2);
    f[3].b=(b1*a2-a1*b2)/(a2*a2+b2*b2);
  //  printf("%lf\n", qu(f[3].a));
 /*   for(int i=0; i<4; i++)
    {
        printf("%.1lf %.1lf\n\n", f[i].a, f[i].b);
        f[i].a=qu(f[i].a);
        f[i].b=qu(f[i].b);
        printf("%.1lf %.1lf\n", f[i].a, f[i].b);
    }*/
    for(int i=0; i<4; i++)    //判断并输出
    {
        if(pan(qu(f[i].a), qu(f[i].b))==1)
            printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = %.1lfi\n", a1, b1, ch[i], a2, b2, f[i].b);
        else if(pan(qu(f[i].a), qu(f[i].b))==2)
            printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = %.1lf\n", a1, b1, ch[i], a2, b2, f[i].a);
        else if(pan(qu(f[i].a), qu(f[i].b))==3)
            printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = 0.0\n", a1, b1, ch[i], a2, b2);
        else if(pan(qu(f[i].a), qu(f[i].b))==0)
            printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = %.1lf%+.1lfi\n", a1, b1, ch[i], a2, b2, f[i].a, f[i].b);
    }
    return 0;
}


别人的思路


#include<stdio.h>
void shizi(double x,double y);    //负责打印式子
void result(double x,double y);   //负责打印结果
struct Love{    
  double shi,xu;  //表示实部,虚部
}love1,love2;
int main()
{ 
  scanf("%lf%lf%lf%lf",&love1.shi,&love1.xu,&love2.shi,&love2.xu);
  //下面的计算根据复数的实部与虚部计算公式:
  //求加法的实部与虚部值
  double jia_shi = love1.shi + love2.shi;
  double jia_xu  = love1.xu  + love2.xu;
  //求减法的实部与虚部值
  double jian_shi = love1.shi - love2.shi;
  double jian_xu  = love1.xu  - love2.xu;
  //求乘法的实部与虚部值
  double cheng_shi = love1.shi*love2.shi-love1.xu*love2.xu;
  double cheng_xu  = love1.xu*love2.shi+love1.shi*love2.xu;
  //求除法的实部与虚部值
  double chu_shi = (love1.shi*love2.shi+love1.xu*love2.xu)/(love2.shi*love2.shi+love2.xu*love2.xu);
  double chu_xu  = (love1.xu*love2.shi-love1.shi*love2.xu)/(love2.shi*love2.shi+love2.xu*love2.xu);
  //因为题目的输出结果要分多种情况,那就在函数里来判断情况输出吧
  //加法
  shizi(love1.shi,love1.xu);  //式子
  printf(" + ");
  shizi(love2.shi,love2.xu);  //式子
  printf(" = ");
  result(jia_shi,jia_xu);   //结果
  //减法
  shizi(love1.shi,love1.xu);  //式子
  printf(" - ");
  shizi(love2.shi,love2.xu);  //式子
  printf(" = ");
  result(jian_shi,jian_xu);   //结果
  //乘法
  shizi(love1.shi,love1.xu);  //式子
  printf(" * ");
  shizi(love2.shi,love2.xu);  //式子
  printf(" = ");
  result(cheng_shi,cheng_xu);   //结果
  //除法
  shizi(love1.shi,love1.xu);  //式子
  printf(" / ");
  shizi(love2.shi,love2.xu);  //式子
  printf(" = ");
  result(chu_shi,chu_xu);   //结果
  return 0;
}
void shizi(double x,double y)   //负责打印式子
{
  if(y<0)             //虚部为负数的时候不用加'+'号
    printf("(%.1lf%.1lfi)",x,y);
  else
    printf("(%.1lf+%.1lfi)",x,y); //反之成立
}
void result(double rshi,double rxu)
{ 
  //虚部不存在时,只需输出实部
  if(rxu<=0.05&&rxu>=-0.05) //其实这里也判断了两段都没有的情况,输出0.0
    printf("%.1lf\n",rshi);
  else if(rshi<=0.05&&rshi>=-0.05)  //实部不存在时,输出虚部单个即可
    printf("%.1lfi\n",rxu);
  else if(rxu<0)
    printf("%.1lf%.1lfi\n",rshi,rxu); //结果虚部为负数的时候不用加'+'号
  else
    printf("%.1lf+%.1lfi\n",rshi,rxu);  //反之成立
}


/*虚部 实部分别进行计算
 * 
 * 
 * 
*/ 
#include<stdio.h>
#include<math.h>
void result(double a1,double b1,double a2,double b2,int i); 
int main()
{
  double  a1,b1,a2,b2;
  scanf("%lf%lf%lf%lf",&a1,&b1,&a2,&b2);
  for(int i=0;i<4;i++)
  {
    if(i==0)
    {
      if(b1>=0&&b2>=0)
      printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0 && b2>=0)
      printf("(%.1lf%.1lfi) + (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0 && b2<0)
      printf("(%.1lf%.1lfi) + (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      else
      printf("(%.1lf+%.1lfi) + (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      result(a1,b1,a2,b2,i);
    }
    else if(i==1){
      if(b1>=0&&b2>=0)
      printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0 && b2>=0)
      printf("(%.1lf%.1lfi) - (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0 &&b2<0)
      printf("(%.1lf%.1lfi) - (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      else
      printf("(%.1lf+%.1lfi) - (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      result(a1,b1,a2,b2,i);
    }
    else if(i==2){
      if(b1>=0&&b2>=0)
      printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0 && b2>=0)
      printf("(%.1lf%.1lfi) * (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0&&b2<0)
      printf("(%.1lf%.1lfi) * (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      else
      printf("(%.1lf+%.1lfi) * (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      result(a1,b1,a2,b2,i);
    }
    else if(i==3){
      if(b1>=0&&b2>=0)
      printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0 && b2>=0)
      printf("(%.1lf%.1lfi) / (%.1lf+%.1lfi) = ",a1,b1,a2,b2);
      else if(b1<0&&b2<0)
      printf("(%.1lf%.1lfi) / (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      else
      printf("(%.1lf+%.1lfi) / (%.1lf%.1lfi) = ",a1,b1,a2,b2);
      result(a1,b1,a2,b2,i);
    }
  }
}
void result(double a1,double b1,double a2,double b2,int i){
  double really,v;
  if(i==0){
    really=a1+a2;
    v=b1+b2;
  }
  else if(i==1){
    really=a1-a2;
    v=b1-b2;
  }
  else if(i==2){
    really=a1*a2-b1*b2;
    v=a2*b1+a1*b2;
  }
  else if(i==3){
    /*double fenzi,fenmu;
//    MultReally(a1,b1,a2,-b2)+MultV(a1,b1,a2,-b2);
    fenmu=a2*a2-b2*b2;
    really=MultReally(a1,b1,a2,-b2)/fenmu;
    v=MultV(a1,b1,a2,-b2)/fenmu;*/
    really=(a1*a2+b1*b2)/(a2*a2+b2*b2);
      v=(a2*b1-a1*b2)/(a2*a2+b2*b2);
    if(a2==0&&b2==0){
      really=0;
      v=0;
    }
  }
//  if(really==0) printf("yes\n");
  if(fabs(really)<0.1&&fabs(v)>0.1){//实部为0 
    printf("%.1fi\n",v);
  }
  else if(fabs(really)>0.1&&fabs(v)<0.1)
  {//虚部为0 
    printf("%.1lf\n",really);
  }
  else if(fabs(really)>0.1&&fabs(v)>0.1){
    if(v<0)
      printf("%.1lf%.1lfi\n",really,v);
    else
      printf("%.1lf+%.1lfi\n",really,v);
  }
  else if(fabs(really)<0.1&&fabs(v)<0.1) printf("0.0\n");
}


#include<iostream>
using namespace std;
void reslut(double x,double y){
    if(y<0.05&&y>=-0.05)printf("%.1lf\n",x);
    else if(x<=0.05&&x>=-0.05)printf("%.1lfi\n",y);
    else if(y<0)printf("%.1lf%.1lfi\n",x,y);
    else printf("%.1lf+%.1lfi\n",x,y);
}
void shizi(double a,double b){
    if(b<0)printf("(%.1lf%.1lfi)",a,b);
    else printf("(%.1lf+%.1lfi)",a,b);
}
int main(){
//     double a,b,c,d,x=0,y=0;
//     cin>>a>>b>>c>>d;
//     for(int i=0;i<4;i++){
//         if(i==0){
//             x=a+c;
//             y=b+d;
//             if(y<0.05&&y>=-0.05)printf("(%.1lf-%.1lfi) + (%.1lf+%.1lfi) = %.1lf\n",a,b,c,d,x);
//             else if(x<=0.05&&x>=-0.05)printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = %.1lfi\n",a,b,c,d,y);
//             else if(y<0)printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = %.1lf%.1lfi\n",a,b,c,d,x,y);
//             else printf("(%.1lf+%.1lfi) + (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a,b,c,d,x,y);
//         }
//         if(i==1){
//             x=a-c;
//             y=b-d;
//             if(y<0.05&&y>=-0.05)printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lf\n",a,b,c,d,x);
//             else if(x<=0.05&&x>=-0.05)printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lfi\n",a,b,c,d,y);
//             else if(y<0)printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lf%.1lfi\n",a,b,c,d,x,y);
//             else printf("(%.1lf+%.1lfi) - (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a,b,c,d,x,y);
//         }
//         if(i==2){
//             x=a*c-b*d;
//             y=a*d+b*c;
//             if(y<0.05&&y>=-0.05)printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lf\n",a,b,c,d,x);
//             else if(x<=0.05&&x>=-0.05)printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lfi\n",a,b,c,d,y);
//             else if(y<0)printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lf%.1lfi\n",a,b,c,d,x,y);
//             else printf("(%.1lf+%.1lfi) * (%.1lf+%.1lfi) = %.1lf+%.1lfi\n",a,b,c,d,x,y);
//         }
//         if(i==3){
//             x=(a*c+b*d)/(c*c+d*d);
//             y=(-a*d+b*c)/(c*c+d*d);
//             if(y<0.05&&y>=-0.05)printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = %.1lf\n",a,b,c,d,x);
//             else if(x<=0.05&&x>=-0.05)printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = %.1lfi\n",a,b,c,d,y);
//             else if(y<0)printf("(%.1lf+%.1lfi) / (%.1lf+%.1lfi) = %.1lf%.1lfi\n",a,b,c,d,x,y);
//             else printf("(%.1lf+%.1lfi) + (%.1lf/%.1lfi) = %.1lf+%.1lfi\n",a,b,c,d,x,y);
//         }
//     }
    double a,b,c,d,x,y;
    cin>>a>>b>>c>>d;
    for(int i=0;i<4;i++){
        if(i==0){
            x=a+c;
            y=b+d;
            shizi(a,b);
            cout<<" + ";
            shizi(c,d);
            cout<<" = ";
            reslut(x,y);
        }
        if(i==1){
            x=a-c;
            y=b-d;
            shizi(a,b);
            cout<<" - ";
            shizi(c,d);
            cout<<" = ";
            reslut(x,y);
        }
        if(i==2){
            x=a*c-b*d;
            y=a*d+b*c;
            shizi(a,b);
            cout<<" * ";
            shizi(c,d);
            cout<<" = ";
            reslut(x,y);
        }
        if(i==3){
            x=(a*c+b*d)/(c*c+d*d);
            y=(-a*d+b*c)/(c*c+d*d);
            shizi(a,b);
            cout<<" / ";
            shizi(c,d);
            cout<<" = ";
            reslut(x,y);
        }
    }
    return 0;
}


目录
相关文章
|
1月前
复数相加
复数相加。
42 5
|
2月前
什么是复数
【10月更文挑战第12天】什么是复数
195 1
|
2月前
|
存储 C语言
复数相减之运算符的重载
复数相减之运算符的重载
30 0
|
6月前
537. 复数乘法
537. 复数乘法
|
7月前
leetcode-537:复数乘法
leetcode-537:复数乘法
37 0
wustojc2002整数四则运算
wustojc2002整数四则运算
67 0
深入理解算数运算符
深入理解算数运算符
56 0
|
机器学习/深度学习 SQL 算法
用Python实现复数的四则运算
用Python实现复数的四则运算
|
存储 程序员 数据安全/隐私保护
算数运算符
在 Python 中 `*` 运算符还可以用于字符串,计算结果就是字符串重复指定次数的结果。`+`运算符可以让两个字符串相加
108 0