c语言大数加法指针函数,大数加减法c语言
C语言大数相加,在线等,急
效果:
站在用户的角度思考问题,与客户深入沟通,找到绥芬河网站设计与绥芬河网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册、网站空间、企业邮箱。业务覆盖绥芬河地区。
代码:
#includestdio.h
#includestring.h
void reverse(char s[]) {
int len = strlen(s), h = len / 2;
char temp;
for (int i = 0; i h; ++i) {
temp = s[i];
s[i] = s[len - i - 1];
s[len - i - 1] = temp;
}
}
#define System 10
#define MAX 24
const char mx = '9';
const char mn = '0';
char* Sum(char s1[], char s2[]) {
char ans[MAX];
int len1 = strlen(s1), len2 = strlen(s2), ad = 0, min = len1 len2 ? len1 : len2, max = len1len2 ? len1 : len2, i;
char* st1, *st2;
if (len1 len2) { st1 = s2; st2 = s1; }
else { st1 = s1; st2 = s2; }
reverse(st1);
reverse(st2);
for (i = 0; i min; i++) {
ans[i] = st1[i] + st2[i] - '0' + ad;
if (ans[i] mx) {
ans[i] -= System;
ad = 1;
}
else ad = 0;
}
while (ad != 0 || i max) {
if (i max)ans[i] = st2[i] + ad;
else ans[i] = mn + ad;
if (ans[i] mx) {
ans[i] -= System;
ad = 1;
}
else ad = 0;
i++;
}
ans[i] = '\0';
reverse(ans);
return ans;
}
int main() {
char A[21], B[21], C[50], *ans;
int n, m;
scanf("%d %d", m, n);
scanf("%s %s", A, B);
ans = Sum(A, B);
strcpy(C, ans);
printf("%s\n", C);
}
c语言 大数相加
c/c++中int和unsigned类型变量,都不能保存超过10位的整数,但有时我们需要计算位数非常长的整数或小数的加法。一般我们称这种基本数据类型无法表示的整数为大整数。如何表示和存放大整数呢?基本的思想就是:用数组存放和表示大整数。一个数组元素,存放大整数中的一位。
现在假如我们要计算俩个200位数的加法。显然,任何C/C++固有类型的变量都无法保存它。最直观的想法是可以用一个字符串来保存它。字符串本质上就是一个字符数组,因此为了编程更方便,我们也可以用数组int an[200]来保存一个200 位的整数,让an[0]存放个位数,an[1]存放十位数,an[2]存放百位数……那么如何实现两个大整数相加呢?方法很简单,就是模拟小学生列竖式做加法,从个位开始逐位相加,超过或达到10 则进位。也就是说,用int an1[201]保存第一个数,用int an2[200]表示第二个数,然后逐位相加,相加的结果直接存放在an1 中。要注意处理进位。另外,an1 数组长度定为201,是因为两个200 位整数相加,结果可能会有201 位。实际编程时,不一定要费心思去把数组大小定得正好合适,稍微开大点也无所谓,以免不小心没有算准这个“正好合适”的数值,而导致数组小了,产生越界错误。
下面是具体程序:
#includestdio.h
#includestring.h
#defineMAX_LEN 200
int an1[MAX_LEN+10];
int an2[MAX_LEN+10];
charszLine1[MAX_LEN+10];
charszLine2[MAX_LEN+10];
int main(void)
{
scanf("%s", szLine1);
scanf("%s", szLine2);
inti, j;
memset( an1, 0, sizeof(an1));
memset( an2, 0, sizeof(an2));
int nLen1 = strlen( szLine1);
for( j = 0, i = nLen1 - 1;i = 0 ; i--)
an1[j++] = szLine1[i]- '0';
int nLen2 = strlen(szLine2);
for( j = 0, i = nLen2 - 1;i = 0 ; i--)
an2[j++] = szLine2[i]- '0';
for( i = 0;i MAX_LEN ; i++ )
{ an1[i]+= an2[i];//逐位相加
if( an1[i] = 10 )
{ //看是否要进位
an1[i] -= 10;
an1[i+1] ++; //进位
}
}
for( i = MAX_LEN; (i= 0) (an1[i] == 0); i-- ) ;
if(i=0)
for( ; i = 0; i--)
printf("%d", an1[i]);
else printf("0");
return 0;
}
c语言大数相加,简单点的~
#includestdio.h
#includestring.h
int Maxlen ;
char a[110] , b[110] ;
int an1[110] , an2[110] ;
int Addition(int maxlen , int an1[] , int an2[] );
int main()
{
int i , j ;
scanf("%s" , a) ;
scanf("%s" , b) ;
memset( an1 , 0 , sizeof(an1) ) ;
memset( an2 , 0 , sizeof(an2) ) ;
int len1 = strlen(a) ;
int len2 = strlen(b) ;
if(len1 len2 )
Maxlen = len1 ;
Maxlen = len2 ;
for(j = 0, i = strlen(a)- 1 ; i = 0 ; i --)
an1[j++] = a[i] - '0';
for(j = 0 , i = len2 - 1 ; i = 0 ; i --)
an2[j++] = b[i] - '0';
Addition ( Maxlen , an1 , an2 ) ;
for( i = 100 ; i = 0; i -- )
{
if( an1[i] != 0 )break;
}
for(;i=0;i--)
printf("%d" ,an1[i]) ;
printf("\n") ;
}
int Addition( int maxlen , int an1[] , int an2[] )//大数相加函数
{
for( int i = 0 ; i maxlen ; i++ )
{
an1[i] += an2[i] ;
if( an1[i] = 10 )
{
an1[i] -= 10 ;
an1[i + 1] ++ ;
}
}
return 0;
}
C语言 大数加法
#include stdio.h
#include stdlib.h
#define NumLen 1000
typedef struct {
int sign; //符号位
int nLen; //本数字实际长度
unsigned short value[NumLen]; //存储数值
}BigNumber;
void InitBigNumber(BigNumber * n);
void InputNumber(BigNumber * n);
void OutputBigNumber(BigNumber * n);
void CopyBigNumber(BigNumber * source, BigNumber * target);
void BigNumberPlus(BigNumber * n1, BigNumber * n2, BigNumber * result);
void BigNumberSubtract(BigNumber * n1, BigNumber * n2, BigNumber * result);//n1减n2
void BigNumberMultiply(BigNumber * n1, BigNumber * n2, BigNumber * result);
void BigNumberDivide(BigNumber * n1, BigNumber * n2, BigNumber * result, BigNumber * residue); //n1除以n2
int main()
{
int count,i;
scanf("%d", count);
BigNumber * n1, * n2, * result;
n1 = (BigNumber *)malloc(sizeof(BigNumber));
n2 = (BigNumber *)malloc(sizeof(BigNumber));
result = (BigNumber *)malloc(sizeof(BigNumber));
for(i=1; i=count; i++)
{
InitBigNumber(n1);
InitBigNumber(n2);
InputNumber(n1);
InputNumber(n2);
BigNumberPlus(n1, n2, result);
printf("Case %d:\n", i);
OutputBigNumber(n1);
printf(" + ");
OutputBigNumber(n2);
printf(" = ");
OutputBigNumber(result);
if (i != count)
{
printf("\n\n");
}
else
{
printf("\n");
}
}
}
void InitBigNumber(BigNumber * n){
int i;
n-sign = 1;
n-nLen = 0;
for(i=0; iNumLen; i++)
n-value[i] = 0;
}//end function InitBigNumber
void InputNumber(BigNumber * n){
char num_t[NumLen * 4 + 5];
int i=0,flag = 1;
int j, k = 0,len = 0;
unsigned short tem;
scanf("%s",num_t);
while((num_t[i]!=0)flag){ //查询有效数字的长度
if ((i == 0)(num_t[i] == '-')){
n-sign = -1;
i++;
continue;
}//end if
if ((i == 0)(num_t[i] == '+')){
n-sign = 1;
i++;
continue;
}//end if
switch(num_t[i]){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
len++;
break;
default:
if (i == 0){
printf("输入有误!\n");
exit(-1);
}
flag = 0;
break;
}//end switch
i++;
}//end while
if ((num_t[0] == '-')||(num_t[0] == '+')){ //调整数据
for(i=0; ilen; i++)
num_t[i] = num_t[i+1];
}//end if
for(i=len-1; i=0; i--){ //录入数据
tem = num_t[len - i - 1] - '0';
k = i / 4;
j = i % 4;
n-value[k] += tem(j*4);
}//end for
n-nLen = len;
}//end function InputNumber
void OutputBigNumber(BigNumber * n){
int i,k,j;
unsigned short tem;
int flag = 0;
if (n-sign == -1) printf("-");
for(i=n-nLen-1; i=0; i--){
k = i / 4;
j = i % 4;
tem = (n-value[k](j*4)) % 0x10;
if (tem !=0) flag = 1;
while(flag){
printf("%c", tem + '0');
break;
}
}//end for
if (flag == 0 ) printf("0");
}//end function OutputBigNumber
void BigNumberPlus(BigNumber * n1, BigNumber * n2, BigNumber * result){
int i,j;
unsigned short FlagCarry=0; //进位标志
unsigned short temByteA, temByteB;
unsigned short temBitA, temBitB;
unsigned temSum;
int length;
InitBigNumber(result);
if (n1-sign == n2-sign){
length = (n1-nLen) (n2-nLen) ? n1-nLen : n2-nLen;
for(i=0; i length; i++){ //处理
temByteA = n1-value[i]; temByteB = n2-value[i];
for(j=0; j4; j++){
temBitA = temByteA % 0x10;
temBitB = temByteB % 0x10;
temByteA /= 0x10;
temByteB /= 0x10;
temSum = FlagCarry + temBitA + temBitB;
FlagCarry = temSum / 10;
result-value[i] += (temSum % 10)(j*4);
}//end for j
}//end for i
result-value[length] = FlagCarry;
result-nLen = length + 1;
result-sign = n1-sign;
}//end if
else{
if (n1-sign == -1 ){
n1-sign = 1;
BigNumberSubtract(n2,n1,result);
n1-sign = -1;
}
else{
n2-sign = 1;
BigNumberSubtract(n1,n2,result);
n2-sign = -1;
}//end inner if-else
}//end outer if-else
}//end function BigNumberPlus
void BigNumberSubtract(BigNumber * n1, BigNumber * n2, BigNumber * result){
int i,j;
unsigned short FlagBorrow = 0; //借位标志
unsigned short temByteA, temByteB;
unsigned short temBitA, temBitB;
unsigned short temDifference;
int length;
InitBigNumber(result);
if (n1-sign != n2-sign ){
n2-sign *= -1;
BigNumberPlus(n1,n2,result);
n2-sign *= -1;
}
else{
if (n2-sign == -1){
n2-sign = 1;
BigNumberPlus(n1,n2,result);
n2-sign = -1;
}
else{
if (n2-sign == -1){
n2-sign = 1;
BigNumberPlus(n1,n2,result);
n2-sign = -1;
}
else{
length = (n1-nLen) (n2-nLen) ? n1-nLen : n2-nLen;
for(i=0; i length; i++){ //处理
temByteA = n1-value[i]; temByteB = n2-value[i];
for(j=0; j4; j++){
temBitA = temByteA % 0x10;
temBitB = temByteB % 0x10;
temByteA /= 0x10;
temByteB /= 0x10;
if (temBitA - temBitB - FlagBorrow 0){
temDifference = temBitA - temBitB - FlagBorrow +10;
FlagBorrow = 1;
}
else{
temDifference = temBitA - temBitB - FlagBorrow;
FlagBorrow = 0;
}//end if-else
result-value[i] += (temDifference % 10)(j*4);
}//end for j
}//end for i
result-nLen = length;
if (temByteA FlagBorrow){
InitBigNumber(result);
result-sign = -1;
FlagBorrow = 0;
for(i=0; i length; i++){ //重新处理
temByteA = n2-value[i]; temByteB = n1-value[i];
for(j=0; j4; j++){
temBitA = temByteA % 0x10;
temBitB = temByteB % 0x10;
temByteA /= 0x10;
temByteB /= 0x10;
if (temBitA - temBitB - FlagBorrow 0){
temDifference = temBitA - temBitB - FlagBorrow +10;
FlagBorrow = 1;
}
else{
temDifference = temBitA - temBitB - FlagBorrow;
FlagBorrow = 0;
}//end if-else
result-value[i] += (temDifference % 10)(j*4);
}//end for j
}//end for i
result-nLen = length;
}//end if
}//end inner if-else
}//end middle if-else
}//end outer if-else
}//end function BigNumberSubtract
c语言大数的加法用数组char指针实现
做大数加法,首先要能保存大整数。C能提供的最大的整数类型也就是long long int了吧,还是有上限。用整数类型这条路不通。所以想到把大整数看作字符串(即char数组),一位数字就是数组的一个元素。数组能有多长?几万位不止,应付大数加法很轻松。
基本做法就是把两个加数各自存为字符串。(怎么把数字转换成字符?每个数字加'0'就行了。比如 '7'就是7+'0'。)然后从个位起逐位加。(加的时候把字符变回数字,'7'-'0'就是7。)算出来的结果再转成字符存到第三个数组的相应位里,遇到进位就把上一位加个1,简单的很。最后第三个字符串就是结果,用puts打印出来就行了。做的时候为了方便可能会把个位存在数组第一位,那样的话就倒序输出字符串就行了。
代码自己写。
怎样用C语言做超大整数的加减运算?
用高精度算法来实现,即用数组或指针来储存数字,例如A〔20〕来储存a ,用B〔20〕来储存b,这样a 和b就可以是很大的数,再用一个C〔21〕来储存结果,为什么C要21呢,你知道,加法是要近位的,呵呵。这里给出相加的伪代码,d =0/*用来存储近位*/,for i=0到19{c=A〔i〕+B〔i〕+d ,d =c/10,c=c%10,C〔i〕=c}if d 不等于0 C〔i+1〕=d ,再逆的输出C就可以了!编程要学会思考,现在你可以试试编下高精度乘法,例如可以输出100的阶乘!
网页名称:c语言大数加法指针函数,大数加减法c语言
文章链接:http://cdiso.cn/article/dsipohe.html