我正在编写将接收整数数组的代码,然后可以将其转换为所需的基数,例如base-16。
由于某种原因,终端通过该程序并打印出来
“转换后的数字=”
这是我的代码:
#include <stdio.h>
#include <cs50.h>
int convertedNumber[64];
int base;
int digit = 0;
void getNumberAndBase(void) {
int size;
printf("How many numbers to be converted??\n");
size = GetInt();
int array[size];
for (int i = 0; i < size; i++) {
printf("Number to be converted?\n");
array[i] = GetInt();
}
printf("Base?\n");
do {
base = GetInt();
if (base < 2 || base > 16) {
printf("Bad base - must be between 2 and 16. Try again!\n");
}
} while (base < 2 || base > 16);
void convertNumber(int size, int array[size]);
}
void convertNumber(int size, int numberToConvert[size]) {
for (int i = 0; i < size; i++) {
do {
convertedNumber[digit] = numberToConvert[i] % base;
digit++;
numberToConvert[i] /= base;
} while (numberToConvert[i] != 0);
}
}
void displayConvertedNumber(void) {
const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int nextDigit;
printf("Converted number = ");
for (--digit; digit >= 0; --digit) {
nextDigit = convertedNumber[digit];
printf("%c", baseDigits[nextDigit]);
}
printf("\n");
}
int main(void) {
void getNumberAndBase(void), displayConvertedNumber(void);
getNumberAndBase();
displayConvertedNumber();
return 0;
}
答案 0 :(得分:5)
在您的代码中,您从未对convertNumber()
函数进行调用。您应该从
getNumberAndBase()
函数的最后一部分
void convertNumber(int size, int array[size]);
到
convertNumber (size, array);
拨打电话。
也就是说,您应该在convertedNumber
中定义base
,digit
和main()
,然后将它们作为函数的一些参数传递(以便能够制作)在被称为函数内部使用它)。一般而言,它们没有理由成为全球性的。
此外,将函数声明移出main()
;把它们放在档案范围内。
答案 1 :(得分:1)
您的代码中有几个错误。这是你的代码的工作版本,尽可能保持不变(我拒绝清理它的冲动)。我对我所做的更改添加了评论。
#include <stdio.h>
#include <cs50.h>
/* I moved your prototypes up here rather than leaving them inline. */
void displayConvertedNumber(void);
void convertNumber(int size, int *array);
int convertedNumber[64];
int base;
int digit = 0;
void getNumberAndBase (void)
{
/* I moved your variable declarations here. If you want your C to be
portable, define your variables at the beginning of your function.
Don't expect "int i = 0;" to work in your for loop on all C compilers. */
int size;
int array[size];
int i;
printf("How many numbers to be converted??\n");
size = GetInt();
for(i = 0; i < size; i++){
printf("Number to be converted?\n");
array[i] = GetInt();
}
printf("Base?\n");
do{
base = GetInt();
if(base < 2 || base > 16)
{
printf("Bad base - must be between 2 and 16. Try again!\n");
}
} while(base < 2 || base > 16);
/* I corrected your call to this function. */
convertNumber(size, array);
}
void convertNumber (int size, int numberToConvert[size])
{
int i;
for(i = 0; i < size; i++)
{
do{
convertedNumber[digit] = numberToConvert[i] % base;
digit++;
numberToConvert[i] /= base;
}
while(numberToConvert[i] != 0);
/* I added a call to display the number here. The way
you've written your code means each number has to be
displayed after it is converted. You cannot convert
them all first and then attempt to display them since
you're using a single variable and index (convertedNumber
and digit) for the conversion. */
displayConvertedNumber();
}
}
void displayConvertedNumber (void)
{
const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int nextDigit;
printf("Converted number = ");
for(--digit; digit >= 0; --digit)
{
nextDigit = convertedNumber[digit];
printf("%c", baseDigits[nextDigit]);
}
printf("\n");
/* I reset your digit variable here. Otherwise it would have
been left at -1 since that was the exit condition for your
loop above. */
digit = 0;
}
int main (void)
{
getNumberAndBase();
/* I removed the other function call because now everything is
handled in this function. */
return 0;
}