Files
spl/stage1/test08_arrays.spl
2026-07-05 21:45:43 +08:00

42 lines
981 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ===== 模块5复合类型 =====
* test08_arrays — 数组
* 难度2/5
* 验证点:数组字面量 [N]T{...}、数组索引、数组元素修改
*/
fn main() i32 {
/* 数组字面量 */
var arr: [3]i32 = [3]i32{1, 2, 3};
if arr[0] != 1 { ret 1; }
if arr[1] != 2 { ret 2; }
if arr[2] != 3 { ret 3; }
/* 数组元素修改 */
arr[1] = 99;
if arr[1] != 99 { ret 4; }
/* 数组求和 */
var nums: [5]i32 = [5]i32{10, 20, 30, 40, 50};
var sum: i32 = 0;
sum = sum + nums[0];
sum = sum + nums[1];
sum = sum + nums[2];
sum = sum + nums[3];
sum = sum + nums[4];
if sum != 150 { ret 5; }
/* 数组通过循环访问while */
var vals: [4]i32 = [4]i32{2, 4, 6, 8};
var i: i32 = 0;
var s: i32 = 0;
while i < 4 {
s = s + vals[i];
i = i + 1;
}
if s != 20 { ret 6; }
/* 多维效果:数组元素为数组 */
/* 目前只测试一维 */
ret 0;
}