gpt4 book ai didi

c - 十进制转二进制转换程序 "abort trap: 6"错误

转载 作者:行者123 更新时间:2023-11-30 19:13:44 26 4
gpt4 key购买 nike

我用 C 语言编写了一个小程序来帮助自己学习从十进制到二进制的转换。当我运行它时,它会打印出一个随机数供我转换,并等待我输入转换后的数字。输入十进制数字并按 Enter 键后,它将发送我的“恭喜!”如果我正确的话,会出现消息,但也会出现错误:

Abort trap: 6

在输出中,即使程序从头到尾大部分都运行良好。我知道 Abort trap: 6 与数组有关,但除此之外我不确定。有任何想法吗?哦,下面是我的程序。

#include <stdlib.h>
#include <time.h>
#include <stdio.h>

#define MAX_NUMBER 256 // random number will fall between 0 and 255
#define BI_NUM_SIZE 8 // represents the 8 spaces needed for each number

/* This function does the exponentiation operation
to ints and returns an int */
int exponent (int base, int power)
{
int answer = base, i;
if (power < 0) {
printf ("ERROR: no negative numbers as the power "
"in the exponent function!\n");
return 0;
}
if (power == 0) {
return 1;
} else {
for (i = 1; i < power; i++) {
answer *= base;
}
return answer;
}
}

int main () {

int number_to_convert, i, j, k;
char possible_answer[BI_NUM_SIZE];
char answer[BI_NUM_SIZE];
char answer_check;
srand (time (NULL));

number_to_convert = rand () % MAX_NUMBER;

// this for loop solves the decimal to binary conversion
// and stores the character array answer in 'answer';
// as i decrements for the conversion math j will from 0 to place
// characters in the answer array
j = 0; // allows for indexing through answer[]

// starts as number_to_convert but gets widled down
int temp_number = number_to_convert;

for (i = BI_NUM_SIZE; i > 0; i--) {
// as part of the
if (temp_number >= exponent (2, i - 1)) {
answer[j] = '1';
temp_number -= exponent (2, i - 1);
} else {
answer[j] = '0';
}
j++;
}
printf ("Convert %3d to binary:\n", number_to_convert);
printf ("type your answer and press enter:\n");
scanf ("%s", &possible_answer);

answer_check = 0;
for (i = 0; i < BI_NUM_SIZE; i++) {
if (answer[i] == possible_answer[i]) {
answer_check++;
}
}

if (answer_check == BI_NUM_SIZE) {
printf ("CONGRATS! you got it right!\n");
} else {
printf ("sorry the answer is ");
for (i = 0; i < BI_NUM_SIZE; i++) {
printf ("%c", answer[i]);
}
}
return 0;
}

最佳答案

当使用scanf格式说明符%s读取信息时,scanf将读取字符,直到遇到第一个空白,将结果作为以nul结尾字符串存储在给定的相应指针参数中。如果您打算读取用户在 possible_answer 中输入的 8 个字符,则具有适当宽度限制的 scanf 调用的正确形式为:

scanf ("%8s", possible_answer);

(注意:您不能在 possible_answer 前面包含 '&',因为它已经是一个指针。您还应该检查返回以确保 1 次成功转换。)

您必须为要读取的字符数 +1 提供足够的空间来表示 nul-termination 字符。如果您打算将 8 个字符读入 possible_answer,则必须为 9 个字符提供存储空间。例如:

#define BI_NUM_SIZE 8+1

另请注意,这将需要调整依赖于下面的 BI_NUM_SIZE 的所有循环和测试边界。例如:

for (i = BI_NUM_SIZE - 1; i > 0; i--) {
...
if (answer_check == BI_NUM_SIZE - 1) {

包含每个更改的相关代码是:

    for (i = BI_NUM_SIZE - 1; i > 0; i--) {
// as part of the
if (temp_number >= exponent (2, i - 1)) {
answer[j] = '1';
temp_number -= exponent (2, i - 1);
} else {
answer[j] = '0';
}
j++;
}
printf ("Convert %3d to binary\n", number_to_convert);
printf ("answer and press enter: ");
scanf ("%8s", possible_answer);

answer_check = 0;
for (i = 0; i < BI_NUM_SIZE - 1; i++) {
if (answer[i] == possible_answer[i]) {
answer_check++;
}
}

if (answer_check == BI_NUM_SIZE - 1) {
printf ("\nCONGRATS! you got it right!\n\n");
} else {
printf ("sorry the answer is : ");
for (i = 0; i < BI_NUM_SIZE - 1; i++) {
printf ("%c", answer[i]);
}
putchar ('\n');
}
return 0;

进行这些更改后,您的代码应该可以正常运行。 注意:在编写代码时,您必须在答案中提供所有前导 0。例如:

输出

$ ./bin/bincnv
Convert 117 to binary
answer and press enter: 00000001
sorry the answer is : 01110101

$ ./bin/bincnv
Convert 71 to binary
answer and press enter: 01000111

CONGRATS! you got it right!

关于c - 十进制转二进制转换程序 "abort trap: 6"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com