回 帖 发 新 帖 刷新版面

主题:复数的加减乘除运算,谁把我代码优化一下

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <ctype.h>
#include <math.h>

void GetComplex(struct COMPLEX *x);
void PutComplex(struct COMPLEX *x);
struct COMPLEX CAdd(struct COMPLEX *x, struct COMPLEX *y);//复数加法
struct COMPLEX CSub(struct COMPLEX *x, struct COMPLEX *y);//复数减法
struct COMPLEX CMul(struct COMPLEX *x, struct COMPLEX *y);//复数乘法
struct COMPLEX CDiv(struct COMPLEX *x, struct COMPLEX *y);//复数除法

int Gcd(int m, int n);// 求最大公约数

struct COMPLEX
{
    double rpart, ipart;
};

int main()
{
    struct COMPLEX x;
    struct COMPLEX y;
    struct COMPLEX z;
    int m;
    int i;
    char ch;//是否继续进行复数运算

    time_t now, now1;

    printf("系统登陆中...\n");
    for(i = 0; i < 3; i ++)
    {
        printf("%d ", 3 - i);
        Sleep(1000);
    }
    printf("\n");
    printf("登陆成功!\n");
    printf("页面跳转中...\n");
    Sleep(3000);
    now = time(NULL);//记录当前时间
flag:
    system("cls");//清屏

    printf("登陆时间:%s", ctime(&now));
    
    printf("============================\n");
    printf("       复数的相关运算\n");
    printf("  小范于2010年11月21日编写\n");
    printf("============================\n");
    
    printf("(1)复数加法\n(2)复数减法\n(3)复数乘法\n(4)复数除法\n(5)退出系统\n\n");
    printf("请选择:");
    scanf("%d", &m);
    fflush(stdin);
    if(m == 1 || m == 2 || m == 3 || m == 4 || m == 5)
    {
        switch(m)
        {
        case 1:
            GetComplex(&x);printf("\n");//输入一个复数
            PutComplex(&x);
            GetComplex(&y);printf("\n");//输入另一个复数
            PutComplex(&y);
            printf("\n");

            printf("加法之后");
            z = CAdd(&x, &y);//复数加法
            PutComplex(&z);
            break;
        case 2:
            GetComplex(&x);printf("\n");//输入一个复数
            PutComplex(&x);
            GetComplex(&y);printf("\n");//输入另一个复数
            PutComplex(&y);
            printf("\n");

            printf("减法之后");
            z = CSub(&x, &y);//复数减法
            PutComplex(&z);
            break;
        case 3:
            GetComplex(&x);printf("\n");//输入一个复数
            PutComplex(&x);
            GetComplex(&y);printf("\n");//输入另一个复数
            PutComplex(&y);
            printf("\n");

            printf("乘法之后");
            z = CMul(&x, &y);//复数乘法
            PutComplex(&z);
            break;
        case 4:
            GetComplex(&x);printf("\n");//输入一个复数
            PutComplex(&x);
            GetComplex(&y);printf("\n");//输入另一个复数
            PutComplex(&y);
            printf("\n");

            printf("除法之后");
            z = CDiv(&x, &y);//复数除法
            break;
        case 5:
            printf("退出系统成功!\n");
            now1 = time(NULL);//记录退出系统时间
            printf("离开时间:%s\n", ctime(&now1));
            printf("用户登陆系统的时间为:%lg秒\n", difftime(now1, now));
            exit(0);
            break;
        default: 
            printf("输入有误!\n");
            Sleep(1000);
            goto flag;
        }
    }
    else
    {
        printf("输入有误!\n");
        Sleep(1000);
        goto flag;
    }
    printf("\n");
    printf("是否继续进行复数运算?->(1)Y    (2)N\n");
    fflush(stdin);
flag1:
    printf("->>请选择:");
    ch = getchar();
    if(isalpha(ch) && (ch == 'y' || ch == 'n' || ch == 'Y' || ch == 'N'))
    {
        if('Y' == toupper(ch))
        {
            goto flag;
        }
        else
        {
            printf("退出系统成功!\n");
        }
    }
    else
    {
        printf("输入有误!请输入y或者n\n");
        Sleep(1000);
        fflush(stdin);
        goto flag1;
    }

    now1 = time(NULL);//记录退出系统时间
    printf("离开时间:%s\n", ctime(&now1));
    printf("用户登陆系统的时间为:%lg秒\n", difftime(now1, now));
//    printf("用户登陆系统的时间为:%lg秒\n", double(now1 - now) / CLOCKS_PER_SEC * 1000);
    system("pause");
    return 0;
}

void GetComplex(struct COMPLEX *x)
{

    printf("请输入复数的实部:");
    scanf("%lg", &x->rpart);
    printf("请输入复数的虚部:");
    scanf("%lg", &x->ipart);
}

void PutComplex(struct COMPLEX *x)
{
//    printf("实部:%lg\n", x->rpart);//测试代码
//    printf("虚部:%lg\n", x->ipart);
    if(x->rpart != 0)
    {
        if(x->ipart <  0)
        {
            if(x->ipart == -1)
            {
                printf("复数:%lg - i\n", x->rpart);
            }
            else
            {
                printf("复数:%lg - %lgi\n", x->rpart, -x->ipart);
            }
        }
        else if(x->ipart == 0)
        {
            printf("复数:%lg\n", x->rpart);
        }
        else
        {
            if(x->ipart == 1)
            {
                printf("复数:%lg + i\n", x->rpart);
            }
            else
            {
                printf("复数:%lg + %lgi\n", x->rpart, x->ipart);
            }
        }
    }
    else
    {
        if(x->ipart <  0)
        {
            if(x->ipart == -1)
            {
                printf("复数: - i\n");
            }
            else
            {
                printf("复数: - %lgi\n", -x->ipart);
            }
        }
        else if(x->ipart == 0)
        {
            printf("复数:%lg\n", x->rpart);
        }
        else
        {
            if(x->ipart == 1)
            {
                printf("复数:i\n");
            }
            else
            {
                printf("复数:%lgi\n", x->ipart);
            }
        }
    }
}

struct COMPLEX CAdd(struct COMPLEX *x, struct COMPLEX *y)//复数加法
{
    struct COMPLEX m;

    m.rpart = x->rpart + y->rpart;
    m.ipart = x->ipart + y->ipart;

    return m;
}

struct COMPLEX CSub(struct COMPLEX *x, struct COMPLEX *y)//复数减法
{
    struct COMPLEX m;

    m.rpart = x->rpart - y->rpart;
    m.ipart = x->ipart - y->ipart;

    return m;
}

struct COMPLEX CMul(struct COMPLEX *x, struct COMPLEX *y)//复数乘法
{
    struct COMPLEX m;

    m.rpart = x->rpart * y->rpart + (x->ipart * y->ipart) * (- 1);
    m.ipart = x->rpart * y->ipart + x->ipart * y->rpart;

    return m;
}

struct COMPLEX CDiv(struct COMPLEX *x, struct COMPLEX *y)//复数除法
{
    struct COMPLEX m;
    double n;
    int r;
    int r1;
    
    if((y->rpart == 0) && (y->ipart == 0))
    {
        printf("\b\b\b\b\b\b\b\b除数不能为零!\n");
        exit(0);
    }
    m.rpart = x->rpart * y->rpart + x->ipart * y->ipart;
    m.ipart = x->rpart * (-y->ipart) + x->ipart * y->rpart;
    
    n = y->rpart * y->rpart + y->ipart * y->ipart;
//    printf("n = %lg\n", n);//测试代码
    r =    Gcd((int)m.rpart, (int)n);//实部与分母求约分
    r = abs(r);
//    printf("r = %d\n", r);//测试代码
    
//////////////////实部
    if((double)x->rpart / y->rpart == (double)x->ipart / y->ipart)
    {
        printf("复数:%lg\n", (double)x->rpart / y->rpart);
    }
    else
    {
        if(r == 1)
        {
            if(m.rpart > 0 && n > 0)
            {
                if(n == 1)
                {
                    printf("复数:%lg", m.rpart);
                }
                else
                {
                    printf("复数:%lg/%lg", m.rpart, n);
                }
            }
            else if(m.rpart > 0 && n < 0)
            {
                if(n == -1)
                {
                    printf("复数:-%lg", m.rpart);
                }
                else
                {
                    printf("复数:-%lg/%lg", m.rpart, -n);
                }
            }
            else if(m.rpart < 0 && n > 0)
            {
                if(n == 1)
                {
                    printf("复数:%lg", m.rpart);
                }
                else
                {
                    printf("复数:%lg/%lg", m.rpart, n);
                }
            }
            else if(m.rpart < 0 && n < 0)
            {
                if(n == -1)
                {
                    printf("复数:%lg", -m.rpart);
                }
                else
                {
                    printf("复数:%lg/%lg", -m.rpart, -n);
                }
            }
            else
            {
                printf("程序实部与分母求约分时出错!\n");
                exit(0);
            }
        }
        else
        {
            m.rpart = m.rpart / r;
            n = n / r;
            if(m.rpart > 0 && n > 0)
            {
                if(n == 1)
                {
                    printf("复数:%lg", m.rpart);
                }
                else
                {
                    printf("复数:%lg/%lg", m.rpart, n);
                }
            }
            else if(m.rpart > 0 && n < 0)
            {
                if(n == -1)
                {
                    printf("复数:-%lg", m.rpart);
                }
                else
                {
                    printf("复数:-%lg/%lg", m.rpart, -n);
                }
            }
            else if(m.rpart < 0 && n > 0)
            {
                if(n == 1)
                {
                    printf("复数:%lg", m.rpart);
                }
                else
                {
                    printf("复数:%lg/%lg", m.rpart, n);
                }
            }
            else if(m.rpart < 0 && n < 0)
            {
                if(n == -1)
                {
                    printf("复数:%lg", -m.rpart);
                }
                else
                {
                    printf("复数:%lg/%lg", -m.rpart, -n);
                }
            }
            else
            {
                printf("程序实部与分母求约分时出错!\n");
                exit(0);
            }
        }

        r1 = Gcd((int)m.ipart, (int)n);//虚部与分母求约分
        r1 = abs(r1);
    //    printf("r1 = %d\n", r1);//测试代码
    //////////////////虚部
        if(r1 == 1)
        {
            if(m.ipart > 0 && n > 0)
            {
                printf("+");
                if(n == 1)
                {
                    printf("i", m.ipart);
                }
                else
                {
                    printf("%lg/%lgi", m.ipart, n);
                }
            }
            else if(m.ipart > 0 && n < 0)
            {
                printf("-");
                if(n == -1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", m.ipart, -n);
                }
            }
            else if(m.ipart < 0 && n > 0)
            {
                printf("-");
                if(n == 1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", -m.ipart, n);
                }
            }
            else if(m.ipart < 0 && n < 0)
            {
                printf("+");
                if(n == -1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", -m.ipart, -n);
                }
            }
            else
            {
                printf("程序虚部与分母求约分时出错!\n");
                exit(0);
            }
        }
        else
        {
            m.ipart = m.ipart / r1;
            n = n / r1;
            if(m.ipart > 0 && n > 0)
            {
                printf("+");
                if(n == 1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", m.ipart, n);
                }
            }
            else if(m.ipart > 0 && n < 0)
            {
                printf("-");
                if(n == -1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", m.ipart, -n);
                }
            }
            else if(m.ipart < 0 && n > 0)
            {
                printf("-");
                if(n == 1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", -m.ipart, n);
                }
            }
            else if(m.ipart < 0 && n < 0)
            {
                printf("+");
                if(n == -1)
                {
                    printf("i");
                }
                else
                {
                    printf("%lg/%lgi", -m.ipart, -n);
                }
            }
            else
            {
                printf("程序虚部与分母求约分时出错!\n");
                exit(0);
            }
        }
    }
    return m;
}

int Gcd(int m, int n)// 求最大公约数
{
    int r;

    while(r = m % n)
    {
        m = n;
        n = r;
    }

    return n;
}

回复列表 (共3个回复)

沙发

c99已经有复数类型

板凳

我感觉输出和除法,貌似写得复杂了,
比如输出1+i,i之前不能输出1,当然我也考虑了正负数。
除法,我用分数表示,因为有可能会碰到除不尽。
但是感觉自己的代码写得不是很好,还请各位大虾们指教。

3 楼

如果不想用C99标准的复数类型的话
给你想个高招。
因为fortran原生支持复数类型计算,C/Fortran联合混编就好了。
当然并没说你写的代码不好,因为太长实在不想看。

我来回复

您尚未登录,请登录后再回复。点此登录或注册