Skip to content

Commit d412cc7

Browse files
author
William Yang
committed
feat: 添加caesar题目 (stage配置 + starter代码 + 更新stage_order)
1 parent e5580d9 commit d412cc7

File tree

6 files changed

+245
-0
lines changed

6 files changed

+245
-0
lines changed

course.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ stage_order:
4242
- hello
4343
- mario-less
4444
- cash
45+
- caesar
4546
- me
4647
- mario-more
4748
- credit

stages/caesar.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Caesar
2+
3+
## 背景
4+
5+
凯撒密码是最简单的加密技术之一。它通过将字母表中的每个字母替换为固定位置之后的字母来实现加密。例如,密钥为 1 时,A 变成 B,B 变成 C,依此类推。
6+
7+
## 任务
8+
9+
实现一个程序,使用凯撒密码加密消息。
10+
11+
## 用法
12+
13+
```
14+
./caesar key
15+
```
16+
17+
其中 `key` 是一个非负整数。
18+
19+
## 示例
20+
21+
```
22+
$ ./caesar 1
23+
plaintext: HELLO
24+
ciphertext: IFMMP
25+
```
26+
27+
```
28+
$ ./caesar 13
29+
plaintext: hello, world
30+
ciphertext: uryyb, jbeyq
31+
```
32+
33+
```
34+
$ ./caesar 3
35+
plaintext: Be sure to drink your Ovaltine!
36+
ciphertext: Eh vxuh wr gulqn brxu Rydowlqh!
37+
```
38+
39+
## 规范
40+
41+
1. 程序接受一个命令行参数:一个非负整数作为密钥
42+
2. 如果参数无效(不是数字、缺失或过多),程序应输出错误信息并返回 1
43+
3. 程序应提示用户输入明文 `plaintext: `
44+
4. 程序应输出密文 `ciphertext: ` 后跟加密后的文本
45+
5. 只加密字母(a-z, A-Z),保持大小写不变
46+
6. 非字母字符保持原样
47+
48+
## 提示
49+
50+
<details>
51+
<summary>点击查看提示</summary>
52+
53+
### 算法
54+
55+
对于每个字母:
56+
57+
1. 确定它是大写还是小写
58+
2. 计算它在字母表中的位置(0-25)
59+
3. 加上密钥
60+
4. 对 26 取模以处理回绕
61+
5. 转换回字母
62+
63+
### C 语言参考
64+
65+
```c
66+
#include <cs50.h>
67+
#include <stdio.h>
68+
#include <ctype.h>
69+
#include <stdlib.h>
70+
#include <string.h>
71+
72+
int main(int argc, char *argv[])
73+
{
74+
// 检查参数数量
75+
if (argc != 2)
76+
{
77+
printf("Usage: ./caesar key\n");
78+
return 1;
79+
}
80+
81+
// 验证密钥是数字
82+
for (int i = 0; argv[1][i] != '\0'; i++)
83+
{
84+
if (!isdigit(argv[1][i]))
85+
{
86+
printf("Usage: ./caesar key\n");
87+
return 1;
88+
}
89+
}
90+
91+
int key = atoi(argv[1]);
92+
93+
// 获取明文
94+
string plaintext = get_string("plaintext: ");
95+
96+
printf("ciphertext: ");
97+
98+
// 加密每个字符
99+
for (int i = 0; plaintext[i] != '\0'; i++)
100+
{
101+
char c = plaintext[i];
102+
if (isupper(c))
103+
{
104+
printf("%c", (c - 'A' + key) % 26 + 'A');
105+
}
106+
else if (islower(c))
107+
{
108+
printf("%c", (c - 'a' + key) % 26 + 'a');
109+
}
110+
else
111+
{
112+
printf("%c", c);
113+
}
114+
}
115+
printf("\n");
116+
117+
return 0;
118+
}
119+
```
120+
121+
</details>
122+
123+
## 参考资料
124+
125+
- [凯撒密码 - 维基百科](https://zh.wikipedia.org/wiki/凱撒密碼)

stages/caesar.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# stages/caesar.yml
2+
version: "1.0"
3+
4+
slug: caesar
5+
name: "Caesar"
6+
description: caesar.md
7+
summary: |
8+
实现凯撒密码加密程序,使用命令行参数指定密钥。
9+
10+
category: Week 2
11+
12+
# 多语言配置
13+
languages:
14+
c:
15+
starter: starters/c/caesar
16+
files:
17+
required:
18+
- caesar.c
19+
allowed:
20+
- "*.c"
21+
- "*.h"
22+
blocked:
23+
- "*.o"
24+
- "*.exe"
25+
python:
26+
starter: starters/python/caesar
27+
files:
28+
required:
29+
- caesar.py
30+
allowed:
31+
- "*.py"
32+
blocked:
33+
- "__pycache__"
34+
- "*.pyc"
35+
java:
36+
starter: starters/java/caesar
37+
files:
38+
required:
39+
- Caesar.java
40+
allowed:
41+
- "*.java"
42+
blocked:
43+
- "*.class"
44+
45+
# 评测配置
46+
evaluation:
47+
timeout: 60
48+
49+
hints:
50+
- "记得验证命令行参数的数量和格式"
51+
- "使用取模运算处理超过 26 的密钥"
52+
- "只加密字母,保留其他字符不变"

starters/c/caesar/caesar.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TODO: 实现 caesar.c
2+
#include <cs50.h>
3+
#include <stdio.h>
4+
#include <ctype.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
int main(int argc, char *argv[])
9+
{
10+
// 检查命令行参数数量
11+
12+
// 验证密钥是否为数字
13+
14+
// 将密钥转换为整数
15+
16+
// 提示用户输入明文
17+
18+
// 加密并输出密文
19+
20+
}

starters/java/caesar/Caesar.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// TODO: 实现 Caesar.java
2+
import java.util.Scanner;
3+
4+
public class Caesar {
5+
public static void main(String[] args) {
6+
// 检查命令行参数数量
7+
if (args.length != 1) {
8+
System.out.println("Usage: java Caesar key");
9+
System.exit(1);
10+
}
11+
12+
// 验证密钥是否为数字
13+
14+
// 将密钥转换为整数
15+
16+
// 提示用户输入明文
17+
Scanner scanner = new Scanner(System.in);
18+
System.out.print("plaintext: ");
19+
20+
// 获取用户输入
21+
22+
// 加密并输出密文
23+
24+
scanner.close();
25+
}
26+
}

starters/python/caesar/caesar.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# TODO: 实现 caesar.py
2+
import sys
3+
4+
5+
def main():
6+
# 检查命令行参数数量
7+
if len(sys.argv) != 2:
8+
print("Usage: python caesar.py key")
9+
sys.exit(1)
10+
11+
# 验证密钥是否为数字
12+
13+
# 将密钥转换为整数
14+
15+
# 提示用户输入明文
16+
17+
# 加密并输出密文
18+
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)