SOURCE

console 命令行工具 X clear

                    
>
console
#!/usr/bin/env bash
# 一键编译并运行本教程全部 Fortran 示例
# 用法:bash run_all.sh
# 依赖:gfortran(GCC 自带,安装方法见教程第 03 章)
set -u
cd "$(dirname "$0")"

FC="${FC:-gfortran}"
if ! command -v "$FC" >/dev/null 2>&1; then
  echo "错误:没有找到 $FC,请先安装 gfortran(见教程第 03 章)。"
  exit 1
fi

mkdir -p build
pass=0
fail=0

show() { printf '\n===== %s =====\n' "$1"; }

# 单文件示例:one 源文件 [标准输入]
one() {
  local f=$1 in=${2-}
  local exe="build/${f%.f90}"
  show "$f"
  if ! "$FC" -Wall "$f" -o "$exe"; then
    echo "!! 编译失败:$f"
    fail=$((fail + 1))
    return
  fi
  if [ -n "$in" ]; then
    printf '%s\n' "$in" | "./$exe"
  else
    "./$exe" </dev/null
  fi
  pass=$((pass + 1))
}

# 多文件示例:multi 可执行文件名 源文件...
multi() {
  local name=$1
  shift
  show "$name($*)"
  if ! "$FC" -Wall "$@" -o "build/$name"; then
    echo "!! 编译失败:$name"
    fail=$((fail + 1))
    return
  fi
  "./build/$name"
  pass=$((pass + 1))
}

echo "编译器:$("$FC" --version | head -1)"
echo "工作目录:$(pwd)"

# 第 03–04 章:第一个程序与程序结构
one hello.f90
one layout.f90

# 第 05 章:变量与数据类型
one vars.f90
one kind_demo.f90

# 第 06 章:输入输出与格式化
one simple_io.f90 "5 2.5"
one io.f90 "5 2.5"
one write_unit.f90

# 第 07 章:表达式与运算
one calc.f90
one compare.f90

# 第 08 章:条件分支
one branch.f90

# 第 09 章:循环
one loop.f90
one nested.f90

# 第 10 章:数组
one array.f90
one mask_demo.f90

# 第 11 章:函数与子程序
one func.f90
one result_style.f90

# 第 12 章:模块
multi circle geometry.f90 use_geometry.f90

# 第 13 章:派生类型
one derived.f90
one type_array.f90

# 第 14 章:文件读写
one file_io.f90
one read_csv.f90

# 第 15 章:综合实战(读取同目录下的 students.txt)
multi grade grade_mod.f90 grade_report.f90

# 第 17 章:练习题
one ex1.f90 "37"
one ex2.f90 "100"
one ex3.f90 "97"
one ex4.f90
one ex5.f90 "2"
one ex6.f90
one ex7.f90
one ex8.f90

printf '\n===== 结果:%d 个程序运行成功,%d 个失败 =====\n' "$pass" "$fail"
[ "$fail" -eq 0 ]