Calculatrice à opérateurs binaires

Description

ATTENTION : Ce code a été écrit sous Unix avec comme éditeur de texte Emacs.

Il répond au Standard C99 c'est pourquoi il faut le spécifier au compilateur car la plupart fonctionnent toujorus en ANSI par défault.
Exemple pour compiler (avec GCC sous Linux) : gcc --std=CC99 --pedantic -W -Wall nomdufichier.c
Le résultat sera par défault dans le fichier ./a.out, si vous voulez spécifiez le fichier de destination, rajouter
-o nomdufichier.
Les options W & Wall servent à afficher tous les warnings !

Programme : vous rentrer une chaine de caracteres
"operande operateur operande operateur ... operande q" ( q pour quitter)
Et le programme calcul en vous affichant toutes les valuers intermédiaires.
Ceci est mon 2ème programme , soyez indulgent :p

Source / Exemple :


/****************************************

  • Projet de Programmation : Projet n°2 *
  • Author : Stevens Benjamin *
  • Mail : bstevens@student.ulg.ac.be *
                                                                              • /
#include <stdio.h> #include <ctype.h> #include <stdbool.h> #define NOTHING 0 #define A_NUMBER 1 #define AN_OPERATOR 2 int calcul(result,operator,number,quit); //This function calcul & return the result to main int itsanop(char); //This function verify if we got a good operator int puis(result,number); //Same as pow() function but we can't use <math.h> so we must create it /*------------------------------Function main()-------------------------------*/ int main() { char operator='o'; char c; int number; int previous=NOTHING; int result=0; bool quit=false; printf("Enter a calcul (number operator number ... operator q) \n"); printf("You can only use : *,-,/,+,^ : \n"); while (((c=getchar())!='q') && (quit==false)) { //if we got an integer (and not a char) if (isdigit(c)) { //we put the first number in result if(previous==NOTHING) { result=c-48; //'Char To Int' convertion previous=A_NUMBER; } //if we got an other number, we have a cypher and we put it into "result" because it's the first else if((previous==A_NUMBER) && (operator=='o')) { result*=10; result+=(c-48); } //same as the previous but not the first, so we put into "number" else if((previous==A_NUMBER) && (operator!='o')) { number*=10; number+=(c-48); } //if we got a number after an operator, e put it into "number" else if(previous==AN_OPERATOR) { number=c-48; previous=A_NUMBER; } } //if we got a char (and not a integer) && the char is a operator +,-,+,/,^ if ((itsanop(c)) && (previous==A_NUMBER)) { //if the operator is the first, we put the car into operator if (operator=='o') { operator=c; previous=AN_OPERATOR; } //else if it's not the first, we use the function "calcul" else { result=calcul(result,operator,number,quit); printf("Actual value :%d\n",result); operator=c; previous=AN_OPERATOR; } } //if the user gives an operator 2 times else if((itsanop(c)) && (previous==AN_OPERATOR)) { printf("You must respect the conditions!\n"); quit=true; } //is the user begin by an operator or a other character else if((isdigit(c)==0) && (previous==NOTHING)) { printf("You must begin by a number!\n"); quit=true; } } /*at this time c='q' & we can print the final result*/ //if there is as minimum 2 number/cypher & an operator, we make the last calcul if ((quit==false) && (operator!='o')) { result=calcul(result,operator,number,quit); printf("Final Result: %d\n",result); } //if there is no operator, we only printf the first number/cypher else if ((quit==false) && (operator=='o')) { printf("Final Result: %d\n",result); } return 0; } //end of main /*****************************
  • Here's the three functions *
  • used in the main function *
                                                          • /
/*--------------------------Function calcul()---------------------------------*/ int calcul(result,operator,number,quit) { printf("%d %c %d \n", result, operator, number); switch(operator) { case'+': result+=number; break; case'-': result-=number; break; case'*': result*=number; break; case'/': if (number==0) { printf("You can't divide by 0!\n"); quit=true; break; } else { result/=number; } break; case'^': result=puis(result,number); //the 3rd function break; } if (quit==false) { return result; } //when the user want to divide by 0, we stop the program if (quit==true) return result=0; } /*-----Function itsanop() : this function verify if the char is a good operator----------*/ int itsanop(char c) { if ((c=='+') || (c=='-') || (c=='*') || (c=='/') || (c=='^') || (c=='o')) return 1; else return 0; } /*---Function puis() : same as pow() function but we can't use <math.h> so we must crate it-----*/ static int puis(result,number) { //we must remain the initial result int base=result; if (number>1) { while (number>1) { result=result*base; number=number-1; } return result; } else if (number==0) return 1; else if (number==1) return result; } /*------------------------------------E N D----------------------------------------*/

Conclusion :


Liez les comments, ce n'est pas un anglais très difficile...

Codes Sources

A voir également

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.