Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit b807d0f

Browse files
committed
添加多个演示程序,包括基础语法、递归函数、控制流、模块化编程等,展示CodeNothing语言的多种特性。
1 parent 9b315a8 commit b807d0f

40 files changed

+2757
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// CodeNothing 高级特性演示程序(修复版本)
2+
// 展示递归、数学函数、复杂控制流等特性
3+
4+
using lib<io>;
5+
6+
// 全局常量
7+
const FIBONACCI_LIMIT : int = 6;
8+
const FACTORIAL_INPUT : int = 5;
9+
10+
// 递归函数:斐波那契数列
11+
fn fibonacci(n : int) : int {
12+
if (n <= 1) {
13+
return n;
14+
} else {
15+
return fibonacci(n - 1) + fibonacci(n - 2);
16+
};
17+
};
18+
19+
// 递归函数:阶乘
20+
fn factorial(n : int) : int {
21+
if (n <= 1) {
22+
return 1;
23+
} else {
24+
return n * factorial(n - 1);
25+
};
26+
};
27+
28+
// 最大公约数
29+
fn gcd(a : int, b : int) : int {
30+
while (b != 0) {
31+
temp : int = b;
32+
b = a % b;
33+
a = temp;
34+
};
35+
return a;
36+
};
37+
38+
// 质数判断
39+
fn isPrime(n : int) : bool {
40+
if (n <= 1) {
41+
return false;
42+
};
43+
if (n <= 3) {
44+
return true;
45+
};
46+
if (n % 2 == 0) {
47+
return false;
48+
};
49+
50+
i : int = 3;
51+
while (i * i <= n) {
52+
if (n % i == 0) {
53+
return false;
54+
};
55+
i = i + 2;
56+
};
57+
return true;
58+
};
59+
60+
// 数字分类
61+
fn classifyNumber(num : int) : string {
62+
if (num == 0) {
63+
return "零";
64+
};
65+
66+
result : string = "";
67+
absNum : int = num;
68+
69+
if (num > 0) {
70+
result = "正数";
71+
} else {
72+
result = "负数";
73+
absNum = -num;
74+
};
75+
76+
if (absNum % 2 == 0) {
77+
result = result + ", 偶数";
78+
} else {
79+
result = result + ", 奇数";
80+
};
81+
82+
return result;
83+
};
84+
85+
// 嵌套循环演示
86+
fn multiplicationTable() : void {
87+
std::println("=== 乘法表演示 ===");
88+
i : int = 1;
89+
while (i <= 3) {
90+
j : int = 1;
91+
while (j <= 3) {
92+
product : int = i * j;
93+
std::println(i + " x " + j + " = " + product);
94+
j = j + 1;
95+
};
96+
i = i + 1;
97+
};
98+
};
99+
100+
// 主函数
101+
fn main() : int {
102+
std::println("=== CodeNothing 高级特性演示 ===");
103+
104+
// 递归函数演示
105+
std::println("=== 递归函数演示 ===");
106+
std::println("斐波那契数列前" + FIBONACCI_LIMIT + "项:");
107+
i : int = 0;
108+
while (i < FIBONACCI_LIMIT) {
109+
fib : int = fibonacci(i);
110+
std::println("F(" + i + ") = " + fib);
111+
i = i + 1;
112+
};
113+
114+
fact : int = factorial(FACTORIAL_INPUT);
115+
std::println(FACTORIAL_INPUT + "! = " + fact);
116+
117+
// 数学函数演示
118+
std::println("=== 数学函数演示 ===");
119+
a : int = 48;
120+
b : int = 18;
121+
gcd_result : int = gcd(a, b);
122+
std::println("gcd(" + a + ", " + b + ") = " + gcd_result);
123+
124+
// 质数判断演示
125+
std::println("=== 质数判断演示 ===");
126+
test_nums : int = 2;
127+
while (test_nums <= 10) {
128+
if (isPrime(test_nums)) {
129+
std::println(test_nums + " 是质数");
130+
};
131+
test_nums = test_nums + 1;
132+
};
133+
134+
// 嵌套循环演示
135+
multiplicationTable();
136+
137+
// 数字分类演示
138+
std::println("=== 数字分类演示 ===");
139+
numbers : int = -3;
140+
while (numbers <= 3) {
141+
if (numbers != 0) {
142+
classification : string = classifyNumber(numbers);
143+
std::println(numbers + ": " + classification);
144+
};
145+
numbers = numbers + 1;
146+
};
147+
148+
std::println("高级特性演示完成!");
149+
return 0;
150+
};
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// CodeNothing 高级特性演示程序(工作版本)
2+
// 展示递归、数学函数、复杂控制流等特性
3+
4+
using lib<io>;
5+
6+
// 全局常量
7+
const FIBONACCI_LIMIT : int = 8;
8+
const FACTORIAL_INPUT : int = 5;
9+
10+
// 递归函数:斐波那契数列
11+
fn fibonacci(n : int) : int {
12+
if (n <= 1) {
13+
return n;
14+
} else {
15+
return fibonacci(n - 1) + fibonacci(n - 2);
16+
};
17+
};
18+
19+
// 递归函数:阶乘
20+
fn factorial(n : int) : int {
21+
if (n <= 1) {
22+
return 1;
23+
} else {
24+
return n * factorial(n - 1);
25+
};
26+
};
27+
28+
// 最大公约数
29+
fn gcd(a : int, b : int) : int {
30+
while (b != 0) {
31+
temp : int = b;
32+
b = a % b;
33+
a = temp;
34+
};
35+
return a;
36+
};
37+
38+
// 质数判断
39+
fn isPrime(n : int) : bool {
40+
if (n <= 1) {
41+
return false;
42+
};
43+
if (n <= 3) {
44+
return true;
45+
};
46+
if (n % 2 == 0) {
47+
return false;
48+
};
49+
50+
i : int = 3;
51+
while (i * i <= n) {
52+
if (n % i == 0) {
53+
return false;
54+
};
55+
i = i + 2;
56+
};
57+
return true;
58+
};
59+
60+
// 数字分类
61+
fn classifyNumber(num : int) : string {
62+
if (num == 0) {
63+
return "零";
64+
};
65+
66+
result : string = "";
67+
absNum : int = num;
68+
69+
if (num > 0) {
70+
result = "正数";
71+
} else {
72+
result = "负数";
73+
absNum = -num;
74+
};
75+
76+
if (absNum % 2 == 0) {
77+
result = result + ", 偶数";
78+
} else {
79+
result = result + ", 奇数";
80+
};
81+
82+
return result;
83+
};
84+
85+
// 嵌套循环演示
86+
fn multiplicationTable() : void {
87+
std::println("=== 乘法表演示 ===");
88+
i : int = 1;
89+
while (i <= 3) {
90+
j : int = 1;
91+
while (j <= 3) {
92+
product : int = i * j;
93+
std::println(i + " x " + j + " = " + product);
94+
j = j + 1;
95+
};
96+
i = i + 1;
97+
};
98+
};
99+
100+
// 主函数
101+
fn main() : int {
102+
std::println("=== CodeNothing 高级特性演示 ===");
103+
104+
// 递归函数演示
105+
std::println("=== 递归函数演示 ===");
106+
std::println("斐波那契数列前" + FIBONACCI_LIMIT + "项:");
107+
i : int = 0;
108+
while (i < FIBONACCI_LIMIT) {
109+
fib : int = fibonacci(i);
110+
std::println("F(" + i + ") = " + fib);
111+
i = i + 1;
112+
};
113+
114+
fact : int = factorial(FACTORIAL_INPUT);
115+
std::println(FACTORIAL_INPUT + "! = " + fact);
116+
117+
// 数学函数演示
118+
std::println("=== 数学函数演示 ===");
119+
a : int = 48;
120+
b : int = 18;
121+
gcd_result : int = gcd(a, b);
122+
std::println("gcd(" + a + ", " + b + ") = " + gcd_result);
123+
124+
// 质数判断演示
125+
std::println("=== 质数判断演示 ===");
126+
test_nums : int = 2;
127+
while (test_nums <= 10) {
128+
if (isPrime(test_nums)) {
129+
std::println(test_nums + " 是质数");
130+
};
131+
test_nums = test_nums + 1;
132+
};
133+
134+
// 嵌套循环演示
135+
multiplicationTable();
136+
137+
// 数字分类演示
138+
std::println("=== 数字分类演示 ===");
139+
numbers : int = -3;
140+
while (numbers <= 3) {
141+
if (numbers != 0) {
142+
classification : string = classifyNumber(numbers);
143+
std::println(numbers + ": " + classification);
144+
};
145+
numbers = numbers + 1;
146+
};
147+
148+
std::println("高级特性演示完成!");
149+
return 0;
150+
};

0 commit comments

Comments
 (0)