From a78b7b05f34d133fca4b2ef1c5c82deb75e5b32f Mon Sep 17 00:00:00 2001 From: EdamAme <121654029+EdamAme-x@users.noreply.github.com> Date: Mon, 10 Jul 2023 23:44:21 +0000 Subject: [PATCH 1/5] add --- pages/quadratic-equation.tsx | 124 +++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 pages/quadratic-equation.tsx diff --git a/pages/quadratic-equation.tsx b/pages/quadratic-equation.tsx new file mode 100644 index 0000000..5896b54 --- /dev/null +++ b/pages/quadratic-equation.tsx @@ -0,0 +1,124 @@ +import type { NextPage } from 'next' +import { useRouter } from 'next/router' +import { useRef, useEffect, useState } from 'react' +import MathJax from "react-mathjax" +import styles from '../styles/Home.module.css' + +/** + * @param x 解(暫定) + * @param a 次数 + * @param b 係数 + * @param c 定数 + */ + +interface Question { + x: number | null + a: number + b: number + c: number +} + +const randint = (min: number, max: number): number => Math.floor(Math.random() * (max - min)) + min + +const QEquation: NextPage = ({ chime, correct, incorrect }) => { + const createQuestion = (): Question => { + let x: number | null = null + let a: number + let b: number + let c: number + let dis: number + + do { + x = randint(-8, 8) + a = randint(2, 8) // coefficient of x^2 + b = randint(2, 12) // coefficient of x + c = a * x * x + b * x + dis = b * b - 4 * a * c + } while (dis < 0 || Math.sqrt(dis) % 1 !== 0) // √b^2 - 4acが整数でない場合や、判別式が負の場合に再抽選 + + return { + x, + a, + b, + c, + } + } + + const [questionText, setQuestionText] = useState("") + const [answers, setAnswers] = useState<(number | null)[]>([]) + useEffect(() => { + const questionData = createQuestion() + const mathJax = String.raw`${questionData.a}x^2 + ${questionData.b}x = ${questionData.c}` // 問題文生成 + setQuestionText(mathJax) + setAnswers([questionData.x]) + }, []) + + const verify = async () => { + const parsedAnswers = ans.split("||").map(answer => answer.trim()) + + const parsedIntAnswers = parsedAnswers.map(answer => { + const parsedFraction = answer.split("/") // 分数処理 + if (parsedFraction.length === 2) { + const numerator = parseInt(parsedFraction[0].trim()) + const denominator = parseInt(parsedFraction[1].trim()) + if (!isNaN(numerator) && !isNaN(denominator) && denominator !== 0) { + return numerator / denominator + } + } else { + const parsedInt = parseInt(answer) + if (!isNaN(parsedInt)) { + return parsedInt + } + } + return null + }) + + const filteredAnswers = parsedIntAnswers.filter(answer => answer !== null) as number[] + + if (answers.some(answer => filteredAnswers.includes(answer))) { + chime.current.pause() + correct.current.play() + const result = await fetch(`/api/send?command=1`) // Faild to Fetch ..? + console.log(await result.json()) + router.push('/wakeup') + } else { + incorrect.current.play() + setflash(true) + setTimeout(() => { + setflash(false) + setAns('') + }, 200) + } + } + + const router = useRouter() + const [ans, setAns] = useState('') + const [isFlash, setflash] = useState(false) + + return ( + <> +
+ + + 解を求めよ + +
+ { setAns(e.target.value) }} inputMode="numeric"/> +
+ +
+ + ) +} + +/* +回答形式 +x1 || x2 +解が存在しない場合はnull +*/ + +export default QEquation \ No newline at end of file From 03cab5830e92195b2b4c073a79525d910f6bfa27 Mon Sep 17 00:00:00 2001 From: EdamAme <121654029+EdamAme-x@users.noreply.github.com> Date: Tue, 11 Jul 2023 00:28:05 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E5=9B=9E=E7=AD=94=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E3=82=92=20=E3=83=91=E3=82=A4=E3=83=97=E3=81=8B=E3=82=89?= =?UTF-8?q?=E3=82=AB=E3=83=B3=E3=83=9E=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/quadratic-equation.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pages/quadratic-equation.tsx b/pages/quadratic-equation.tsx index 5896b54..a085ed5 100644 --- a/pages/quadratic-equation.tsx +++ b/pages/quadratic-equation.tsx @@ -54,7 +54,7 @@ const QEquation: NextPage = ({ chime, correct, incorrect }) => { }, []) const verify = async () => { - const parsedAnswers = ans.split("||").map(answer => answer.trim()) + const parsedAnswers = ans.split(",").map(answer => answer.trim()) const parsedIntAnswers = parsedAnswers.map(answer => { const parsedFraction = answer.split("/") // 分数処理 @@ -105,7 +105,7 @@ const QEquation: NextPage = ({ chime, correct, incorrect }) => { 解を求めよ
- { setAns(e.target.value) }} inputMode="numeric"/> + { setAns(e.target.value) }} inputMode="numeric"/>
+ + + { setAns(e.target.value) }} inputMode="numeric"/> + , +
+ + +
+ { setAns2(e.target.value) }} inputMode="numeric"/> +