/*
因為計算大數除法時需要用到乘法和減法,
但是不指定字串長度的乘法和減法不容易用字元陣列表示,
所以這裡就沒寫用字元陣列計算的大數除法。o(╯□╰)o
*//***********大數加減乘/僅限正整數***************/
//加法測試:hdu 1002
//減法測試:百練oj 2736
//乘法測試:百練oj 2980
#include
#include
#include
#include
#include
#include
using
namespace
std;
const
int max_n=100010;
char a[max_n],b[max_n],ope[10],ans[max_n];
int data[max_n];
void big_plus()
else
int carry=0,lenans=max(lena,lenb);
ans[lenans]='\0';//新增結束符
int tmp=lenans-1;
while(tmp>=0)
if(carry)
}void big_sub()
}i=0;
//去除前導0
while(a[i]=='0'&&i1) i++;
strcpy(ans,a+i);//將結果複製到ans陣列
} else
}j=0;
while(b[j]=='0'&&j1) j++;
ans[0]='-';//運算結果為負
strcpy(ans+1,b+j);
}}void big_mul()
int carry=0;
for(int i=0;iint tmp=data[i]+carry;
carry=tmp/10;
data[i]=tmp%10;
}while(carry)
while(data[lenans-1]==0&&lenans>1) lenans--;
for(int i=0;i<=lenans-1;i++) ans[i]=data[lenans-1-i]+'0';
ans[lenans]='\0'; //新增結束符
}int main()
else
if(ope[0]=='-')else
if(ope[0]=='*')
puts(ans);
}return
0;}
/********大數加減乘除c++string類實現**********/
//運算數僅限正整數
//加法測試:hdu 1002
//減法測試:百練oj 2736
//乘法測試:百練oj 2980
//除法測試:百練oj 2737
#include
#include
#include
#include
#include
#include
#include
using
namespace
std;
//string比較函式:相等返回0,str1>str2返回1,str1int compare(string str1,string str2)
string big_plus(string str1,string str2)
}else
}int len=max(len1,len2);
int carry=0;
for(int i=len-1;i>=0;i--)
if(carry) ans=char(carry+'0')+ans;
return ans;
}//支援大數減小數
string big_sub(string str1,string str2)
else
}for(int i=difference-1;i>=0;i--)else
}//去除前導0
ans.erase(0,ans.find_first_not_of('0'));
if(ans.empty()) ans="0";
return ans;
}string big_mul(string str1,string str2)
for(int j=len1-1;j>=0;j--)
if(carry!=0) tmpstr=char(carry+'0')+tmpstr;
}ans=big_plus(ans,tmpstr);
}ans.erase(0,ans.find_first_not_of('0'));
if(ans.empty()) ans="0";
return ans;
}//正數相除,商為quotient,餘數為residue
void big_div(string str1,string str2,string& quotient,string& residue)
if(str1=="0")
int res=compare(str1,str2);
if(res<0)else
if(res==0)else }}
residue=tmpstr;
}quotient.erase(0,quotient.find_first_not_of('0'));
if(quotient.empty()) quotient="0";
}int main()
大數加減乘除
include include include include includeusing namespace std compare比較函式 相等返回0,大於返回1,小於返回 1 int compare string str1,string str2 if cf 0 str char cf 0 st...
大數加減乘除
對於大數的加減乘除都要先把輸入的大數字串轉成倒序的陣列,再進行運算。字串的處理 scanf s s a1,b1 len 0 for int i strlen a1 1 i 0 i len 0 for int i strlen b1 1 i 0 i len max strlen a1 strlen b...
大數的加減乘除
當數的位數非常大而常規的資料型別不足以儲存的時候,可以考慮使用大數。即用乙個長的陣列來儲存相應的位數,如int a 100 a的每一位儲存一位數字或幾位數字,根據常規的加減乘除運算對陣列進行操作。使用string型別對陣列進行操作,雖然比用整型簡便,但可 能增加了執行時間。pku1001 expon...