Skip to content

Commit 4cae14e

Browse files
committed
feat: 添加随机生成数据按钮
1 parent f5b29a9 commit 4cae14e

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/components/InputPanel.css

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
font-size: 13px;
4949
}
5050

51-
.run-button {
51+
.run-button,
52+
.random-button {
5253
padding: 6px 16px;
53-
background: #0e639c;
5454
color: white;
5555
border: none;
5656
border-radius: 4px;
@@ -59,10 +59,26 @@
5959
transition: background 0.2s;
6060
}
6161

62+
.run-button {
63+
background: #0e639c;
64+
}
65+
6266
.run-button:hover {
6367
background: #1177bb;
6468
}
6569

6670
.run-button:active {
6771
background: #0d5a8c;
6872
}
73+
74+
.random-button {
75+
background: #6a4c93;
76+
}
77+
78+
.random-button:hover {
79+
background: #7d5ba6;
80+
}
81+
82+
.random-button:active {
83+
background: #5a3d7a;
84+
}

src/components/InputPanel.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ export default function InputPanel({
5050
}
5151
};
5252

53+
const generateRandom = () => {
54+
// 随机课程数量 3-8
55+
const num = Math.floor(Math.random() * 6) + 3;
56+
57+
// 生成随机 DAG(有向无环图)
58+
const prereqs: number[][] = [];
59+
const edgeCount = Math.floor(Math.random() * (num * 2)) + 1;
60+
const existingEdges = new Set<string>();
61+
62+
for (let i = 0; i < edgeCount; i++) {
63+
// 确保 from > to 来避免环
64+
const to = Math.floor(Math.random() * (num - 1));
65+
const from = Math.floor(Math.random() * (num - to - 1)) + to + 1;
66+
const key = `${from}-${to}`;
67+
68+
if (!existingEdges.has(key)) {
69+
existingEdges.add(key);
70+
prereqs.push([from, to]);
71+
}
72+
}
73+
74+
setNumCourses(num.toString());
75+
setPrerequisites(JSON.stringify(prereqs));
76+
setError(null);
77+
onSubmit(num, prereqs);
78+
};
79+
5380
return (
5481
<div className="input-panel">
5582
<div className="input-group">
@@ -75,6 +102,9 @@ export default function InputPanel({
75102
<button className="run-button" onClick={handleSubmit}>
76103
运行
77104
</button>
105+
<button className="random-button" onClick={generateRandom}>
106+
🎲 随机生成
107+
</button>
78108
</div>
79109
);
80110
}

0 commit comments

Comments
 (0)