传递给接受 char 参数的函数时打印 int 的实际值
Print actual value of int when passed to a function that accepts a char parameter
我有一个函数 PutChar(int, int, char)
可以在给定的行和列中打印出给定的字符。如果我向它传递一个字符,这个函数就可以正常工作。我还希望能够将 0-9 范围内的 int 传递给函数并让它打印实际的 int 值。例如,我希望以下代码打印 int x 的实际值:
int x = 5;
PutChar(1, 1, x);
void PutChar(int row, int col, char ch) {
char str[20];
sprintf(str, "\e[%d;%dH", row, col); // move cursor to row-col
write(1, str, strlen(str));
write(1, &ch, 1);
}
我尝试在将 int 传递给函数时转换为 char,但它打印出奇怪的字符。
编辑: 可以打印的整数范围是 0-9。这是家庭作业,所以我不能更改函数的原型。
您需要使用 sprintf()
来实现此目的。在那里,您可以使用
%c
打印 char
值
%d
打印 int
值。
但是,您需要更改逻辑以包含一个 标志 来指示任何一种情况。
对我来说,让 PutChar()
函数打印字符以外的东西看起来不是个好主意。
如果您需要打印一个 int,请编写一个函数 PutInt()
来执行此操作。您可能需要另一个函数 PutString()
来实现它。
完成后,您可能重写了标准库的大部分...
由于整数的范围是 0-9,我可以在传递的参数中添加“0”。例如:
int x = 5;
PutChar(1, 1, x + '0');
如果可行,您可以在 PutChar 实现中使用 VA_ARGS:
PutChar(int row, int col, char* formatString, ...) {
sprintf(str, "\e[%d;%dH", row, col); // move cursor to row-col
va_list args;
va_start (args, format);
vprintf (format, args); // untested example
va_end (args);
}
如果您应该使用 PutChar()
函数输出单个数字,通过添加 '0'
可以很容易地将它们转换为具有适当值的 char
,如山建议:
PutChar(1, 1, x + '0');
如果您要修改 PutChar()
函数以在传递小整数时输出数字,也可以这样做。
假设您的终端支持 ASCII 或一些基于 ASCII 的编码,例如 Latin1 或 UTF-8,32 (space) 以下的字符是控制字符。 将它们放在屏幕上的给定位置没有多大意义。因此,您可以修改 PutChar()
以专门处理 0
和 9
之间的 char
值,而不会与实际 ASCII 字符 32 及以上的当前行为冲突(如果 char
已签名):
void PutChar(int row, int col, char ch) {
char str[20];
sprintf(str, "\e[%d;%dH", row, col); // move cursor to row-col
write(1, str, strlen(str));
if (ch >= 0 && ch <= 9)
ch += '0'; // convert small integer to digit
write(1, &ch, 1);
}
void Test(void) {
for (int x = 0; x < 10; x++) {
PutChar(x + 1, x + 1, x);
}
}
另请注意,您的程序是不安全的:sprintf
会导致 row
和 col
的大值发生缓冲区溢出。您应该使用 snprintf()
和更大的缓冲区。您可以进一步简化代码并调用 write
一次:
void PutChar(int row, int col, char ch) {
char str[46];
int len;
if (ch >= 0 && ch <= 9)
ch += '0'; // convert small integer to digit
len = snprintf(str, sizeof str, "\e[%d;%dH%c", row, col, ch);
if (len > 0)
write(1, str, len);
}
在调用PutChar()
之前,如果要输出0..9范围内的int
,则在字符上加上0x30。如果要输出任何其他 char
则不要添加 0x30
我有一个函数 PutChar(int, int, char)
可以在给定的行和列中打印出给定的字符。如果我向它传递一个字符,这个函数就可以正常工作。我还希望能够将 0-9 范围内的 int 传递给函数并让它打印实际的 int 值。例如,我希望以下代码打印 int x 的实际值:
int x = 5;
PutChar(1, 1, x);
void PutChar(int row, int col, char ch) {
char str[20];
sprintf(str, "\e[%d;%dH", row, col); // move cursor to row-col
write(1, str, strlen(str));
write(1, &ch, 1);
}
我尝试在将 int 传递给函数时转换为 char,但它打印出奇怪的字符。
编辑: 可以打印的整数范围是 0-9。这是家庭作业,所以我不能更改函数的原型。
您需要使用 sprintf()
来实现此目的。在那里,您可以使用
%c
打印char
值%d
打印int
值。
但是,您需要更改逻辑以包含一个 标志 来指示任何一种情况。
对我来说,让 PutChar()
函数打印字符以外的东西看起来不是个好主意。
如果您需要打印一个 int,请编写一个函数 PutInt()
来执行此操作。您可能需要另一个函数 PutString()
来实现它。
完成后,您可能重写了标准库的大部分...
由于整数的范围是 0-9,我可以在传递的参数中添加“0”。例如:
int x = 5;
PutChar(1, 1, x + '0');
如果可行,您可以在 PutChar 实现中使用 VA_ARGS:
PutChar(int row, int col, char* formatString, ...) {
sprintf(str, "\e[%d;%dH", row, col); // move cursor to row-col
va_list args;
va_start (args, format);
vprintf (format, args); // untested example
va_end (args);
}
如果您应该使用 PutChar()
函数输出单个数字,通过添加 '0'
可以很容易地将它们转换为具有适当值的 char
,如山建议:
PutChar(1, 1, x + '0');
如果您要修改 PutChar()
函数以在传递小整数时输出数字,也可以这样做。
假设您的终端支持 ASCII 或一些基于 ASCII 的编码,例如 Latin1 或 UTF-8,32 (space) 以下的字符是控制字符。 将它们放在屏幕上的给定位置没有多大意义。因此,您可以修改 PutChar()
以专门处理 0
和 9
之间的 char
值,而不会与实际 ASCII 字符 32 及以上的当前行为冲突(如果 char
已签名):
void PutChar(int row, int col, char ch) {
char str[20];
sprintf(str, "\e[%d;%dH", row, col); // move cursor to row-col
write(1, str, strlen(str));
if (ch >= 0 && ch <= 9)
ch += '0'; // convert small integer to digit
write(1, &ch, 1);
}
void Test(void) {
for (int x = 0; x < 10; x++) {
PutChar(x + 1, x + 1, x);
}
}
另请注意,您的程序是不安全的:sprintf
会导致 row
和 col
的大值发生缓冲区溢出。您应该使用 snprintf()
和更大的缓冲区。您可以进一步简化代码并调用 write
一次:
void PutChar(int row, int col, char ch) {
char str[46];
int len;
if (ch >= 0 && ch <= 9)
ch += '0'; // convert small integer to digit
len = snprintf(str, sizeof str, "\e[%d;%dH%c", row, col, ch);
if (len > 0)
write(1, str, len);
}
在调用PutChar()
之前,如果要输出0..9范围内的int
,则在字符上加上0x30。如果要输出任何其他 char
则不要添加 0x30