大整数除法、乘法、加法(C++实现)

//#include<bits/stdc++.h>
#include<iostream>
#include<cctype>
using namespace std;
string s;
// 大整数 / 一位整数
string divide(string s, int x, int &yushu){
    yushu = 0;
    for(int i=0; i<s.size(); i++){
        yushu = yushu*10 + (s[i]-'0');
        s[i] = yushu / x + '0';
        yushu = yushu % x;
    }
    int i = 0;
    while(s[i]=='0'){
        i++;
    }
    if(i>=s.size())
        s = "??";
    else
        s = s.substr(i);
    return s;
}
// 大整数 * 一位整数
string multiple(string s, int x){
    int temp = 0;
    for(int i=s.size()-1; i>=0; i--){
        temp = (s[i]-'0') * x + temp;
        s[i] = temp%10 + '0';
        temp = temp/10;
    }
    // 最高位存在进位
    if(temp!=0){
        s = to_string(temp) +  s;
    }
    return s;
}
// 大整数 + 一位整数
string add(string s, int x){
    int temp = x;
    if(x==0)
        return s;
    else{
        for(int i=s.size()-1; i>=0; i--){
            temp = (s[i]-'0') + temp;
            s[i] = temp%10 + '0';
            temp = temp/10;
        }
    }
    // 最高位存在进位
    if(temp!=0){
        s = to_string(temp) + s;
    }
    return s;
}
int main()
{
    cin>>s;
    int yushu;
    string ans = divide(s, 2, yushu);
    cout<<ans<<endl;

    ans = multiple(s, 2);
    cout<<ans<<endl;

    ans = add(s, 1);
    cout<<ans<<endl;
    return 0;
    //样例:173   
    //	86  346  174 
}
//例题:http://t.cn/AiCuoHKg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值