Files
SyncHome/trunk/workspace/Atari/c_math/math.c
2023-03-13 08:36:51 +00:00

376 lines
9.4 KiB
C

/* *************************************************************** *
* ATARI ST VBCC Math Trainer *
* (c) 05.05.2018 Paolo Iocco *
* *
***************************************************************** */
/*
* ESC J Erase from the cursor to the end of the screen.
* ESC K Erase from the cursor to the end of the line.
*
* https://en.wikipedia.org/wiki/VT52 (ATARI)
* http://ascii-table.com/ansi-escape-sequences-vt-100.php (VT100)
*/
/*
* #define _ATARI // resolved in makefile
* make program => ATARI
* make amiga => AMIGA
* make all => .exe
*
*/
#ifdef _ATARI
#define cls() printf("\33E") /* ESC E Clear Home (not standard) */
#define home() printf("\33H") /* ESC H Move the cursor to the home position. */
#define cur_up() printf("\33A") /* ESC A Cursor up. */
#define cur_down() printf("\33B") /* ESC B Cursor down. */
#define cur_right() printf("\33C") /* ESC C Cursor right. */
#define cur_left() printf("\33D") /* ESC D Cursor left. */
#define gotoxy(x,y) printf("\33Y%c%c",32+y,32+x) /* ESC Yrc Move the cursor to given row and column. (not standard) */
#define cur_ins() printf("\33L") /* ESC L Insert Line at cursor (not standard) */
#define rev_on printf("\33p") /* ESC p Reverse mode on */
#define rev_off printf("\33q") /* ESC q Reverse mode off */
#define cur_on printf("\33e") /* ESC e Cursor visible */
#define cur_off printf("\33f") /* ESC f Cursor not visible */
#define color(c) printf("\33b%c",32+c) /* ESC b#color foreground color (low nybble) (not standard) */
#define backc(c) printf("\33c%c",32+c) /* ESC c#color background color (low nibble) (not standard) */
int cWHITE=0;
int cRED=1;
int cGREEN=2;
int cBLACK=3;
#include <tos.h>
#elif _AMIGA
#define cls() printf("\33E") /* ESC E Clear Home (not standard) */
#define home() printf("\33H") /* ESC H Move the cursor to the home position. */
#define cur_up() printf("\33A") /* ESC A Cursor up. */
#define cur_down() printf("\33B") /* ESC B Cursor down. */
#define cur_right() printf("\33C") /* ESC C Cursor right. */
#define cur_left() printf("\33D") /* ESC D Cursor left. */
#define gotoxy(x,y) printf("\33Y%c%c",32+y,32+x) /* ESC Yrc Move the cursor to given row and column. (not standard) */
#define cur_ins() printf("\33L") /* ESC L Insert Line at cursor (not standard) */
#define rev_on printf("\33p") /* ESC p Reverse mode on */
#define rev_off printf("\33q") /* ESC q Reverse mode off */
#define cur_on printf("\33e") /* ESC e Cursor visible */
#define cur_off printf("\33f") /* ESC f Cursor not visible */
#define color(c) printf("\33b%c",32+c) /* ESC b#color foreground color (low nybble) (not standard) */
#define backc(c) printf("\33c%c",32+c) /* ESC c#color background color (low nibble) (not standard) */
int cWHITE=0;
int cRED=1;
int cGREEN=2;
int cBLACK=3;
#else
/* Powershell VT100: Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Type DWORD 1 */
#define cls() printf("\33[2J") /* printf("\33[34mHello \33[31mWorld\33[0m") */
#define home() printf("\33[H")
#define cur_up() printf("\33[1A")
#define cur_down() printf("\33[1B")
#define cur_right() printf("\33[1C")
#define cur_left() printf("\33[1D")
#define gotoxy(x,y) printf("\33[%d;%dH",y+1,x+1)
#define cur_ins()
#define rev_on printf("\33[7m")
#define rev_off printf("\33[27m")
#define cur_on /* printf("\33[e") */
#define cur_off /* printf("\33[f") */
#define color(c) printf("\33[%dm",c+30)
#define backc(c) printf("\33[%dm",c+40)
#define cWHITE 0 /* black... */
#define cRED 1 /* */
#define cGREEN 2 /* */
#define cBLACK 7 /* white... */
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
typedef int bool;
int PuntiTot=0;
const int MAX_VAL_SS=100+1;
const int MAX_VAL_M=10+1;
const int MAX_VAL_EURO=1000; /* 10.00€ */
bool isEqual(double x, double y) {
const double epsilon = 1e-1;
return (abs(x - y) <= epsilon);
}
float stof(const char* s){
int point_seen;
int d;
float rez = 0, fact = 1;
if (*s == '-'){
s++;
fact = -1;
};
for (point_seen = 0; *s; s++){
if (*s == '.'){
point_seen = 1;
continue;
};
d = *s - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0f;
rez = rez * 10.0f + (float)d;
};
};
return (rez * fact);
}
void Print_OK(){
color(cGREEN);
printf("OK");
color(cBLACK);
}
void Print_ERROR(){
rev_on;
color(cRED);
printf("Errore!");
color(cBLACK);
rev_off;
}
int SumSub(char volte){
int punti=0, primo=0, secondo=0, swap=0, risultato=0, volta=0;
int a;
char b[20];
char segno='+';
cls();
home();
printf("- Somme e Sottrazioni -\n");
printf("-----------------------\n");
while ( volte-- ) {
volta++;
primo = rand() % MAX_VAL_SS;
secondo = rand() % MAX_VAL_SS;
while (primo+secondo>100){
secondo = rand() % MAX_VAL_SS;
}
if((rand() % 2)==1){
segno='+';
risultato=primo+secondo;
}
else {
segno='-';
if (secondo>primo){
swap=primo;
primo=secondo;
secondo=swap;
}
risultato=primo-secondo;
}
printf("\n\nQuanto fa %d %c %d ? ", primo, segno, secondo);
gets(b);
a=atoi(b);
gotoxy(27,volta*2+2);
if (a==risultato){
Print_OK();
punti++;
}
else {
Print_ERROR();
}
}
printf("\n\n");
printf("Premi un tasto per continure...");
getchar();
return(punti);
}
int Platzhalter(char volte){
int punti=0;
printf("- Platzhalter -\n");
printf("---------------\n\n");
while ( volte-- ) {
printf("%2d\n",volte);
}
printf("\n");
return(punti);
}
int ZahlenMauer(char volte){
int punti=0;
printf("- Zahlenmauer -\n");
printf("---------------\n\n");
while ( volte-- ) {
printf("%2d\n",volte);
}
printf("\n");
return(punti);
}
int MalRechnen(char volte){
int punti=0, primo=0, secondo=0, risultato=0, volta=0;
int a;
char b[20];
char segno='*';
cls();
home();
printf("- Moltiplicazioni e divisioni -\n");
printf("-------------------------------\n");
while ( volte-- ){
volta++;
primo = rand() % MAX_VAL_M;
secondo = rand() % MAX_VAL_M;
if((rand() % 2)==1){
segno='*';
risultato=primo*secondo;
}
else {
segno=':';
if (primo==0)
primo=1;
if (secondo==0)
secondo=1;
risultato=primo;
primo=primo*secondo;
}
printf("\n\nQuanto fa %d %c %d ? ",primo,segno,secondo);
gets(b);
a=atoi(b);
gotoxy(27,volta*2+2);
if (a==risultato){
Print_OK();
punti++;
}
else {
Print_ERROR();
}
}
printf("\n\n");
printf("Premi un tasto per continure...");
getchar();
return(punti);
}
int Sachaufgaben(char volte){
int punti=0;
printf("- Problemi -\n");
printf("------------\n\n");
while ( volte-- ) {
printf("%2d\n",volte);
}
printf("\n");
return(punti);
}
int EuroRechnen(char volte){
int punti=0, primo=0, secondo=0, risultato=0, volta=0;
float a=0;
char b[20];
char segno='+';
cls();
home();
printf("- Euro e Monete -\n");
printf("-----------------\n");
while ( volte-- ) {
volta++;
primo = rand() % MAX_VAL_EURO;
secondo = rand() % MAX_VAL_EURO;
while (primo+secondo>MAX_VAL_EURO){
secondo = rand() % MAX_VAL_EURO;
}
if((rand() % 2)==1){
segno='+';
risultato=primo+secondo;
}
else {
segno='-';
primo=((primo+secondo)/100)*100+100;
risultato=primo-secondo;
}
printf("\n\nQuanto fa %d.%02d Euro %c %d.%02d Euro ? ", primo/100, primo % 100 , segno, secondo/100, secondo % 100);
gets(b);
a=stof(b);
gotoxy(43,volta*2+2);
if (isEqual(risultato,a*100)){
Print_OK();
punti++;
}
else {
Print_ERROR();
}
}
printf("\n\n");
printf("Premi un tasto per continure...");
getchar();
return(punti);
}
int main(void){
int r1=0, r2=0, r3=0, r4=0, r5=0, rt=0, voto=0;
int v1=10, v2=0, v3=10, v4=10, v5=0, vt=0;
int passo=1;
#ifdef _ATARI
if (Getrez()==2) {
cGREEN=1;
}
#endif
cls();
home();
cur_on;
srand(time(NULL));
printf("- Benvenuto nel programma Matematicone 3000 -\n");
printf("---------------------------------------------\n\n");
r1=SumSub(v1);
r2=Platzhalter(v2);
r3=MalRechnen(v3);
r4=EuroRechnen(v4);
r5=Sachaufgaben(v5);
rt=r1+r2+r3+r4+r5;
vt=v1+v2+v3+v4+v5;
passo=vt/10;
voto=(vt-rt)/passo+1;
if (voto>6)
voto=6;
cls();
home();
printf("---------------------------------------------\n");
printf("- Risultati -\n");
printf("---------------------------------------------\n");
printf("- -\n");
printf("- Somme e sottrazioni: %2d/%2d -\n",r1,v1);
printf("- Platzhalter: %2d/%2d -\n",r2,v2);
printf("- Moltiplicazioni: %2d/%2d -\n",r3,v3);
printf("- Euro e monete: %2d/%2d -\n",r4,v4);
printf("- Problemi: %2d/%2d -\n",r5,v5);
printf("- -\n");
printf("- In totale hai raggiunto: %2d/%2d punti -\n",rt,vt);
printf("- corrispondente al voto: %2d -\n",voto);
printf("- -\n");
printf("---------------------------------------------\n");
gotoxy(0,23);
printf("Premi un tasto per uscire...");
getchar();
return(0);
}