-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_program.plint
More file actions
161 lines (134 loc) · 3.83 KB
/
test_program.plint
File metadata and controls
161 lines (134 loc) · 3.83 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
fun fibonacci(n: const int): int {
return match(n){
0: 0,
1: 1,
_: fibonacci(n - 1) + fibonacci(n - 2)
};
}
fun fizzbuzz(n: const int): void {
temp: int = 1;
while(temp <= n){
match(temp % 3, temp % 5){
0, 0: print("fizzbuzz\n"),
0, _: print("fizz\n"),
_, 0: print("buzz\n"),
_, _: print(to_str_int(temp) | "\n")
}
temp = temp + 1;
}
}
fun divisible_by_3(n: const int): bool {
return n % 3 == 0;
}
fun divisible_by_5(n: const int): bool {
return n % 5 == 0;
}
#demonstrates how bool variables, function calls and function identifiers
#can be used in match expressions
fun fizzbuzz2(n: const int): void {
temp: int = 1;
while(temp <= n){
divisible_by_5: const bool = temp % 5 == 0;
match(temp, temp){
divisible_by_3(temp), divisible_by_5: print("fizzbuzz\n"),
divisible_by_3, _ : print("fizz\n"),
_, divisible_by_5: print("buzz\n"),
_, _ : print(to_str_int(temp) | "\n")
}
temp = temp + 1;
}
}
fun progressive_tax(salary: const float): float { # not accurate
tax_rate: float = match(salary){
< 1000.0: 0.1,
< 2500.0: 0.3,
_: 0.5
};
return salary * tax_rate;
}
fun is_even(n: const int): bool {
return match(n % 2){
0: true,
1: false
};
}
fun is_even2(n: const int): bool {
return match(n){
is_even: true,
_: false
};
}
fun while_return_test(): void{
number: int = 4;
while(number > 0){
if(number == 1){
return;
}
print(to_str_int(number) | "\n");
number = number - 1;
}
}
fun print_file(filename: const str): void {
handle: file = open_file(filename);
if(bad_file(handle)){
return;
}
else{
line: str = read_line(handle);
while(line != EOF_MARKER){
print(line | "\n");
line = read_line(handle);
}
close_file(handle);
}
}
fun add_one(number: int): void {
number = number + 1;
}
fun multiple_matches(): int{
return match(1, 2, 3){
is_even2 and is_even, is_even2, is_even2: 0,
_, is_even2, _ : 1,
_, _, _ : 2
};
}
fun main(): int{
if(arguments_number() >= 1){
print("Opening and printing file " | argument(0) | "\n");
print_file(argument(0));
print("Done!\n");
} else {
print("No filename provided as argument, skipping...\n");
}
# bad_variable: int = 14.33;
# print(to_str_int(bad_variable));
number: int = 20;
print("Fibonacci[" | to_str_int(number) | "] number is " | to_str_int(fibonacci(number)) | ".\n");
print("Checking if passing by reference works - printed below should be five (4 after incrementing)\n");
do_dodania: int = 4;
add_one(do_dodania);
print(to_str_int(do_dodania) | "\n");
#add_one(do_dodania + 1); #generuje error - dodanie do stalej (r-wartosci)
#print(to_str_int(do_dodania) | "\n");
print("2 + 3 * 2 = " | to_str_int(2+3*2) | " == 8?\n");
if(number < 12 and is_even(number) or not is_even(number)){
print("Logical precedence works!\n");
}
print("Tax for 1500zl = " | to_str_float(progressive_tax(1500.0)) | "\n");
print("If multiple pattern elements and many levels of match calls work,\nthis should be a 1: " | to_str_int(multiple_matches()) | "\n");
while_return_test();
if(not is_even2(3)){
print("ok\n");
}
fizzbuzz2(15);
return 0;
}
fun inna(x: int): int{
# inna2();
}
fun inna2(): int{
return 5;
}
fun p(x: const int) : void{
print(to_str_int(x) | "\n");
}