導航:首頁 > 編程語言 > 儲存40位的大整數型代碼

儲存40位的大整數型代碼

發布時間:2021-10-14 08:45:25

A. 如何實現用c++語言編寫存放40位的整數

1,用3個int型的變數,聯合表示

2,用字元串存放

B. java裡面如何定義一個40位的整數

可以用一個長度為40的數組,沒位數值為0-9,這樣的數組算是一個數組

C. C++裡面大於longlong的數字怎麼聲明啊,比如一個40位的數字(十進制)……

沒有哦,你可以用高精度,例如高加,你只需改變N的值(這里是500位的)就可以了:
#include<iostream>
#include<string>
using namespace std;
const int N=501;
string a,b;
int c[N],d[N],e[N],k;
int main()
{
int i,j;
cin>>a>>b;
for(i=0;i<a.size();i++)
c[i]=a[a.size()-i-1]-'0';
for(i=0;i<b.size();i++)
d[i]=b[b.size()-i-1]-'0';
if (a.size()>=b.size())
k=a.size();
else
k=b.size();
for(i=0;i<k;i++)
{
c[i]+=d[i];
if(c[i]>=10)
{
c[i]=c[i]%10;
c[i+1]++;
}
}
if(!c[k])
for(j=k-1;j>=0;j--)
cout<<c[j];
else
for(j=k;j>=0;j--)
cout<<c[j];
return 0;
}

D. 大整數加法 C語言 求代碼 要求如下

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

voidreverse(char*s)/*將字元串逆置*/
{
intlength;
inti=0;
chartemp;
length=strlen(s);
while(i<length-i-1)
{
temp=s[i];
s[i]=s[length-i-1];
s[length-i-1]=temp;
i++;
}
}

voidAddBigNum(char*s1,char*s2,char*result)
{
intlen1=strlen(s1);
intlen2=strlen(s2);
intacc=0,temp,i;/*acc為進位標記*/
if(s1==NULL||s2==NULL||result==NULL)
{
return;
}
reverse(s1);
reverse(s2);
for(i=0;i<len1&&i<len2;i++)
{
temp=s1[i]-'0'+s2[i]-'0'+acc;/*計算每位的實際和*/
result[i]=temp%10+'0';/*通過求余數來確定每位的最終值*/
if(temp>=10)/*通過這個if..else..條件來判斷是否有進位,並設置進位值*/
acc=1;
else
acc=0;
}
if(i<len1)/*兩個加數位數不同*/
{
for(;i<len1;i++)
{
temp=s1[i]-'0'+acc;/*依舊要考慮進位,比如9999+1的情況*/
result[i]=temp%10+'0';
if(temp>=10)
acc=1;
else
acc=0;
}
}
if(i<len2)
{
for(;i<len2;i++)
{
temp=s2[i]-'0'+acc;
result[i]=temp%10+'0';
if(temp>=10)
acc=1;
else
acc=0;
}
}

if(acc==1)/*考慮如:123+911=1034的情況,如果不增加這個條件會得到結果為034,進位被舍棄*/

result[i++]='1';

result[i]='';
reverse(result);
}

main()
{
chars1[405];
chars2[405];
charresult[405];
intN;
scanf("%d",&N);
while(N--)
{
scanf("%s%s",s1,s2);
AddBigNum(s1,s2,result);
printf("%s ",result);
}
return0;
}

運行結果如圖:

E. 求用C語言寫的「大整數的四則運算」程序源代碼,請看詳細描述,刷分勿近!

/////這個是C++的,C++下比較模塊化一點用的是字元串存儲整數,比數組存儲容易實現點。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////1. 輸入形式為:A[空格或換行]O[空格或換行]B;
//// 2. 1中A、B為大數,O為運算符(如輸入:123456789 / 432432);
//// 3. 既然處理大數,就沒必要輸入小數點位了;
//// 4.加減不能處理負號,乘除可以;
//// 5. 用於學習交流,若發現錯誤或不妥之處可聯系
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<string>
using namespace std;
class BigFigure{
string num1,num2;
string outcome;
int precision;
char operation;
public:
BigFigure(){
num1=num2="0";
outcome="0";
precision=5;
}
string& plus(string &, string &);
string& subtration(string &, string &);
string& multiplication(string &, string &);
string& division(string &, string &);
void show();
void BigFigureInterface();
string &revese(string&);
friend istream& operator>>(istream& i, BigFigure& a){
return i>>a.num1>>a.operation>>a.num2;
}
~BigFigure(){ }
};
void BigFigure::show(){
cout<<"Result: "<<outcome<<endl;
}
void BigFigure::BigFigureInterface(){
BigFigure a;
cout<<"*********************************************************/n";
cout<<" Welcome... /n";
cout<<" Four Arithmetic Operations of Big Figures/n";
cout<<"*********************************************************/n/n";
cout<<"Notes:/n";
cout<<" 1. 輸入形式為:A[空格或換行]O[空格或換行]B。/n";
cout<<" 2. 1中A、B為大數,O為運算符(如輸入:123456789 / 432432)。/n";
cout<<" 3. 既然處理大數,就沒必要輸入小數點位了。/n";
cout<<" 4. 加減不能處理負號,乘除可以。/n";
cout<<" 5. 用於學習交流,若發現錯誤可聯系519916178@qq.com。/n/n";
cout<<"Now Start, Input 0 0 0 to end if you want to quit!/n/n";
cout<<"[BigFigure #] ";
cin>>a;
while(a.operation!='0'){
switch(a.operation){
case '+': a.plus(a.num1, a.num2);
a.show(); break;
case '-': a.subtration(a.num1, a.num2);
a.show(); break;
case '*': a.multiplication(a.num1, a.num2);
a.show(); break;
case '/': a.division(a.num1, a.num2);
a.show(); break;
default:cout<<a.operation<<" is not Arithmetic Operation./n";
}
cout<<"[BigFigure #] ";
cin>>a;
}
system("cls");
cout<<"/n/n/n/n/n/n/t/t Quited.../n/n/n/n/n/n/n";
system("pause");
}

string& BigFigure::revese(string& s){
char c;
int t=s.size();
for(int i=0; i<t/2; i++){
c=s[i];
s[i]=s[t-i-1];
s[t-i-1]=c;
}
return s;
}
string& BigFigure::plus(string &str1, string &str2){//加法運算,未處理符號
int min=0,i,t=0;
string temp;
outcome.clear();
str1=revese(str1);
str2=revese(str2);
min=str1.size()<str2.size() ? str1.size() : str2.size();
for(i=0; i<min; i++){
temp+=(str1[i]+str2[i]-96+t)%10+48;
t=(str1[i]+str2[i]-96+t)/10;
}
if(min==str1.size()){
while(i<str2.size()){
temp+=(str2[i]+t-48)%10+48;
t=(str2[i]-48+t)/10;
i++;
}
if(t) temp+='1';
}
else{
while(i<str1.size()){
temp+=(str1[i]+t-48)%10+48;
t=(str1[i]-48+t)/10;
i++;
}
if(t) temp+='1';
}
outcome=revese(temp);
return outcome;
}

string &BigFigure::subtration(string &str1, string &str2){//減法運算,未處理符號
int min=0,flag,i,t=0;
string temp;
outcome.clear();
if(str1.size()>str2.size() || (str1.size()==str2.size() && str1.compare(str2)==1)){
str1=revese(str1);
str2=revese(str2);
flag=0;
min=str2.size();
}
else if(str1.size()==str2.size()){
if(!str1.compare(str2)){
outcome='0';
return outcome;
}
if(str1.compare(str2)==-1){
temp=str1;
str1=revese(str2);
str2=revese(temp);
flag=1;
min=str2.size();
}
}
else{
temp=str1;
str1=revese(str2);
str2=revese(temp);
flag=1;
min=str2.size();
}
temp.clear();
for(i=0; i<min; i++){
if(str1[i]-t<str2[i]){
temp+=str1[i]+10-t-str2[i]+48;
t=1;
}
else{
temp+=str1[i]-t-str2[i]+48;
t=0;
}
}
while(i<str1.size()){
if(!t){
while(i<str1.size()){
temp+=str1[i];
i++;
}
break;
}
if(str1[i]!='0'){ temp+=str1[i]-t; t=0; }
else temp+='9';
i++;
}
string s;
for(unsigned int k=temp.size()-1; k>=0; k--){
if(temp[k]!='0'){
for(int n=k; n>=0; n--)
s+=temp[n];
break;
}
}
if(flag) s='-'+s;
outcome=s;
return outcome;
}
string& BigFigure::multiplication(string &str1, string &str2){//乘法運算,已處理符號
char c='0',flag=0;
string temp1,temp2="0";
if(str1[0]=='0' || str2[0]=='0'){
outcome="0";
return outcome;
}
if(str1[0]=='-'){ flag++; str1.erase(0,1); }
if(str2[0]=='-'){ flag++; str2.erase(0,1); }
str1=revese(str1);
str2=revese(str2);
for(unsigned int i=0; i<str2.size(); i++){
c='0';
for(unsigned int j=0; j<str1.size(); j++){
temp1+=((str2[i]-48)*(str1[j]-48)+c-48)%10+48;
c=((str2[i]-48)*(str1[j]-48)+c-48)/10+48;
}
if(c!='0') temp1+=c;
temp1=revese(temp1);
for(int k=0; k<i; k++)
temp1+='0';
temp2=plus(temp1, temp2);
temp1.clear();
}
if(flag%2) temp2='-'+temp2;
outcome=temp2;
return outcome;
}

string& BigFigure::division(string &str1, string &str2){//除法運算,已處理符號
int str=0,flag=0,flag1=0,flag2=0;
string temp,temps,tempt;
if(str2=="0"){
outcome="Inf";
return outcome;
}
if(str2=="1" || str1=="0"){
outcome=str1;
return outcome;
}
if(str1[0]=='-'){ flag++; str1.erase(0,1); }
if(str2[0]=='-'){ flag++; str2.erase(0,1); }
for(unsigned int i=0; i<str1.size(); i++){//整除處理
str=0;
temp+=str1[i];
if(temp[0]=='0') temp.erase(0,1);
if(temp.size()>str2.size() ||
(temp.size()==str2.size() && temp.compare(str2)>=0)){
while(temp.size()>str2.size() ||
(temp.size()==str2.size() && temp.compare(str2)>=0)){
tempt=str2;
temp=subtration(temp, tempt);
str++;
}
temps+=str+48;
}
else if(temp.size()==str2.size() && temp.compare(str2)==0) temps+='1';
else temps+='0';
}
flag1=temps.size();
if(temp!="0" && precision) temps+='.';
for(int i=0; i<=precision && temp!="0"; i++){//小數後位的處理
str=0;
temp+='0';
if(temp.size()>str2.size() ||
(temp.size()==str2.size() && temp.compare(str2)>=0)){
while(temp.size()>str2.size() ||
(temp.size()==str2.size() &&temp.compare(str2)>=0)){
tempt=str2;
temp=subtration(temp, tempt);
str++;
}
temps+=str+48;
}
else if(temp.size()==str2.size() && temp.compare(str2)==0) temps+='1';
else temps+='0';
}
flag2=temps.size()-2;
if(temps[temps.size()-1]>='5' && flag2-flag1==precision){//四捨五入處理
temps.erase(flag1,1);
temps.erase(temps.size()-1, 1);
temp="1";
temps=plus(temps, temp);
if(temps.size()>flag2) temps.insert(flag1+1, 1, '.');
else temps.insert(flag1, 1, '.');
}
else if(flag2-flag1==precision) temps.erase(temps.size()-1, 1);
temp.clear();
for(unsigned int k=0; k<temps.size(); k++){//左端去零處理
if(temps[k]!='0' || temps[k+1]=='.'){
for(int n=k; n<temps.size(); n++)
temp+=temps[n];
break;
}
}
if(flag%2) temp='-'+temp;
outcome=temp;
return outcome;
}
int main(){//主函數
BigFigure a;
a.BigFigureInterface();
return 0;

F. C語言中的大整數儲存問題

以字元串的形式存儲在數組里

G. C++~如何儲存大整數~10位以上!!!

用int的數組,一位位存進去,要計算的話,只能自己手寫代碼了

H. c++輸入兩個大的整數(40位以內),求它們的差並輸出 急哦!!!

#include <memory.h>
#include <iostream>
#include <cstring>

using namespace std;
void minus(char num1[], char num2[], char ret[]);

int main()
{
char num1[41], num2[41], result[42];
memset(result, 0, 42);

cout << "請輸入兩個數字" << endl;
cin >> num1 >> num2;
minus(num1, num2, result);
cout << result << endl;

return 0;
}

void minus(char num1[], char num2[], char ret[])
{
int l1, l2, i, j;
l1 = strlen(num1), l2 = strlen(num2);
i = l1 - 1, j = l2 - 1;

if (l1 > l2)
{
L1:
for (; j >= 0; --i, --j)
{
int temp = num1[i] - num2[j];

if (temp < 0)
{
num1[i] += (10 - (num2[j] - '0'));
num1[i - 1] -= 1;

for (int k = i - 1; k >= 0; --k)
{
if (num1[k] < '0')
{
num1[k] += 10;

if (k - 1 >= 0)
num1[k - 1] -= 1;
}
else
break;
}
}
else
num1[i] = ('0' + temp);
}

i = 0;
while (num1[i] <= '0' || num1[i] > '9') ++i;

//strcpy(ret, &num1[i]);
strcat(ret, &num1[i]);
}
else if (l1 < l2)
{
L2:
for (; i >= 0; --i, --j)
{
int temp = num2[j] - num1[i];

if (temp < 0)
{

num2[j] += (10 - (num1[i] - '0'));
num2[j - 1] -= 1;

for (int k = j - 1; k >= 0; --k)
{
if (num2[k] < '0')
{
num2[k] += 10;

if (k - 1 >= 0)
num2[k - 1] -= 1;
}
else
break;
}
}
else
num2[j] = ('0' + temp);
}

ret[0] = '-';
i = 0;

while (num2[i] <= '0' || num2[i] > '9') ++i;

strcpy(&ret[1], &num2[i]);
}
else
{
int less = 0;

for (int k = l1 - 1; k >= 0; --k)
{
if (num1[k] < num2[k])
less = 1;
else if (num1[k] > num2[k])
less = 0;
}

if (!less)
{
goto L1;
}
else
{
goto L2;
}
}
}

I. JAVA中long型代碼,支持大整數的四則運算

public long add(long a , long b){
BigInteger bigIntA = new BigInteger(a + "");
BigInteger bigIntB = new BigInteger(b + "");
return bigIntA.add(bigIntB).longValue;
}public long subtract(long a , long b){
BigInteger bigIntA = new BigInteger(a + "");
BigInteger bigIntB = new BigInteger(b + "");
return bigIntA.subtract(bigIntB).longValue;
}public long multiply(long a , long b){
BigInteger bigIntA = new BigInteger(a + "");
BigInteger bigIntB = new BigInteger(b + "");
return bigIntA.multiply(bigIntB).longValue;
}public long divide(long a , long b){
BigInteger bigIntA = new BigInteger(a + "");
BigInteger bigIntB = new BigInteger(b + "");
return bigIntA.divide(bigIntB).longValue;
}

J. 實現一個大整數類HugeInteger。在該類中使用一個40個元素的整形數組來存儲一個大整數(每一

#include<stdio.h>int main()
{
int a[10];
int i,j;
int t;
printf("Input 10 integer:\n");
for(i = 0;i < 10;i++)
scanf("%d",&a[i]);
for(i = 0;i < 10;i++)
for(j = 0;j < 10-i-1;j++)
if(a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
for(i = 0;i < 10;i++)
printf("%d ",a[i]);
return 0;
}

閱讀全文

與儲存40位的大整數型代碼相關的資料

熱點內容
多張圖紙怎樣列印到一個pdf文件里 瀏覽:347
安裝cad2020出現無法定位inf文件 瀏覽:728
百度競價數據如何分析 瀏覽:965
ps文件發送第三方列印 瀏覽:547
linux命令界面顯示文件名 瀏覽:930
超級錄屏視頻在文件夾里不能播放 瀏覽:549
最簡單的編程軟體有哪些 瀏覽:14
博客簽到系統如何設計資料庫 瀏覽:639
論文怎麼設置目錄word 瀏覽:609
廣電網路dlna是什麼意思 瀏覽:12
js變數加下劃線 瀏覽:18
app開發工作是吃青春飯嗎 瀏覽:117
蘋果手機國家查詢 瀏覽:765
蘋果6照片刪了怎麼找回 瀏覽:399
文件夾控制面板 瀏覽:536
人工神經網路人臉識別 瀏覽:531
打開cad提示參照文件 瀏覽:521
如何計算數軸上三點abc對應的數據 瀏覽:985
文件夾到u盤變成多少kb 瀏覽:351
sfs文件怎麼解壓 瀏覽:39

友情鏈接