大整數類加減乘除的簡單實現 C

2021-07-09 18:19:44 字數 3516 閱讀 7188

總時間限制: 

1000ms 

記憶體限制: 

65536kb

描述 給出兩個正整數以及四則運算操作符(+ - * /),求運算結果。

輸入第一行:正整數a,長度不超過100

第二行:四則運算符o,o是「+」,「-」,「*」,「/」中的某乙個

第三行:正整數b,長度不超過100

保證輸入不含多餘的空格或其它字元

輸出一行:表示式「a o b」的值。

補充說明:

1. 減法結果有可能為負數

2. 除法結果向下取整

3. 輸出符合日常書寫習慣,不能有多餘的0、空格或其它字元

樣例輸入

9876543210

+9876543210

樣例輸出

19753086420

這次的大整數加減乘除的基本思想是用字串構造乙個類,記錄十進位制的數字。

用豎式計算實現加和減。接下來要注意,對於乘法,因為只有+/- 可以呼叫,若要求求得a*b,僅僅用a次迴圈的+=b是極慢的。所以,要用遞迴來實現,若用res記錄結果,第一次res=b,

第n次可使res=res+res;這樣res的值以二次方增長,大大節省時間。除法也是如此。

#include #include #include #include #include #include using namespace std;

void itoa1(int a, char* b, int c);

long long atoi1(const char s) //atoi

while (*s != 0)

temp = temp * 10ll + (long long)(*s - '0');

s++;

} if (*ptr == '-')

return temp;

}class longint ;

void itoa1(int n, char *str, int c) //itoa

if (str == null)

while (temp)

len = n < 0 ? ++i : i; //如果n是負數,則多需要一位來儲存負號

str[i] = 0; //末尾是結束符0

while (1)

str[i] = buf[len - i - 1]; //把buf陣列裡的字元拷到字串

} if (i == 0) }

int cmpl(string f, string l);

int main()

/* char tes[30] = {}; long long k = 0;

while (cin >> k)

}return 0;

}longint::longint(string b)

longint::longint()

longint::longint(const char *b)

string longint::operator=(const char * b)

string longint::operator+(longint b)

else

bool ad = 0;

string sr; sr.resize(maxlen + 1, '0');

for (int i = maxlen; i >= 0; i--)

char in1[2] = ;

char in2[2] = ;

res += atoi1(in1) + atoi1(in2);

if (res > 9) ; itoa1(res, temp, 10);

sr[i] = temp[1];

} else ; itoa1(res, temp, 10);

sr[i] = temp[0];

} }if (sr[0] == '0')

return sr;

}string longint::operator-(longint b)

else if (cmpl(a, b.a) == -1)

else return "0";

bool ad = 0;

string sr; sr.resize(1, '0'); sr.resize(maxlen + 1, '0');

int res = 0;

for (int i = maxlen; i >= 0; i--)

char in1[2] = ;

char in2[2] = ;

if (in1[0] + res< in2[0])

else res += atoi1(in1) - atoi1(in2);

char temp[2] = {}; itoa1(res, temp, 10);

sr[i] = temp[0];

} while (sr[0] == '0'&&sr[1] == '0')

if (nag)

else

if (sr[0] == 0)

return sr;

}string longint::operator*(longint b)

else

longint times = minn;

longint sr;

longint i = "1";

sr = maxn;

/*for (; cmpl(i+i, times.a) != 1; i = i + i) i = i - "1";

return i.a;}/*

741269586512 // 一些測試資料

*984562478654

56765156165465786156467

/54657815454782

48789151687984156

*56487112346786

8465156478654165468421

-15646878115487854312234567

48951487894146576543215646

+5123123165784512313215686

87894123467984512789

/5684

1.729826221447080406314848

2.2755958292703544831466745522616

3.1038555157

4.-15638412959009200146766146

5.54074611059931088856431332

6.15463427774100019

*/

另外,大整數也可以用鍊錶、陣列等來實現。 

大整數加減乘除(樸素版)

思路見注釋。include include includeusing namespace std int cmp string a,string b 兩個正整數相加 string add string a,string b reverse a.begin a.end 逆轉回正常的數字 return ...

大整數的加減乘除求餘

include include define n 50 n是計算的位數 class integer 大整數類 過載建構函式 友元函式實現運算子過載 friend integer operator integer integer friend integer operator integer int ...

大整數的加減乘除取模

include include using namespace std inline int compare string str1,string str2 高精度加法 string add int string str1,string str2 else else else int int1 0,...