导航:首页 > 编程语言 > 储存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位的大整数型代码相关的资料

热点内容
暴风转码如何添加文件夹 浏览:515
延安整合网络营销有哪些 浏览:74
查找word打开过的文件在哪里 浏览:137
b树java代码 浏览:683
电脑文件存储 浏览:657
兰州中考征集志愿在哪个网站 浏览:215
cs文件上传下载 浏览:244
拷贝文件到根目录下重命名linux 浏览:603
api函数的头文件 浏览:249
华为怎么绑定迷你编程 浏览:215
机构怎么申请少儿编程考级 浏览:495
昆山数控编程哪里好学 浏览:459
jspcfor跳出 浏览:65
word怎么插入罗马数字i 浏览:315
哪个网站可以找到法人代表 浏览:106
苹果5s日版a1453支持什么网络 浏览:297
微信打开文件如何设置 浏览:218
漫画书app中非可视组件是什么 浏览:3
d盘文件隐藏怎么恢复 浏览:287
5s怎么设置联通4g网络 浏览:15

友情链接