Soyez le premier à donner votre avis sur cette source.
Vue 5 312 fois - Téléchargée 285 fois
Fichier C++ (main) : #include <iostream> #include <string> using namespace std; #include "vcalc.h" void accueil(void) { cout <<'\n' <<"---------------------------------------------------------------------------"<<'\n' <<"--------------------------VCALCULATOR V1.0---------------------------------"<<'\n' <<"---------------------------------------------------------------------------"<<'\n' <<'\n'<<"compilé sous mandrake linux avec g++"<<'\n'; cout <<'\n'<<'\n' <<"tapez 0 avant une operation binaire" <<'\n' <<"ex : 0sqrt25 retournera la valeur 5" <<'\n' <<'\n' <<'\n'; } void affresult(float nb) { cout <<'\n' <<'\n' <<"resultat : "<<nb <<'\n'; } int main(void) { float a,b,result; string op; vcalc calculator; for(;;) { cin >>a>>op>>b; if(a!=0) { result = calculator.calc(a,b,op); affresult(result); } else { result = calculator.calc(b,op); affresult(result); } } return 0; } La classe vcalc.h : //debut inclusions---------------------------------------------------------- #include <cmath> #include <string> using namespace std; //fin inclusions------------------------------------------------------------ //VISUALSNAKE CALC CLASS v1.0 //debut classe-------------------------------------------------------------- class vcalc { public: float calc(float a, float b, string op); float calc(float a, string op); string help(void); }; float vcalc::calc(float a, float b, string op) { float ret; if(op!="") { if(op=="+")ret= a+b; if(op=="-")ret= a-b; if(op=="*")ret= a*b; if(op=="/")ret= a/b; if(op=="atan2")ret= atan2(a,b); if(op=="pow")ret= pow(a,b); if(op=="fmod")ret= fmod(a,b); } else { ret= 0; } return ret; } float vcalc::calc(float a, string op) { float ret2; if(op!="") { if(op=="log")ret2= log(a); if(op=="log10")ret2= log10(a); if(op=="sin")ret2= sin(a); if(op=="cos")ret2= cos(a); if(op=="tan")ret2= tan(a); if(op=="asin")ret2= asin(a); if(op=="acos")ret2= acos(a); if(op=="atan")ret2= atan(a); if(op=="sinh")ret2= sinh(a); if(op=="cosh")ret2= cosh(a); if(op=="tanh")ret2= tanh(a); if(op=="sqrt")ret2= sqrt(a); if(op=="exp")ret2= exp(a); if(op=="ceil")ret2= ceil(a); if(op=="fabs")ret2= fabs(a); if(op=="floor")ret2= floor(a); } else { ret2= 0; } return ret2; } string vcalc::help(void) { string helpstr; helpstr += "\nadd(float a, float b){return a + b;}"; helpstr += "\nsous(float a, float b){return a - b;}"; helpstr += "\nmult(float a, float b){return a * b;}"; helpstr += "\ndiv(float a, float b){return a / b;}"; helpstr += "\natan2(float a,float b){return atan2(a,b);}"; helpstr += "\npow(float a,float b){return pow(a,b);}"; helpstr += "\nfmod(float a,float b){return fmod(a,b);}"; helpstr += "\nlog(float a) {return log(a);}"; helpstr += "\nlog10(float a) {return log10(a);}"; helpstr += "\nsin(float a) {return sin(a);}"; helpstr += "\ncos(float a) {return cos(a);}"; helpstr += "\ntan(float a) {return tan(a);}"; helpstr += "\nsinh(float a) {return sinh(a);}"; helpstr += "\ncosh(float a) {return cosh(a);}"; helpstr += "\ntanh(float a) {return tanh(a);}"; helpstr += "\nasin(float a) {return asin(a);}"; helpstr += "\nacos(float a) {return acos(a);}"; helpstr += "\natan(float a) {return atan(a);}"; helpstr += "\nsqrt(float a) {return sqrt(a);}"; helpstr += "\nexp(float a) {return exp(a);}"; helpstr += "\nceil(float a) {return ceil(a);}"; helpstr += "\nfabs(float a) {return fabs(a);}"; helpstr += "\nfloor(float a) {return floor(a);}\n"; return helpstr; } //fin classe --------------------------------------------------------------- et une version fonctions pour ceux qui aiment pas les objets : //debut inclusions---------------------------------------------------------- #include <cmath> #include <string> using namespace std; //fin inclusions------------------------------------------------------------ //VISUALSNAKE CALC FUNCTION v1.0 //debut fonction-------------------------------------------------------------- float calc(float a, float b, string op) { float ret; if(op!="") { if(op=="+")ret= a+b; if(op=="-")ret= a-b; if(op=="*")ret= a*b; if(op=="/")ret= a/b; if(op=="atan2")ret= atan2(a,b); if(op=="pow")ret= pow(a,b); if(op=="fmod")ret= fmod(a,b); } else { ret= 0; } return ret; } float calc(float a, string op) { float ret2; if(op!="") { if(op=="log")ret2= log(a); if(op=="log10")ret2= log10(a); if(op=="sin")ret2= sin(a); if(op=="cos")ret2= cos(a); if(op=="tan")ret2= tan(a); if(op=="asin")ret2= asin(a); if(op=="acos")ret2= acos(a); if(op=="atan")ret2= atan(a); if(op=="sinh")ret2= sinh(a); if(op=="cosh")ret2= cosh(a); if(op=="tanh")ret2= tanh(a); if(op=="sqrt")ret2= sqrt(a); if(op=="exp")ret2= exp(a); if(op=="ceil")ret2= ceil(a); if(op=="fabs")ret2= fabs(a); if(op=="floor")ret2= floor(a); } else { ret2= 0; } return ret2; } string help(void) { string helpstr; helpstr += "\natan2(float a,float b)"; helpstr += "\npow(float a,float b)"; helpstr += "\nfmod(float a,float b)"; helpstr += "\nlog(float a)"; helpstr += "\nlog10(float a)"; helpstr += "\nsin(float a); helpstr += "\ncos(float a)"; helpstr += "\ntan(float a)"; helpstr += "\nsinh(float a)"; helpstr += "\ncosh(float a)"; helpstr += "\ntanh(float a)"; helpstr += "\nasin(float a)"; helpstr += "\nacos(float a)"; helpstr += "\natan(float a)"; helpstr += "\nsqrt(float a)"; helpstr += "\nexp(float a)"; helpstr += "\nceil(float a)"; helpstr += "\nfabs(float a)"; helpstr += "\nfloor(float a)\n"; return helpstr; } //fin fonction ---------------------------------------------------------------
18 sept. 2003 à 19:40
ou bien on le fait ensemble mais je ne crois pas que c'est tellement nécessaire pour une calculette, en passant par ce projet tu pourras apprendre le Qt :)
18 sept. 2003 à 19:35
Mais j'ai aucune connaissances en interfaces graphiques linux :/
18 sept. 2003 à 18:33
j'avais seulement lu le nom des fonctions mdr, j'ai pas regarder le bout de code ou tu compare l'operateur mdr. :)
mais pour la racine carre il existe une touche special alors tu pourrait faire:
if ( op != "" );
{
if ( op == "sqrt" ) ...
if ( op == "²" ) ...
}
mais bon ce n'est plus tellement si utile :) maintenant que tu as terminé de le faire en console, essaye donc de le faire avec QT ou bien GTK + (tout les deux multi systeme). alors la tu auras p-e ta calculette dans la prochaine version de mandrake :p:p:p
bon boulot ;)
18 sept. 2003 à 17:43
18 sept. 2003 à 13:56
mais sinon j'aime bien ton code
Continue comme ça!!!
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.