Last active
November 6, 2025 02:35
-
-
Save 14790897/daad5effef11598c4b8ba4c22bbf81f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ------------------------1 | |
| package demo1 | |
| main() { | |
| // 创建一个包含家庭成员姓名、年龄和每日三餐饮食情况的元组嵌套 | |
| // ___①___ 补全处 | |
| let familyMember = ("John", 35, (("早餐", "煎蛋"), ("午餐", "三明治"), ("晚餐", "意大利面"))) | |
| // 解构元组,提取家庭成员的姓名、年龄和三餐信息 | |
| // ___②___ 补全处 | |
| let (name, age, meals) = familyMember | |
| // 手动解构每日三餐的信息 | |
| // ___③___ 补全处 | |
| let (breakfast, lunch, dinner) = meals | |
| // 输出家庭成员的基本信息 | |
| // ___④___ 补全处 | |
| println("家庭成员: ${name}, 年龄: ${age}") | |
| // 手动构造一个数组来表示三餐 | |
| let mealArray = [breakfast, lunch, dinner] | |
| // 循环遍历并解构输出每日三餐的信息 | |
| // ___⑤___ 补全处 | |
| for (meal in mealArray) { | |
| // 在循环内部再次解构每一餐 | |
| let (time, food) = meal | |
| println("正在处理: ${time}") | |
| println(" ${time}: ${food}") | |
| } | |
| } | |
| package demo1 | |
| main() { | |
| println("家庭成员: John, 年龄: 35") | |
| println("正在处理: 早餐") | |
| println(" 早餐: 煎蛋") | |
| println("正在处理: 午餐") | |
| println(" 午餐: 三明治") | |
| println("正在处理: 晚餐") | |
| println(" 晚餐: 意大利面") | |
| } | |
| ---------------------------2 | |
| package demo2 | |
| main() { | |
| println("购物清单统计:") | |
| println("苹果的数量超过了5个。") | |
| println("总共买了14件商品。") | |
| } | |
| -------------------------3 | |
| package demo3 | |
| import std.math.* | |
| import std.time.* | |
| // 定义struct类型的坐标点 | |
| // ___①___ 补全处 | |
| struct Point { | |
| // 定义不可变成员变量(值类型核心特性) | |
| // ___②___ 补全处 | |
| let x: Int64 | |
| let y: Int64 | |
| // 定义构造函数 | |
| // ___③___ 补全处 | |
| init(x: Int64, y: Int64) { | |
| this.x = x | |
| this.y = y | |
| } | |
| // 点平移:返回新点(体现不可变性) | |
| // dx: 表示坐标x的移动值 | |
| // dy: 表示坐标y的移动值 | |
| // ___④___ 补全处 | |
| func translated(dx: Int64, dy: Int64): Point { | |
| // 返回新的坐标点对象 | |
| // ___⑤___ 补全处 | |
| return Point(this.x + dx, this.y + dy) | |
| } | |
| } | |
| // 定义struct类型的矩形 | |
| // ___⑥___ 补全处 | |
| struct Rectangle { | |
| // 定义可变的成员变量 | |
| // ___⑦___ 补全处 | |
| var topLeft: Point | |
| var width: Int64 | |
| var height: Int64 | |
| //定义构造函数 | |
| // ___⑧___ 补全处 | |
| init(topLeft: Point, width: Int64, height: Int64) { | |
| this.topLeft = topLeft | |
| this.width = width | |
| this.height = height | |
| } | |
| // 矩形平移,可在函数内修改成员变量 | |
| // ___⑨___ 补全处 | |
| mut func translate(dx: Int64, dy: Int64): Unit { | |
| this.topLeft = topLeft.translated(dx, dy) | |
| } | |
| } | |
| // 定义class类型的坐标点(引用类型对照组) | |
| // ___⑩___ 补全处 | |
| class RefPoint { | |
| // 定义成员变量 | |
| // ___⑪___ 补全处 | |
| var x: Int64 | |
| var y: Int64 | |
| // 定义主构造函数 | |
| // ___⑫___ 补全处 | |
| init(x: Int64, y: Int64) { | |
| this.x = x | |
| this.y = y | |
| } | |
| // 点平移 | |
| func translated(dx: Int64, dy: Int64): Unit{ | |
| x += dx | |
| y += dy | |
| } | |
| } | |
| // 值类型/引用类型性能测试 | |
| func performanceTesting(dataType: String, x!: Int64 = 1, y!: Int64 = 2): Duration { | |
| // 开始时间 | |
| let valueTypeStart = DateTime.now().toUnixTimeStamp() | |
| for (_ in 0..100000) { | |
| if (dataType == 'value') { | |
| // 创建Point对象(栈分配,无需GC) | |
| // ___⑬___ 补全处 | |
| let p = Point(x, y) | |
| } else if (dataType == 'reference') { | |
| // 创建RefPoint对象(堆分配,产生GC压力) | |
| // ___⑭___ 补全处 | |
| let rp = RefPoint(x, y) | |
| } | |
| } | |
| // 返回运行时间差对象 | |
| return DateTime.now().toUnixTimeStamp() - valueTypeStart | |
| } | |
| main() { | |
| let valueTypeTime = performanceTesting('value') | |
| let refTypeStart = performanceTesting('reference') | |
| //1.性能测试:值类型vs引用类型 | |
| println("值类型耗时: ${valueTypeTime}ms") // 预期:更短(无GC) | |
| println("引用类型耗时: ${refTypeStart}ms") // 预期:更长(GC开销) | |
| //2.值&引用类型行为差异验证 | |
| var p1 = Point(0, 0) | |
| var p2 = p1 | |
| var p3 = p2.translated(3, 4) | |
| println("p1: (${p1.x}, ${p1.y}), p3: (${p3.x}, ${p3.y})") | |
| var rp1 = RefPoint(0, 0) | |
| var rp2 = rp1 | |
| rp2.translated(3, 4) | |
| println("rp1: (${rp1.x}, ${rp1.y}), rp2: (${rp2.x}, ${rp2.y})") | |
| //3.调用矩形 | |
| var rectangle = Rectangle(p2, 300, 200) | |
| rectangle.translate(5, 6) | |
| println("rectangle: (${rectangle.topLeft.x},${rectangle.topLeft.y}), width: ${rectangle.width}, height:${rectangle.height}") | |
| } | |
| -------------------4 | |
| package demo4 | |
| import std.math.* | |
| import std.time.* | |
| import std.convert.* | |
| // 定义空气调节模式函数 | |
| func getAirConditionerMode(temp: Float64) { | |
| // if表达式作为返回值,类型自动推导 | |
| // ___①___ 补全处 | |
| return if (temp > 28.0) { | |
| "制冷模式" | |
| } else if (temp < 8.0) { | |
| "制热模式" | |
| } else if (temp >= 22.0 && temp <= 26.0) { | |
| "节能模式" | |
| } else { | |
| "自动模式" | |
| } | |
| } | |
| // 定义获取风速函数 | |
| func getFanSpeed(tempDiff: Float64) { | |
| // 根据if表达式获取基础风速 | |
| // ___②___ 补全处 | |
| let baseSpeed: Int64 = if (tempDiff > 5.0) { | |
| 3 | |
| } else if (tempDiff > 3.0) { | |
| 2 | |
| } else { | |
| 1 | |
| } | |
| // 获取当前时间点 | |
| let currentHour = DateTime.now().hour | |
| // 根据时间动态调整风速(模拟夜间自动降速) | |
| // ___③___ 补全处 | |
| return if (currentHour >= 22 || currentHour < 6) { | |
| // 夜间 | |
| if (baseSpeed > 1) { | |
| baseSpeed - 1 | |
| } else { | |
| 1 | |
| } | |
| } else { | |
| // 白天 | |
| baseSpeed | |
| } | |
| } | |
| main() { | |
| let temperatures = [6.5, 23.0, 27.8, 30.2] | |
| for (temp in temperatures) { | |
| let mode = getAirConditionerMode(temp) | |
| let diff = abs(temp - 24.0) // 与舒适温度的差值 | |
| let speed = getFanSpeed(diff) | |
| println("当前温度: ${temp.format('.1')}°C") | |
| println("推荐模式: ${mode}") | |
| println("推荐风速: ${speed}级\n") | |
| } | |
| } | |
| -----5 需要额外创建dataUtils.cj | |
| --------------main.cj | |
| package demo5 | |
| import std.convert.* | |
| import std.math.* // 导入 math 库以使用 pow (幂运算) | |
| // 数学计算器类 | |
| // ___①___ 补全处 | |
| class Calculator { | |
| // 参与计算的当前值 | |
| private var currentValue: Float64 | |
| // 定义主构造函数,参数列表为非命名参数 | |
| // ___②___ 补全处 | |
| init(initialValue: Float64) { | |
| this.currentValue = initialValue | |
| } | |
| // 基础操作:加法 | |
| func add(operand: Float64): Calculator { | |
| // 通过Lambda表达式进行加法计算 | |
| // ___③___ 补全处 | |
| let operation = { v: Float64 => v + operand } | |
| currentValue = operation(currentValue) | |
| // 类的实例对象的引用作为返回值(支持链式调用) | |
| // ___④___ 补全处 | |
| return this | |
| } | |
| // 高级操作:幂运算 | |
| // ___⑤___ 补全处 (operand 是非命名参数, debug 是命名参数) | |
| func power(operand: Float64, debug!: Bool = false): Calculator { | |
| // 通过Lambda表达式进行幂计算 | |
| // ___⑥___ 补全处 | |
| let operation = { v: Float64 => | |
| // 修正:直接调用 pow,移除 'std.math.' 前缀 | |
| let result = pow(v, operand) | |
| if (debug) { | |
| // 题目要求:打印内容可自定义 | |
| println("[DEBUG] 幂运算: ${v} ^ ${operand} = ${result}") | |
| } | |
| return result | |
| } | |
| currentValue = operation(currentValue) | |
| // 类的实例对象的引用作为返回值(支持链式调用) | |
| // ___⑦___ 补全处 | |
| return this | |
| } | |
| // 获取计算结果 | |
| // ___⑧___ 补全处 (precision 是命名参数) | |
| func getResult(precision!: Int64 = 2): String { | |
| return formatFloat(currentValue, precision) | |
| } | |
| } | |
| // 演示用法 | |
| main() { | |
| //创建Calculator对象,并通过链式调用得到计算结果 | |
| // ___⑨___ 补全处 | |
| // 链式调用示例: 10.0 + 5.0 = 15.0 | |
| // 15.0 ^ 2.0 = 225.0 (开启debug) | |
| // 格式化, 保留1位小数 | |
| let result = Calculator(10.0) | |
| .add(5.0) | |
| .power(2.0, debug: true) | |
| .getResult(precision: 1) | |
| println("计算结果: ${result}") // 输出计算结果 | |
| } | |
| ----dataUtils.cj | |
| package demo5 | |
| // 浮点数保留 n 位小数(四舍五入) | |
| public func formatFloat(v: Float64, digits: Int64): String { | |
| if (digits <= 0) { | |
| return Int64(v).toString() | |
| } | |
| // 1. 放大 10^n 倍 | |
| var scale: Int64 = 1 | |
| for (_ in 0..digits) { | |
| scale *= 10 | |
| } | |
| // 2. 加 0.5 后截断,实现四舍五入 | |
| let scaled = v * Float64(scale) | |
| let rounded = if (scaled >= 0.0) { | |
| scaled + 0.5 | |
| } else { | |
| scaled - 0.5 | |
| } | |
| let n = Int64(rounded) | |
| // 3. 拆整数 / 小数 | |
| let intPart = (n / scale).toString() | |
| var fracPart = (if (n < 0) { | |
| -n | |
| } else { | |
| n | |
| } % scale).toString() | |
| // 4. 左侧补零 | |
| while (fracPart.size < digits) { | |
| fracPart = "0" + fracPart | |
| } | |
| return "${intPart}.${fracPart}" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment