每天打卡一小时 第十九天 编译四部曲

第一部曲 自然语言 

先将大数类的框架写好,再定义其中的函数

分别写出每一个函数,通过分步骤的方法解决问题

 

有参构造函数

首先声明函数时,默认参数

定义 根据数值的正负进行选择 

然后进行循环将数字进行输入

拷贝构造函数

循环进行赋值操作

公有函数成员

选择正负符号 循环赋值

直接输出

 

重载运算符 

好难

此题中最难的部分

明天细说

 

第二部曲 流程图

第三部曲 代码

#include <cstring>

class BigNum
{
private:
    char num[N];
public:
    BigNum( char c[N] = "+0" );
    BigNum( const BigNum &p );
    ~BigNum()
    {
        cout<<"BigNum Destructor run"<<endl;
    }
    void setNum( char c[N] );
    char const * getNum(void) const;
    BigNum operator + ( const BigNum &a );
    BigNum operator - (  BigNum &b );
    void show() const;
};
 
 
BigNum::BigNum( char c[N] )
{
    int i,j,a = strlen(c);
    if( c[0] != '-')
    {
        num[0] = '+';
        if( c )
        {
            for( i=strlen(c), j=0; i>=1; i--,j++)
            {
                num[i] = c[j];
            }
        }
        num[a+1] = '\0';
    }
    else
    {
        num[0] = c[0];
        for( i=strlen(c)-1, j=1; i>=1; i--,j++)
        {
            num[i] = c[j];
        }
        num[a] = '\0';
    }
    cout<<"BigNum Constructor run"<<endl;
}
 
BigNum::BigNum( const BigNum &p )
{
    int i=0;
    if(p.num)
    {
        while( p.num[i] != '\0' )
        {
            num[i] = p.num[i];
            i++;
        }
        num[i] = '\0';
    }
    cout<<"BigNum CopyConstructor run"<<endl;
}

void BigNum::setNum( char c[N] )
{
    int i,j,a = strlen(c);
    if( c[0] != '-')
    {
        num[0] = '+';
        if( c )
        {
            for( i=strlen(c), j=0; i>=1; i--,j++)
            {
                num[i] = c[j];
            }
        }
        num[a+1] = '\0';
    }
    else
    {
        num[0] = c[0];
        for( i=strlen(c)-1, j=1; i>=1; i--,j++)
        {
            num[i] = c[j];
        }
        num[a] = '\0';
    }
}
 
char const * BigNum::getNum(void) const
{
    return num;
}
 
BigNum BigNum::operator + ( const BigNum &a )
{
    if(num[0]=='+' && a.num[0]=='-')
    {
        BigNum s2(a);
        s2.num[0] = '+';
        return  (*this) - s2;
    }
    if(num[0]=='-' && a.num[0]=='+')
    {
        BigNum s2(a);
        num[0] = '+';
        return s2 - (*this);
    }
    BigNum s;
    int i;
    char s2[N];
    for( i=0; a.num[i]!='\0'; i++)
    {
        s2[i] = a.num[i];
    }
    s2[i] = '\0';
    int len1 = strlen(num);
    int len2 = strlen(s2);
    int Max=0;
    if( len1 > len2)
    {
        Max = len1;
        num[len1] = '0';
        for( i=len2; i<=len1; i++)
        {
            s2[i] = '0';
        }
    }
    else
    {
        if( len1 < len2 )
        {
            Max = len2;
            s2[len2] = '0';
            for( i=len1; i<=len2; i++)
            {
                num[i] = '0';
            }
        }
        else
        {
            Max = len1;
            s2[len2] = '0';
            num[len1] = '0';
        }
    }
    if( (num[0]=='+' && s2[0]=='+') || (num[0]=='-' && s2[0]=='-') )
    {
        for( i=1; i<=Max; i++)
        {
            int c = ( (num[i]-'0') + (s2[i]-'0') );
            if( c>= 10 )
            {
                c -= 10;
                s.num[i] = char( c +'0');
                s2[i+1]++;
            }
            else
            {
                s.num[i] = char( c + '0');
            }
        }
        s.num[i] = '\0';
        if(num[0]=='+' && s2[0]=='+')
        {
            s.num[0] = '+';
        }
        if(num[0]=='-' && s2[0]=='-' )
        {
            s.num[0] = '-';
        }
    }
 
    return s;
}
 
BigNum BigNum::operator - (  BigNum &b )
{
    if(num[0]=='+' && b.num[0]=='-')
    {
        BigNum s2(b);
        s2.num[0] = '+';
        return  (*this) + s2;
    }
    if(num[0]=='-' && b.num[0]=='+')
    {
        BigNum s2(b);
        s2.num[0] = '-';
        return  (*this) + s2;
    }
    BigNum s;
    int i;
    char s2[N];
    for( i=0; b.num[i]!='\0'; i++)
    {
        s2[i] = b.num[i];
    }
    s2[i] = '\0';
    int len1 = strlen(num);
    int len2 = strlen(s2);
    int Max=0;
    if( len1 > len2)
    {
        Max = len1;
        num[len1] = '0';
        for( i=len2; i<=len1; i++)
        {
            s2[i] = '0';
        }
    }
    else
    {
        if( len1 < len2 )
        {
            Max = len2;
            s2[len2] = '0';
            for( i=len1; i<=len2; i++)
            {
                num[i] = '0';
            }
        }
        else
        {
            Max = len1;
            s2[len2] = '0';
            num[len1] = '0';
        }
    }
    if( (num[0]=='+' && s2[0]=='+') || (num[0]=='-' && s2[0]=='-') )
    {
        if(num[0]=='-' && s2[0]=='-')
        {
            s2[0] = '+';
            num[0] = '+';
            for( i=0; num[i]!='\0'; i++ )
            {
                char ch;
                ch = num[i];
                num[i] = s2[i];
                s2[i] = ch;
            }
        }
        char s1[N];
        char s3[N];
        int j;
        for( i=Max,j=0; i>0; j++,i--)
        {
            s1[j] = num[i];
        }
        s1[j] = '\0';
        for( i=Max,j=0; i>0; j++,i--)
        {
            s3[j] = s2[i];
        }
        s3[j] = '\0';
        if( strcmp(s1,s3)>0 )
        {
            s.num[0] = '+';
            for( i=1; i<=Max; i++)
            {
                int c = ( (num[i]-'0') - (s2[i]-'0'));
                if( c<0 )
                {
                    c+=10;
                    s.num[i] = char( c + '0');
                    num[i+1]--;
                }
                else
                {
                    s.num[i] = char( c + '0');
                }
            }
            s.num[i] = '\0';
        }
        else
        {
            if( strcmp(s1,s3)<0 )
            {
                s.num[0] = '-';
                for( i=1; i<=Max; i++)
                {
                    int c = ( (s2[i]-'0') - (num[i]-'0'));
 
                    if( c<0 )
                    {
                        c+=10;
                        s.num[i] = char( c + '0');
 
                        s2[i+1]--;
                    }
                    else
                    {
                        s.num[i] = char( c + '0');
 
                    }
                }
                s.num[i] = '\0';
            }
            else
            {
                s.num[0] = '+';
                s.num[1] = '0';
                s.num[2] = '\0';
            }
        }
    }
    return s;
}
 
void BigNum::show() const
{
    char ch[N] = "+0";
    char c[N] = "+00";
    //cout<<num<<endl;
    if( strcmp(ch,num) == 0 || strcmp(c,num) == 0 )
    {
        cout<<"BigNum("<<ch<<")"<<endl;
    }
    else
    {
        cout<<"BigNum("<<num[0];
        int i;
        int flag;
        for( i=strlen(num)-1; i>=0; i--)
        {
            if(num[i]!='0')
            {
                flag = i;
                break;
            }
        }
        for( i=flag; i>=1; i--)
        {
            cout<<num[i];
        }
        cout<<")"<<endl;
    }
}

  

第四步曲 总结

通过拆分问题解决问题,看似很麻烦,但是可以将其拆解为多个函数的增加

无非就是几个函数叠合在一个,可以建立很多个文件,每一个文件放一个函数,解决一个函数再进行下一个函数

posted @ 2023-05-05 23:04  财神给你送元宝  阅读(12)  评论(0编辑  收藏  举报