-
Notifications
You must be signed in to change notification settings - Fork 7
add 二次方程式 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EdamAme-x
wants to merge
5
commits into
lambda-tech-club:master
Choose a base branch
from
EdamAme-x:add-qeq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−0
Open
add 二次方程式 #4
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 次数 // -8 ~ 8 | ||
| * @param b 係数 // 2 ~ 12 | ||
| * @param c 定数 // a,b,xから算出 | ||
| */ | ||
|
|
||
| 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 }: any) => { | ||
| 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 ..? | ||
EdamAme-x marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 ( | ||
| <> | ||
| <div className={styles.formula} style={isFlash ? { | ||
| background: 'rgba(255, 0, 0, 0.7)' | ||
| } : {}}> | ||
| <MathJax.Provider> | ||
| <MathJax.Node formula={questionText} /> | ||
| 解を求めよ | ||
| </MathJax.Provider> | ||
| <div> | ||
| <input className={styles.textbox} placeholder="x1 , x2" type="text" value={ans} onChange={(e) => { setAns(e.target.value) }} inputMode="numeric"/> | ||
|
||
| </div> | ||
| <button className={styles.button} onClick={verify}> | ||
| 決定 | ||
| </button> | ||
| </div> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| /* | ||
| 回答形式 | ||
| x1 , x2 | ||
| 解が存在しない場合はnull | ||
| */ | ||
|
|
||
| export default QEquation | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| declare module 'react-mathjax'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
どちらか片方の解が入力されていれば正解と判定されますが、想定通りの挙動でしょうか?
(仕様的に結構な割合で0を入力すると正解になってしまうのはかんたんすぎるような気もして💭)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
順序を無視して採点する為に どちらかが正解なら通すようにしてしまってますね…
他の採点方法に切り替えます
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI
https://github.com/yoidea/applied-math-alarm/blob/c3b3c28fdf6734713ea65d1f2e116e9e47ffa8e5/pages/differential.tsx#L18-L20