mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-14 22:38:46 +00:00
Update homework 1 and 2
This commit is contained in:
1
examples/.gitignore
vendored
Normal file
1
examples/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.exe
|
||||
3205
examples/hw1/complete_cond.c
Normal file
3205
examples/hw1/complete_cond.c
Normal file
File diff suppressed because it is too large
Load Diff
13
examples/hw1/cond_and_loop.c
Normal file
13
examples/hw1/cond_and_loop.c
Normal file
@@ -0,0 +1,13 @@
|
||||
int main() {
|
||||
int i;
|
||||
int p = 2;
|
||||
int q = 5;
|
||||
int r = (0 ? ((p > q) ? (p -= 2) : (p += 2)) : (p + q));
|
||||
|
||||
for (i = 0; i < 11; ((i % 2) ? (i += 2) : ++i)) {
|
||||
if (i % 2) { p += q; }
|
||||
else { p += r; }
|
||||
}
|
||||
|
||||
return p == 34;
|
||||
}
|
||||
15
examples/hw1/gcd.c
Normal file
15
examples/hw1/gcd.c
Normal file
@@ -0,0 +1,15 @@
|
||||
int gcd(int a, int b) {
|
||||
a = (a > 0) ? a : -a;
|
||||
b = (b > 0) ? b : -b;
|
||||
|
||||
while(a != b) {
|
||||
if(a > b) { a -= b; }
|
||||
else { b -= a; }
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return gcd(18, 21) == 3;
|
||||
}
|
||||
53
examples/meta/make_cond.py
Normal file
53
examples/meta/make_cond.py
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""Make c program which uses complicated conditional expression
|
||||
|
||||
To make c program, execute `python3 make_cond.py > file_name.c`
|
||||
"""
|
||||
|
||||
import random
|
||||
|
||||
def eval_cond(arr_cond):
|
||||
"""Evaluate conditional expression
|
||||
"""
|
||||
if len(arr_cond) == 1:
|
||||
return arr_cond[0]
|
||||
new_arr_cond = []
|
||||
for cond_start in range(len(arr_cond) // 3):
|
||||
cond_val = arr_cond[3*cond_start + 1] if arr_cond[3*cond_start] else arr_cond[3*cond_start + 2]
|
||||
new_arr_cond.append(cond_val)
|
||||
return eval_cond(new_arr_cond)
|
||||
|
||||
def make_func(i):
|
||||
"""Make a function that contains a conditional expression
|
||||
"""
|
||||
func_signature = "int " + "func_" + str(i) + "()"
|
||||
variables = "abcdefghijklmnopqrstuvwxyzA"
|
||||
func_inner = []
|
||||
val_bitmap = []
|
||||
|
||||
# Variable initializiation
|
||||
for var in variables:
|
||||
val = random.randint(0, 1)
|
||||
val_bitmap.append(val)
|
||||
decl = "\tint " + var + " = " + str(val) + ";"
|
||||
func_inner.append(decl)
|
||||
|
||||
expr_val = eval_cond(val_bitmap)
|
||||
func_inner.append("\treturn (((a ? b : c) ? (d ? e : f) : (g ? h : i)) ? ((j ? k : l) ? (m ? n : o) : (p ? q : r)) : ((s ? t : u) ? (v ? w : x) : (y ? z : A))) == " + str(expr_val) + ";")
|
||||
|
||||
return "\n".join([func_signature, "{"] + func_inner + ["}"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
src = ""
|
||||
return_stmt = "\treturn ("
|
||||
NUM_FUNC = 100
|
||||
for i in range(NUM_FUNC):
|
||||
src += make_func(i)
|
||||
src += "\n\n"
|
||||
return_stmt += "func_" + str(i) + "()"
|
||||
return_stmt += " && " if i != (NUM_FUNC - 1) else ") == "
|
||||
return_stmt += "1;"
|
||||
src += "int main()\n{\n" + return_stmt + "\n}\n"
|
||||
|
||||
print(src)
|
||||
10
examples/simple_cond.c
Normal file
10
examples/simple_cond.c
Normal file
@@ -0,0 +1,10 @@
|
||||
int f(int x) {
|
||||
return x + 8;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int x = 0;
|
||||
int y = (x++ == 1) ? 1 : 2;
|
||||
|
||||
return f((x < y) ? x : 2) == 9;
|
||||
}
|
||||
Reference in New Issue
Block a user