我制作了一个程序,其中要乘以实际上比默认整数大的大整数,并且我们必须用一个数字字符串处理这个数字,我们应该将它变成一个int数组。现在我们必须将一个大数字与一个小于10的数字相乘。我尝试了这种乘法,并且在纸上都可以使用,但是现在当我尝试该程序时,这很奇怪,因为我收到了消息“分段错误(核心已转储)”。 这是什么意思,我该如何解决? 我的主要功能:
int main(int argc, char *argv[])
{
struct BigInt firstNumber;
struct BigInt result;
char userInput[2];
printf("Pyramid of numbers\n\n");
printf("Please enter a number: " );
scanf("%s",userInput );
int len=strlen(userInput);
len=strtobig_int(userInput, len, &firstNumber);
firstNumber.digits_count=len;
print_big_int(&firstNumber);
printf("\n%d\n",len);
multiply(&firstNumber, 5, &result);
print_big_int(&firstNumber);
printf("\n\n");
print_big_int(&result);
return 0;
}
我的乘法函数:
void multiply(const struct BigInt *big_int, int factor, struct BigInt *big_result){
int overflowNumber=0;
for (int i = big_int->digits_count-1 ; i >0; i++) { //for loop which counts from the end of the integer to the beginning, like we'd do it in reallife
int tempResult=big_int->the_int[i]*factor; //tempResult is the temporary Result
if (tempResult>9) { //here we check if the tempResult is bigger than 9, because if it is, we'd have to split the two parts (e.g. 1 and 4 for 14) and put 1 to the next
big_result->the_int[i]=tempResult%10+overflowNumber;
overflowNumber=tempResult/10;
}else{
big_result->the_int[i]=tempResult+overflowNumber;
}
}
}