|
| 1 | +using lib <io>; |
| 2 | +using ns std; |
| 3 | + |
| 4 | +// 函数指针数组测试 |
| 5 | + |
| 6 | +fn add(a : int, b : int) : int { |
| 7 | + return a + b; |
| 8 | +}; |
| 9 | + |
| 10 | +fn subtract(a : int, b : int) : int { |
| 11 | + return a - b; |
| 12 | +}; |
| 13 | + |
| 14 | +fn multiply(a : int, b : int) : int { |
| 15 | + return a * b; |
| 16 | +}; |
| 17 | + |
| 18 | +fn divide(a : int, b : int) : int { |
| 19 | + if (b != 0) { |
| 20 | + return a / b; |
| 21 | + } else { |
| 22 | + return 0; |
| 23 | + }; |
| 24 | +}; |
| 25 | + |
| 26 | +fn main() : int { |
| 27 | + std::println("=== 函数指针数组测试 ==="); |
| 28 | + std::println(""); |
| 29 | + |
| 30 | + // 测试1:函数指针数组声明和初始化 |
| 31 | + testFunctionPointerArrayDeclaration(); |
| 32 | + |
| 33 | + // 测试2:函数指针数组访问和调用 |
| 34 | + testFunctionPointerArrayAccess(); |
| 35 | + |
| 36 | + // 测试3:Lambda函数指针数组 |
| 37 | + testLambdaFunctionPointerArray(); |
| 38 | + |
| 39 | + std::println(""); |
| 40 | + std::println("=== 函数指针数组测试完成 ==="); |
| 41 | + return 0; |
| 42 | +}; |
| 43 | + |
| 44 | +fn testFunctionPointerArrayDeclaration() : void { |
| 45 | + std::println("1. 函数指针数组声明和初始化测试"); |
| 46 | + std::println("==============================="); |
| 47 | + |
| 48 | + // 声明函数指针数组类型 |
| 49 | + operations : []*fn(int, int) : int = [add, subtract, multiply, divide]; |
| 50 | + |
| 51 | + std::println("✓ 函数指针数组声明成功"); |
| 52 | + std::println(" 数组包含4个数学运算函数指针"); |
| 53 | + std::println(""); |
| 54 | +}; |
| 55 | + |
| 56 | +fn testFunctionPointerArrayAccess() : void { |
| 57 | + std::println("2. 函数指针数组访问和调用测试"); |
| 58 | + std::println("============================="); |
| 59 | + |
| 60 | + // 创建函数指针数组 |
| 61 | + operations : []*fn(int, int) : int = [add, subtract, multiply, divide]; |
| 62 | + |
| 63 | + // 通过数组索引访问和调用函数指针 |
| 64 | + result1 : int = operations[0](10, 5); // add |
| 65 | + std::println("operations[0](10, 5) = " + result1); |
| 66 | + |
| 67 | + result2 : int = operations[1](10, 5); // subtract |
| 68 | + std::println("operations[1](10, 5) = " + result2); |
| 69 | + |
| 70 | + result3 : int = operations[2](10, 5); // multiply |
| 71 | + std::println("operations[2](10, 5) = " + result3); |
| 72 | + |
| 73 | + result4 : int = operations[3](10, 5); // divide |
| 74 | + std::println("operations[3](10, 5) = " + result4); |
| 75 | + |
| 76 | + std::println(""); |
| 77 | +}; |
| 78 | + |
| 79 | +fn testLambdaFunctionPointerArray() : void { |
| 80 | + std::println("3. Lambda函数指针数组测试"); |
| 81 | + std::println("========================="); |
| 82 | + |
| 83 | + // 创建Lambda函数指针数组 |
| 84 | + lambdas : []*fn(int, int) : int = [ |
| 85 | + ((a, b) => a + b), |
| 86 | + ((a, b) => a - b), |
| 87 | + ((a, b) => a * b), |
| 88 | + ((a, b) => a * a + b * b) |
| 89 | + ]; |
| 90 | + |
| 91 | + // 调用Lambda函数指针 |
| 92 | + result1 : int = lambdas[0](3, 4); |
| 93 | + std::println("lambdas[0](3, 4) = " + result1); |
| 94 | + |
| 95 | + result2 : int = lambdas[1](3, 4); |
| 96 | + std::println("lambdas[1](3, 4) = " + result2); |
| 97 | + |
| 98 | + result3 : int = lambdas[2](3, 4); |
| 99 | + std::println("lambdas[2](3, 4) = " + result3); |
| 100 | + |
| 101 | + result4 : int = lambdas[3](3, 4); |
| 102 | + std::println("lambdas[3](3, 4) = " + result4); |
| 103 | + |
| 104 | + std::println(""); |
| 105 | +}; |
0 commit comments