Last active
June 5, 2021 07:54
-
-
Save viz3/d8d3c9d52dd9536178a0ea4161ad8ad5 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
int addition(char *s) | |
{ | |
int x = ((unsigned)rand() % 99) + 1; | |
int y = ((unsigned)rand() % 99) + 1; | |
sprintf(s, "%d + %d = ", x, y); | |
return x + y; | |
} | |
int subtraction(char *s) | |
{ | |
int x, y; | |
while (1) { | |
x = ((unsigned)rand() % 99) + 1; | |
y = ((unsigned)rand() % 99) + 1; | |
if (x > y) { | |
break; | |
} | |
} | |
sprintf(s, "%d - %d = ", x, y); | |
return x - y; | |
} | |
int multiplication(char *s) | |
{ | |
int x = ((unsigned)rand() % 9) + 1; | |
int y = ((unsigned)rand() % 9) + 1; | |
sprintf(s, "%d x %d = ", x, y); | |
return x * y; | |
} | |
int division(char *s) | |
{ | |
int x = ((unsigned)rand() % 9) + 1; | |
int y = ((unsigned)rand() % 9) + 1; | |
sprintf(s, "%d / %d = ", x*y, y); | |
return x; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int (*funcp[])(char *s) = {addition, subtraction, multiplication, division}; | |
int correct; | |
int answer; | |
char question[64]; | |
char buf[64]; | |
srand((unsigned)time(NULL)); | |
while (1) { | |
memset(question, 0x0, sizeof(question)); | |
correct = (*funcp[rand() % 4])(question); | |
while (1) { | |
printf("%s", question); | |
scanf("%s", buf); | |
answer = atoi(buf); | |
if (answer == correct) { | |
break; | |
} | |
} | |
} | |
return 0; | |
} | |
This file contains 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
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"os" | |
"strconv" | |
"time" | |
) | |
type Calc interface { | |
GetQuestion() string | |
GetCorrect() int | |
} | |
type Addition struct { | |
x, y int | |
} | |
func NewAddition() *Addition { | |
addition := new(Addition) | |
rand.Seed(time.Now().UnixNano()) | |
addition.x = rand.Intn(10) | |
addition.y = rand.Intn(10) | |
return addition | |
} | |
func (addition *Addition) GetQuestion() string { | |
return fmt.Sprintf("%d + %d = ", addition.x, addition.y) | |
} | |
func (addition *Addition) GetCorrect() int { | |
return addition.x + addition.y | |
} | |
type Subtraction struct { | |
x, y int | |
} | |
func NewSubtraction() *Subtraction { | |
subtraction := new(Subtraction) | |
rand.Seed(time.Now().UnixNano()) | |
for { | |
x := rand.Intn(10) | |
y := rand.Intn(10) | |
if x > y { | |
subtraction.x = x | |
subtraction.y = y | |
break | |
} | |
} | |
return subtraction | |
} | |
func (subtraction *Subtraction) GetQuestion() string { | |
return fmt.Sprintf("%d - %d = ", subtraction.x, subtraction.y) | |
} | |
func (subtraction *Subtraction) GetCorrect() int { | |
return subtraction.x - subtraction.y | |
} | |
func main() { | |
var calc Calc | |
rand.Seed(time.Now().UnixNano()) | |
for { | |
i := rand.Intn(2) | |
if i == 0 { | |
calc = NewAddition() | |
} else { | |
calc = NewSubtraction() | |
} | |
for { | |
fmt.Print(calc.GetQuestion()) | |
var s string | |
fmt.Scanf("%s\n", &s) | |
if s == "q" { | |
os.Exit(0) | |
} | |
a, _ := strconv.Atoi(s) | |
if a == calc.GetCorrect() { | |
fmt.Println("correct") | |
break | |
} else { | |
fmt.Println("incorrect") | |
} | |
} | |
} | |
} |
This file contains 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
<form name="test" action="#"> | |
<div id="question"></div> | |
<input type="text" name="answer"> | |
<input type="button" value="?" onclick="func()"> | |
<div id="answer"></div> | |
</form> | |
<script> | |
class Addition { | |
constructor() { | |
this.x = Math.ceil(Math.random() * 9); | |
this.y = Math.ceil(Math.random() * 9); | |
} | |
question() { | |
return this.x + " + " + this.y + " = "; | |
} | |
correct() { | |
return this.x + this.y; | |
} | |
} | |
var addition = new Addition(); | |
document.getElementById("question").innerHTML = addition.question(); | |
function func() { | |
var answer = parseFloat(document.test.answer.value) | |
alert((answer == addition.correct() ? "correct" : "incorrect")) | |
} | |
</script> | |
This file contains 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
#!/usr/bin/env python | |
import sys | |
import random | |
class Addition: | |
def __init__(self): | |
self.x = random.randint(1, 10) | |
self.y = random.randint(1, 10) | |
def question(self): | |
return str(self.x) + ' + ' + str(self.y) + ' = ' | |
def correct(self): | |
return self.x + self.y | |
class Subtraction: | |
def __init__(self): | |
while True: | |
self.x = random.randint(1, 10) | |
self.y = random.randint(1, 10) | |
if self.x > self.y: | |
break | |
def question(self): | |
return str(self.x) + ' - ' + str(self.y) + ' = ' | |
def correct(self): | |
return self.x - self.y | |
def main(): | |
calc_class_list = ['Addition', 'Subtraction'] | |
while True: | |
random.shuffle(calc_class_list) | |
calc = globals()[calc_class_list[0]]() | |
while True: | |
print(calc.question(), end='') | |
s = input() | |
if s == 'q': | |
sys.exit(0) | |
if int(s) == calc.correct(): | |
print('正解') | |
break | |
else: | |
print('不正解') | |
if __name__ == '__main__': | |
main() |
This file contains 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
#!/usr/bin/env ruby | |
class Addition | |
def initialize | |
random = Random.new | |
@x = random.rand(1..99) | |
@y = random.rand(1..99) | |
end | |
def question | |
"#{@x} + #{@y} = " | |
end | |
def correct | |
@x + @y | |
end | |
end | |
class Subtraction | |
def initialize | |
random = Random.new | |
while true | |
@x = random.rand(1..99) | |
@y = random.rand(1..99) | |
break if @x > @y | |
end | |
end | |
def question | |
"#{@x} - #{@y} = " | |
end | |
def correct | |
@x - @y | |
end | |
end | |
class Multiplication | |
def initialize | |
random = Random.new | |
@x = random.rand(1..9) | |
@y = random.rand(1..9) | |
end | |
def question | |
"#{@x} x #{@y} = " | |
end | |
def correct | |
@x * @y | |
end | |
end | |
while true | |
q = [Addition, Subtraction, Multiplication].shuffle.first.new | |
while true | |
print q.question | |
answer = $stdin.gets.strip.to_i | |
break if answer == q.correct | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment