2012年(106)
分类: C/C++
2012-05-07 17:14:18
9、大整数
(1)大整数的输入
思路:用字符类型输入。
完整程序
#include
#include
int main()
{
char ch[101];
scanf("%s",ch);//或gets(ch);
return 0;
}
封装函数
void Input_Big_Num(char ch[101])
{
scanf("%s",ch);//或gets(ch);
}
调用函数
#include
#include
int main()
{
char ch[101];
void Input_Big_Num(char ch[101]);
Input_Big_Num(ch);
return 0;
}
(2)大整数的处理
①将大整数个位到最高位放在下标从0开始的整型数
完整程序
#include
#include
int main()
{
char ch[101];
int data[100];
int len,i;
scanf("%s",ch);//或gets(ch);
len=strlen(ch);
for(i=0;i data[i]=ch[len-i-1]-'0'; return 0; } 封装函数 int Chuli_Big_Num(char ch[101],intdata[100]) { int len,i; len=strlen(ch); for(i=0;i data[i]=ch[len-i-1]-'0'; return len-1; } 调用函数 #include #include int main() { char ch[101]={'\0'}; int data[100]={0}; void Input_Big_Num(char ch[101]); int Chuli_Big_Num(char ch[101],intdata[100]); Input_Big_Num(ch); Chuli_Big_Num(ch,data); return 0; } ②将大整数最高位到个位放在下标从0开始的整型数 完整程序 #include #include int main() { char ch[101]; int data[100]; int len,i; scanf("%s",ch);//或gets(ch); len=strlen(ch); for(i=0;i data[i]=ch[i]-'0'; return 0; } 封装函数 int Chuli2_Big_Num(char ch[101],intdata[100]) { int len,i; len=strlen(ch); for(i=0;i data[i]=ch1[i]-'0'; return len-1; } 调用函数 #include #include int main() { char ch[101]={'\0'}; int data[100]={0}; void Input_Big_Num(char ch[101]); int Chuli2_Big_Num(char ch[101],intdata[100]); Input_Big_Num(ch); Chuli_Big_Num(ch,data); return 0; } (3)大整数的输出 完整程序 #include #include int main() { char ch[101]; int data[100]; int len,i; scanf("%s",ch);//或gets(ch); len=strlen(ch); for(i=0;i data[i]=ch1[len-i-1]-'0'; for(i=len-1;i>=0;i--) printf("%d",data[i]); return 0; } 封装函数 void Output_Big_Num(int data[100],intweishu) { int i; for(i=weishu-1;i>=0;i--) printf("%d",data[i]); } 调用函数 #include #include int main() { char ch[101]={'\0'}; int data[100]={0}; int weishu; void Input_Big_Num(char ch[101]); int Chuli_Big_Num(char ch[101],intdata[100]); void Output_Big_Num(int data[100],intweishu); Input_Big_Num(ch); weishu=Chuli_Big_Num(ch,data); Output_Big_Num(data,weishu); return 0; } (4)大整数的加法(两个大整数相加) 完整程序 #include #include int main() { char ch1[101]={'\0'},ch2[101]={'\0'}; intdata1[100]={0},data2[100]={0},data3[101]={0}; int len1,len2,len,i,jinwei=0; scanf("%s",ch1);//或gets(ch1); scanf("%s",ch2);//或gets(ch2); len1=strlen(ch1); for(i=0;i data1[i]=ch1[len-i-1]-'0'; len2=strlen(ch2); for(i=0;i data2[i]=ch2[len-i-1]-'0'; for(i=0;i<100;i++) { data3[i]=data1[i]+data2[i]+jinwei; jinwei=data3[i]/10; data3[i]=data3[i]%10; } for(i=100;i>=0;i--) if(data3[i]!=0) { len=i; break; } for(i=len;i>=0;i--) printf("%d",data3[i]); return 0; } 封装函数 int Plus_Big_Num(int data1[100],intdata2[100],int data3[101]) { int i,jinwei=0; for(i=0;i<100;i++) { data3[i]=data1[i]+data2[i]+jinwei; jinwei=data3[i]/10; data3[i]=data3[i]%10; } for(i=100;i>=0;i--) if(data3[i]!=0) { weishu=i; break; } return weishu; } 调用函数 #include #include int main() { char ch1[101]={'\0'},ch2[101]={'\0'}; intdata1[100]={0},data2[100]={0},data3[101]={0}; int weishu; void Input_Big_Num(char ch[101]); int Chuli_Big_Num(char ch[101],intdata[100]); void Output_Big_Num(int data[100],intweishu); void Plus_Big_Num(int data1[100],intdata2[100],int data3[101]); Input_Big_Num(ch1); Input_Big_Num(ch2); Chuli_Big_Num(ch1,data1); Chuli_Big_Num(ch2,data2); weishu=Plus_Big_Num(data1,data2,data3); Output_Big_Num(data3,weishu); return 0; } (5)大整数的减法(两个大整数相减) #include #include int main() { void Input_Big_Num(char ch[101]); int Minus_Big_Num(int data1[100],intdata2[100],int data3[100],int len1,int len2,int *weishu); int Chuli_Big_Num(char ch[101],int data[100]); void Output_Big_Num(int data[100],intweishu); char ch1[101]={'\0'},ch2[101]={'\0'}; int len1=0,len2=0; int weishu,f; intdata1[100]={0},data2[100]={0},data3[100]={0}; int *p=&weishu; Input_Big_Num(ch1); len1=Chuli_Big_Num(ch1,data1); Input_Big_Num(ch2); len2=Chuli_Big_Num(ch2,data2); f=Minus_Big_Num(data1,data2,data3,len1,len2,p); if(f==1) printf("-"); Output_Big_Num(data3,weishu); return 0; } void Input_Big_Num(char ch[101]) { scanf("%s",ch);//或gets(ch); } int Chuli_Big_Num(char ch[101],intdata[100]) { int len,i; len=strlen(ch); for(i=0;i data[i]=ch[len-i-1]-'0'; return len-1; } int Minus_Big_Num(int data1[100],intdata2[100],int data3[100],int len1,int len2,int *weishu) { int i,flag; if(len1 { for(i=0;i<100;i++) { if(data2[i] { data2[i+1]--; data2[i]+=10; } data3[i]=data2[i]-data1[i]; } flag=1; } else { for(i=0;i<100;i++) { if(data1[i] { data1[i+1]--; data1[i]+=10; } data3[i]=data1[i]-data2[i]; } flag=0; } for(i=99;i>=0;i--) if(data3[i]!=0) { *weishu=i; break; } return flag; } void Output_Big_Num(int data[100],intweishu) { int i; for(i=weishu;i>=0;i--) printf("%d",data[i]); } (6)大整数的乘法(一个大整数乘一个小整数) 完整程序 #include #include int main() { char ch[101]={'\0'}; int data[100]={0}; int len,i,r,f=0,n; int len,i,r,f=0,n; scanf("%s",ch); scanf("%d",&n); len=strlen(ch); for(i=0;i data[i]=ch[len-i-1]-'0'; for(i=0;i<100;i++) data[i]*=n;//每一位上的数分别乘以n,并存放 for(i=0;i<100;i++) { if(data[i]>=10)//如果某一位上的数大于10,则要进位 { data[i+1]+=data[i]/10;//逢十进一 data[i]=data[i]%10;//进位之后,保留余数 } } for(i=99;i>=0;i--) { if(data[i]!=0 && f==0) { printf("%d",data[i]); f=1; } else if(f) printf("%d",data[i]); } return 0; } 封装函数 void Mutiply_BigNum_SmallNum(intData[100],int n,int *p) { int i,r; for(i=0;i<100;i++) Data[i]*=n;//每一位上的数分别乘以n,并存放 for(i=0;i<100;i++) { if(Data[i]>=10)//如果某一位上的数大于10,则要进位 { Data[i+1]+=Data[i]/10;//逢十进一 Data[i]=Data[i]%10;//进位之后,保留余数 } } for(i=99;i>=0;i--) { if(Data[i]!=0) { *p=i+1; break; } } } 调用函数 #include int main() { char ch[101]={'\0'}; int data[100]={0}; int n,weishu=0; int *p=&weishu; void Input_Big_Num(char ch[101]); int Chuli_Big_Num(char ch[101],intdata[100]); void Output_Big_Num(int data[100],intweishu); void Mutiply_BigNum_SmallNum(intData[100],int n,int *p); Input_Big_Num(ch); Chuli_Big_Num(ch,data); scanf("%d",&n); Mutiply_BigNum_SmallNum(data,n,p); Output_Big_Num(data,*p); return 0; } (7)大整数的乘法(一个大整数乘一个大整数) 完整程序 #include #include int main(){ int data1[100]={0},data2[100]={0}; int r[200]={0}; char ch1[101]={'\0'},ch2[101]={'\0'}; int i,j,k=0; int len1,len2; gets(ch1); gets(ch2); len1=strlen(ch1); len2=strlen(ch2); for(i=0;i data1[i]=ch1[len1-i-1]-'0'; for(i=0;i data2[i]=ch2[len2-i-1]-'0'; for(i=0;i { for(j=0;j { r[j+i]+=data1[j]*data2[i]; } } for(i=0;i<200;i++) { r[i+1]+=r[i]/10; r[i]%=10; } for(i=199;i>=0;i--) if(r[i]!=0) break; for(i=i;i>=0;i--) { printf("%d",r[i]); } return 0; } 封装函数 int Mutiply_BigNum_BigNum(intdata1[100],int data2[100],int r[200],int len1,int len2) { int i,j; for(i=0;i { for(j=0;j { r[j+i]+=data1[j]*data2[i]; } } for(i=0;i<200;i++) { r[i+1]+=r[i]/10; r[i]%=10; } for(i=199;i>=0;i--) if(r[i]!=0) break; return i; } 调用函数 #include #include int main() { int data1[100]={0},data2[100]={0}; int r[200]={0}; char ch1[101]={'\0'},ch2[101]={'\0'}; int i,j,k=0; int len1,len2; void Input_Big_Num(char ch[101]); int Chuli_Big_Num(char ch[101],intdata[100]); void Output_Big_Num(int data[100],intweishu); int Mutiply_BigNum_BigNum(intdata1[100],int data2[100],int r[200],int len1,int len2); Iuput_Big_Num(ch1); Input_Big_Num(ch2); len1=Chuli_Big_Num(ch1,data1)+1; len2=Chuli_Big_Num(ch2,data2)+1; i=Mutiply_BigNum_BigNum(data1,data2,r,len1,len2); for(i=i;i>=0;i--) { printf("%d",r[i]); } return 0; } (8)大整数的除法(一个大整数除以一个小整数且能整只保留整数) 完整程序 #include int main() { long int Data[100]={0},result[100]={0}; int i,k,flag=0,len; char ch[101]={'\0'}; scanf("%s",ch); len=strlen(ch); scanf("%d",&k); for(i=0;i Data[i]=ch[i]-'0'; for(i=0;i { result[i]=Data[i]/k; Data[i+1]+=Data[i]%k*10; } for(i=0;i { if(result[i]!=0) flag=1; if(flag) printf("%d",result[i]); } return 0; 封装函数 int Division_Big_Num(int Data[100],intresult[100],int k,int len) { int i,flag=0; for(i=0;i Data[i]=ch[i]-'0'; for(i=0;i { result[i]=Data[i]/k; Data[i+1]+=Data[i]%k*10; } return len; } 调用函数 #include #include int main() { char ch[101]={'\0'}; int data[100]={0},result[100]={0}; int weishu,flag=0,k,len; void Input_Big_Num(char ch[101]); int Chuli2_Big_Num(char ch[101],intdata[100]); int Division_Big_Num(int Data[100],intresult[100],int k,int len); Input_Big_Num(ch); weishu=Chuli2_Big_Num(ch,data); scanf("%d",&k); len=Division_Big_Num(data,result,k,weishu) for(i=0;i { if(result[i]!=0) flag=1; if(flag) printf("%d",result[i]); } return 0; } (9)大整数的除法(一个大整数除以一个小整数且能保留100位有效数字) 完整程序 #include int main() { long int Data[100]={0},result[100]={0}; int i,k,flag=0,len; char ch[101]={'\0'}; scanf("%s",ch); len=strlen(ch); scanf("%d",&k); for(i=0;i Data[i]=ch[i]-'0'; for(i=0;i<100;i++) { result[i]=Data[i]/k; Data[i+1]+=Data[i]%k*10; } for(i=0;i<100;i++) { if(result[i]!=0) flag=1; if(flag) printf("%d",result[i]); if(i==len-1) printf("."); } return 0; } 封装函数 int Division_Big_Num(intData[100],result[100],int k) { int i,flag=0,len; for(i=0;i<100;i++) { result[i]=Data[i]/k; Data[i+1]+=Data[i]%k*10; } return len; } 调用函数 #include #include int main() { char ch[101]={'\0'}; int data[100]={0},result[100]={0}; int weishu,flag=0,k,len; void Input_Big_Num(char ch[101]); int Chuli2_Big_Num(char ch[101],intdata[100]); int Division_Big_Num(int Data[100],intresult[100],int k,int len); Input_Big_Num(ch); weishu=Chuli2_Big_Num(ch,data); scanf("%d",&k); len=Division_Big_Num(data,result,k,weishu); for(i=0;i<100;i++) { if(result[i]!=0) flag=1; if(flag) printf("%d",result[i]); if(i==len-1) printf("."); } return 0; }