美文网首页
(改)~-4 复数四则运算

(改)~-4 复数四则运算

作者: laochonger | 来源:发表于2018-03-14 08:32 被阅读0次

    以下是我的错误代码(失了智)

    #include<cstdio>
    using namespace std;
    
    double q,w;
    void p(double a, double b, double c, double d){
        double jj,kk;
        double j = a + c;
        double k = b + d;
        //printf("%f    %f \n", j ,k);
        if(j < 0) jj = -j;
        if(k < 0) kk = -k;
        if( jj<0.05  && kk<0.05)
        printf("(%.1f+%.1fi) + (%.1f-%.1fi) = %.1f\n", a, b, c, d, 0.0);
        else if(jj<0.05) printf("(%.1f+%.1fi) + (%.1f-%.1fi) = %.1fi\n", a,b,c,d,k);
        else if(kk<0.05) printf("(%.1f+%.1fi) + (%.1f-%.1fi) = %.1f\n", a,b,c,d,j);
        else
            printf("(%.1f+%.1fi) + (%.1f-%.1fi) = %.1f+%.1fi\n", a,b,c,d,j,k);
    }
    
    void de(double a, double b, double c, double d){
        double jj,kk;
        double j = a - c;
        double k = b - d;
        if(j < 0) jj = -j;
        if(k < 0) kk = -k;
        if(jj<0.05&& kk<0.05)
        printf("(%.1f+%.1fi) - (%.1f-%.1fi) = %.1f\n", a, b, c, d, 0.0);
        else if(jj<0.05) printf("(%.1f+%.1fi) - (%.1f-%.1fi) = %.1fi\n", a,b,c,d,k);
        else if(kk<0.05) printf("(%.1f+%.1fi) - (%.1f-%.1fi) = %.1f\n", a,b,c,d,j);
        else
            printf("(%.1f+%.1fi) - (%.1f-%.1fi) = %.1f+%.1fi\n", a,b,c,d,j,k);
    }
    
    void x(double a, double b, double c, double d){
        q = a * c - b * d;
        w = a * d + b * c;
    }
    
    void chu(double a, double b, double c, double d){
        double qq,ww;
        double j = c * c + d * d;
        x(a, b, c , -d);
        q = q/j;
        w = w/j;
        if(q < 0) qq = -q;
        if(w < 0) ww = -w;
        if(q<0.05&& w<0.05)
        printf("(%.1f+%.1fi) / (%.1f-%.1fi) = %.1f\n", a, b, c, d, 0.0);
        else if(qq<0.05) printf("(%.1f+%.1fi) / (%.1f-%.1fi) = %.1fi\n", a,b,c,d,w);
        else if(ww<0.05) printf("(%.1f+%.1fi) / (%.1f-%.1fi) = %.1f\n", a,b,c,d,q);
        else
            printf("(%.1f+%.1fi) / (%.1f-%.1fi) = %.1f+%.1fi\n", a,b,c,d,q,w);
    }
    
    int main(){
        double a,b,c,d;
        while( scanf("%lf %lf %lf %lf", &a, &b ,&c ,&d) == 4) {
            double qq,ww;
            p(a,b,c,d);
            de(a,b,c,d);
            x(a,b,c,d);
            if(q < 0) qq = -q;
            if(w < 0) ww = -w;
            if(qq<0.05&& ww<0.05)
                printf("(%.1f+%.1fi) * (%.1f-%.1fi) = %.1f\n", a, b, c, d, 0.0);
            else if(qq<0.05) printf("(%.1f+%.1fi) * (%.1f-%.1fi) = %.1fi\n", a,b,c,d,w);
            else if(ww<0.05) printf("(%.1f+%.1fi) * (%.1f-%.1fi) = %.1f\n", a,b,c,d,q);
            else
                printf("(%.1f+%.1fi) * (%.1f-%.1fi) = %.1f+%.1fi\n", a,b,c,d,q,w);
            chu(a,b,c,d);
        }
        return 0;
    }
    

    以下是学长的代码:

    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    #include <queue>
    
    using namespace std;
    #define For(i,l,r) for(int (i)=(int)(l);(i)<(int)(r);++(i))
    #define FOR(i,l,r) for(int (i)=(int)(l);(i)<=(int)(r);++(i))
    #define rof(i,l,r) for(int (i)=(int)(l);(i)>=(int)(r);--(i))
    typedef pair<int,int> pii;
    typedef long long ll;
    typedef unsigned long long llu;
    typedef unsigned int ui;
    
    #define eps 0.05
    double a1,b1,a2,b2;
    void out(double a,double b){
       // cout<<a<<' '<<b<<endl;
        if(fabs(a)<eps&&fabs(b)<eps)printf("0.0");
        else if(fabs(a)<eps)printf("%.1fi",b);
        else if(fabs(b)<eps)printf("%.1f",a);
        else{
            printf("%.1f",a);
            if(b>eps)printf("+");
            printf("%.1fi",b);
        }
    }
    void print(double a,double b){
        printf("(");out(a,b);printf(")");
    }
    void solve(){
        print(a1,b1);printf(" + ");print(a2,b2);printf(" = ");out(a1+a2,b1+b2);puts("");
        print(a1,b1);printf(" - ");print(a2,b2);printf(" = ");out(a1-a2,b1-b2);puts("");
        print(a1,b1);printf(" * ");print(a2,b2);printf(" = ");out(a1*a2-b1*b2,a1*b2+a2*b1);puts("");
        print(a1,b1);printf(" / ");print(a2,b2);printf(" = ");
        double fm=a2*a2+b2*b2;
        out((a1*a2+b1*b2)/fm,(b1*a2-a1*b2)/fm);puts("");
    }
    int main(){
        //freopen("in.txt","r",stdin);
        scanf("%lf%lf%lf%lf",&a1,&b1,&a2,&b2);
        solve();
        return 0;
    }
    

    其实我的思路问题不大,就是代码写得少了,经验不足,这里有重复性的步骤,我应该要想到用函数的,然后fabs()我却用了复杂的转化。。。最后是我的代码没有考虑b为-的时候表示的问题。

    相关文章

      网友评论

          本文标题:(改)~-4 复数四则运算

          本文链接:https://www.haomeiwen.com/subject/tkrwfftx.html