zoukankan      html  css  js  c++  java
  • 使用string实现一个用于储存那些太大而无法使用 long long 的数

    类的定义:

    class stringInt {
    public:
        stringInt();
        stringInt(string num);
        stringInt(int num);
        stringInt operator+(stringInt & num);
        stringInt operator-(stringInt & num);
            
        // ... 还可以乘除之类的操作
    
    private:
        string str;
    };

    所用的函数:

    // 将string和int相互转换
    string num2string(int num);
    int string2num(string str);
    int compareTwoNum(string firstStr, string secondStr);  // 比较两个用string表示的数的大小
    int twoCharAdd(char ch1, char ch2, bool moreOne);  // 对两个用char表示的数的加操作
    int twoCharSub(char ch1, char ch2, bool &lessOne);  // 对两个用char表示的数的减操作
    void transpose(string &str);  // 将一个string转置

    在对加、减操作的实现中,主要依赖stringAdd()、stringSubtract()函数,用于对两个用string表示的数做加减

    具体实现:

    string stringAdd(string firstStr, string secondStr)
    {
        string outStr;
        int firstStrLen, secondStrLen, sum;
        firstStrLen = firstStr.length();
        secondStrLen = secondStr.length();
        bool moreOne = false;
        string temp = "0";
    
        //cout << firstStr << endl;
        //cout << secondStr << endl;
        //cout << firstStrLen << "  " << secondStrLen << endl;
    
        //cout << "Init outStr: " << outStr << endl;
    
        if (firstStrLen >= secondStrLen) {
            //int cnt = firstStrLen;
    
            for (int i = firstStrLen - 1; i >= 0; i--) {
    
                //cout << "i = " << i << endl;
    
                if (secondStrLen > 0) {
                    sum = twoCharAdd(firstStr[i], secondStr[secondStrLen - 1], moreOne);
    
                    cout << "sum = " << sum << endl;
    
                    if (sum >= 10) {
                        moreOne = true;
                        outStr.append(num2string(sum - 10));   
                        // string 没有在字符串头部增加一个字符的函数,虽然实现这个函数并不复杂,但此处如果使用头部
                        // 增加一个元素的函数,则在每一个循环都需要使用,与使用append() 再 将结果字符串反转相比,需要
                        // 的代价相差太大,所以此处选用后一种方法
                    }
                    else {
                        moreOne = false;
                        outStr.append(num2string(sum));
                    }
    
                    secondStrLen--;
    
                    //cout << "secondStrLen = " << secondStrLen << "  " << outStr << endl;
    
                    continue;
                }
    
                if (moreOne) {
                    sum = twoCharAdd(firstStr[i], '0', moreOne);
    
                    //cout << "No continue sum = " << sum << endl;
    
                    if (sum >= 10) {
                        moreOne = true;
                        outStr.append(num2string(sum - 10));
                    }
                    else {
                        moreOne = false;
                        outStr.append(num2string(sum));
                    }
                }
                else {
                    if (i != 0) {    // 当 i = 0 时,&firstStr[0] 表示整个 firstStr 字符串
                        outStr.append(&firstStr[i]);  
                    }
                    else {
                        temp[0] = firstStr[i];
                        outStr.append(temp);
                    }
                    
    
                }
            }
            //cout << "End for outStr = " << endl;
        }
        else {
            int cnt = secondStrLen;
            for (int i = cnt - 1; i >= 0; i--) {
                if (firstStrLen > 0) {
                    sum = twoCharAdd(firstStr[firstStrLen - 1], secondStr[i], moreOne);
    
                    if (sum >= 10) {
                        moreOne = true;
                        outStr.append(num2string(sum - 10));
                    }
                    else {
                        moreOne = false;
                        outStr.append(num2string(sum));
                    }
    
                    firstStrLen--;
                    continue;
                }
    
                if (moreOne) {
                    sum = twoCharAdd('0', secondStr[i], moreOne);
    
                    if (sum >= 10) {
                        moreOne = true;
                        outStr.append(num2string(sum - 10));
                    }
                    else {
                        moreOne = false;
                        outStr.append(num2string(sum));
                    }
                }
                else {
                    if (i != 0) {   
                        outStr.append(&secondStr[i]);
                    }
                    else {
                        temp[0] = secondStr[i];
                        outStr.append(temp);
                    }
                }
            }
        }
    
        transpose(outStr);
    
        return outStr;
    }
    
    string stringSubtract(string firstStr, string secondStr)  // 第一个是减数,第二个是被减数
    {
        int firstStrLen, secondStrLen;
        string outStr;
        firstStrLen = firstStr.length();
        secondStrLen = secondStr.length();
    
        //cout << "初始outStr:" << outStr << endl;
    
        //cout << "两字符串长度为:" << firstStrLen << "  " << secondStrLen << endl;
        //cout << "两字符串为:" << firstStr << "  " << secondStr << endl;
    
        char ch = '0';
        string minus = "-";
        bool lessOne = false;
    
        //cout << "compareTN: " << compareTwoNum(firstStr, secondStr) << endl;
    
        if (compareTwoNum(firstStr, secondStr) == 1) {
            for (int i = firstStrLen - 1; i >= 0; i--) {
                if (secondStrLen > 0) {
                    int sub = twoCharSub(firstStr[i], secondStr[secondStrLen -1], lessOne);
                    
                    cout << "sub = " << sub << endl;
                    cout << "n2s_sub = " << num2string(sub) << endl;
                    
                    outStr.append(num2string(sub));
                    secondStrLen--;
    
                    //cout << outStr << endl;
    
                    continue;
                }
    
                int sub = twoCharSub(firstStr[i], '0', lessOne);
                outStr.append(num2string(sub));
    
                //cout << outStr << endl;
            }
    
            //cout << outStr << endl;
        }
        else if (compareTwoNum(firstStr, secondStr) == -1) {
            for (int i = secondStrLen - 1; i >= 0; i--) {
                if (firstStrLen > 0) {
                    int sub = twoCharSub(secondStr[i], firstStr[firstStrLen - 1], lessOne);
                    outStr.append(num2string(sub));
                    firstStrLen--;
                    continue;
                }
    
                int sub = twoCharSub(secondStr[i], '0', lessOne);
                outStr.append(num2string(sub));
            }
    
            //cout << "加负号前 outStr = " << outStr << endl;
    
            outStr.append(minus);
        }
        else {
            outStr.append(&ch);
        }
    
        //cout << "加负号后,转置前 outStr = " << outStr << endl;
    
        transpose(outStr);
    
        return outStr;
    }
    
    string stringMultiply(string firstStr, string secondStr)
    {
        return string();
    }

    将string和int相互转换

    string num2string(int num)
    {
        //cout << "num = " << num << endl;
    
        stringstream ss;
        ss << num;
    
        string str = ss.str();
    
        //cout << str << endl;
    
        return str;
    }
    
    int string2num(string str)
    {
        stringstream ss;
        ss << str;
        int num;
        ss >> num;
    
        return num;
    }

    其他的辅助函数的实现

    int twoCharAdd(char ch1, char ch2, bool moreOne)
    {
        int sum, num1, num2;
        num1 = string2num(&ch1);
        num2 = string2num(&ch2);
    
        if (moreOne) {
            sum = num1 + num2 + 1;
        }
        else {
            sum = num1 + num2;
        }
    
        return sum;
    }
    
    void transpose(string &str)
    {
        int len = str.length();
    
        for (int i = 0; i < len / 2; i++) {
            char tempChar = str[i];
            str[i] = str[len - 1 - i];
            str[len - 1 - i] = tempChar;
        }
    
        return;
    }
    
    int compareTwoNum(string firstStr, string secondStr)
    {
        int firstStrLen, secondStrLen;
        firstStrLen = firstStr.length();
        secondStrLen = secondStr.length();
    
        if (firstStrLen > secondStrLen) {
            return 1;
        }
        else if (firstStrLen < secondStrLen) {
            return -1;
        }
        else {
            for (int i = 0; i < firstStrLen; i++) {
                if (string2num(&firstStr[i]) > string2num(&secondStr[i])) {
                    return 1;
                }
                else if (string2num(&firstStr[i]) < string2num(&secondStr[i])){
                    return -1;
                }
                else {
                    continue;
                }
            }
        }
    
        return 0;
    }
    
    int twoCharSub(char ch1, char ch2, bool &lessOne)
    {
        //cout << ch1 << "  " << ch2 << "  " << lessOne << endl;
    
        int num1, num2;
    
        if (lessOne) {
            num1 = string2num(&ch1) - 1;
        }
        else {
            num1 = string2num(&ch1);
        }
        
        num2 = string2num(&ch2);
    
        if (num1 >= num2) {
            lessOne = false;
            return num1 - num2;
        }
        else {
            lessOne = true;
            return 10 + num1 - num2;
        }
    }

    收获:

    更加熟悉string等...

  • 相关阅读:
    jenkins配置完全正确,控制台显示邮件发送成功,但未收到问题
    中移4G模块-ML302-OpenCpu开发-串口开发
    中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-RRPC通讯)
    中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-接收和发送数据)
    中移4G模块-ML302-OpenCpu开发-GPIO
    中移4G模块-ML302-OpenCpu开发-服务器搭建
    中移4G模块-ML302-OpenCpu开发-前端网页搭建
    中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-订阅主题)
    中移4G模块-ML302-OpenCpu开发-2-MQTT连接阿里云
    MateBook14一个多月的使用体验(开发向)
  • 原文地址:https://www.cnblogs.com/lnlin/p/9519229.html
Copyright © 2011-2022 走看看