wang_yue_xi 2019-10-17 22:20 采纳率: 100%
浏览 124

for循环的问题,来自萌新的仰视?

c++

for循环终止条件不起作用了......

大佬帮帮我......

(只看第169~199行即可,高精度除法)

#include<bits/stdc++.h>
using namespace std;

template<class C>
void exchage(C& a,C& b){
    C z;
    z = b;
    b = a;
    a = z;
    return ;
}

template<size_t SIZE>
class HP{
    private:
        size_t Len;
        bool ifPlus;
        short hp[SIZE];
    public:
        void get(){
            char s;
            Len = 0;
            ifPlus = true;
            while(s=getchar()){
                if(s == '\n' || s == ' '){
                    if(Len) break;
                    continue;
                }
                if(!Len && s == '-') ifPlus = false;
                else{
                    hp[Len] = s - '0';
                    ++Len;
                }
            }
            rev();
            return ;
        }
        void print(...){
            char* p;
            va_list str;
            va_start(str,0);
            p = va_arg(str,char*);
            if(Len == 0) return ;
            int i;
            if(ifPlus == false) printf("-");
            for(i=Len - 1; i>=0; --i)
                printf("%d",hp[i]);
            printf(p);
            return ;
        }
        HP(){
            Len = 0;
            ifPlus = true;
            memset(hp,0,sizeof(hp));
        }
        void clear(){
            Len = 0;
            ifPlus = true;
            memset(hp,0,sizeof(hp));
        }
        void rev(){
            int i;
            for(i=0; i<Len/2; ++i) swap(hp[i],hp[Len - i - 1]);
        }
        HP append(int first,int last){
            assert(first <= last);
            HP temp;
            int i;
            for(i = first; i < last; ++i)
                temp.hp[i - first] = hp[i];
            temp.Len = last - first;
            return temp;
        }
        HP cat(HP a){
            HP temp;
            int i;
            for(i = 0; i < Len; ++i)
                temp.hp[i] = hp[i];
            for(i = Len; i < Len + a.Len; ++i)
                temp.hp[i] = a.hp[i - Len];
            temp.Len = Len + a.Len;
            return temp;
        }
        void operator=(HP a){
            int i;
            clear();
            ifPlus = a.ifPlus;
            Len = a.Len;
            for(i=0; i<a.Len; ++i) hp[i] = a.hp[i];
            return ;
        }
        template<typename T>
        void operator=(T a){
            int i = 0;
            clear();
            if(a < 0) ifPlus = false;
            else ifPlus = true;
            while(a > 0){
                hp[i] = a % 10;
                a /= 10;
                ++i;
            }
            Len = i;
            return ;
        }
        HP operator+(HP a){
            HP b = *this;
            if(b.ifPlus != a.ifPlus){
                if(b.ifPlus == false){
                    b.ifPlus = true;
                    return a - b;
                }
                else{
                    a.ifPlus = true;
                    return b - a;
                }
            }
            int i;
            size_t maxLen;
            maxLen = max(b.Len,a.Len);
            HP sum;
            sum.clear();
            for(i=0; i<maxLen; ++i){
                sum.hp[i] += b.hp[i] + a.hp[i];
                sum.hp[i + 1] = sum.hp[i]/10;
                sum.hp[i] = sum.hp[i]%10;
            }
            sum.ifPlus = a.ifPlus;
            sum.Len = maxLen;
            return sum;
        }
        HP operator-(HP a){
            HP b = *this;
            int i;
            HP ans;
            bool flag = true;
            if(b.ifPlus != a.ifPlus) return *this + a;
            if(b < a){
                exchage(a,b);
                flag = false;
            }
            for(i=0; i<Len; ++i){
                ans.hp[i] += b.hp[i] - a.hp[i];
                if(ans.hp[i] < 0){
                    ans.hp[i] += 10;
                    ans.hp[i + 1] -= 1;
                } 
            }
            ans.Len = b.Len;
            ans.ifPlus = flag;
            while(!ans.hp[ans.Len - 1]) --ans.Len;
            return ans;
        }
        HP operator*(HP a){
            HP ans,b = *this;
            int i,j;
            for(i=0; i<b.Len; ++i){
                for(j=i; j<a.Len+i; ++j){
                    ans.hp[j] += a.hp[j - i] * b.hp[i];
                    ans.hp[j + 1] += ans.hp[j] / 10;
                    ans.hp[j] %= 10;
                }
            }
            ans.Len = b.Len + a.Len;
            ans.ifPlus = !(a.ifPlus ^ b.ifPlus);
            while(!ans.hp[ans.Len - 1]) --ans.Len;
            return ans;
        }
        HP operator/(HP a){
            HP b = *this,p[10],quotient,num,HPi,remainder;//quotient商 
            int i,j;
            size_t left = 0,right = 9,mid,k = 0;
            for(i = 1; i <= 9; ++i){
                HPi = i;
                p[i] = a * HPi;
            }
            for(i = b.Len - 1; i - a.Len > 0; --i){
                num = b.append(i - a.Len + 1,i + 1);
                left = 0;
                right = 9;
                num = num.cat(remainder);
                while(left <= right){
                    mid = (left + right) / 2;
                    remainder = num;
                    remainder -= p[mid];
                    if(remainder.ifPlus == false) right = mid - 1;
                    else if(remainder >= a) left = mid + 1;
                    else{
                        quotient.hp[Len + a.Len - (++k) - 1] = mid;
                        for(j = i; j > i - a.Len; --j)
                            b.hp[i] = remainder.hp[j - i];
                        break;
                    }
                }
            }
            quotient.Len = Len - a.Len;
            for(i = quotient.Len; quotient.hp[i] == 0; --i) --quotient.Len;
            return quotient;
        }
        void operator-=(HP a){
            *this = *this - a;
            return ;
        }
        void operator+=(HP a){
            *this = *this + a;
            return ;
        }
        bool operator>(HP a){
            int i;
            if(Len > a.Len) return true;
            if(Len < a.Len) return false;
            for(i=Len-1; i>=0; --i){
                if(hp[i] > a.hp[i]) return true;
                if(hp[i] < a.hp[i]) return false;
            }
            return false;
        }
        bool operator<(HP a){
            int i;
            if(Len < a.Len) return true;
            if(Len > a.Len) return false; 
            for(i=Len-1; i>=0; --i){
                if(hp[i] < a.hp[i]) return true;
                if(hp[i] > a.hp[i]) return false;
            }
            return false;
        }
        bool operator==(HP a){
            int i;
            if(Len != a.Len) return false; 
            for(i=Len-1; i>=0; --i)
                if(hp[i] != a.hp[i]) return false;
            return true;
        }
        bool operator>=(HP a){
            int i;
            if(Len < a.Len) return false;
            if(Len > a.Len) return true; 
            for(i=Len-1; i>=0; --i){
                if(hp[i] < a.hp[i]) return false;
                if(hp[i] > a.hp[i]) return true;
            }
            return true;
        }
        bool operator<=(HP a){
            int i;
            if(Len > a.Len) return false;
            if(Len < a.Len) return true; 
            for(i=Len-1; i>=0; --i){
                if(hp[i] > a.hp[i]) return false;
                if(hp[i] < a.hp[i]) return true;
            }
            return true;
        }
};
int main()
{
    HP<10> n,m,k;
    n.get();
    m.get();
    k = n / m;
    k.print("\n");
    return 0;
}

谢谢大佬

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作