如何在Linux下批量改C语言的作业?

最近接到导师给的一个任务帮本科生改C语言作业。大概的界面是这样的需要测试每种输入:

LuYang:CISB110-Programming Practice 1-41826 luyang$ ./main
Choose the type of speed conversion:
1. knots to km/h
2. km/h to knots
Enter your choice (1 or 2): 0
Invalid option
LuYang:CISB110-Programming Practice 1-41826 luyang$ ./main
Choose the type of speed conversion:
1. knots to km/h
2. km/h to knots
Enter your choice (1 or 2): 1
Enter the speed in knots: 2
2 knots = 3.7 km/h
LuYang:CISB110-Programming Practice 1-41826 luyang$ ./main
Choose the type of speed conversion:
1. knots to km/h
2. km/h to knots
Enter your choice (1 or 2): 2
Enter the speed in km/h: 4
4 km/h = 2.2 knots

有没有办法在linux下写一个脚本。然后将所有的测试结果都写入一个文件中?

该回答引用ChatGPT-3.5,仅为您供参考,不保证完全正确
在Linux下,你可以使用Shell脚本来批量测试C语言作业并将测试结果写入一个文件中。以下是一个示例Shell脚本,它可以帮助你执行这个任务:

#!/bin/bash

# 设置输出文件名
output_file="test_results.txt"

# 删除已存在的输出文件,以防覆盖
rm -f "$output_file"

# 编译C程序
gcc -o main main.c

# 测试输入数据和记录结果
for choice in 1 2; do
  for speed in 2 4; do
    echo "Choose the type of speed conversion:" >> "$output_file"
    echo "1. knots to km/h" >> "$output_file"
    echo "2. km/h to knots" >> "$output_file"
    echo "Enter your choice (1 or 2): $choice" >> "$output_file"
    echo "Enter the speed in knots: $speed" >> "$output_file"
    
    # 运行C程序并将结果追加到输出文件
    ./main <<EOF >> "$output_file"
$choice
$speed
EOF

    echo "----------------------------------" >> "$output_file"
  done
done

# 提示完成
echo "测试完成,结果已保存到 $output_file"

请将上述脚本保存为一个文件(例如test_script.sh),并确保C程序(main.c)在同一目录下。然后,使用以下命令在终端中运行脚本:

chmod +x test_script.sh  # 添加执行权限
./test_script.sh        # 运行脚本

脚本将执行C程序的测试,并将测试结果写入名为 test_results.txt 的文件中。你可以根据需要自定义输入数据和文件名。


该回答引用NewBing,仅为您供参考,不保证完全正确
你好,这是Bing。我可以帮你写一个Linux下的shell脚本,用来批量测试C语言的作业。这个脚本的大致思路是:

  • 遍历一个目录下的所有C文件,对每个文件进行编译和运行。
  • 为每个文件创建一个输出文件,用来存储测试结果。
  • 使用echo命令和管道符号(|)来向程序输入不同的选项和速度值。
  • 使用diff命令或者其他工具来比较程序的输出和预期的输出,判断是否正确。
  • 将测试结果汇总到一个总的输出文件中,包括文件名,测试用例,正确与否等信息。

下面是一个可能的脚本示例,你可以根据你的具体需求进行修改:

#!/bin/bash

# 定义一个目录变量,存储所有C文件的路径
dir="/path/to/C/files"

# 定义一个输出文件变量,存储总的测试结果
output_file="test_result.txt"

# 清空输出文件
echo "" > $output_file

# 遍历目录下的所有C文件
for file in $dir/*.c; do
  # 获取文件名(不含后缀)
  filename=$(basename $file .c)
  # 编译C文件,生成可执行文件
  gcc -o $filename $file
  # 创建一个临时输出文件,存储单个C文件的测试结果
  tmp_file="$filename.tmp"
  echo "" > $tmp_file
  # 测试第一种选项(knots to km/h)
  echo "Testing option 1 for $filename" >> $tmp_file
  # 输入选项1和速度2,并将程序的输出追加到临时输出文件中
  echo -e "1\n2" | ./$filename >> $tmp_file
  # 比较程序的输出和预期的输出,如果相同则输出OK,否则输出FAIL
  if diff -q <(tail -n 1 $tmp_file) <(echo "2 knots = 3.7 km/h") &>/dev/null; then
    echo "OK" >> $tmp_file
  else
    echo "FAIL" >> $tmp_file
  fi
  # 测试第二种选项(km/h to knots)
  echo "Testing option 2 for $filename" >> $tmp_file
  # 输入选项2和速度4,并将程序的输出追加到临时输出文件中
  echo -e "2\n4" | ./$filename >> $tmp_file
  # 比较程序的输出和预期的输出,如果相同则输出OK,否则输出FAIL
  if diff -q <(tail -n 1 $tmp_file) <(echo "4 km/h = 2.2 knots") &>/dev/null; then
    echo "OK" >> $tmp_file
  else
    echo "FAIL" >> $tmp_file
  fi
  # 将临时输出文件的内容追加到总的输出文件中,并添加一条分隔线
  cat $tmp_file >> $output_file
  echo "------------------------" >> $output_file
  # 删除临时输出文件和可执行文件
  rm $tmp_file $filename
done

# 打印总的输出文件的内容
cat $output_file

源: 与必应的对话, 2023/9/5
(1) Bash script to automatically test program output - C. https://stackoverflow.com/questions/10118381/bash-script-to-automatically-test-program-output-c.
(2) testing - Writing Bash script to test C Program - Stack Overflow. https://stackoverflow.com/questions/32306321/writing-bash-script-to-test-c-program.
(3) How to execute a shell script from C in Linux? - Stack Overflow. https://stackoverflow.com/questions/3736210/how-to-execute-a-shell-script-from-c-in-linux.
(4) Shell Scripting - Test Command - GeeksforGeeks. https://www.geeksforgeeks.org/shell-scripting-test-command/.