嘿伙计们,我的程序任务要求我编写一个程序来将大数相加并通过调用函数返回它。我很难理解如何返回要显示的值。我相信我的错误在于我对指针的错误使用,但我对它的理解还不够,无法弄清楚。请帮忙!
我在我认为问题所在的行旁边放了一个箭头,但我不知道如何解决它。谢谢!
这是代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *addlarge(char, char, char); <--
int main(int argc, char* argv[])
{
unsigned char number1 [256] = {'0'};
unsigned char number2 [256] = {'0'};
unsigned char result [256] = {0};
//ask for numbers
printf ("Please enter a number upto 255 digits long:\n");
scanf ("%s", number1);
printf ("Please enter another number upto 255 digits long:\n");
scanf ("%s", number2);
char *result1 = addLarge(*number1, *number2, *result); <--
printf("\nThe sum is %s\n", result1); <--
}
char *addLarge(char *number1, char *number2, char *result) <--
{
int x, z, b, padding1, padding2, padding, y=0, sum, carry=0;
//right aligns the numbers
x = strlen(number1);
padding1 = 255 - x;
memmove(number1+padding1,number1,x);
memset(number1, 0, padding1);
z = strlen(number2);
padding2 = 255 - z;
memmove(number2+padding2,number2,z);
memset(number2, 0, padding2);
//works it out
for(y=254; y>=0; y--) {
if ( (number1[y] != 0) || (number2[y] != 0) ) {
if ( (number1[y] != 0) && (number2[y] != 0) ) {
sum = 0;
sum += carry;
carry = 0;
sum += number1[y] - '0' + number2[y] - '0';
carry = sum / 10;
result[y+1] = sum % 10 + '0';
}
else {
sum = 0;
sum += carry;
carry = 0;
sum += number1[y] - '0' + number2[y];
carry = sum / 10;
result[y+1] = sum % 10 + '0';
}
}
//adds the carries
if ((number1[y] == 0) && (number2[y] == 0) && (carry==1)){
result[y+1] = 1 + '0';
carry = 0;
}
}
//shifts number back
for (b=255; result[b] != 0; b--){
// result[b] = result[b];
}
padding = strlen(result) + 1;
system ("PAUSE");
return result; <--
}