课程设计__复数的计算

1、操作符重载

2、这里的复数并没有用class,下一篇会有。

#include <stdio.h>
#include <algorithm>
#include <iostream>

using namespace std;

struct Complex
{
    float real;
    float imag;
};

Complex operator +(Complex &a,Complex &b)
{
    Complex c;
    c.real=a.real+b.real;
    c.imag=a.imag+b.imag;
    return c;
}

Complex operator -(Complex &a,Complex &b)
{
    Complex c;
    c.real=a.real-b.real;
    c.imag=a.imag-b.imag;
    return c;
}

Complex operator *(Complex &a,Complex &b)
{
    Complex c;
    c.real=a.real*b.real-a.imag*b.imag;
    c.imag=a.real*b.imag+a.imag*b.real;
    return c;
}

Complex operator /(Complex &a,Complex &b)
{
    float fm;
    fm=a.real*b.real-a.imag*b.imag;///分母
    Complex c;
    c.real=(a.real*b.real-a.imag*b.imag)/fm;
    c.imag=(a.real*b.imag+a.imag*b.real)/fm;
    return c;
}

void printFormerAdd(Complex &a,Complex &b)///打印加法的原式
{
    printf("(%.1f ,%.1f i)+(%.1f ,%.1f i)=",a.real,a.imag,b.real,b.imag);
}

void printFormerSub(Complex &a,Complex &b)///打印减法的原式
{
    printf("(%.1f ,%.1f i)-(%.1f ,%.1f i)=",a.real,a.imag,b.real,b.imag);
}

void printFormerMul(Complex &a,Complex &b)///打印乘法的原式
{
    printf("(%.1f ,%.1f i)*(%.1f ,%.1f i)=",a.real,a.imag,b.real,b.imag);
}

void printFormerDiv(Complex &a,Complex &b)///打印除法的原式
{
    printf("(%.1f ,%.1f i)/(%.1f ,%.1f i)=",a.real,a.imag,b.real,b.imag);
}

void print(Complex c)///打印结果
{
    printf("(%.1f ,%.1f i)\n",c.real,c.imag);
}

int main()
{
        Complex a,b,c;
        printf("This is complex's operation\n");
        printf("Please input your wants complex's operation\n");
        char o;
        scanf("%c",&o);
        system("cls");
        switch (o)
        {
        case '+':
            printf("Please input two complex's real and imag\n");
            scanf("%f%f",&a.real,&a.imag);
            scanf("%f%f",&b.real,&b.imag);
            c=a+b;
            printFormerAdd(a,b);
            print(c);
            break;
        case '-':
            printf("Please input two complex's real and imag\n");
            scanf("%f%f",&a.real,&a.imag);
            scanf("%f%f",&b.real,&b.imag);
            c=a-b;
            printFormerSub(a,b);
            print(c);
            break;
        case '*':
            printf("Please input two complex's real and imag\n");
            scanf("%f%f",&a.real,&a.imag);
            scanf("%f%f",&b.real,&b.imag);
            c=a*b;
            printFormerMul(a,b);
            print(c);
            break;
        case '/':
            printf("Please input two complex's real and imag\n");
            scanf("%f%f",&a.real,&a.imag);
            scanf("%f%f",&b.real,&b.imag);
            c=a/b;
            printFormerDiv(a,b);
            print(c);
            break;
        }
        system("pause");
    return 0;
}

 

转载于:https://www.cnblogs.com/TreeDream/p/5247124.html

密码学分为两类密码:对称密码和非对称密码。对称密码主要用于数据的加/解密,而非对称密码则主要用于认证、数字签名等场合。非对称密码在加密和解密时,是把加密的数据当作一个大的正整数来处理,这样就涉及到大整数的加、减、乘、除和指数运算等,同时,还需要对大整数进行输出。请采用相应的数据结构实现大整数的加、减、乘、除和指数运算,以及大整数的输入和输出。 【基本要求】 1.要求采用链表来实现大整数的存储和运算,不允许使用标准模板类的链表类(list)和函数。 同时要求可以从键盘输入大整数,也可以文件输入大整数,大整数可以输出至显示器,也可以输出至文件。大整数的存储、运算和显示,可以同时支持二进制和十进制,但至少要支持十进制。大整数输出显示时,必须能清楚地表达出整数的位数。测试时,各种情况都需要测试,并附上测试截图;要求测试例子要比较详尽,各种极限情况也要考虑到,测试的输出信息要详细易懂,表明各个功能的执行正确。 2.要求大整数的长度可以不受限制,即大整数的十进制位数不受限制,可以为十几位的整数,也可以为500多位的整数,甚至更长;大整数的运算和显示时,只需要考虑正的大整数。如果可能的话,请以秒为单位显示每次大整数运算的时间。 3.要求采用类的设计思路,不允许出现类以外的函数定义,但允许友元函数。主函数中只能出现类的成员函数的调用,不允许出现对其它函数的调用。 4.要求采用多文件方式:.h文件存储类的声明,.cpp文件存储类的实现,主函数main存储在另外一个单独的cpp文件中。如果采用类模板,则类的声明和实现都放在.h文件中。 5.不强制要求采用类模板,也不要求采用可视化窗口;要求源程序中有相应注释。 6.要求采用Visual C++ 6.0及以上版本进行调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值