我正在嘗試撰寫一個腳本,該腳本從文本檔案中獲取輸入,在 C 程式中運行輸入并確定答案是否正確。文本檔案包括輸入值和用逗號分隔的正確值。問題是它不適用于-20。我認為這與 - 符號有關,因為如果我將文本檔案更改為前面有一個空格或洗掉 - 符號,它就可以作業。我將 C 程式、文本檔案和腳本放在下面。我還包括了一個示例輸出。突出顯示的行不應該在那里。提前致謝!
(nb。我不允許將文本檔案更改為有效的解決方案)
輸出

C 程式
#include <stdio.h>
int increment(int a);
void main(void){
int a;
scanf("%i", &a);
a = increment(a);
printf("%i\n", a);
return;
}
int increment(int a){
int b = a;
b = b 1;
return b;
}
測驗輸入檔案
1,2
-20,-19
37,38
1.4,2.4
0,1
0.0,1.0
'20','21'
lab04,lab05
Bash 腳本
#!/bin/bash
:
echo""
echo "start building main program"
echo "start compiling to assembly lines..."
cc main.c -S
cc increment.c -S
echo "translating to op codes"
cc main.s -c
cc increment.s -c
echo "statically linking all required op codes..."
cc main.o increment.c -o main
echo "build successfully done!"
echo "running main program"
echo ""
passedCount=0
failedCount=0
while read input
do
IFS=,
set $input
correctAnswer=($2)
#runs main with the input of $1 and stores the result in output
output=$(./main <<< $1)
if [ $output == $correctAnswer ]; then
echo "input: $1, main: $output, correct: $correctAnswer ==> passed"
passedCount=$((passedCount 1))
fi
if [ $output != $correctAnswer ]; then
echo "input: $1, main: $output, correct: $correctAnswer ==> failed"
failedCount=$((failedCount 1))
fi
done <"./test_inputs.txt"
echo ""
echo "Total Passed: $passedCount"
echo "Total Failed: $failedCount"
uj5u.com熱心網友回復:
set $input以破折號開頭時會引發錯誤$input,因為 set 認為它是一個標志。改用set -- $input:
-- Assign any remaining arguments to the positional parameters. If there are no remaining arguments, the positional parameters are unset.
—help set
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515327.html
標籤:重击壳
