zoukankan      html  css  js  c++  java
  • 采用C/C++语言如何实现复数抽象数据类型Complex

    记录一下!

    采用C/C++语言如何实现复数抽象数据类型Complex

    #include <stdio.h>
     
    typedef struct Complex
    {
        double e1; // 实部
        double e2; // 虚部
     
    }COMPLEX;
     
    // 函数声明
    COMPLEX AssignComplex(double v1, double v2);
    void DestroyComplex(COMPLEX pc);
    double GetReal(COMPLEX pc);
    double GetImag(COMPLEX pc);
    COMPLEX Add(COMPLEX z1, COMPLEX z2);
     
     
    int main(void)
    {
        double v1 = 2.0, v2 = 3.0;
        double v3 = 5.0, v4 = 8.5;
        COMPLEX sum;
        COMPLEX pc1 = AssignComplex(v1, v2);
        COMPLEX pc2 = AssignComplex(v3, v4);
     
        printf("第一个复数的实部为:%e, 虚部为:%e
    ", GetReal(pc1), GetImag(pc1));
     
        sum = Add(pc1, pc2);
        printf("复数pc1与pc2之和的实部为:%e,虚部为:%e
    ", sum.e1, sum.e2);
        return 0;
    }
     
     
    COMPLEX AssignComplex(double v1, double v2)
    {
        COMPLEX pc;
        pc.e1 = v1;
        pc.e2 = v2;
        return pc;
    }
    void DestroyComplex(COMPLEX pc)
    {
        // 销毁复数
    }
    double GetReal(COMPLEX pc)
    {
        double real = pc.e1;
        return real;
    }
     
    double GetImag(COMPLEX pc)
    {
        double imag = pc.e2;
        return imag;
    }
    COMPLEX Add(COMPLEX z1, COMPLEX z2)
    {
        COMPLEX sum;
        double z1_e1, z2_e1;
        double z1_e2, z2_e2;
     
        z1_e1 = z1.e1;
        z2_e1 = z2.e1;
     
        z1_e2 = z1.e2;
        z2_e2 = z2.e2;
     
        sum.e1 = z1_e1 + z2_e1;
        sum.e2 = z1_e2 + z2_e2;
     
        return sum;
    }
  • 相关阅读:
    php省市联动实现
    json学习笔记
    ajax无刷新技术
    session和cookie
    缩略图,透明度,水印,图片合并
    PHP画矩形,椭圆,圆,画椭圆弧 ,饼状图
    PHP字母数字验证码和中文验证码
    PHP画图的基本步骤GD库
    文件上传和多文件上传
    js身份证验证
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/6677256.html
Copyright © 2011-2022 走看看