GY-63_MS5611/libraries/Fraction/examples/fractionExerciser/fractionExerciser.ino
2020-11-27 11:16:22 +01:00

189 lines
3.8 KiB
C++

//
// FILE: fractionExerciser.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: demo sketch for fraction math
// DATE: 2015-03-29
// URL: https://github.com/RobTillaart/Fraction
//
#include "fraction.h"
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(115200);
Serial.print("\n\nStart fractionExcerciser: ");
// Serial.println(FRACTIONLIBVERSION);
Serial.println();
randomSeed(analogRead(A0) * 256 + analogRead(A1));
}
Fraction readFraction()
{
int a = 0;
int b = 0;
while (Serial.available()) Serial.read();
while (Serial.available() == 0);
char c = Serial.read();
while (c != '.' && c != '/')
{
a = a * 10 + c - '0';
while (Serial.available() == 0);
c = Serial.read();
}
while (Serial.available() == 0);
c = Serial.read();
while (c != '.')
{
b = b * 10 + c - '0';
while (Serial.available() == 0);
c = Serial.read();
}
Fraction fr(a, b);
return fr;
}
char choice()
{
while (Serial.available()) Serial.read();
while (Serial.available() == 0);
return Serial.read();
}
void loop()
{
Serial.println("\n\n\which fractions exercise:");
Serial.println("1: add ");
Serial.println("2: subtract");
Serial.println("3: multiply");
Serial.println("4: divide");
Serial.println("5: larger (>) or smaller (<)");
Serial.println();
int count = 0;
switch (choice() - '0')
{
case 1: count = add(5); break;
case 2: count = sub(5); break;
case 3: count = mul(5); break;
case 4: count = div(5); break;
case 5: count = equ(5); break;
}
Serial.print("your score = ");
Serial.println(count);
}
int add(int n)
{
int count = 0;
Serial.println("type n/m.");
for (int i = 0; i < n; i++)
{
Fraction a(1 + random(9), 1 + random(9));
Fraction b(1 + random(9), 1 + random(9));
Serial.print(a);
Serial.print(" + ");
Serial.print(b);
Serial.print(" = ");
Fraction c = readFraction();
Serial.print(c);
Serial.print("\t\t");
Serial.println(a + b);
if (c == a + b ) count++;
}
return count;
}
int sub(int n)
{
int count = 0;
Serial.println("type n/m.");
for (int i = 0; i < n; i++)
{
Fraction a(1 + random(9), 1 + random(9));
Fraction b(1 + random(9), 1 + random(9));
Serial.print(a);
Serial.print(" - ");
Serial.print(b);
Serial.print(" = ");
Fraction c = readFraction();
Serial.print(c);
Serial.print("\t\t");
Serial.println(a - b);
if (c == a - b ) count++;
}
return count;
}
int mul(int n)
{
int count = 0;
Serial.println("type n/m.");
for (int i = 0; i < n; i++)
{
Fraction a(1 + random(9), 1 + random(9));
Fraction b(1 + random(9), 1 + random(9));
Serial.print(a);
Serial.print(" * ");
Serial.print(b);
Serial.print(" = ");
Fraction c = readFraction();
Serial.print(c);
Serial.print("\t\t");
Serial.println(a * b);
if (c == a * b ) count++;
}
return count;
}
int div(int n)
{
int count = 0;
Serial.println("type n/m.");
for (int i = 0; i < n; i++)
{
Fraction a(1 + random(9), 1 + random(9));
Fraction b(1 + random(9), 1 + random(9));
Serial.print(a);
Serial.print(" / ");
Serial.print(b);
Serial.print(" = ");
Fraction c = readFraction();
Serial.print(c);
Serial.print("\t\t");
Serial.println(a / b);
if (c == a / b ) count++;
}
return count;
}
int equ(int n)
{
int count = 0;
Serial.println("press < = or > ");
for (int i = 0; i < n; i++)
{
Fraction a(1 + random(9), 1 + random(9));
Fraction b(1 + random(9), 1 + random(9));
Serial.print(a);
Serial.print("\t?\t");
Serial.print(b);
char c = choice();
Serial.print("\t");
Serial.println(c);
if (c == '<' && a < b) count++;
if (c == '=' && a == b) count++;
if (c == '>' && a > b) count++;
}
return count;
}