-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrade.sh
More file actions
executable file
·119 lines (107 loc) · 2.38 KB
/
grade.sh
File metadata and controls
executable file
·119 lines (107 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
# grading all codes in the same directory
# usage : ./grade.sh
function is_exist_txt(){
if test -e ./"$inputfile"; then
if test -e ./"$answerfile"; then
return 0
else
echo "Error : Not exist answer.txt in this directory"
fi
else
echo "Error : Not exist input.txt in this directory"
fi
return 1
}
function is_correct_extension(){
for i in "${extensionarr[@]}"
do
if [ "$i" = "$1" ]; then
return 0
fi
done
return 1
}
function compare(){
DIFF="$(diff <(sed -e '$a\' $outputfile) <(sed -e '$a\' ./$answerfile))"
if [ "$DIFF" != "" ]
then
echo 1
else
echo 0
fi
}
function get_result(){
com=0
case "$extension" in
c )
gcc -o "$compiledname".out "$file" >& /dev/null || com=3
if [ "$com" -ne 3 ]
then
./"$compiledname".out < ./"$inputfile" > "$outputfile"
fi
;;
cpp )
g++ -o "$compiledname".out "$file" >& /dev/null || com=3
if [ "$com" -ne 3 ]
then
./"$compiledname".out < ./"$inputfile" > "$outputfile"
fi
;;
java )
javac "$file" >& /dev/null || com=3
if [ "$com" -ne 3 ]
then
java "$file_name" < ./"$inputfile" > "$outputfile"
mv "$file_name".class "$compiledname".class
fi
;;
py )
python "$file" < ./"$inputfile" >& /dev/null || com=3
if [ "$com" -ne 3 ]
then
python "$file" < ./"$inputfile" > "$outputfile"
fi
;;
* )
echo 2 ;;
esac
if [ "$com" -ne 3 ]
then
test -e "$compiledname".* && rm "$compiledname".*
com=`compare`
test -e "$outputfile" && rm "$outputfile"
fi
echo "$com"
}
declare -r inputfile="input.txt"
declare -r answerfile="answer.txt"
declare -a extensionarr=("c" "cpp" "java" "py")
if [ "$(uname)" == "Darwin" ]; then
declare -a resultarr=(
"\x1B[92mCorrect\x1B[39m"
"\x1B[91mWrong\x1B[39m"
"Can not grade this file extension"
"\x1B[94mCompile Error\x1B[39m" )
else
declare -a resultarr=(
"\e[92mCorrect\e[39m"
"\e[91mWrong\e[39m"
"Can not grade this file extension"
"\e[94mCompile Error\e[39m" )
fi
if ! is_exist_txt; then
exit 0
fi
for file in *
do
IFS='.' read file_name extension <<< "$file"
if ! is_correct_extension "$extension"; then
continue
fi
timestamp=`date "+%Y%m%d_%H%M%S"`
compiledname=compiled_"$timestamp"
outputfile=output_"$timestamp".tmp
res=`get_result`
echo -e "$file\t${resultarr[$res]}"
done