Une Calculatrice Scientifique en C à l'aide de fonctions
J'ai écrit un programme qui a utilisé la plupart des mathématiques.h bibliothèque de fonctions pour la création d'une sorte de calculatrice scientifique. Cependant, je ne suis pas d'obtenir le résultat souhaité. J'ai toujours 0.000000 à la fin de la sortie. Merci de m'aider à identifier mon erreur, je vous Remercie.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
float sine(float x);
float cosine(float x);
float tangent(float x);
float sineh(float x);
float cosineh(float x);
float tangenth(float x);
float logten(float x);
float squareroot(float x);
float exponent(float x);
float power(float x,float y);
int main()
{
int x,y,n,answer;
printf("What do you want to do?\n");
printf("1.sin 2.cos 3. tan 4. sinh 5.cosh 6.tanh 7.1og10 8. square root. 9.exponent 10.power.");
scanf ("%d",&n);
if (n<9 && n>0)
{
printf("\n What is x? ");
scanf("%f",&x);
switch (n)
{
case 1: answer = sine(x); break;
case 2: answer = cosine(x); break;
case 3: answer = tangent(x); break;
case 4: answer = sineh(x); break;
case 5: answer = cosineh(x); break;
case 6: answer = tangenth(x); break;
case 7: answer = logten(x); break;
case 8: answer = squareroot(x); break;
case 9: answer = exponent(x); break;
}
}
if (n==10)
{
printf("What is x and y?\n");
scanf("%f%f",&x,&y);
answer = power(x,y);
}
if (n>0 && n<11)
printf("%f",answer);
else
printf("Wrong input.\n");
return 0;
}
float sine(float x)
{
return (sin (x*PI/180));
}
float cosine(float x)
{
return (cos (x*PI/180));
}
float tangent(float x)
{
return (tan(x*PI/180));
}
float sineh(float x)
{
return (sinh(x));
}
float cosineh(float x)
{
return (sinh(x));
}
float tangenth(float x)
{
return (sinh(x));
}
float logten(float x)
{
return (log10(x));
}
float squareroot(float x)
{
return (sqrt(x));
}
float exponent(float x)
{
return(exp(x));
}
float power(float x, float y)
{
return (pow(x,y));
}
Vous devez vous connecter pour publier un commentaire.
Vous avez déclaré que ces quatre variables comme les entiers:
mais
n
est le seul qui vous fait traiter comme un entier. Déclarerx
,y
etanswer
commefloat
:Si j'active -Wall et -Wconversion, je vois les avertissements suivants, qui, si vous l'avez résolu, permettrait de résoudre votre problème:
Vous avez déclaré
x
et d'autres variables, commeint
. Essayez de déclarer quefloat
au lieuLe problème est que le spécificateur de format est incorrect. Changement de tous les
%f
à%d
dans lescanf
déclaration.