码迷,mamicode.com
首页 > 其他好文 > 详细

大数减法

时间:2018-08-05 22:27:21      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:eve   lse   put   inpu   return   rev   set   amp   bool   

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define MaxSize 1000

bool _TwoSubStrcmp(char* Minuend,char* Subtrahend)
{
    int MinuendLen = strlen(Minuend);
    int SubtrahendLen = strlen(Subtrahend);
    
    if(MinuendLen > SubtrahendLen)
        return true;
    else if(MinuendLen == SubtrahendLen)
        return (strcmp(Minuend,Subtrahend)>0);
    else if(MinuendLen < SubtrahendLen)
        return false;
}

char *_TwoBigNumSub(char *InputMinuend,char *InputSubtrahend)
{
    char *Result = malloc(MaxSize*sizeof(char));
    int ResultEnd = 0;
    
    int MinuendLen = strlen(InputMinuend);
    int SubtrahendLen = strlen(InputSubtrahend);
    
    int Minuend[MaxSize];
    int Subtrahend[MaxSize];
    memset(Minuend,0,sizeof(Minuend));
    memset(Subtrahend,0,sizeof(Subtrahend));
    
    //reverse to store
    int InputSubend,SubendEnd;
    for(InputSubend = MinuendLen-1,SubendEnd = 0;InputSubend >= 0;InputSubend --)
    {
        Minuend[SubendEnd ++] = InputMinuend[InputSubend] - 0;
    }
    for(InputSubend = SubtrahendLen-1,SubendEnd = 0;InputSubend >= 0;InputSubend --)
    {
        Subtrahend[SubendEnd ++] = InputSubtrahend[InputSubend] - 0;
    }
    
    //Sub && Abdicate
    int Abdication = 0;
    for(SubendEnd = 0;SubendEnd < MaxSize;SubendEnd ++)
    {
        Minuend[SubendEnd] -= Abdication;
        if(Minuend[SubendEnd] < Subtrahend[SubendEnd])
        {
            Minuend[SubendEnd] += 10;
            Abdication = 1;
        }
        else
        {
            Abdication = 0;
        }
        Subtrahend[SubendEnd] = Minuend[SubendEnd] - Subtrahend[SubendEnd];
    }
    
    //store to return
    //0 screening
    for(SubendEnd = MaxSize-1;SubendEnd>=0 && Subtrahend[SubendEnd]==0;SubendEnd --)
        ;
    if(SubendEnd >= 0)
    {
        for( ;SubendEnd >= 0;SubendEnd --)
        {
            Result[ResultEnd++] = Subtrahend[SubendEnd] + 0;
        }
    }
    else
    {
        Result[ResultEnd++] = 0;
    }
    Result[ResultEnd] = \0;
    return Result;
} 

char *TwoBigNumSub(char *InputMinuend,char *InputSubtrahend)
{
    char *TempResult;
    char *Result = malloc(MaxSize*sizeof(char));
    int ResultEnd = 0;
    int TempResultEnd = 0;
    
    if(_TwoSubStrcmp(InputMinuend,InputSubtrahend)==true)
    {
        TempResult = _TwoBigNumSub(InputMinuend,InputSubtrahend);
    }
    else
    {
        Result[ResultEnd++] = -;
        TempResult = _TwoBigNumSub(InputSubtrahend,InputMinuend);
    }
    
    for(TempResultEnd = 0;TempResultEnd < MaxSize;TempResultEnd ++)
    {
        Result[ResultEnd++] = TempResult[TempResultEnd];
    }
    free(TempResult);
    return Result;
}

int main()
{
    char InputMinuend[MaxSize] = "5";
    char InputSubtrahend[MaxSize] = "692355225511444222555";
    
    char *Result = TwoBigNumSub(InputMinuend,InputSubtrahend);
    puts(Result);
    return 0;
}

 

大数减法

标签:eve   lse   put   inpu   return   rev   set   amp   bool   

原文地址:https://www.cnblogs.com/Asurudo/p/9427262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!