大数减法
Posted asurudo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数减法相关的知识,希望对你有一定的参考价值。
#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] = ‘