Skip to content

Commit

Permalink
[Phase4] Add test cases 4-15
Browse files Browse the repository at this point in the history
  • Loading branch information
liqwang committed Jan 8, 2024
1 parent 8fdcf86 commit 86f5311
Show file tree
Hide file tree
Showing 36 changed files with 1,308 additions and 0 deletions.
48 changes: 48 additions & 0 deletions phase4/test/test_4_r04.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FUNCTION mod :
PARAM x
PARAM n
$4 := x / n
$3 := $4 * n
$1 := x - $3
RETURN $1

FUNCTION isPalindrome :
PARAM num
reversed := #0
original := num
digit := #0
LABEL label1 :
IF num != #0 GOTO label2
GOTO label3
LABEL label2 :
ARG #10
ARG num
digit := CALL mod
$12 := reversed * #10
reversed := $12 + digit
num := num / #10
GOTO label1
LABEL label3 :
IF reversed == original GOTO label4
GOTO label5
LABEL label4 :
RETURN #1
GOTO label6
LABEL label5 :
RETURN #0
LABEL label6 :

FUNCTION main :
READ number
ARG number
$22 := CALL isPalindrome
IF $22 == #1 GOTO label7
GOTO label8
LABEL label7 :
WRITE #1
GOTO label9
LABEL label8 :
$26 := #0 - #1
WRITE $26
LABEL label9 :
RETURN #0
32 changes: 32 additions & 0 deletions phase4/test/test_4_r04.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
int mod(int x, int n)
{
return x - (x / n) * n;
}

int isPalindrome(int num) {
int reversed = 0;
int original = num;
int digit = 0;
while (num != 0) {
digit = mod(num , 10);
reversed = reversed * 10 + digit;
num = num / 10;
}
if(reversed == original){
return 1;
}else{
return 0;
}
}

int main() {
int number;
number = read();
if (isPalindrome(number)==1) {
write(1);
} else {
write(-1);
}

return 0;
}
39 changes: 39 additions & 0 deletions phase4/test/test_4_r04.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
0
----------
1

114514
----------
-1

3296923
----------
1

3296913
----------
-1

2332
----------
1

1010101
----------
1

101101
----------
1

1001101
----------
-1

100
----------
-1

99
----------
1
37 changes: 37 additions & 0 deletions phase4/test/test_4_r05.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FUNCTION calculateBinomialCoefficient :
PARAM row
PARAM col
coefficient := #1
i := #0
IF row < col GOTO label1
GOTO label2
LABEL label1 :
RETURN #0
LABEL label2 :
$5 := row - col
IF col > $5 GOTO label3
GOTO label4
LABEL label3 :
col := row - col
LABEL label4 :
LABEL label5 :
IF i < col GOTO label6
GOTO label7
LABEL label6 :
$13 := row - i
coefficient := coefficient * $13
$17 := i + #1
coefficient := coefficient / $17
i := i + #1
GOTO label5
LABEL label7 :
RETURN coefficient

FUNCTION main :
READ r
READ c
ARG c
ARG r
coe := CALL calculateBinomialCoefficient
WRITE coe
RETURN #0
30 changes: 30 additions & 0 deletions phase4/test/test_4_r05.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*row=n, col=k*/
int calculateBinomialCoefficient(int row, int col) {
int coefficient = 1;
int i = 0;

if (row < col) {
return 0;
}

if (col > row - col) {
col = row - col;
}

while (i < col) {
coefficient = coefficient * (row - i);
coefficient = coefficient / (i + 1);
i=i+1;
}

return coefficient;
}

int main() {
int r, c, coe;
r = read();
c = read();
coe = calculateBinomialCoefficient(r, c);
write(coe);
return 0;
}
39 changes: 39 additions & 0 deletions phase4/test/test_4_r05.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
1 1
----------
1

4 0
----------
1

4 4
----------
1

4 2
----------
6

5 3
----------
10

6 3
----------
20

6 3
----------
84

10 5
----------
252

10 7
----------
120

13 7
----------
1716
34 changes: 34 additions & 0 deletions phase4/test/test_4_r06.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FUNCTION main :
N := #30
num := #0
i := #1
k := #1
LABEL label1 :
IF k <= N GOTO label2
GOTO label3
LABEL label2 :
LABEL label4 :
IF i <= k GOTO label5
GOTO label6
LABEL label5 :
$7 := k / i
$6 := $7 * i
IF k == $6 GOTO label7
GOTO label8
LABEL label7 :
num := num + #1
LABEL label8 :
i := i + #1
GOTO label4
LABEL label6 :
IF num == #2 GOTO label9
GOTO label10
LABEL label9 :
WRITE k
LABEL label10 :
i := #1
num := #0
k := k + #1
GOTO label1
LABEL label3 :
RETURN #0
18 changes: 18 additions & 0 deletions phase4/test/test_4_r06.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//output: 2 3 5 7 11 13 17 19 23 29
int main(){
int N = 30;
int num = 0, i = 1, k = 1;
while(k <= N){
while(i <= k){
if(k == (k / i * i))
num = num + 1;
i = i + 1;
}
if(num == 2)
write(k);
i = 1;
num = 0;
k = k +1;
}
return 0;
}
12 changes: 12 additions & 0 deletions phase4/test/test_4_r06.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

----------
2
3
5
7
11
13
17
19
23
29
55 changes: 55 additions & 0 deletions phase4/test/test_4_r07.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
FUNCTION mod :
PARAM x
PARAM n
$4 := x / n
$3 := $4 * n
$1 := x - $3
RETURN $1

FUNCTION isPerfectNumber :
PARAM number
sum := #0
j := #1
LABEL label1 :
$9 := number / #2
IF j <= $9 GOTO label2
GOTO label3
LABEL label2 :
ARG j
ARG number
$12 := CALL mod
IF $12 == #0 GOTO label4
GOTO label5
LABEL label4 :
sum := sum + j
LABEL label5 :
j := j + #1
GOTO label1
LABEL label3 :
IF sum == number GOTO label6
GOTO label7
LABEL label6 :
RETURN #1
GOTO label8
LABEL label7 :
RETURN #0
LABEL label8 :

FUNCTION main :
count := #0
i := #1
LABEL label9 :
IF i <= #100 GOTO label10
GOTO label11
LABEL label10 :
ARG i
$26 := CALL isPerfectNumber
IF $26 == #1 GOTO label12
GOTO label13
LABEL label12 :
WRITE i
LABEL label13 :
i := i + #1
GOTO label9
LABEL label11 :
RETURN #0
36 changes: 36 additions & 0 deletions phase4/test/test_4_r07.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//output: 6 28
int mod(int x,int n)
{
return x -(x / n) * n;
}


int isPerfectNumber(int number) {
int sum = 0;
int j = 1;
while( j <= number / 2) {
if ( mod(number , j) == 0) {
sum = sum + j;
}
j = j + 1;
}

if (sum == number) {
return 1;
} else {
return 0;
}
}

int main() {
int count = 0;
int i = 1;
while(i <= 100) {
if (isPerfectNumber(i) == 1 ) {
write(i);
}
i = i + 1;
}

return 0;
}
Loading

0 comments on commit 86f5311

Please sign in to comment.