diff --git a/ADT.xcodeproj/project.xcworkspace/xcuserdata/jalor.xcuserdatad/UserInterfaceState.xcuserstate b/ADT.xcodeproj/project.xcworkspace/xcuserdata/jalor.xcuserdatad/UserInterfaceState.xcuserstate index fd3f3b4..eeea0ed 100644 Binary files a/ADT.xcodeproj/project.xcworkspace/xcuserdata/jalor.xcuserdatad/UserInterfaceState.xcuserstate and b/ADT.xcodeproj/project.xcworkspace/xcuserdata/jalor.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ADT.xcodeproj/xcuserdata/jalor.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ADT.xcodeproj/xcuserdata/jalor.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 5482817..61b4553 100644 --- a/ADT.xcodeproj/xcuserdata/jalor.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/ADT.xcodeproj/xcuserdata/jalor.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -20,38 +20,6 @@ landmarkType = "9"> - - - - - - - - @@ -78,8 +46,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "84" - endingLineNumber = "84" + startingLineNumber = "130" + endingLineNumber = "130" landmarkName = "BigNum::operator +(another)" landmarkType = "7"> @@ -94,8 +62,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "114" - endingLineNumber = "114" + startingLineNumber = "160" + endingLineNumber = "160" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> @@ -110,8 +78,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "127" - endingLineNumber = "127" + startingLineNumber = "173" + endingLineNumber = "173" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> @@ -126,8 +94,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "115" - endingLineNumber = "115" + startingLineNumber = "161" + endingLineNumber = "161" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> @@ -142,8 +110,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "110" - endingLineNumber = "110" + startingLineNumber = "156" + endingLineNumber = "156" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> @@ -158,8 +126,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "108" - endingLineNumber = "108" + startingLineNumber = "154" + endingLineNumber = "154" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> @@ -174,8 +142,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "121" - endingLineNumber = "121" + startingLineNumber = "167" + endingLineNumber = "167" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> @@ -190,8 +158,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "65" - endingLineNumber = "65" + startingLineNumber = "111" + endingLineNumber = "111" landmarkName = "BigNum::operator +(another)" landmarkType = "7"> @@ -206,8 +174,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "20" - endingLineNumber = "20" + startingLineNumber = "66" + endingLineNumber = "66" landmarkName = "BigNum::BigNum(s)" landmarkType = "7"> @@ -222,8 +190,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "21" - endingLineNumber = "21" + startingLineNumber = "67" + endingLineNumber = "67" landmarkName = "BigNum::BigNum(s)" landmarkType = "7"> @@ -238,8 +206,8 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "19" - endingLineNumber = "19" + startingLineNumber = "65" + endingLineNumber = "65" landmarkName = "BigNum::BigNum(s)" landmarkType = "7"> @@ -254,11 +222,139 @@ filePath = "ADT/BIGNUM.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "93" - endingLineNumber = "93" + startingLineNumber = "139" + endingLineNumber = "139" landmarkName = "BigNum::operator -(another)" landmarkType = "7"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ADT/BIGNUM.cpp b/ADT/BIGNUM.cpp index e17ff2f..2321880 100644 --- a/ADT/BIGNUM.cpp +++ b/ADT/BIGNUM.cpp @@ -11,6 +11,52 @@ #include using namespace std; +/* + 是否能被减的判断函数 + @param head 被减数的头指针 + @param a 被减数 + @param b 减数 + */ +bool compare(int head,int tail,BigNum &a,const BigNum &b){ + + string str1="",str2="";//比大小 + + for(int i = head,j = b.length - 1;i >= tail; i--, j--){ + str1 += a.bit[i] + 48;//被减数 + if(j >= 0){ + str2 += b.bit[j] + 48;//减数 + } else { + str2.insert(str2.begin(), '0'); + } + } + + if(str1 >= str2) + return 1;// + else + return 0;//如果大于等于就减 小于则不能减 +} + +/* + 函数减 + @param head 被减数的头指针 + @param a 被减数 + @param b 减数 + @param ans 答案 + */ +void sub(int& head,int tail,BigNum &a,const BigNum &b,BigNum &ans){ + while(a.bit[head]==0)head--;//去除a前面的0 便于计算 + + for(int i = tail,j = 0;i <= head; i++,j++){//从后往前减 + a.bit[i] = a.bit[i] - b.bit[j];//减法 + if(a.bit[i] < 0){//如果不够减借一位 + a.bit[i] += 10; + a.bit[i+1] --; + } + } + + ans.bit[tail] ++;//统计答案 +} + BigNum::BigNum(){ memset(this->bit, 0, sizeof(this->bit)); } @@ -144,9 +190,64 @@ BigNum BigNum::operator -(BigNum& another){ return big; } +/* + 乘法 + */ +BigNum BigNum::operator *(const BigNum& another){ + //将结果储存在 resullt中,result[i + j] += a[i] * b[j]是关键算法 + + BigNum result; + /* 设置符号位 */ + if(this->flag != true && another.flag != true){//-x * -y + result.flag = true; + }else if(this->flag == false || another.flag == false){ //-x || -y + result.flag = false; + } + + /* 乘法运算 */ + for(int i = 0; i < this->length; i++) + { + for(int j = 0; j < another.length; j++) + { + result.bit[i + j] += this->bit[i] * another.bit[j]; + } + } + + //从低位到高位进行进位 + for(int i = 0; i < (this->length + another.length); i++) + { + if(result.bit[i] > 9) + { + result.bit[i+1] += result.bit[i]/10; + result.bit[i] %= 10; + } + } + + result.length = (this->length + another.length); + return result; +} -//BigNum operator *(const BigNum&); -//BigNum operator /(const BigNum&); +/* + 除法 + */ +BigNum BigNum::operator / (const BigNum& another){ + int head = (*this).length - 1;//被减数头指针 + BigNum result;//存储结果 + + int tail = head - another.length + 1;//被除数的尾指针 + + for(; tail >= 0;tail--){//查看该数放在哪一个位置 + if( !compare(head,tail,(*this),another) ){//如果a小于b 则继续下一层 即把b数往后挪一位 + continue; + } + else while( compare(head,tail,(*this),another) ){ + sub(head,tail,(*this),another,result); + } + //只要能减就一直减 直到a小于b 同时在 tail处统计答案 + } + result.length = (*this).length; + return result; +} /* diff --git a/ADT/BIGNUM.hpp b/ADT/BIGNUM.hpp index 3d16a2c..d61072e 100644 --- a/ADT/BIGNUM.hpp +++ b/ADT/BIGNUM.hpp @@ -25,10 +25,12 @@ class BigNum{ BigNum(); BigNum operator +(BigNum&); BigNum operator -(BigNum&); -// BigNum operator *(const BigNum&); -// BigNum operator /(const BigNum&); + BigNum operator *(const BigNum&); + BigNum operator /(const BigNum&); /* 输入输出 */ friend ostream & operator <<(ostream &output,const BigNum &); friend istream & operator >>( istream &input,BigNum &); + friend bool compare(int head,int tail,BigNum &a,const BigNum & b); + friend void sub(int& head,int tail,BigNum &a,const BigNum &b,BigNum &ans); }; #endif /* BIGNUM_hpp */ diff --git a/ADT/main.cpp b/ADT/main.cpp index 7213e83..075b7db 100644 --- a/ADT/main.cpp +++ b/ADT/main.cpp @@ -12,13 +12,48 @@ #include "BIGNUM.hpp" using namespace std; int main(int argc, const char * argv[]) { - cout<<"Input num1:"<>num1; - cout<<"Input num2:"<>num2; - BigNum num3 = num1 + num2; - cout<<"Ans:"<>str; + + int idx; + idx = str.find('+'); + if(idx > -1){ + string s1 = str.substr(0,idx); + string s2 = str.substr(idx+1); + num1 = BigNum(s1); + num2 = BigNum(s2); + cout<<"="< -1){ + string s1 = str.substr(0,idx); + string s2 = str.substr(idx+1); + num1 = BigNum(s1); + num2 = BigNum(s2); + cout<<"="< -1){ + string s1 = str.substr(0,idx); + string s2 = str.substr(idx+1); + num1 = BigNum(s1); + num2 = BigNum(s2); + cout<<"="< -1){ + string s1 = str.substr(0,idx); + string s2 = str.substr(idx+1); + num1 = BigNum(s1); + num2 = BigNum(s2); + cout<<"="<