模拟...

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
#define maxn 10000

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

int modNum(char* ch,int b) // 大数对整数的取模运算
{
    int left=0,i;
    for(i=-1; ch[i+1]!='\0'; ++i);
    for(int j=0; j<=i; ++j)
    {
        left = (left*10)%b+ch[j]-'0';
        left %=b;
    }
    return left;
}

char* div(char *ch, int b) { // 大数对整数的相除运算
    char ansdiv[1010] = "\0";
    int cnt = 0, remain = 0;
    for (int i=0; ch[i]; ++i) {
        if (remain*10+ch[i]-'0' >= b) { // 够除
            ansdiv[cnt++] = (remain*10 + ch[i]-'0')/b+'0';
            remain = (remain*10 + ch[i]-'0')%b;
        }
        else if (cnt != 0){
            ansdiv[cnt++] = '0'; // 否则添0
            remain = remain*10 + ch[i] - '0';
        }
        else remain = remain*10 + ch[i] - '0';
    }
    if (remain && cnt==0) {
        ansdiv[0] = '0';
    }
    return ansdiv;
}

char* multi(char *ch, int b)  { // 大数和整数相乘
    int i;
    int jinwei = 0;
    char ansdiv[1010];
    for (i=-1; ch[i+1]; ++i); // i为字符串长度
    int cnt = 0;

    for (int j=i; j>=0; --j) {
        int temp = (ch[j] - '0' + jinwei) * b;
        ansdiv[cnt++] = temp % 10 + '0';
        temp = temp / 10;
    }
    while(jinwei) {
        ansdiv[cnt++] = jinwei % 10;
        jinwei /= 10;
    }
    ansdiv[cnt++] = '\0';
    return ansdiv;
}

int main() {
    int mod;
    int b;
    char res[210];
    char *ans;
    while(cin >> res >> b) {
        // 除法运算
        ans = div(res, b);
        cout << ans << "...\n";
        // 乘法运算
        ans = multi(res, b);
        int len = strlen(ans);
        for (int i=len-1; i>=0; --i) {
            cout << ans[i];
        }
        cout << "---\n";
        //取模运算
        int ansmod = modNum(res, b);
        cout << ansmod << "....\n";
    }
    return 0;
}

  

posted on 2016-05-23 10:59  小小八  阅读(763)  评论(0编辑  收藏  举报