如何通过 Windows 批处理文件将值传递给 scanf()

How to pass values to scanf() via Windows batch file

我想快速给我学生的 HW 打分,所以我决定为此编写一个批处理脚本。我知道在 Ubuntu 中更容易做到这一点,但由于学生需要在 Windows 中编写他们的 HW,我也必须在 Windows 中评估它们。嗯,到目前为止,我写了下面的批处理文件,但它没有使用 char 变量。

请注意,HW 的目的是绘制一个矩形并用键盘给定的字符填充它 (scanf)

@echo off
set file=%1
gcc %file% -o %file%.exe
REM Followings are "row col fill" respectively
echo 6 10 A | %file%.exe :: #1
echo 6 6   | %file%.exe :: #2
move %file% graded\
move %file%.exe graded\

在这里,在#1 和#2 处,它绘制了带有空格的矩形,但是在#1 处它应该用 As 填充它。

如果我的学生可以在网上找到这个问题,我不能把整个解决方案放在这里,但我可以展示 scanf() char 部分:

//take num_of_rows and num_of columns
....
printf("\nFilling char?: ");
fflush(stdin);
scanf("%c",&fill);
...
//draw rectangle

最后,这是所需的输出:

TEST-1
Enter number of rows (between 3-20) : 5
Enter number of columns (between 3-80) : 40
Enter a filling character (one char) : #
 +--------------------------------------+
 |######################################|
 |######################################|
 |######################################|
 +--------------------------------------+
TEST-2
Enter number of rows (between 3-20) : 8
Enter number of columns (between 3-80) : 25
Enter a filling character (one char) : (blank)
 +-----------------------+
 |                       |
 |                       |
 |                       |
 |                       |
 |                       |
 |                       |
 +-----------------------+

警告:请不要放完整的 C 代码解决方案,因为它是一个硬件。

因为我这里没有 运行 Windows 并且不知道作业的其余解决方案看起来如何,这可能会或可能不会。可能 fflush(stdin) 只清除当前输入 而不是完整的输入。

您说(在评论中)您的示例解决方案无需批处理即可工作。

那么您可能想尝试将输入放在输入文件的单独行中,该文件将被输入家庭作业程序:

echo.6>   input.txt
echo.10>> input.txt
echo.A>>  input.txt
%file%.exe < input.txt

echo.6>  input.txt
echo.6>> input.txt
echo. >> input.txt
%file%.exe < input.txt

echo 命令必须如字面所示,当然您会改变行、列和填充字符。

一些注意事项:

  • echo后面的点让它认为给出了一个参数。特别是在显示的最后一个命令中,否则它只会输出 "Echo is ON" (或 OFF.)

  • row/column 数字后不应该有 space 如果你不喜欢在数字后输入

  • 在相应的第三条命令中,echo.>> input.txt之间只有一个字符。这是填充字符,'A'和space,分别是

  • 可以省略>>和文件名input.txt之间的space。这只是为了清楚起见。

  • 以防万一您不知道:> 在写入之前清除输出文件; >> 将附加到它。