Periodiq/src/periodiq.c

703 lines
16 KiB
C
Executable File

/*****************************************************************/
/* */
/* CASIO fx-9860G SDK Library */
/* */
/* File name : [ProjectName].c */
/* */
/* Copyright (c) 2006 CASIO COMPUTER CO., LTD. */
/* */
/*****************************************************************/
#include "periodiq.h"
#include "display.h"
#include "stdlib.h"
#include "stdio.h"
#include "ctype.h"
#include "keyboard.h"
#include "tales.h"
//#include <stdlib.h>
//#include <ctype.h>
int atoi(const char *str)
{
// Variables declaration.
int n=0, sign=1;
// Parsing string until the first glyph.
while(isspace(*str)) str++;
// Checking sign.
if(*str=='-') sign=-1, str++;
else if(*str=='+') sign=+1, str++;
// Reading digits.
while(isdigit(*str))
{
n += *str - '0';
if(*++str) n *= 10;
}
// Returning the final value.
return n*sign;
}
// element family
static const char *family[25] ={
"non metaux", // 0
"halogene", // 1
"gaz rare", // 2
"metalloide", // 3
"metal alcalin", // 4
"metal alcalino terreux", // 5
"lanthanide", // 6
"actinide", // 7
"metal de transition", // 8
"post transition metal"// 9
};
// state element
static const char *etat[9]={
"solide", // 0
"gaz", // 1
"liquide", // 2
"inconnu", // 3
};
extern font_t modern;
extern image_t tableauMini;
#define floor(x) ((int)(x))
/* returns element number (1-118) or 0 if there is nothing at the given cell */
/* x and y can hold any value, even if it's outside the table */
int element_at(int x, int y)
{
static const int counts[] = { 2, 8, 8, 18, 18, 32, 32 };
static const int firsts[] = { 1, 2, 2, 3, 3, 18, 18 };
int result = 0;
if(x < 0 || x > 17 || y < 0 || y > 8 ) return 0;
if(y >= 7)
{
if(x < 3 || x > 17) return 0;
return ( y == 7 ? 57 : 89 ) + x - 3;
}
else if(y >= 5)
{
if(x >= 3) result += 14;
}
if(x >= firsts[y])
{
if(x + (counts[y] - firsts[y]) < 18) return 0;
else x -= (18 - counts[y]);
}
while(y--)
{
result += counts[y];
}
return result + x + 1 ;
}
/* updates x and y if there is an element if the requested direction */
/* in this case, returns the element's number; otherwise returns 0 */
int move(int *x, int *y, enum { up = 0, right, down, left } dir)
{
int dx[] = { 0, 1, 0, -1 };
int dy[] = { -1, 0, 1, 0 };
int elt = element_at(*x + dx[dir], *y + dy[dir]);
if(elt) *x += dx[dir], *y += dy[dir];
return elt;
}
int main()
{
tableauElement(0);
return 1;
}
void Mini(char* chaine, int n ) // fonc defini le numero de la fonction
{
short j;
dtext(n*21+strlen(chaine), 58, chaine);
drect(n*21+2, 56, n*21+20, 63, color_invert);
//ML_display_vram();
}
int tableauElement(int chang)
{
short decalx = 0, decaly = 0;
int test;
char x = 1, y = 1;
unsigned char i;
text_configure(&modern, color_black);
while(1)
{
dclear();
for(i = 0; i < 118; i++)
{
dtext(13*(elements[i].groupe-1)+decalx+1, 8*( elements[i].famille == 6 || elements[i].famille == 7 ? elements[i].periode + 3 : elements[i].periode) +decaly-6, elements[i].symbol);
}
// x = elements[chang].groupe;
// y = ( elements[chang].famille == 6 || elements[chang].famille == 7 ? elements[chang].periode + 3 : elements[chang].periode);
/* test = element_at(x, y);
/* dprint(1,1, "(%d,%d) => %d et %s", x,y, test, elements[test-1].symbol);*/
chang = element_at(x, y);
dprint(1,1, "(%d,%d) = %d", x, y, chang);
drect(13*x,8*y,13*x+12,8*y+8, color_invert);
dupdate();
//drect()
key=getkey();
switch(key)
{
case KEY_UP : y--; break;
case KEY_DOWN : y++; break;
case KEY_LEFT : x--; break;
case KEY_RIGHT : x++; break;
//case KEY_F4 : chang=tableauEleMini(chang); break;
case KEY_F2 : chang=find(chang); break;
// case KEY_F3 : calcul_mole(); break;
case KEY_F6 : case KEY_EXE : chang=detailsElement(chang - 1); break;
case KEY_EXIT : return 1;
}
}
}
int tableauEleMini(int chang)
{
char buffer[50];
char y;
char menu=0;
char choix=-1;
dclear();
dimage(0, 0, &tableauMini);
dupdate();
getkey();
/*
chang=(chang>=118 ? 0 : chang<=-1 ? 117 : chang);
y=(chang>55 && chang<71 ? 9 : chang>87 && chang<103 ? 10 : elements[chang].periode);
if (chang>55 && chang<71 || chang>87 && chang<103)
{
drect(5*elements[chang].groupe-4, 5*y-7, 5*elements[chang].groupe-1, 5*y-4, color_black);
}
else
{
drect(5*elements[chang].groupe-4, 5*y-4, 5*elements[chang].groupe-1, 5*y-1, color_black);
}
dtext(110-3*strlen(elements[chang].symbol), 13, elements[chang].symbol);
sprintf(buffer, "%d", chang+1); // détail des éléments sur la droite
dtext(110-2*strlen(buffer), 5, buffer);
sprintf(buffer, "%.3f", elements[chang].masse_mol);
dtext(110-2*strlen(buffer), 23, buffer);
if(menu==0)
{
Mini("maxi", 0);
Mini("find", 1);
Mini("mol", 2);
Mini("fami", 4);
Mini("det", 5);
key=getkey();
switch(key)
{
case KEY_F1 : case KEY_EXIT : return chang;
case KEY_F2 : chang=find(chang); break;
case KEY_F3 : calcul_mole(); break;
case KEY_F5 : menu=1; break;
case KEY_F6 : chang=detailsElement(chang); break;
}
}
else if (menu==1)
{
Mini("n-me", 0);
Mini("halo", 1);
Mini("g-ra", 2);
Mini("m-ti", 3);
Mini("m-al", 4);
Mini(" -->", 5);
key=getkey();
switch(key)
{
case KEY_EXIT : menu=0; choix=-1; break;
case KEY_F1 : choix=0; break;
case KEY_F2 : choix=1; break;
case KEY_F3 : choix=2; break;
case KEY_F4 : choix=3; break;
case KEY_F5 : choix=4; break;
case KEY_F6 : menu=2; break; // on change dans les 2 menus
}
}
else if (menu==2)
{
Mini("m-at", 0);
Mini("lant", 1);
Mini("acta", 2);
Mini("m-tr", 3);
Mini("p-tr", 4);
Mini(" -->", 5);
key=getkey();
switch(key)
{
case KEY_EXIT : menu=0; choix=-1;break;
case KEY_F1 : choix=5; break;
case KEY_F2 : choix=6; break;
case KEY_F3 : choix=7; break;
case KEY_F4 : choix=8; break;
case KEY_F5 : choix=9; break;
case KEY_F6 : menu=1; break; // on change dans les 2 menus
}
}
switch(key) // pour les touches principales à chaque fois
{
case KEY_LEFT : chang+=1; break;
case KEY_RIGHT : chang-=1; break;
case KEY_DOWN : chang=elements[chang].dessous; break;
case KEY_UP : chang=elements[chang].dessus; break;
case KEY_EXE : chang=detailsElement(chang); break;
}
}*/
}
void choix_famille(int choix)
{
/*
char i=0, y;
for(i;i<118;++i)
{
if (choix==ajout[i].famille)
{
y=(i>55 && i<71 ? 9 : i>87 && i<103 ? 10 : elements[i].periode);
(i>55 && i<71 || i>87 && i<103 ? ML_bmp_or(select, 5*elements[i].groupe-4, 5*y-7, 4, 4) : ML_bmp_or(select, 5*elements[i].groupe-4, 5*y-4, 4, 4));
ML_bmp_or(select, 20, 50, 4, 4);
dtext(30, 50, family[choix]);
}
}*/
}
int detailsElement(int a)
{
unsigned char buffer[50];
unsigned char page = 0;
unsigned char i, j = 0;
text_configure(&modern, color_black);
while (1)
{
dclear();
dprint(20, 2, "%s", elements[a].symbol);
dprint(40, 2, "%s", elements[a].nom);
dprint((10-a/25), 4, "%d", a+1);
dline(0, 10, 128, 10, color_black);
dline(127, 22*page + 12, 127, 22*page + 42, color_black);
switch(page)
{
case 0 :
{
dprint(1, 12, "masse molaire %d.%03d g/mol", elements[a].masse_mol/1000, elements[a].masse_mol % 1000);
dprint(20, 22, "propriete de base");
sprintf(buffer, "groupe : %d ", elements[a].groupe);
if(elements[a].famille==6 || elements[a].famille==7)
{
sprintf(buffer, "groupe inconnu");
}
dtext(1, 32, buffer);
sprintf(buffer, "periode : %d ", elements[a].periode);
dtext(1, 42, buffer);
j=0;
for(i = 0; i < strlen(elements[a].config_electro); i++)
{
if(elements[a].config_electro[i]>=10)
{
buffer[j++] = (int)elements[a].config_electro[i]/10 + '0';
buffer[j++] = elements[a].config_electro[i]%10 + '0';
}
else
{
buffer[j++] = elements[a].config_electro[i] + '0';
}
buffer[j++] = '-';
}
j--;
buffer[j]=0;
sprintf(buffer, "couch electro %s", buffer);
dtext(1, 52, buffer);
break;
}
case 1 :
{
dprint(1, 12, "famille %s", family[elements[a].famille]);
dprint(1, 22, "etat a 293.15 K : %s", etat[elements[a].etat]);
dtext(20, 32, "reactivite :");
if (elements[a].electro_neg == -1)
{
sprintf(buffer, "electro neg inconnue");
}
else
{
sprintf(buffer, "electro neg : %d.%02d ", elements[a].electro_neg/100, elements[a].electro_neg%100);
}
dtext(1, 42, buffer);
if (elements[a].valence == -1)
{
sprintf(buffer, "valence inconnue ");
}
else
{
sprintf(buffer, "valence : %d", elements[a].valence);
}
dtext(1, 52, buffer);
break;
}
}
Mini("ret", 5);
dupdate();
key=getkey();
switch (key)
{
case KEY_LEFT : a-=1; break;
case KEY_RIGHT : a+=1; break;
case KEY_DOWN : page = (page <= 1 ? 1 : page + 1); break;
case KEY_UP : page = (page >=0 ? 0 : page - 1); break;
case KEY_F6 : case KEY_EXE : case KEY_EXIT : return a;
}
a=(a<0 ? 117 : a>117 ? 0 : a);
}
}
unsigned char* input(unsigned char* ask, unsigned char size, unsigned char* char_allowed)
{
unsigned char buffer[50];
unsigned char j,i;
j=0;
while(1)
{
dclear();
dtext(1, 1, ask);
dtext(1, 20, buffer);
dupdate();
key=getkey();
for(i = 0; i < strlen(char_allowed); i++)
{
if(char_allowed[i] == key_char(key) && j < size)
{
buffer[j++] = key_char(key);
buffer[j] = 0;
}
}
switch(key)
{
case KEY_DEL :
{
j--;
buffer[j] = 0;
break;
}
case KEY_EXE :
{
return buffer;
}
case KEY_EXIT :
{
return NULL;
}
}
}
}
int find(chang)
{
char *numero=NULL;
char i=0, j=0;
unsigned char buffer[50];
text_configure(&modern, color_black);
dclear();
dtext(10, 1, "Vous cherchez :");
dtext(10, 15, "1. numero de l'element");
dtext(10, 25, "2. masse molaire");
dtext(10, 35, "3. Symbole de l'element");
dtext(10, 45, "4. Nom de l'element");
dupdate();
/*while (1)
{
key=getkey();
switch(key)
{
case KEY_1 :
{
numero=input("Element num :", 3, "0123456789");
if (atoi(numero)>0 && atoi(numero)<119)
{
return atoi(numero)-1;
}
break;
}
case KEY_2 :
{
while(1)
{
numero=input("Masse molaire de :", 3, "0123456789");
for (i=0;i<118;i++)
{
if (atoi(numero)==floor(elements[i].masse_mol/1000+0.5))
{
return i;
}
}
}
break;
}
/*
case KEY_3 :
{
while(1)
{
Bdisp_AllClr_DDVRAM();
dtext(1, 1, "Vous cherchez le symbole ?", 0);
EI_manage_config(EI_SET_START_MODE, EI_ALPHA_LOCKED);
numero=EI_input_string(3, (const char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
numero[0]=toupper (numero[0]); // on met le 1er caractère en majuscule
for (i=0;i<118;i++)
{
for (j = 1 ; numero[j] != '\0' ; j++)
{
numero[j] = tolower(numero[j]); // on met certains caractères en minuscule pour la recherche
}
if (!strcmp(numero, elements[i].symbol))
{
return i;
}
}
}
}
case KEY_4 :
{
while(1)
{
Bdisp_AllClr_DDVRAM();
dtext(1, 1, "Vous cherchez le nom ?", 0);
EI_manage_config(EI_SET_START_MODE, EI_ALPHA_LOCKED);
numero=EI_input_string(13, (const char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
numero[0]=toupper(numero[0]); // on met le 1er caractère en majuscule
for (i=0;i<118;i++)
{
for (j = 1 ; numero[j] != '\0' ; j++)
{
numero[j] = tolower(numero[j]);// on met certains caractères en minuscule pour la recherche
}
if (!strcmp(numero, elements[i].nom))
{
return i;
}
}
}
}
case KEY_EXIT : return chang;
}
}*/
}
void calcul_mole()
{/*
char *str = NULL, *p=str;
char characts[20]={'\0'};
char i=0;
float result=0, masse;
const char buffer[50]={0};
char cap;
while(1)
{
Bdisp_AllClr_DDVRAM();
dtext(1, 1, "Entrez votre formule : ");
Mini("A a", 0);
EI_init();
EI_manage_config(EI_SET_COLUMN, 1);
EI_manage_config(EI_SET_ROW, 2);
EI_manage_config(EI_SET_START_MODE, EI_NORMAL);
str=EI_input_string(25, (const char*)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
Bdisp_AllClr_DDVRAM();
sprintf(buffer, "%s", str);
PrintXY(64-3*strlen(str), 10, buffer, 36);
while(*str) // on va jusqu'a la fin de la chaine
{
i=0;
while(isalpha(*str)) // on prends seulement les lettres
{
characts[i]=tolower(*str); // on copie la lettre en minuscule directement dans characts
++i;
++str;
}
characts[0]=toupper(characts[0]); // on met le 1er caractère en majuscule
for(i=0;i<118;i++) // on se déplace dans les éléments de la structure
{
if(!strcmp(characts, elements[i].symbol))
{
masse=elements[i].masse_mol; // on a la masse molaire
break;
}
}
for(i=0;i<20;++i) // on remet tout à zéro : évite les erreurs
characts[i]='\0';
i=0;
while(isdigit(*str)) // on prends seulement les chiffres
{
characts[i]=*str; // on copie la lettre dans characts
++i;
++str;
}
result += atoi(characts)*masse;
for(i=0;i<4;++i) // on remet tout à zéro : évite les erreurs
characts[i]='\0';
}
free(p);
dtext(1, 30, "la masse molaire est de");
sprintf(buffer, "%.3f g/mol", result);
dtext(1, 40, buffer);
Mini("calc", 0);
Mini("ret", 5);
result=0;
GetKey(&key);
switch(key)
{
case 29 : return;
}
}*/
}